|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace eZ\Publish\Core\MVC\Symfony\ExpressionLanguage; |
|
10
|
|
|
|
|
11
|
|
|
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException; |
|
12
|
|
|
use eZ\Publish\Core\MVC\Symfony\View\ContentView; |
|
13
|
|
|
use eZ\Publish\Core\MVC\Symfony\View\VariableProviderRegistry; |
|
14
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionFunction; |
|
15
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface; |
|
16
|
|
|
|
|
17
|
|
|
final class TwigVariableProviderExtension implements ExpressionFunctionProviderInterface |
|
18
|
|
|
{ |
|
19
|
|
|
public const PROVIDER_REGISTRY_PARAMETER = 'providerRegistry'; |
|
20
|
|
|
public const VIEW_PARAMETER = 'view'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @return \Symfony\Component\ExpressionLanguage\ExpressionFunction[] |
|
24
|
|
|
*/ |
|
25
|
|
|
public function getFunctions(): array |
|
26
|
|
|
{ |
|
27
|
|
|
return [ |
|
28
|
|
|
new ExpressionFunction( |
|
29
|
|
|
'twig_variable_provider', |
|
30
|
|
|
function (string $identifier) { |
|
|
|
|
|
|
31
|
|
|
return 'Not implemented: Not a Dependency Injection expression'; |
|
32
|
|
|
}, |
|
33
|
|
|
function (array $variables, string $identifier) { |
|
34
|
|
|
if (!$this->hasParameterProvider($variables)) { |
|
35
|
|
|
throw new InvalidArgumentException( |
|
36
|
|
|
self::PROVIDER_REGISTRY_PARAMETER, |
|
37
|
|
|
'Expression parameter is not a valid type of ' . VariableProviderRegistry::class |
|
38
|
|
|
); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$view = $variables[self::VIEW_PARAMETER] ?? new ContentView(); |
|
42
|
|
|
$providerRegistry = $variables[self::PROVIDER_REGISTRY_PARAMETER]; |
|
43
|
|
|
|
|
44
|
|
|
$provider = $providerRegistry->getTwigVariableProvider($identifier); |
|
45
|
|
|
|
|
46
|
|
|
return $provider->getTwigVariables($view, $variables); |
|
47
|
|
|
} |
|
48
|
|
|
), |
|
49
|
|
|
]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function hasParameterProvider(array $variables): bool |
|
53
|
|
|
{ |
|
54
|
|
|
return !empty($variables[self::PROVIDER_REGISTRY_PARAMETER]) |
|
55
|
|
|
&& $variables[self::PROVIDER_REGISTRY_PARAMETER] instanceof VariableProviderRegistry; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.