Completed
Pull Request — master (#5032)
by Grégoire
03:41
created

TemplateRegistryExtension::getAdmin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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\Templating\TemplateRegistryInterface;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
19
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
20
use Twig\Extension\AbstractExtension;
21
use Twig\TwigFunction;
22
23
final class TemplateRegistryExtension extends AbstractExtension
24
{
25
    /**
26
     * @var TemplateRegistryInterface
27
     */
28
    private $globalTemplateRegistry;
29
30
    /**
31
     * @var ContainerInterface
32
     */
33
    private $container;
34
35
    public function __construct(TemplateRegistryInterface $globalTemplateRegistry, ContainerInterface $container)
36
    {
37
        $this->globalTemplateRegistry = $globalTemplateRegistry;
38
        $this->container = $container;
39
    }
40
41
    public function getFunctions()
42
    {
43
        return [
44
            new TwigFunction('get_admin_template', [$this, 'getAdminTemplate']),
45
            new TwigFunction('get_global_template', [$this, 'getGlobalTemplate']),
46
47
            // NEXT MAJOR: Remove this line
48
            new TwigFunction('get_admin_pool_template', [$this, 'getPoolTemplate'], ['deprecated' => true]),
49
        ];
50
    }
51
52
    /**
53
     * @param string $name
54
     * @param string $adminCode
55
     *
56
     * @throws ServiceNotFoundException
57
     * @throws ServiceCircularReferenceException
58
     *
59
     * @return null|string
60
     */
61
    public function getAdminTemplate($name, $adminCode)
62
    {
63
        return $this->getTemplateRegistry($adminCode)->getTemplate($name);
64
    }
65
66
    /**
67
     * @deprecated Sinds 3.x, to be removed in 4.0. Use getGlobalTemplate instead.
68
     *
69
     * @param $name
70
     *
71
     * @return null|string
72
     */
73
    public function getPoolTemplate($name)
74
    {
75
        return $this->getGlobalTemplate($name);
76
    }
77
78
    /**
79
     * @param string $name
80
     *
81
     * @return null|string
82
     */
83
    public function getGlobalTemplate($name)
84
    {
85
        return $this->globalTemplateRegistry->getTemplate($name);
86
    }
87
88
    /**
89
     * @param string $adminCode
90
     *
91
     * @throws ServiceNotFoundException
92
     * @throws ServiceCircularReferenceException
93
     *
94
     * @return TemplateRegistryInterface
95
     */
96
    private function getTemplateRegistry($adminCode)
97
    {
98
        $serviceId = $adminCode.'.template_registry';
99
        $templateRegistry = $this->container->get($serviceId);
100
        if ($templateRegistry instanceof TemplateRegistryInterface) {
101
            return $templateRegistry;
102
        }
103
104
        throw new ServiceNotFoundException($serviceId);
105
    }
106
}
107