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\Bundle\CoreBundle\Site\SiteDefinitionInterface; |
21
|
|
|
use Zikula\ThemeModule\Api\ApiInterface\PageAssetApiInterface; |
22
|
|
|
use Zikula\ThemeModule\Engine\Asset; |
23
|
|
|
use Zikula\ThemeModule\Engine\AssetBag; |
24
|
|
|
|
25
|
|
|
class ThemeExtension extends AbstractExtension |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var SiteDefinitionInterface |
29
|
|
|
*/ |
30
|
|
|
private $site; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var PageAssetApiInterface |
34
|
|
|
*/ |
35
|
|
|
private $pageAssetApi; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Asset |
39
|
|
|
*/ |
40
|
|
|
private $assetHelper; |
41
|
|
|
|
42
|
|
|
public function __construct( |
43
|
|
|
SiteDefinitionInterface $site, |
44
|
|
|
PageAssetApiInterface $pageAssetApi, |
45
|
|
|
Asset $assetHelper |
46
|
|
|
) { |
47
|
|
|
$this->site = $site; |
48
|
|
|
$this->pageAssetApi = $pageAssetApi; |
49
|
|
|
$this->assetHelper = $assetHelper; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getFunctions() |
53
|
|
|
{ |
54
|
|
|
return [ |
55
|
|
|
new TwigFunction('pageAddAsset', [$this, 'pageAddAsset']), |
56
|
|
|
new TwigFunction('getPreviewImagePath', [$this, 'getPreviewImagePath'], ['is_safe' => ['html']]), |
57
|
|
|
new TwigFunction('zasset', [$this, 'getAssetPath']), |
58
|
|
|
new TwigFunction('siteName', [$this, 'getSiteName']), |
59
|
|
|
new TwigFunction('siteSlogan', [$this, 'getSiteSlogan']) |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Zikula allows only the following asset types: |
65
|
|
|
* <ul> |
66
|
|
|
* <li>stylesheet</li> |
67
|
|
|
* <li>javascript</li> |
68
|
|
|
* <li>header</li> |
69
|
|
|
* <li>footer</li> |
70
|
|
|
* </ul> |
71
|
|
|
*/ |
72
|
|
|
public function pageAddAsset(string $type, string $value, int $weight = AssetBag::WEIGHT_DEFAULT): void |
73
|
|
|
{ |
74
|
|
|
$this->pageAssetApi->add($type, $value, $weight); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get path to theme preview image. |
79
|
|
|
*/ |
80
|
|
|
public function getPreviewImagePath(string $themeName, string $size = 'medium'): string |
81
|
|
|
{ |
82
|
|
|
if (!isset($themeName)) { |
83
|
|
|
throw new InvalidArgumentException('Invalid theme name.'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (!in_array($size, ['large', 'medium', 'small'])) { |
87
|
|
|
$size = 'medium'; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
try { |
91
|
|
|
$imagePath = $this->assetHelper->resolve('@' . $themeName . ':images/preview_' . $size . '.png'); |
92
|
|
|
} catch (Exception $exception) { |
93
|
|
|
$imagePath = $this->assetHelper->resolve('@ZikulaThemeModule:images/preview_' . $size . '.png'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $imagePath; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Resolves a given asset path. |
101
|
|
|
*/ |
102
|
|
|
public function getAssetPath(string $path): string |
103
|
|
|
{ |
104
|
|
|
return $this->assetHelper->resolve($path); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Returns site name. |
109
|
|
|
*/ |
110
|
|
|
public function getSiteName(): string |
111
|
|
|
{ |
112
|
|
|
return $this->site->getName(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns site slogan. |
117
|
|
|
*/ |
118
|
|
|
public function getSiteSlogan(): string |
119
|
|
|
{ |
120
|
|
|
return $this->site->getSlogan(); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|