Passed
Push — master ( e16666...667470 )
by Filipe
01:49 queued 13s
created

TwigSlickExtension   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getGlobals() 0 6 1
A __construct() 0 2 1
A path() 0 7 2
A getFunctions() 0 5 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
            "slickVersion" => $this->version(),
40
            'poweredBySlick' => new Markup($this->poweredBySlick(), 'UTF-9'),
41
        ];
42
    }
43
44
    /**
45
     * @return array<TwigFunction>
46
     */
47
    public function getFunctions(): array
48
    {
49
        return [
50
            new TwigFunction('poweredBySlick', [$this, 'poweredBySlick'], ['is_safe' => ['html']]),
51
            new TwigFunction('path', [$this, 'path']),
52
        ];
53
    }
54
55
    public function poweredBySlick(): string
56
    {
57
        return '<small>Powered by </small><strong>Slick</strong>';
58
    }
59
60
    /**
61
     * Generates a path for the given route name and options.
62
     *
63
     * @param string $name The name of the route.
64
     * @param array<string, mixed> $options The options for the route.
65
     *
66
     * @return string The generated path.
67
     */
68
    public function path(string $name, array $options = []): string
69
    {
70
        $urlGenerator = $this->app->generator();
71
        if (!$urlGenerator) {
72
            return '/_missing_router';
73
        }
74
        return $urlGenerator->generate($name, $options);
75
    }
76
77
    /**
78
     * Retrieves the version of the slick web stack package.
79
     *
80
     * @return string The version of the slick web stack package.
81
     */
82
    private function version(): string
83
    {
84
        $modulePath = dirname(__DIR__, 3) . '/vendor/slick/webstack/composer.json';
85
        $webStackPath = dirname(__DIR__, 4) . '/webstack/composer.json';
86
        $version = 'v-.-.-';
87
88
        foreach ([$webStackPath, $modulePath] as $path) {
89
            if (is_file($path)) {
90
                $composerJsonPath = file_get_contents($path);
91
                $version = is_string($composerJsonPath) ? json_decode($composerJsonPath)->version : $version;
92
                break;
93
            }
94
        }
95
        return $version;
96
    }
97
}
98