1 | <?php |
||||
2 | |||||
3 | declare(strict_types=1); |
||||
4 | |||||
5 | namespace LAG\AdminBundle\Twig\Extension; |
||||
6 | |||||
7 | use LAG\AdminBundle\Application\Configuration\ApplicationConfiguration; |
||||
8 | use LAG\AdminBundle\Metadata\Link; |
||||
9 | use LAG\AdminBundle\Metadata\OperationInterface; |
||||
10 | use LAG\AdminBundle\Routing\UrlGenerator\UrlGeneratorInterface; |
||||
11 | use LAG\AdminBundle\Security\Helper\SecurityHelper; |
||||
12 | use LAG\AdminBundle\Twig\Render\ActionRendererInterface; |
||||
13 | use Twig\Extension\AbstractExtension; |
||||
14 | use Twig\TwigFunction; |
||||
15 | |||||
16 | class AdminExtension extends AbstractExtension |
||||
17 | { |
||||
18 | 5 | public function __construct( |
|||
19 | private bool $mediaBundleEnabled, |
||||
20 | private ApplicationConfiguration $applicationConfiguration, |
||||
21 | private SecurityHelper $security, |
||||
22 | private ActionRendererInterface $actionRenderer, |
||||
23 | 5 | private UrlGeneratorInterface $urlGenerator, |
|||
24 | 5 | ) { |
|||
25 | 5 | } |
|||
26 | 5 | ||||
27 | public function getFunctions(): array |
||||
28 | 1 | { |
|||
29 | return [ |
||||
30 | new TwigFunction('lag_admin_config', [$this, 'getConfigurationValue']), |
||||
31 | 1 | new TwigFunction('lag_admin_operation_allowed', [$this, 'isOperationAllowed']), |
|||
32 | 1 | new TwigFunction('lag_admin_action', [$this, 'renderAction'], ['is_safe' => ['html']]), |
|||
33 | 1 | new TwigFunction('lag_admin_operation_url', [$this, 'getOperationUrl'], ['is_safe' => ['html']]), |
|||
34 | 1 | new TwigFunction('lag_admin_media_bundle_enabled', [$this, 'isMediaBundleEnabled']), |
|||
35 | ]; |
||||
36 | } |
||||
37 | |||||
38 | 1 | public function getConfigurationValue(string $name): mixed |
|||
39 | { |
||||
40 | return $this->applicationConfiguration->get($name); |
||||
41 | 1 | } |
|||
42 | 1 | ||||
43 | public function isOperationAllowed(string $resourceName, string $operationName): bool |
||||
44 | { |
||||
45 | return $this->security->isOperationAllowed($resourceName, $operationName); |
||||
46 | } |
||||
47 | |||||
48 | /** |
||||
49 | 1 | * @param array<string, mixed> $options |
|||
50 | */ |
||||
51 | 1 | public function renderAction(Link $action, mixed $data = null, array $options = []): string |
|||
52 | { |
||||
53 | return $this->actionRenderer->render($action, $data, $options); |
||||
54 | } |
||||
55 | |||||
56 | public function getOperationUrl(OperationInterface $operation, mixed $data = null): string |
||||
57 | 1 | { |
|||
58 | return $this->urlGenerator->generateFromOperationName( |
||||
59 | 1 | $operation->getResource()->getName(), |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
60 | $operation->getName(), |
||||
0 ignored issues
–
show
It seems like
$operation->getName() can also be of type null ; however, parameter $operationName of LAG\AdminBundle\Routing\...rateFromOperationName() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
61 | $data, |
||||
62 | ); |
||||
63 | } |
||||
64 | |||||
65 | public function isMediaBundleEnabled(): bool |
||||
66 | { |
||||
67 | return $this->mediaBundleEnabled; |
||||
68 | } |
||||
69 | } |
||||
70 |