1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Twig\Extension; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\Routing\RouterInterface; |
8
|
|
|
use Twig\Extension\AbstractExtension; |
9
|
|
|
use Twig\TwigFunction; |
10
|
|
|
|
11
|
|
|
use function Symfony\Component\String\u; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Routing related stuff. |
15
|
|
|
* |
16
|
|
|
* @see RoutingExtensionTest |
17
|
|
|
*/ |
18
|
|
|
final class RoutingExtension extends AbstractExtension |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var array<int, string>|null |
22
|
|
|
*/ |
23
|
|
|
private ?array $controllers = null; |
24
|
|
|
|
25
|
15 |
|
public function __construct( |
26
|
|
|
private readonly RouterInterface $router, |
27
|
|
|
) { |
28
|
15 |
|
} |
29
|
|
|
|
30
|
6 |
|
public function getFunctions(): array |
31
|
|
|
{ |
32
|
6 |
|
return [ |
33
|
6 |
|
new TwigFunction('ctrl_fqcn', $this->getControllerFqcn(...)), |
34
|
6 |
|
new TwigFunction('attr_if', $this->getAttributeIf(...)), |
35
|
6 |
|
new TwigFunction('aria_current_page_if', $this->getAriaCurrentPageIf(...)), |
36
|
6 |
|
]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* This function returns a complete controller FQCN from a short name. |
41
|
|
|
* If you have multiple controllers/actions with the same name, you can include |
42
|
|
|
* the parent namespace to select the good one like the last examples below. |
43
|
|
|
* |
44
|
|
|
* @example ComposerAction --> App\Controller\ComposerAction |
45
|
|
|
* @example Product\ListAction --> App\Controller\Product\ListAction |
46
|
|
|
* @example Category\ListAction --> App\Controller\Category\ListAction |
47
|
|
|
* |
48
|
|
|
* @return class-string |
49
|
|
|
*/ |
50
|
11 |
|
public function getControllerFqcn(string $ctrlShortname): string |
51
|
|
|
{ |
52
|
11 |
|
if ($this->controllers === null) { |
53
|
11 |
|
$this->controllers = array_unique(array_map( |
54
|
11 |
|
static fn ($value) => u($value)->trimSuffix('::__invoke')->toString(), |
55
|
11 |
|
array_keys($this->router->getRouteCollection()->getAliases()) |
56
|
11 |
|
)); |
57
|
|
|
} |
58
|
|
|
|
59
|
11 |
|
foreach ($this->controllers as $controller) { |
60
|
|
|
/** @var class-string $controller */ |
61
|
11 |
|
if (u($controller)->endsWith($ctrlShortname)) { |
62
|
10 |
|
return $controller; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// If all your ADR controllers live in the "App\Controller\" namespace, |
67
|
|
|
// then you probably don't need all the code above, just use: |
68
|
|
|
// |
69
|
|
|
// return 'App\\Controller\\'. $ctrlShortname; |
70
|
|
|
// |
71
|
|
|
// If the route is not found, then Twig raises a "RouteNotFoundException". |
72
|
|
|
|
73
|
1 |
|
throw new \InvalidArgumentException('No controller found for the "'.$ctrlShortname.'" shortname.'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns an HTML attribute with a given value only if a condition is met. |
78
|
|
|
* |
79
|
|
|
* @see templates/base.html.twig |
80
|
|
|
*/ |
81
|
10 |
|
public function getAttributeIf(bool $condition, string $attribute, string $value): string |
82
|
|
|
{ |
83
|
10 |
|
if (!$condition) { |
84
|
10 |
|
return ''; |
85
|
|
|
} |
86
|
|
|
|
87
|
10 |
|
return \sprintf(' %s="%s"', $attribute, $value); |
88
|
|
|
} |
89
|
|
|
|
90
|
10 |
|
public function getAriaCurrentPageIf(bool $condition): string |
91
|
|
|
{ |
92
|
10 |
|
return $this->getAttributeIf($condition, 'aria-current', 'page'); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|