syntaxHighlighterContentFunction()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 15
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the core-bundle package.
5
 *
6
 * (c) 2017 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\CoreBundle\Twig\Extension\Assets;
13
14
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
15
use Twig\TwigFilter;
16
use Twig\TwigFunction;
17
use WBW\Bundle\CoreBundle\Assets\SyntaxHighlighter\SyntaxHighlighterConfig;
18
use WBW\Bundle\CoreBundle\Assets\SyntaxHighlighter\SyntaxHighlighterDefaults;
19
use WBW\Bundle\CoreBundle\Assets\SyntaxHighlighter\SyntaxHighlighterStrings;
20
use WBW\Library\Types\Helper\ArrayHelper;
21
22
/**
23
 * SyntaxHighlighter Twig extension.
24
 *
25
 * @author webeweb <https://github.com/webeweb>
26
 * @package WBW\Bundle\CoreBundle\Twig\Extension\Assets
27
 */
28
class SyntaxHighlighterTwigExtension extends AbstractSyntaxHighlighterTwigExtension {
29
30
    /**
31
     * Service name.
32
     *
33
     * @var string
34
     */
35
    const SERVICE_NAME = "wbw.core.twig.extension.assets.syntax_highlighter";
36
37
    /**
38
     * Get the Twig filters.
39
     *
40
     * @return TwigFilter[] Returns the Twig filters.
41
     */
42
    public function getFilters(): array {
43
44
        return [
45
            new TwigFilter("syntaxHighlighterScript", [$this, "syntaxHighlighterScriptFilter"], ["is_safe" => ["html"]]),
46
            new TwigFilter("shScript", [$this, "syntaxHighlighterScriptFilter"], ["is_safe" => ["html"]]),
47
        ];
48
    }
49
50
    /**
51
     * Get the Twig functions.
52
     *
53
     * @return TwigFunction[] Returns the Twig functions.
54
     */
55
    public function getFunctions(): array {
56
57
        return [
58
            new TwigFunction("syntaxHighlighterConfig", [$this, "syntaxHighlighterConfigFunction"], ["is_safe" => ["html"]]),
59
            new TwigFunction("shConfig", [$this, "syntaxHighlighterConfigFunction"], ["is_safe" => ["html"]]),
60
61
            new TwigFunction("syntaxHighlighterContent", [$this, "syntaxHighlighterContentFunction"], ["is_safe" => ["html"]]),
62
            new TwigFunction("shContent", [$this, "syntaxHighlighterContentFunction"], ["is_safe" => ["html"]]),
63
64
            new TwigFunction("syntaxHighlighterDefaults", [$this, "syntaxHighlighterDefaultsFunction"], ["is_safe" => ["html"]]),
65
            new TwigFunction("shDefaults", [$this, "syntaxHighlighterDefaultsFunction"], ["is_safe" => ["html"]]),
66
67
            new TwigFunction("syntaxHighlighterScript", [$this, "syntaxHighlighterScriptFilter"], ["is_safe" => ["html"]]),
68
            new TwigFunction("shScript", [$this, "syntaxHighlighterScriptFilter"], ["is_safe" => ["html"]]),
69
70
            new TwigFunction("syntaxHighlighterStrings", [$this, "syntaxHighlighterStringsFunction"], ["is_safe" => ["html"]]),
71
            new TwigFunction("shStrings", [$this, "syntaxHighlighterStringsFunction"], ["is_safe" => ["html"]]),
72
        ];
73
    }
74
75
    /**
76
     * Display a SyntaxHighlighter config.
77
     *
78
     * @param SyntaxHighlighterConfig $config The SyntaxHighlighter config.
79
     * @return string Returns the SyntaxHighlighter config.
80
     */
81
    public function syntaxHighlighterConfigFunction(SyntaxHighlighterConfig $config): string {
82
        return $this->syntaxHighlighterConfig($config);
83
    }
84
85
    /**
86
     * Display a SyntaxHighlighter content.
87
     *
88
     * @param array $args The arguments.
89
     * @return string Returns the SyntaxHighlighter content.
90
     * @throws FileNotFoundException Throws a file not found exception if the file does not exists.
91
     */
92
    public function syntaxHighlighterContentFunction(array $args = []): string {
93
94
        $tag      = ArrayHelper::get($args, "tag", "pre");
95
        $language = ArrayHelper::get($args, "language", "php");
96
        $filename = ArrayHelper::get($args, "filename");
97
        $content  = ArrayHelper::get($args, "content");
98
99
        if (null !== $filename) {
100
            if (false === file_exists($filename)) {
101
                throw new FileNotFoundException(null, 500, null, $filename);
102
            }
103
            $content = file_get_contents($filename);
104
        }
105
106
        return $this->syntaxHighlighterContent($tag, $language, $content);
107
    }
108
109
    /**
110
     * Display a SyntaxHighlighter defaults.
111
     *
112
     * @param SyntaxHighlighterDefaults $defaults The SyntaxHighlighter defaults.
113
     * @return string Returns the SyntaxHighlighter defaults.
114
     */
115
    public function syntaxHighlighterDefaultsFunction(SyntaxHighlighterDefaults $defaults): string {
116
        return $this->syntaxHighlighterDefaults($defaults);
117
    }
118
119
    /**
120
     * Display a SyntaxHighlighter script.
121
     *
122
     * @param string $content The content.
123
     * @return string Returns the SyntaxHighlighter script.
124
     */
125
    public function syntaxHighlighterScriptFilter(string $content): string {
126
        return static::coreHtmlElement("script", "\n" . $content . "\n", ["type" => "text/javascript"]);
127
    }
128
129
    /**
130
     * Display a SyntaxHighlighter strings.
131
     *
132
     * @param SyntaxHighlighterStrings $strings The SyntaxHighlighter strings.
133
     * @return string Returns the SyntaxHighlighter strings.
134
     */
135
    public function syntaxHighlighterStringsFunction(SyntaxHighlighterStrings $strings): string {
136
        return $this->syntaxHighlighterStrings($strings);
137
    }
138
}
139