|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the core-bundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2019 WEBEWEB |
|
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 WBW\Bundle\CoreBundle\Twig\Extension; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
15
|
|
|
use Twig\Environment; |
|
16
|
|
|
use Twig\TwigFunction; |
|
17
|
|
|
use WBW\Bundle\CoreBundle\DependencyInjection\Container\ContainerTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Container Twig extension. |
|
21
|
|
|
* |
|
22
|
|
|
* @author webeweb <https://github.com/webeweb> |
|
23
|
|
|
* @package WBW\Bundle\CoreBundle\Twig\Extension |
|
24
|
|
|
*/ |
|
25
|
|
|
class ContainerTwigExtension extends AbstractTwigExtension { |
|
26
|
|
|
|
|
27
|
1 |
|
use ContainerTrait; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Service name. |
|
31
|
|
|
* |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
const SERVICE_NAME = "wbw.core.twig.extension.container"; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param Environment $twigEnvironment The Twig environment. |
|
40
|
|
|
* @param ContainerInterface $container The container. |
|
41
|
100 |
|
*/ |
|
42
|
100 |
|
public function __construct(Environment $twigEnvironment, ContainerInterface $container) { |
|
43
|
100 |
|
parent::__construct($twigEnvironment); |
|
44
|
|
|
|
|
45
|
|
|
$this->setContainer($container); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Call a native function. |
|
50
|
|
|
* |
|
51
|
30 |
|
* @param string|null $function The function. |
|
52
|
30 |
|
* @param array $arguments The arguments. |
|
53
|
|
|
* @return mixed|null Returns the native method result. |
|
54
|
|
|
*/ |
|
55
|
|
|
public function coreNativeMethodFunction(?string $function, array $arguments = []) { |
|
56
|
|
|
|
|
57
|
|
|
if (null === $function || false === function_exists($function)) { |
|
58
|
|
|
return null; |
|
59
|
|
|
} |
|
60
|
40 |
|
|
|
61
|
40 |
|
return call_user_func_array($function, $arguments); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Call a static method. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string|null $classname The classname. |
|
68
|
|
|
* @param string|null $method The method. |
|
69
|
40 |
|
* @param array $arguments The arguments. |
|
70
|
|
|
* @return mixed|null Returns the static method result. |
|
71
|
40 |
|
*/ |
|
72
|
|
|
public function coreStaticMethodFunction(?string $classname, ?string $method, array $arguments = []) { |
|
73
|
|
|
|
|
74
|
|
|
if (null === $classname || null === $method || false === method_exists($classname, $method)) { |
|
75
|
|
|
return null; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return call_user_func_array("$classname::$method", $arguments); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get a container parameter. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $name The name. |
|
85
|
|
|
* @return array|bool|string|int|float|null Returns the container parameter in case of success, null otherwise. |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getContainerParameterFunction(string $name) { |
|
88
|
|
|
return $this->getContainer()->getParameter($name); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get the Twig functions. |
|
93
|
|
|
* |
|
94
|
|
|
* @return TwigFunction[] Returns the Twig functions. |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getFunctions(): array { |
|
97
|
|
|
|
|
98
|
|
|
return [ |
|
99
|
|
|
new TwigFunction("getContainerParameter", [$this, "getContainerParameterFunction"]), |
|
100
|
|
|
new TwigFunction("coreNativeMethod", [$this, "coreNativeMethodFunction"]), |
|
101
|
|
|
new TwigFunction("coreStaticMethod", [$this, "coreStaticMethodFunction"]), |
|
102
|
|
|
]; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|