1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of template |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Slick\Template\Extension\Twig; |
13
|
|
|
|
14
|
|
|
use Slick\Template\Extension\SlickApp; |
15
|
|
|
use Twig\Extension\AbstractExtension; |
16
|
|
|
use Twig\Extension\GlobalsInterface; |
17
|
|
|
use Twig\Markup; |
18
|
|
|
use Twig\TwigFunction; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* TwigSlickExtension |
22
|
|
|
* |
23
|
|
|
* @package Slick\Template\Extension\Twig |
24
|
|
|
*/ |
25
|
|
|
final class TwigSlickExtension extends AbstractExtension implements GlobalsInterface |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
public function __construct(private readonly SlickApp $app) |
29
|
|
|
{ |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return array<string, mixed> |
34
|
|
|
*/ |
35
|
|
|
public function getGlobals(): array |
36
|
|
|
{ |
37
|
|
|
return [ |
38
|
|
|
"app" => $this->app, |
39
|
|
|
"flash" => $this->app->flash(), |
40
|
|
|
"slickVersion" => $this->version(), |
41
|
|
|
'poweredBySlick' => new Markup($this->poweredBySlick(), 'UTF-9'), |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return array<TwigFunction> |
47
|
|
|
*/ |
48
|
|
|
public function getFunctions(): array |
49
|
|
|
{ |
50
|
|
|
return [ |
51
|
|
|
new TwigFunction('poweredBySlick', [$this, 'poweredBySlick'], ['is_safe' => ['html']]), |
52
|
|
|
new TwigFunction('path', [$this, 'path']), |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function poweredBySlick(): string |
57
|
|
|
{ |
58
|
|
|
return '<small>Powered by </small><strong>Slick</strong>'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Generates a path for the given route name and options. |
63
|
|
|
* |
64
|
|
|
* @param string $name The name of the route. |
65
|
|
|
* @param array<string, mixed> $options The options for the route. |
66
|
|
|
* |
67
|
|
|
* @return string The generated path. |
68
|
|
|
*/ |
69
|
|
|
public function path(string $name, array $options = []): string |
70
|
|
|
{ |
71
|
|
|
$urlGenerator = $this->app->generator(); |
72
|
|
|
return $urlGenerator ? $urlGenerator->generate($name, $options) : '/_missing_router'; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Retrieves the version of the slick web stack package. |
77
|
|
|
* |
78
|
|
|
* @return string The version of the slick web stack package. |
79
|
|
|
*/ |
80
|
|
|
private function version(): string |
81
|
|
|
{ |
82
|
|
|
$modulePath = dirname(__DIR__, 3) . '/vendor/slick/webstack/composer.json'; |
83
|
|
|
$webStackPath = dirname(__DIR__, 4) . '/webstack/composer.json'; |
84
|
|
|
$version = 'v-.-.-'; |
85
|
|
|
|
86
|
|
|
foreach ([$webStackPath, $modulePath] as $path) { |
87
|
|
|
if (is_file($path)) { |
88
|
|
|
$composerJsonPath = file_get_contents($path); |
89
|
|
|
$version = is_string($composerJsonPath) ? json_decode($composerJsonPath)->version : $version; |
90
|
|
|
break; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
return $version; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|