|
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
|
11 |
|
public function __construct( |
|
26
|
|
|
private readonly RouterInterface $router, |
|
27
|
|
|
) { |
|
28
|
11 |
|
} |
|
29
|
|
|
|
|
30
|
4 |
|
public function getFunctions(): array |
|
31
|
|
|
{ |
|
32
|
4 |
|
return [ |
|
33
|
4 |
|
new TwigFunction('ctrl_fqcn', $this->getControllerFqcn(...)), |
|
34
|
4 |
|
new TwigFunction('attr_if', $this->getAttributeIf(...)), |
|
35
|
4 |
|
new TwigFunction('aria_current_page_if', $this->getAriaCurrentPageIf(...)), |
|
36
|
4 |
|
]; |
|
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
|
8 |
|
public function getControllerFqcn(string $ctrlShortname): string |
|
51
|
|
|
{ |
|
52
|
8 |
|
if ($this->controllers === null) { |
|
53
|
8 |
|
$this->controllers = array_unique(array_map( |
|
54
|
8 |
|
static fn ($value) => u($value)->trimSuffix('::__invoke')->toString(), |
|
55
|
8 |
|
array_keys($this->router->getRouteCollection()->getAliases()) |
|
56
|
8 |
|
)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
8 |
|
foreach ($this->controllers as $controller) { |
|
60
|
|
|
/** @var class-string $controller */ |
|
61
|
8 |
|
if (u($controller)->endsWith($ctrlShortname)) { |
|
62
|
7 |
|
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
|
7 |
|
public function getAttributeIf(bool $condition, string $attribute, string $value): string |
|
82
|
|
|
{ |
|
83
|
7 |
|
if (!$condition) { |
|
84
|
7 |
|
return ''; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
7 |
|
return \sprintf(' %s="%s"', $attribute, $value); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
7 |
|
public function getAriaCurrentPageIf(bool $condition): string |
|
91
|
|
|
{ |
|
92
|
7 |
|
return $this->getAttributeIf($condition, 'aria-current', 'page'); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|