Completed
Pull Request — master (#4240)
by Axel
05:34
created

ThemeExtension::getSiteName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\ThemeModule\Twig\Extension;
15
16
use Exception;
17
use InvalidArgumentException;
18
use Twig\Extension\AbstractExtension;
19
use Twig\TwigFunction;
20
use Zikula\ThemeModule\Api\ApiInterface\PageAssetApiInterface;
21
use Zikula\ThemeModule\Engine\Asset;
22
use Zikula\ThemeModule\Engine\AssetBag;
23
24
class ThemeExtension extends AbstractExtension
25
{
26
    /**
27
     * @var PageAssetApiInterface
28
     */
29
    private $pageAssetApi;
30
31
    /**
32
     * @var Asset
33
     */
34
    private $assetHelper;
35
36
    public function __construct(
37
        PageAssetApiInterface $pageAssetApi,
38
        Asset $assetHelper
39
    ) {
40
        $this->pageAssetApi = $pageAssetApi;
41
        $this->assetHelper = $assetHelper;
42
    }
43
44
    public function getFunctions()
45
    {
46
        return [
47
            new TwigFunction('pageAddAsset', [$this, 'pageAddAsset']),
48
            new TwigFunction('getPreviewImagePath', [$this, 'getPreviewImagePath'], ['is_safe' => ['html']]),
49
            new TwigFunction('zasset', [$this, 'getAssetPath'])
50
        ];
51
    }
52
53
    /**
54
     * Zikula allows only the following asset types:
55
     * <ul>
56
     *  <li>stylesheet</li>
57
     *  <li>javascript</li>
58
     *  <li>header</li>
59
     *  <li>footer</li>
60
     * </ul>
61
     */
62
    public function pageAddAsset(string $type, string $value, int $weight = AssetBag::WEIGHT_DEFAULT): void
63
    {
64
        $this->pageAssetApi->add($type, $value, $weight);
65
    }
66
67
    /**
68
     * Get path to theme preview image.
69
     */
70
    public function getPreviewImagePath(string $themeName, string $size = 'medium'): string
71
    {
72
        if (!isset($themeName)) {
73
            throw new InvalidArgumentException('Invalid theme name.');
74
        }
75
76
        if (!in_array($size, ['large', 'medium', 'small'], true)) {
77
            $size = 'medium';
78
        }
79
80
        try {
81
            $imagePath = $this->assetHelper->resolve('@' . $themeName . ':images/preview_' . $size . '.png');
82
        } catch (Exception $exception) {
83
            $imagePath = $this->assetHelper->resolve('@ZikulaThemeModule:images/preview_' . $size . '.png');
84
        }
85
86
        return $imagePath;
87
    }
88
89
    /**
90
     * Resolves a given asset path.
91
     */
92
    public function getAssetPath(string $path): string
93
    {
94
        return $this->assetHelper->resolve($path);
95
    }
96
}
97