-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathTwigExtension.php
66 lines (52 loc) · 1.73 KB
/
TwigExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
declare(strict_types=1);
namespace Bolt\Article;
use Bolt\Common\Json;
use Symfony\Component\Filesystem\Path;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class TwigExtension extends AbstractExtension
{
/** @var ArticleConfig */
private $articleConfig;
public function __construct(ArticleConfig $articleConfig)
{
$this->articleConfig = $articleConfig;
}
public function getFunctions(): array
{
$safe = [
'is_safe' => ['html'],
];
return [
new TwigFunction('article_settings', [$this, 'articleSettings'], $safe),
new TwigFunction('article_includes', [$this, 'articleIncludes'], $safe),
];
}
public function articleSettings(): string
{
$settings = $this->articleConfig->getConfig();
return Json::json_encode($settings, JSON_HEX_QUOT | JSON_HEX_APOS);
}
public function articleIncludes(): string
{
$used = $this->articleConfig->getConfig()['plugins'];
$plugins = collect($this->articleConfig->getPlugins());
$output = '';
foreach ($used as $item) {
if (! is_string($item) || ! $plugins->get($item)) {
continue;
}
foreach ($plugins->get($item) as $file) {
if (Path::getExtension($file) === 'css') {
$output .= sprintf('<link rel="stylesheet" href="/assets/article/plugins/%s">', $file);
}
if (Path::getExtension($file) === 'js') {
$output .= sprintf('<script src="/assets/article/plugins/%s"></script>', $file);
}
$output .= "\n";
}
}
return $output;
}
}