1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\AdminBundle\Twig\Extension; |
15
|
|
|
|
16
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
17
|
|
|
use Sonata\AdminBundle\Templating\TemplateRegistryInterface; |
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
19
|
|
|
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException; |
20
|
|
|
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
21
|
|
|
use Twig\Extension\AbstractExtension; |
22
|
|
|
use Twig\TwigFunction; |
23
|
|
|
|
24
|
|
|
final class TemplateRegistryExtension extends AbstractExtension |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var TemplateRegistryInterface |
28
|
|
|
*/ |
29
|
|
|
private $globalTemplateRegistry; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ContainerInterface |
33
|
|
|
*/ |
34
|
|
|
private $container; |
35
|
|
|
|
36
|
|
|
public function __construct(TemplateRegistryInterface $globalTemplateRegistry, ContainerInterface $container) |
37
|
|
|
{ |
38
|
|
|
$this->globalTemplateRegistry = $globalTemplateRegistry; |
39
|
|
|
$this->container = $container; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getFunctions() |
43
|
|
|
{ |
44
|
|
|
return [ |
45
|
|
|
new TwigFunction('get_admin_template', [$this, 'getAdminTemplate']), |
46
|
|
|
new TwigFunction('get_global_template', [$this, 'getGlobalTemplate']), |
47
|
|
|
|
48
|
|
|
// NEXT MAJOR: Remove this line |
49
|
|
|
new TwigFunction('get_admin_pool_template', [$this, 'getPoolTemplate'], ['deprecated' => true]), |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $name |
55
|
|
|
* @param string $adminCode |
56
|
|
|
* |
57
|
|
|
* @throws ServiceNotFoundException |
58
|
|
|
* @throws ServiceCircularReferenceException |
59
|
|
|
* |
60
|
|
|
* @return null|string |
61
|
|
|
*/ |
62
|
|
|
public function getAdminTemplate($name, $adminCode) |
63
|
|
|
{ |
64
|
|
|
// NEXT_MAJOR: Remove this line and use commented line below it instead |
65
|
|
|
return $this->getAdmin($adminCode)->getTemplate($name); |
|
|
|
|
66
|
|
|
// return $this->getTemplateRegistry($adminCode)->getTemplate($name); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @deprecated Sinds 3.34, to be removed in 4.0. Use getGlobalTemplate instead. |
71
|
|
|
* |
72
|
|
|
* @param $name |
73
|
|
|
* |
74
|
|
|
* @return null|string |
75
|
|
|
*/ |
76
|
|
|
public function getPoolTemplate($name) |
77
|
|
|
{ |
78
|
|
|
return $this->getGlobalTemplate($name); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $name |
83
|
|
|
* |
84
|
|
|
* @return null|string |
85
|
|
|
*/ |
86
|
|
|
public function getGlobalTemplate($name) |
87
|
|
|
{ |
88
|
|
|
return $this->globalTemplateRegistry->getTemplate($name); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $adminCode |
93
|
|
|
* |
94
|
|
|
* @throws ServiceNotFoundException |
95
|
|
|
* @throws ServiceCircularReferenceException |
96
|
|
|
* |
97
|
|
|
* @return TemplateRegistryInterface |
98
|
|
|
*/ |
99
|
|
|
private function getTemplateRegistry($adminCode) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$serviceId = $adminCode.'.template_registry'; |
102
|
|
|
$templateRegistry = $this->container->get($serviceId); |
103
|
|
|
if ($templateRegistry instanceof TemplateRegistryInterface) { |
104
|
|
|
return $templateRegistry; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
throw new ServiceNotFoundException($serviceId); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @deprecated since 3.x, will be dropped in 4.0. Use TemplateRegistry services instead |
112
|
|
|
* |
113
|
|
|
* @param string $adminCode |
114
|
|
|
* |
115
|
|
|
* @throws ServiceNotFoundException |
116
|
|
|
* @throws ServiceCircularReferenceException |
117
|
|
|
* |
118
|
|
|
* @return AdminInterface |
119
|
|
|
*/ |
120
|
|
|
private function getAdmin($adminCode) |
121
|
|
|
{ |
122
|
|
|
$admin = $this->container->get($adminCode); |
123
|
|
|
if ($admin instanceof AdminInterface) { |
124
|
|
|
return $admin; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
throw new ServiceNotFoundException($adminCode); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.