Skip to content

Commit a7d1c30

Browse files
committed
Merge pull request #176 from symfony-cmf/templating-helper-doctrine-setter
using setter injection to avoid cyclic dependencies and be more lazy
2 parents 01aa33c + c736053 commit a7d1c30

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ matrix:
2626
- php: 5.6
2727
env: SYMFONY_VERSION=3.0.*
2828
- php: 5.3
29-
env: SYMFONY_VERSION=2.3.* COMPOSER_FLAGS="--prefer-lowest"
29+
env: SYMFONY_VERSION=2.3.* COMPOSER_FLAGS="--prefer-lowest" SYMFONY_DEPRECATIONS_HELPER=weak
3030
allow_failures:
3131
- php: nightly
3232
- env: SYMFONY_VERSION=2.8.*

DependencyInjection/CmfCoreExtension.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,10 @@ public function load(array $configs, ContainerBuilder $container)
269269
$container->setParameter($this->getAlias() . '.persistence.phpcr.basepath', $config['persistence']['phpcr']['basepath']);
270270

271271
$templatingHelper = $container->getDefinition($this->getAlias() . '.templating.helper');
272-
$templatingHelper->replaceArgument(1, new Reference($config['persistence']['phpcr']['manager_registry']));
272+
$templatingHelper->addMethodCall('setDoctrineRegistry', array(
273+
new Reference($config['persistence']['phpcr']['manager_registry']),
274+
'%cmf_core.persistence.phpcr.manager_name%'
275+
));
273276

274277
if ($config['persistence']['phpcr']['use_sonata_admin']) {
275278
$this->loadSonataPhpcrAdmin($config, $loader, $container);

Resources/config/services.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

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

Templating/Helper/CmfHelper.php

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@
2929
*/
3030
class CmfHelper extends Helper
3131
{
32+
/**
33+
* @var ManagerRegistry
34+
*/
35+
private $doctrineRegistry;
36+
37+
/**
38+
* @var string
39+
*/
40+
private $doctrineManagerName;
41+
3242
/**
3343
* @var DocumentManager
3444
*/
@@ -40,18 +50,35 @@ class CmfHelper extends Helper
4050
protected $publishWorkflowChecker;
4151

4252
/**
53+
* The $registry constructor argument is deprecated in favor of
54+
* setDcotrineRegistry in order to avoid circular dependencies when a
55+
* doctrine event listener needs twig injected.
56+
*
4357
* @param SecurityContextInterface $publishWorkflowChecker
4458
* @param ManagerRegistry $registry For loading PHPCR-ODM documents from
4559
* Doctrine.
46-
* @param string $objectManagerName
60+
* @param string $managerName
4761
*/
48-
public function __construct(SecurityContextInterface $publishWorkflowChecker = null, $registry = null, $objectManagerName = null)
62+
public function __construct(SecurityContextInterface $publishWorkflowChecker = null, $registry = null, $managerName = null)
4963
{
5064
$this->publishWorkflowChecker = $publishWorkflowChecker;
65+
$this->setDoctrineRegistry($registry, $managerName);
66+
}
5167

52-
if ($registry && $registry instanceof ManagerRegistry) {
53-
$this->dm = $registry->getManager($objectManagerName);
68+
/**
69+
* Set the doctrine manager registry to fetch the object manager from.
70+
*
71+
* @param ManagerRegistry $registry
72+
* @param string|null $managerName Manager name if not the default
73+
*/
74+
public function setDoctrineRegistry($registry, $managerName = null)
75+
{
76+
if ($this->doctrineRegistry) {
77+
throw new \LogicException('Do not call this setter repeatedly or after using constructor injection');
5478
}
79+
80+
$this->doctrineRegistry = $registry;
81+
$this->doctrineManagerName = $managerName;
5582
}
5683

5784
/**
@@ -60,7 +87,11 @@ public function __construct(SecurityContextInterface $publishWorkflowChecker = n
6087
protected function getDm()
6188
{
6289
if (!$this->dm) {
63-
throw new \RuntimeException('Doctrine is not available.');
90+
if (!$this->doctrineRegistry) {
91+
throw new \RuntimeException('Doctrine is not available.');
92+
}
93+
94+
$this->dm = $this->doctrineRegistry->getManager($this->doctrineManagerName);
6495
}
6596

6697
return $this->dm;

0 commit comments

Comments
 (0)