1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\NodeSearchBundle\Twig; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Kunstmaan\NodeBundle\Entity\HasNodeInterface; |
7
|
|
|
use Kunstmaan\NodeBundle\Entity\Node; |
8
|
|
|
use Kunstmaan\NodeSearchBundle\Helper\IndexablePagePartsService; |
9
|
|
|
use Kunstmaan\PagePartBundle\Helper\HasPagePartsInterface; |
10
|
|
|
|
11
|
|
|
class KunstmaanNodeSearchTwigExtension extends \Twig_Extension |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var EntityManager |
15
|
|
|
*/ |
16
|
|
|
private $em; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var IndexablePagePartsService |
20
|
|
|
*/ |
21
|
|
|
private $indexablePagePartsService; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param EntityManager $em |
25
|
|
|
* @param IndexablePagePartsService $indexablePagePartsService |
26
|
|
|
*/ |
27
|
|
|
public function __construct(EntityManager $em, IndexablePagePartsService $indexablePagePartsService) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$this->em = $em; |
30
|
|
|
$this->indexablePagePartsService = $indexablePagePartsService; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Returns a list of functions to add to the existing list. |
35
|
|
|
* |
36
|
|
|
* @return array An array of functions |
|
|
|
|
37
|
|
|
*/ |
38
|
|
View Code Duplication |
public function getFunctions() |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
return array( |
41
|
|
|
new \Twig_SimpleFunction('get_parent_page', array($this, 'getParentPage')), |
42
|
|
|
new \Twig_SimpleFunction('render_indexable_pageparts', array($this, 'renderIndexablePageParts'), array('needs_environment' => true, 'needs_context' => true, 'is_safe' => array('html'))), |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param HasNodeInterface $page |
48
|
|
|
* @param string $locale |
49
|
|
|
* |
50
|
|
|
* @return HasNodeInterface |
51
|
|
|
*/ |
52
|
|
|
public function getParentPage(HasNodeInterface $page, $locale) |
53
|
|
|
{ |
54
|
|
|
/** @var Node $node */ |
55
|
|
|
$node = $this->em->getRepository('KunstmaanNodeBundle:Node')->getNodeFor($page); |
56
|
|
|
$parentNode = $node->getParent(); |
57
|
|
|
$nodeTranslation = $parentNode->getNodeTranslation($locale); |
58
|
|
|
$parentPage = $nodeTranslation->getRef($this->em); |
59
|
|
|
|
60
|
|
|
return $parentPage; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param \Twig_Environment $env |
65
|
|
|
* @param array $twigContext The twig context |
66
|
|
|
* @param HasPagePartsInterface $page The page |
67
|
|
|
* @param string $contextName The pagepart context |
68
|
|
|
* @param array $parameters Some extra parameters |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public function renderIndexablePageParts( |
|
|
|
|
73
|
|
|
\Twig_Environment $env, |
74
|
|
|
array $twigContext, |
75
|
|
|
HasPagePartsInterface $page, |
76
|
|
|
$contextName = 'main', |
77
|
|
|
array $parameters = array() |
78
|
|
|
) |
79
|
|
|
{ |
80
|
|
|
$template = $env->loadTemplate('KunstmaanNodeSearchBundle:PagePart:view.html.twig'); |
81
|
|
|
$pageparts = $this->indexablePagePartsService->getIndexablePageParts($page, $contextName); |
82
|
|
|
$newTwigContext = array_merge( |
83
|
|
|
$parameters, |
84
|
|
|
array( |
85
|
|
|
'pageparts' => $pageparts |
86
|
|
|
) |
87
|
|
|
); |
88
|
|
|
$newTwigContext = array_merge($newTwigContext, $twigContext); |
89
|
|
|
|
90
|
|
|
return $template->render($newTwigContext); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.