Skip to content

Commit

Permalink
[2.0] Placing loadClassMetadata() event so users can manipulate class…
Browse files Browse the repository at this point in the history
… meta data through events
  • Loading branch information
jwage committed Jul 15, 2009
1 parent 6c1690d commit c4e330e
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 8 deletions.
4 changes: 1 addition & 3 deletions lib/Doctrine/ORM/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ protected function __construct(Connection $conn, Configuration $config, EventMan
$this->_conn = $conn;
$this->_config = $config;
$this->_eventManager = $eventManager;
$this->_metadataFactory = new ClassMetadataFactory(
$this->_config->getMetadataDriverImpl(),
$this->_conn->getDatabasePlatform());
$this->_metadataFactory = new ClassMetadataFactory($this);
$this->_metadataFactory->setCacheDriver($this->_config->getMetadataCacheImpl());
$this->_unitOfWork = new UnitOfWork($this);
$this->_proxyGenerator = new DynamicProxyGenerator($this, $this->_config->getCacheDir());
Expand Down
27 changes: 27 additions & 0 deletions lib/Doctrine/ORM/Event/LoadClassMetadataEventArgs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Doctrine\ORM\Event;

use Doctrine\Common\EventArgs;

/**
* Class that holds event arguments for a loadMetadata event.
*
* @author Jonathan H. Wage <[email protected]>
* @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;
}
}

1 change: 1 addition & 0 deletions lib/Doctrine/ORM/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ private function __construct() {}
const preUpdate = 'preUpdate';
const postUpdate = 'postUpdate';
const load = 'load';
const loadClassMetadata = 'loadClassMetadata';
}
17 changes: 14 additions & 3 deletions lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use Doctrine\Common\DoctrineException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\ORM\Events;

/**
* The metadata factory is used to create ClassMetadata objects that contain all the
Expand All @@ -38,10 +39,13 @@
*/
class ClassMetadataFactory
{
private $_em;
/** The targeted database platform. */
private $_targetPlatform;
/** The used metadata driver. */
private $_driver;
/** The event manager instance */
private $_evm;
/** The used cache driver. */
private $_cacheDriver;
private $_loadedMetadata = array();
Expand All @@ -51,10 +55,12 @@ class ClassMetadataFactory
*
* @param $driver The metadata driver to use.
*/
public function __construct($driver, AbstractPlatform $targetPlatform)
public function __construct(\Doctrine\ORM\EntityManager $em)
{
$this->_driver = $driver;
$this->_targetPlatform = $targetPlatform;
$this->_em = $em;
$this->_driver = $em->getConfiguration()->getMetadataDriverImpl();
$this->_targetPlatform = $em->getConnection()->getDatabasePlatform();
$this->_evm = $em->getEventManager();
}

/**
Expand Down Expand Up @@ -183,6 +189,11 @@ protected function _loadMetadata($name)

$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->_loadedMetadata[$className] = $class;
Expand Down
5 changes: 5 additions & 0 deletions tests/Doctrine/Tests/Mocks/MetadataDriverMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ public function isTransient($className)
{
return false;
}

public function preload()
{
return array();
}
}
1 change: 1 addition & 0 deletions tests/Doctrine/Tests/ORM/Mapping/AllTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public static function suite()
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\XmlDriverTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\YamlDriverTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\ClassMetadataFactoryTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\ClassMetadataLoadEventTest');

return $suite;
}
Expand Down
16 changes: 14 additions & 2 deletions tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

use Doctrine\Tests\Mocks\MetadataDriverMock;
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\Common\EventManager;

require_once __DIR__ . '/../../TestInit.php';

Expand All @@ -13,8 +17,16 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase

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();
$config->setMetadataDriverImpl($mockDriver);

$entityManager = EntityManagerMock::create($conn, $config, $eventManager);

$mockPlatform = $conn->getDatabasePlatform();
$mockPlatform->setPrefersSequences(true);
$mockPlatform->setPrefersIdentityColumns(false);

Expand All @@ -30,7 +42,7 @@ public function testGetMetadataForSingleClass()
$cm1->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO);

// SUT
$cmf = new ClassMetadataFactoryTestSubject($mockDriver, $mockPlatform);
$cmf = new ClassMetadataFactoryTestSubject($entityManager);
$cmf->setMetadataForClass('Doctrine\Tests\ORM\Mapping\TestEntity1', $cm1);

// Prechecks
Expand Down
51 changes: 51 additions & 0 deletions tests/Doctrine/Tests/ORM/Mapping/ClassMetadataLoadEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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;
}
1 change: 1 addition & 0 deletions tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Doctrine\Tests\ORM\Mapping;

use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Events;

require_once __DIR__ . '/../../TestInit.php';

Expand Down

0 comments on commit c4e330e

Please sign in to comment.