Commit c4e330e5 authored by jwage's avatar jwage

[2.0] Placing loadClassMetadata() event so users can manipulate class meta data through events

parent 6c1690d3
...@@ -141,9 +141,7 @@ class EntityManager ...@@ -141,9 +141,7 @@ class EntityManager
$this->_conn = $conn; $this->_conn = $conn;
$this->_config = $config; $this->_config = $config;
$this->_eventManager = $eventManager; $this->_eventManager = $eventManager;
$this->_metadataFactory = new ClassMetadataFactory( $this->_metadataFactory = new ClassMetadataFactory($this);
$this->_config->getMetadataDriverImpl(),
$this->_conn->getDatabasePlatform());
$this->_metadataFactory->setCacheDriver($this->_config->getMetadataCacheImpl()); $this->_metadataFactory->setCacheDriver($this->_config->getMetadataCacheImpl());
$this->_unitOfWork = new UnitOfWork($this); $this->_unitOfWork = new UnitOfWork($this);
$this->_proxyGenerator = new DynamicProxyGenerator($this, $this->_config->getCacheDir()); $this->_proxyGenerator = new DynamicProxyGenerator($this, $this->_config->getCacheDir());
......
<?php
namespace Doctrine\ORM\Event;
use Doctrine\Common\EventArgs;
/**
* Class that holds event arguments for a loadMetadata event.
*
* @author Jonathan H. Wage <jonwage@gmail.com>
* @since 2.0
*/
class LoadClassMetadataEventArgs extends EventArgs
{
private $_classMetadata;
public function __construct(\Doctrine\ORM\Mapping\ClassMetadata $classMetadata)
{
$this->_classMetadata = $classMetadata;
}
public function getClassMetadata()
{
return $this->_classMetadata;
}
}
...@@ -40,4 +40,5 @@ final class Events ...@@ -40,4 +40,5 @@ final class Events
const preUpdate = 'preUpdate'; const preUpdate = 'preUpdate';
const postUpdate = 'postUpdate'; const postUpdate = 'postUpdate';
const load = 'load'; const load = 'load';
const loadClassMetadata = 'loadClassMetadata';
} }
\ No newline at end of file
...@@ -23,6 +23,7 @@ namespace Doctrine\ORM\Mapping; ...@@ -23,6 +23,7 @@ namespace Doctrine\ORM\Mapping;
use Doctrine\Common\DoctrineException; use Doctrine\Common\DoctrineException;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\ORM\Events;
/** /**
* The metadata factory is used to create ClassMetadata objects that contain all the * The metadata factory is used to create ClassMetadata objects that contain all the
...@@ -38,10 +39,13 @@ use Doctrine\DBAL\Platforms\AbstractPlatform; ...@@ -38,10 +39,13 @@ use Doctrine\DBAL\Platforms\AbstractPlatform;
*/ */
class ClassMetadataFactory class ClassMetadataFactory
{ {
private $_em;
/** The targeted database platform. */ /** The targeted database platform. */
private $_targetPlatform; private $_targetPlatform;
/** The used metadata driver. */ /** The used metadata driver. */
private $_driver; private $_driver;
/** The event manager instance */
private $_evm;
/** The used cache driver. */ /** The used cache driver. */
private $_cacheDriver; private $_cacheDriver;
private $_loadedMetadata = array(); private $_loadedMetadata = array();
...@@ -51,10 +55,12 @@ class ClassMetadataFactory ...@@ -51,10 +55,12 @@ class ClassMetadataFactory
* *
* @param $driver The metadata driver to use. * @param $driver The metadata driver to use.
*/ */
public function __construct($driver, AbstractPlatform $targetPlatform) public function __construct(\Doctrine\ORM\EntityManager $em)
{ {
$this->_driver = $driver; $this->_em = $em;
$this->_targetPlatform = $targetPlatform; $this->_driver = $em->getConfiguration()->getMetadataDriverImpl();
$this->_targetPlatform = $em->getConnection()->getDatabasePlatform();
$this->_evm = $em->getEventManager();
} }
/** /**
...@@ -183,6 +189,11 @@ class ClassMetadataFactory ...@@ -183,6 +189,11 @@ class ClassMetadataFactory
$class->setParentClasses($visited); $class->setParentClasses($visited);
if ($this->_evm->hasListeners(Events::loadClassMetadata)) {
$eventArgs = new \Doctrine\ORM\Event\LoadClassMetadataEventArgs($class);
$this->_evm->dispatchEvent(Events::loadClassMetadata, $eventArgs);
}
$this->_generateStaticSql($class); $this->_generateStaticSql($class);
$this->_loadedMetadata[$className] = $class; $this->_loadedMetadata[$className] = $class;
......
...@@ -13,4 +13,9 @@ class MetadataDriverMock ...@@ -13,4 +13,9 @@ class MetadataDriverMock
{ {
return false; return false;
} }
public function preload()
{
return array();
}
} }
\ No newline at end of file
...@@ -23,6 +23,7 @@ class AllTests ...@@ -23,6 +23,7 @@ class AllTests
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\XmlDriverTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Mapping\XmlDriverTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\YamlDriverTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Mapping\YamlDriverTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\ClassMetadataFactoryTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Mapping\ClassMetadataFactoryTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\ClassMetadataLoadEventTest');
return $suite; return $suite;
} }
......
...@@ -4,7 +4,11 @@ namespace Doctrine\Tests\ORM\Mapping; ...@@ -4,7 +4,11 @@ namespace Doctrine\Tests\ORM\Mapping;
use Doctrine\Tests\Mocks\MetadataDriverMock; use Doctrine\Tests\Mocks\MetadataDriverMock;
use Doctrine\Tests\Mocks\DatabasePlatformMock; use Doctrine\Tests\Mocks\DatabasePlatformMock;
use Doctrine\Tests\Mocks\EntityManagerMock;
use Doctrine\Tests\Mocks\ConnectionMock;
use Doctrine\Tests\Mocks\DriverMock;
use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\Common\EventManager;
require_once __DIR__ . '/../../TestInit.php'; require_once __DIR__ . '/../../TestInit.php';
...@@ -13,8 +17,16 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase ...@@ -13,8 +17,16 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase
public function testGetMetadataForSingleClass() public function testGetMetadataForSingleClass()
{ {
$mockPlatform = new DatabasePlatformMock(); $driverMock = new DriverMock();
$config = new \Doctrine\ORM\Configuration();
$eventManager = new EventManager();
$conn = new ConnectionMock(array(), $driverMock, $config, $eventManager);
$mockDriver = new MetadataDriverMock(); $mockDriver = new MetadataDriverMock();
$config->setMetadataDriverImpl($mockDriver);
$entityManager = EntityManagerMock::create($conn, $config, $eventManager);
$mockPlatform = $conn->getDatabasePlatform();
$mockPlatform->setPrefersSequences(true); $mockPlatform->setPrefersSequences(true);
$mockPlatform->setPrefersIdentityColumns(false); $mockPlatform->setPrefersIdentityColumns(false);
...@@ -30,7 +42,7 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase ...@@ -30,7 +42,7 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase
$cm1->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO); $cm1->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO);
// SUT // SUT
$cmf = new ClassMetadataFactoryTestSubject($mockDriver, $mockPlatform); $cmf = new ClassMetadataFactoryTestSubject($entityManager);
$cmf->setMetadataForClass('Doctrine\Tests\ORM\Mapping\TestEntity1', $cm1); $cmf->setMetadataForClass('Doctrine\Tests\ORM\Mapping\TestEntity1', $cm1);
// Prechecks // Prechecks
......
<?php
namespace Doctrine\Tests\ORM\Mapping;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Events;
require_once __DIR__ . '/../../TestInit.php';
class ClassMetadataLoadEventTest extends \Doctrine\Tests\OrmTestCase
{
public function testEvent()
{
$em = $this->_getTestEntityManager();
$metadataFactory = $em->getMetadataFactory();
$evm = $em->getEventManager();
$evm->addEventListener(Events::loadClassMetadata, $this);
$classMetadata = $metadataFactory->getMetadataFor('Doctrine\Tests\ORM\Mapping\LoadEventTestEntity');
$this->assertTrue($classMetadata->hasField('about'));
}
public function loadClassMetadata(\Doctrine\ORM\Event\LoadClassMetadataEventArgs $eventArgs)
{
$classMetadata = $eventArgs->getClassMetadata();
$field = array(
'fieldName' => 'about',
'type' => 'string',
'length' => 255
);
$classMetadata->mapField($field);
}
}
/**
* @Entity
* @Table(name="load_event_test_entity")
*/
class LoadEventTestEntity
{
/**
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @Column(type="string", length=255)
*/
private $name;
private $about;
}
\ No newline at end of file
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace Doctrine\Tests\ORM\Mapping; namespace Doctrine\Tests\ORM\Mapping;
use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Events;
require_once __DIR__ . '/../../TestInit.php'; require_once __DIR__ . '/../../TestInit.php';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment