Skip to content
Browse files

Making the creation of entity manager filterable

  • Loading branch information...
1 parent b9bef23 commit a1657631da16653bc46f72d6440caa2f334b1247 @mariano committed Jan 6, 2012
Showing with 153 additions and 57 deletions.
  1. +12 −19 README.md
  2. +141 −38 extensions/data/source/Doctrine.php
View
31 README.md
@@ -93,29 +93,22 @@ Libraries::add('Gedmo', array(
));
```
-And then you would use the `createEntityManager` connection option in the
-connection defied in `app/config/connections.php` to implement the callable that
-enables the behaviors:
+And then you would filter the `createEntityManager` method in the `Doctrine`
+datasource to add the behaviors. Edit your `app/config/connections.php` file
+and add the following right below the connection definition:
```php
-Connections::add('default', array(
- 'type' => 'Doctrine',
- 'driver' => 'pdo_mysql',
- 'host' => 'localhost',
- 'user' => 'root',
- 'password' => 'password',
- 'dbname' => 'my_db',
- 'createEntityManager' => function(array $params) {
- $params['eventManager']->addEventSubscriber(new \Gedmo\Timestampable\TimestampableListener());
- $params['eventManager']->addEventSubscriber(new \Gedmo\Sluggable\SluggableListener());
-
- return \Doctrine\ORM\EntityManager::create(
- $params['connection'],
- $params['configuration'],
- $params['eventManager']
+Connections::get('default')->applyFilter('createEntityManager',
+ function($self, $params, $chain) {
+ $params['eventManager']->addEventSubscriber(
+ new \Gedmo\Timestampable\TimestampableListener()
);
+ $params['eventManager']->addEventSubscriber(
+ new \Gedmo\Sluggable\SluggableListener()
+ );
+ return $chain->next($self, $params, $chain);
}
-));
+);
```
[lithium]: http://lithify.me
View
179 extensions/data/source/Doctrine.php
@@ -1,76 +1,179 @@
<?php
+/**
+ * Lithium: the most rad php framework
+ *
+ * @copyright Copyright 2012, Mariano Iglesias (http://marianoiglesias.com.ar)
+ * @license http://opensource.org/licenses/bsd-license.php The BSD License
+ */
+
namespace li3_doctrine2\extensions\data\source;
use Doctrine\ORM\Events;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
-class Doctrine {
+/**
+ * This datasource provides integration of Doctrine2 models
+ */
+class Doctrine extends \lithium\core\Object {
+ /**
+ * Connection settings for Doctrine
+ *
+ * @var array
+ */
+ protected $connectionSettings;
+
+ /**
+ * Doctrine's configuration
+ *
+ * @var object
+ */
+ protected $configuration;
+
+ /**
+ * Doctrine's event manager
+ *
+ * @var object
+ */
+ protected $eventManager;
+
+ /**
+ * Doctrine's entity manager
+ *
+ * @var object
+ */
protected $entityManager;
+ /**
+ * Build data source
+ *
+ * @param array $config Configuration
+ */
public function __construct(array $config = array()) {
$defaults = array(
'models' => LITHIUM_APP_PATH . '/models',
'proxies' => LITHIUM_APP_PATH . '/proxies',
- 'proxyNamespace' => 'proxies',
- 'createEntityManager' => function(array $params) {
+ 'proxyNamespace' => 'proxies'
+ );
+ $this->connectionSettings = array_diff_key($config, array_merge(
+ $defaults, array(
+ 'type' => null,
+ 'adapter' => null,
+ 'login' => null,
+ 'filters' => null
+ )
+ ));
+ parent::__construct($config + $defaults);
+ }
+
+ /**
+ * Initialize datasource
+ */
+ protected function _init() {
+ $this->configuration = new \Doctrine\ORM\Configuration();
+ $this->configuration->setProxyDir($this->_config['proxies']);
+ $this->configuration->setProxyNamespace(
+ $this->_config['proxyNamespace']
+ );
+ $this->configuration->setMetadataCacheImpl(
+ new \Doctrine\Common\Cache\ArrayCache()
+ );
+
+ $annotationDriver = $this->configuration->newDefaultAnnotationDriver(
+ (array) $this->_config['models']
+ );
+ $this->configuration->setMetadataDriverImpl($annotationDriver);
+
+ $this->eventManager = new \Doctrine\Common\EventManager();
+ $this->eventManager->addEventListener(array(
+ Events::postLoad,
+ Events::prePersist,
+ Events::preUpdate
+ ), $this);
+ }
+
+ /**
+ * Create an entity manager
+ *
+ * @param array $params Parameters
+ * @return object Entity manager
+ * @filter
+ */
+ protected function createEntityManager() {
+ $connection = $this->connectionSettings;
+ $configuration = $this->configuration;
+ $eventManager = $this->eventManager;
+ $params = compact('connection', 'configuration', 'eventManager');
+ return $this->_filter(__METHOD__, $params,
+ function($self, $params) {
return \Doctrine\ORM\EntityManager::create(
$params['connection'],
$params['configuration'],
$params['eventManager']
);
}
);
- $config += $defaults;
- $connection = array_diff_key($config, array_merge($defaults, array(
- 'type' => null,
- 'adapter' => null,
- 'login' => null,
- 'filters' => null
- )));
-
- $configuration = new \Doctrine\ORM\Configuration();
- $annotationDriver = $configuration->newDefaultAnnotationDriver((array) $config['models']);
-
- $configuration->setProxyDir($config['proxies']);
- $configuration->setProxyNamespace($config['proxyNamespace']);
- $configuration->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache());
- $configuration->setMetadataDriverImpl($annotationDriver);
-
- $eventManager = new \Doctrine\Common\EventManager();
- $eventManager->addEventListener(array(
- Events::postLoad,
- Events::prePersist,
- Events::preUpdate
- ), $this);
+ }
- $this->entityManager = $config['createEntityManager'](compact(
- 'connection',
- 'configuration',
- 'eventManager'
- ));
+ /**
+ * Get the entity manager
+ *
+ * @return object Entity Manager
+ */
+ public function getEntityManager() {
+ if (!isset($this->entityManager)) {
+ $this->entityManager = $this->createEntityManager();
+ }
+ return $this->entityManager;
}
+ /**
+ * Event executed after a record was loaded
+ *
+ * @param object $eventArgs Event arguments
+ */
public function postLoad(LifecycleEventArgs $eventArgs) {
- $this->dispatchEntityEvent($eventArgs->getEntity(), 'onPostLoad', array($eventArgs));
+ $this->dispatchEntityEvent($eventArgs->getEntity(), 'onPostLoad',
+ array($eventArgs)
+ );
}
+ /**
+ * Event executed before a record is to be created
+ *
+ * @param object $eventArgs Event arguments
+ */
public function prePersist(LifecycleEventArgs $eventArgs) {
- $this->dispatchEntityEvent($eventArgs->getEntity(), 'onPrePersist', array($eventArgs));
+ $this->dispatchEntityEvent($eventArgs->getEntity(), 'onPrePersist',
+ array($eventArgs)
+ );
}
+ /**
+ * Event executed before a record is to be updated
+ *
+ * @param object $eventArgs Event arguments
+ */
public function preUpdate(PreUpdateEventArgs $eventArgs) {
- $this->dispatchEntityEvent($eventArgs->getEntity(), 'onPreUpdate', array($eventArgs));
+ $this->dispatchEntityEvent($eventArgs->getEntity(), 'onPreUpdate',
+ array($eventArgs)
+ );
}
+ /**
+ * Dispatch an entity event
+ *
+ * @param object $entity Entity on where to dispatch event
+ * @param string $event Event name
+ * @param array $args Event arguments
+ */
protected function dispatchEntityEvent($entity, $eventName, array $args) {
- if (method_exists($entity, $eventName) && is_callable(array($entity, $eventName))) {
+ if (
+ method_exists($entity, $eventName) &&
+ is_callable(array($entity, $eventName))
+ ) {
call_user_func_array(array($entity, $eventName), $args);
}
}
-
- public function getEntityManager() {
- return $this->entityManager;
- }
}
?>

0 comments on commit a165763

Please sign in to comment.
Something went wrong with that request. Please try again.