diff --git a/DependencyInjection/CmfCoreExtension.php b/DependencyInjection/CmfCoreExtension.php index 0917ba51..da798996 100644 --- a/DependencyInjection/CmfCoreExtension.php +++ b/DependencyInjection/CmfCoreExtension.php @@ -269,7 +269,10 @@ public function load(array $configs, ContainerBuilder $container) $container->setParameter($this->getAlias() . '.persistence.phpcr.basepath', $config['persistence']['phpcr']['basepath']); $templatingHelper = $container->getDefinition($this->getAlias() . '.templating.helper'); - $templatingHelper->replaceArgument(1, new Reference($config['persistence']['phpcr']['manager_registry'])); + $templatingHelper->addMethodCall('setDoctrineRegistry', array( + new Reference($config['persistence']['phpcr']['manager_registry']), + '%cmf_core.persistence.phpcr.manager_name%' + )); if ($config['persistence']['phpcr']['use_sonata_admin']) { $this->loadSonataPhpcrAdmin($config, $loader, $container); diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 9c4455ce..d00efdd3 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -19,8 +19,6 @@ - - %cmf_core.persistence.phpcr.manager_name% diff --git a/Templating/Helper/CmfHelper.php b/Templating/Helper/CmfHelper.php index ef842025..d5cdef06 100644 --- a/Templating/Helper/CmfHelper.php +++ b/Templating/Helper/CmfHelper.php @@ -29,6 +29,16 @@ */ class CmfHelper extends Helper { + /** + * @var ManagerRegistry + */ + private $doctrineRegistry; + + /** + * @var string + */ + private $doctrineManagerName; + /** * @var DocumentManager */ @@ -40,18 +50,35 @@ class CmfHelper extends Helper protected $publishWorkflowChecker; /** + * The $registry constructor argument is deprecated in favor of + * setDcotrineRegistry in order to avoid circular dependencies when a + * doctrine event listener needs twig injected. + * * @param SecurityContextInterface $publishWorkflowChecker * @param ManagerRegistry $registry For loading PHPCR-ODM documents from * Doctrine. - * @param string $objectManagerName + * @param string $managerName */ - public function __construct(SecurityContextInterface $publishWorkflowChecker = null, $registry = null, $objectManagerName = null) + public function __construct(SecurityContextInterface $publishWorkflowChecker = null, $registry = null, $managerName = null) { $this->publishWorkflowChecker = $publishWorkflowChecker; + $this->setDoctrineRegistry($registry, $managerName); + } - if ($registry && $registry instanceof ManagerRegistry) { - $this->dm = $registry->getManager($objectManagerName); + /** + * Set the doctrine manager registry to fetch the object manager from. + * + * @param ManagerRegistry $registry + * @param string|null $managerName Manager name if not the default + */ + public function setDoctrineRegistry($registry, $managerName = null) + { + if ($this->doctrineRegistry) { + throw new \LogicException('Do not call this setter repeatedly or after using constructor injection'); } + + $this->doctrineRegistry = $registry; + $this->doctrineManagerName = $managerName; } /** @@ -60,7 +87,11 @@ public function __construct(SecurityContextInterface $publishWorkflowChecker = n protected function getDm() { if (!$this->dm) { - throw new \RuntimeException('Doctrine is not available.'); + if (!$this->doctrineRegistry) { + throw new \RuntimeException('Doctrine is not available.'); + } + + $this->dm = $this->doctrineRegistry->getManager($this->doctrineManagerName); } return $this->dm;