Completed
Push — 3.x ( a302a7...034439 )
by Grégoire
04:21
created

TemplateRegistryExtension::getAdmin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 9
rs 9.6666
c 1
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
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\Admin\AdminInterface;
15
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
18
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
19
use Twig\Extension\AbstractExtension;
20
use Twig\TwigFunction;
21
22
final class TemplateRegistryExtension extends AbstractExtension
23
{
24
    /**
25
     * @var TemplateRegistryInterface
26
     */
27
    private $globalTemplateRegistry;
28
29
    /**
30
     * @var ContainerInterface
31
     */
32
    private $container;
33
34
    public function __construct(TemplateRegistryInterface $globalTemplateRegistry, ContainerInterface $container)
35
    {
36
        $this->globalTemplateRegistry = $globalTemplateRegistry;
37
        $this->container = $container;
38
    }
39
40
    public function getFunctions()
41
    {
42
        return [
43
            new TwigFunction('get_admin_template', [$this, 'getAdminTemplate']),
44
            new TwigFunction('get_global_template', [$this, 'getGlobalTemplate']),
45
46
            // NEXT MAJOR: Remove this line
47
            new TwigFunction('get_admin_pool_template', [$this, 'getPoolTemplate'], ['deprecated' => true]),
48
        ];
49
    }
50
51
    /**
52
     * @param string $name
53
     * @param string $adminCode
54
     *
55
     * @throws ServiceNotFoundException
56
     * @throws ServiceCircularReferenceException
57
     *
58
     * @return null|string
59
     */
60
    public function getAdminTemplate($name, $adminCode)
61
    {
62
        // NEXT_MAJOR: Remove this line and use commented line below it instead
63
        return $this->getAdmin($adminCode)->getTemplate($name);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Twig\...ryExtension::getAdmin() has been deprecated with message: since 3.x, will be dropped in 4.0. Use TemplateRegistry services instead

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.

Loading history...
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...nterface::getTemplate() has been deprecated with message: since 3.x. To be removed in 4.0. Use TemplateRegistry services instead

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.

Loading history...
64
        // return $this->getTemplateRegistry($adminCode)->getTemplate($name);
65
    }
66
67
    /**
68
     * @deprecated Sinds 3.34, to be removed in 4.0. Use getGlobalTemplate instead.
69
     *
70
     * @param $name
71
     *
72
     * @return null|string
73
     */
74
    public function getPoolTemplate($name)
75
    {
76
        return $this->getGlobalTemplate($name);
77
    }
78
79
    /**
80
     * @param string $name
81
     *
82
     * @return null|string
83
     */
84
    public function getGlobalTemplate($name)
85
    {
86
        return $this->globalTemplateRegistry->getTemplate($name);
87
    }
88
89
    /**
90
     * @param string $adminCode
91
     *
92
     * @throws ServiceNotFoundException
93
     * @throws ServiceCircularReferenceException
94
     *
95
     * @return TemplateRegistryInterface
96
     */
97
    private function getTemplateRegistry($adminCode)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
98
    {
99
        $serviceId = $adminCode.'.template_registry';
100
        $templateRegistry = $this->container->get($serviceId);
101
        if ($templateRegistry instanceof TemplateRegistryInterface) {
102
            return $templateRegistry;
103
        }
104
105
        throw new ServiceNotFoundException($serviceId);
106
    }
107
108
    /**
109
     * @deprecated since 3.x, will be dropped in 4.0. Use TemplateRegistry services instead
110
     *
111
     * @param string $adminCode
112
     *
113
     * @throws ServiceNotFoundException
114
     * @throws ServiceCircularReferenceException
115
     *
116
     * @return AdminInterface
117
     */
118
    private function getAdmin($adminCode)
119
    {
120
        $admin = $this->container->get($adminCode);
121
        if ($admin instanceof AdminInterface) {
122
            return $admin;
123
        }
124
125
        throw new ServiceNotFoundException($adminCode);
126
    }
127
}
128