Completed
Push — 3.x ( bb878e...c3ea70 )
by Grégoire
12:58 queued 08:06
created

TemplateRegistryExtension::getTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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\Admin\Pool;
16
use Symfony\Component\HttpFoundation\RequestStack;
17
use Twig\Extension\AbstractExtension;
18
use Twig\TwigFunction;
19
20
final class TemplateRegistryExtension extends AbstractExtension
21
{
22
    /**
23
     * @var Pool
24
     */
25
    private $pool;
26
27
    /**
28
     * @var RequestStack
29
     */
30
    private $requestStack;
31
32
    public function __construct(Pool $pool, RequestStack $requestStack)
33
    {
34
        $this->pool = $pool;
35
        $this->requestStack = $requestStack;
36
    }
37
38
    public function getFunctions()
39
    {
40
        return [
41
            new TwigFunction('get_admin_template', [$this, 'getTemplate']),
42
            new TwigFunction('get_admin_pool_template', [$this, 'getPoolTemplate']),
43
        ];
44
    }
45
46
    /**
47
     * @param string $name
48
     *
49
     * @return null|string
50
     */
51
    public function getTemplate($name)
52
    {
53
        return $this->getAdmin()->getTemplate($name);
54
    }
55
56
    /**
57
     * @param string $name
58
     *
59
     * @return null|string
60
     */
61
    public function getPoolTemplate($name)
62
    {
63
        return $this->pool->getTemplate($name);
64
    }
65
66
    /**
67
     * @return AdminInterface|false|null
68
     */
69
    private function getAdmin()
70
    {
71
        return $this->pool->getAdminByAdminCode(
72
            $this->requestStack->getCurrentRequest()->get('_sonata_admin')
73
        );
74
    }
75
}
76