|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sonata Project package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sonata\AdminBundle\Twig\Extension; |
|
13
|
|
|
|
|
14
|
|
|
use Sonata\AdminBundle\Templating\TemplateRegistryInterface; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
|
18
|
|
|
use Twig\Extension\AbstractExtension; |
|
19
|
|
|
use Twig\TwigFunction; |
|
20
|
|
|
|
|
21
|
|
|
final class TemplateRegistryExtension extends AbstractExtension |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var TemplateRegistryInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $globalTemplateRegistry; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var ContainerInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $container; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct(TemplateRegistryInterface $globalTemplateRegistry, ContainerInterface $container) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->globalTemplateRegistry = $globalTemplateRegistry; |
|
36
|
|
|
$this->container = $container; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getFunctions() |
|
40
|
|
|
{ |
|
41
|
|
|
return [ |
|
42
|
|
|
new TwigFunction('get_admin_template', [$this, 'getAdminTemplate']), |
|
43
|
|
|
new TwigFunction('get_global_template', [$this, 'getGlobalTemplate']), |
|
44
|
|
|
|
|
45
|
|
|
// NEXT MAJOR: Remove this line |
|
46
|
|
|
new TwigFunction('get_admin_pool_template', [$this, 'getPoolTemplate'], ['deprecated' => true]), |
|
47
|
|
|
]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $name |
|
52
|
|
|
* @param string $adminCode |
|
53
|
|
|
* |
|
54
|
|
|
* @throws ServiceNotFoundException |
|
55
|
|
|
* @throws ServiceCircularReferenceException |
|
56
|
|
|
* |
|
57
|
|
|
* @return null|string |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getAdminTemplate($name, $adminCode) |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->getTemplateRegistry($adminCode)->getTemplate($name); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @deprecated Sinds 3.x, to be removed in 4.0. Use getGlobalTemplate instead. |
|
66
|
|
|
* |
|
67
|
|
|
* @param $name |
|
68
|
|
|
* |
|
69
|
|
|
* @return null|string |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getPoolTemplate($name) |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->getGlobalTemplate($name); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string $name |
|
78
|
|
|
* |
|
79
|
|
|
* @return null|string |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getGlobalTemplate($name) |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->globalTemplateRegistry->getTemplate($name); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $adminCode |
|
88
|
|
|
* |
|
89
|
|
|
* @throws ServiceNotFoundException |
|
90
|
|
|
* @throws ServiceCircularReferenceException |
|
91
|
|
|
* |
|
92
|
|
|
* @return TemplateRegistryInterface |
|
93
|
|
|
*/ |
|
94
|
|
|
private function getTemplateRegistry($adminCode) |
|
95
|
|
|
{ |
|
96
|
|
|
$serviceId = $adminCode.'.template_registry'; |
|
97
|
|
|
$templateRegistry = $this->container->get($serviceId); |
|
98
|
|
|
if ($templateRegistry instanceof TemplateRegistryInterface) { |
|
99
|
|
|
return $templateRegistry; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
throw new ServiceNotFoundException($serviceId); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|