Skip to content

Commit

Permalink
Merge pull request #256 from doctrine/hotfix/issue-#213-lazy-entity-r…
Browse files Browse the repository at this point in the history
…esolver

Hotfix/issue #213 lazy entity resolver
  • Loading branch information
Ocramius committed Aug 2, 2013
2 parents 4e1aa2a + 791a2f8 commit bd23f5f
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 16 deletions.
13 changes: 0 additions & 13 deletions src/DoctrineORMModule/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
namespace DoctrineORMModule;

use Zend\ModuleManager\Feature\ControllerProviderInterface;
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\ModuleManager\Feature\InitProviderInterface;
use Zend\ModuleManager\Feature\DependencyIndicatorInterface;
Expand All @@ -44,7 +43,6 @@
*/
class Module implements
ControllerProviderInterface,
BootstrapListenerInterface,
ConfigProviderInterface,
InitProviderInterface,
DependencyIndicatorInterface
Expand All @@ -65,17 +63,6 @@ function () use ($manager) {
$events->getSharedManager()->attach('doctrine', 'loadCli.post', array($this, 'initializeConsole'));
}

/**
* {@inheritDoc}
*/
public function onBootstrap(EventInterface $event)
{
/* @var $application \Zend\Mvc\ApplicationInterface */
$application = $event->getTarget();

$application->getServiceManager()->get('doctrine.entity_resolver.orm_default');
}

/**
* {@inheritDoc}
*/
Expand Down
30 changes: 30 additions & 0 deletions src/DoctrineORMModule/Options/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ class EntityManager extends AbstractOptions
*/
protected $connection = 'orm_default';

/**
* Set the connection key for the EntityResolver, which is
* a service of type {@see \Doctrine\ORM\Tools\ResolveTargetEntityListener}.
* The EntityResolver service name is assembled
* as "doctrine.entity_resolver.{key}"
*
* @var string
*/
protected $entityResolver = 'orm_default';

/**
* @param string $configuration
* @return self
Expand Down Expand Up @@ -62,4 +72,24 @@ public function getConnection()
{
return 'doctrine.connection.' . $this->connection;
}

/**
* @param string $entityResolver
* @return self
*/
public function setEntityResolver($entityResolver)
{
$this->entityResolver = (string) $entityResolver;

return $this;
}

/**
* @return string
* @return self
*/
public function getEntityResolver()
{
return 'doctrine.entity_resolver.' . $this->entityResolver;
}
}
7 changes: 6 additions & 1 deletion src/DoctrineORMModule/Service/EntityManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ class EntityManagerFactory extends AbstractFactory
public function createService(ServiceLocatorInterface $sl)
{
/* @var $options \DoctrineORMModule\Options\EntityManager */
$options = $this->getOptions($sl, 'entitymanager');
$options = $this->getOptions($sl, 'entitymanager');
$connection = $sl->get($options->getConnection());
$config = $sl->get($options->getConfiguration());

// initializing the resolver
// @todo should actually attach it to a fetched event manager here, and not
// rely on its factory code
$sl->get($options->getEntityResolver());

return EntityManager::create($connection, $config);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* <http://www.doctrine-project.org>.
*/

namespace DoctrineORMModuleTest\Collector;
namespace DoctrineORMModuleTest\Options;

use PHPUnit_Framework_TestCase as TestCase;
use DoctrineORMModule\Options\Configuration;
Expand Down
44 changes: 44 additions & 0 deletions tests/DoctrineORMModuleTest/Options/EntityManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace DoctrineORMModuleTest\Collector;

use PHPUnit_Framework_TestCase;
use DoctrineORMModule\Options\EntityManager;

/**
* Tests for {@see \DoctrineORMModule\Options\EntityManager}
*
* @covers \DoctrineORMModule\Options\EntityManager
*
* @author Marco Pivetta <ocramius@gmail.com>
*/
class EntityManagerTest extends PHPUnit_Framework_TestCase
{
public function testSetGetResolver()
{
$options = new EntityManager();

$this->assertSame('doctrine.entity_resolver.orm_default', $options->getEntityResolver());

$options->setEntityResolver('foo');

$this->assertSame('doctrine.entity_resolver.foo', $options->getEntityResolver());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* <http://www.doctrine-project.org>.
*/

namespace DoctrineORMModuleTest\Collector;
namespace DoctrineORMModuleTest\Options;

use PHPUnit_Framework_TestCase as TestCase;
use DoctrineORMModule\Options\SQLLoggerCollectorOptions;
Expand Down

0 comments on commit bd23f5f

Please sign in to comment.