Skip to content

Commit

Permalink
using setter injection to avoid cyclic dependencies and be more lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu authored and lsmith77 committed Aug 27, 2015
1 parent 01aa33c commit d47ac0f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
5 changes: 4 additions & 1 deletion DependencyInjection/CmfCoreExtension.php
Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions Resources/config/services.xml
Expand Up @@ -19,8 +19,6 @@

<service id="cmf_core.templating.helper" class="%cmf_core.templating.helper.class%" public="false">
<argument type="service" id="cmf_core.publish_workflow.checker" on-invalid="ignore"/>
<argument type="service" id="doctrine_phpcr" on-invalid="ignore"/>
<argument>%cmf_core.persistence.phpcr.manager_name%</argument>
<tag name="templating.helper" alias="cmf"/>
</service>

Expand Down
41 changes: 36 additions & 5 deletions Templating/Helper/CmfHelper.php
Expand Up @@ -29,6 +29,16 @@
*/
class CmfHelper extends Helper
{
/**
* @var ManagerRegistry
*/
private $doctrineRegistry;

/**
* @var string
*/
private $doctrineManagerName;

/**
* @var DocumentManager
*/
Expand All @@ -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;
}

/**
Expand All @@ -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;
Expand Down

0 comments on commit d47ac0f

Please sign in to comment.