Completed
Push — 3.x ( e95e95...638cd1 )
by Oskar
05:54
created

src/Twig/Extension/TemplateRegistryExtension.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 string|null
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);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Twig\...ryExtension::getAdmin() has been deprecated with message: since 3.34, 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.35. 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...
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 string $name
73
     *
74
     * @return string|null
75
     */
76
    public function getPoolTemplate($name)
77
    {
78
        return $this->getGlobalTemplate($name);
79
    }
80
81
    /**
82
     * @param string $name
83
     *
84
     * @return string|null
85
     */
86
    public function getGlobalTemplate($name)
87
    {
88
        return $this->globalTemplateRegistry->getTemplate($name);
89
    }
90
91
    /**
92
     * @throws ServiceNotFoundException
93
     * @throws ServiceCircularReferenceException
94
     */
95
    private function getTemplateRegistry(string $adminCode): TemplateRegistryInterface
96
    {
97
        $serviceId = $adminCode.'.template_registry';
98
        $templateRegistry = $this->container->get($serviceId);
99
        if ($templateRegistry instanceof TemplateRegistryInterface) {
100
            return $templateRegistry;
101
        }
102
103
        throw new ServiceNotFoundException($serviceId);
104
    }
105
106
    /**
107
     * @deprecated since 3.34, will be dropped in 4.0. Use TemplateRegistry services instead
108
     *
109
     * @throws ServiceNotFoundException
110
     * @throws ServiceCircularReferenceException
111
     */
112
    private function getAdmin(string $adminCode): AdminInterface
113
    {
114
        $admin = $this->container->get($adminCode);
115
        if ($admin instanceof AdminInterface) {
116
            return $admin;
117
        }
118
119
        throw new ServiceNotFoundException($adminCode);
120
    }
121
}
122