TwigSlickExtension   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 21
c 1
b 0
f 0
dl 0
loc 69
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A path() 0 4 2
A getFunctions() 0 5 1
A getGlobals() 0 7 1
A poweredBySlick() 0 3 1
A version() 0 14 4
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