Passed
Push — master ( 2a327d...e16666 )
by Filipe
01:33 queued 14s
created

TwigSlickExtension::getFunctions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
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
        ];
52
    }
53
54
    public function poweredBySlick(): string
55
    {
56
        return '<small>Powered by </small><strong>Slick</strong>';
57
    }
58
59
    /**
60
     * Retrieves the version of the slick web stack package.
61
     *
62
     * @return string The version of the slick web stack package.
63
     */
64
    private function version(): string
65
    {
66
        $modulePath = dirname(__DIR__, 3) . '/vendor/slick/webstack/composer.json';
67
        $webStackPath = dirname(__DIR__, 4) . '/webstack/composer.json';
68
        $version = 'v-.-.-';
69
70
        foreach ([$webStackPath, $modulePath] as $path) {
71
            if (is_file($path)) {
72
                $composerJsonPath = file_get_contents($path);
73
                $version = is_string($composerJsonPath) ? json_decode($composerJsonPath)->version : $version;
74
                break;
75
            }
76
        }
77
        return $version;
78
    }
79
}
80