Passed
Push — master ( 38868e...4e6da1 )
by WEBEWEB
02:05
created

syntaxHighlighterDefaultsFunction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the syntaxhighligter-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\SyntaxHighlighterBundle\Twig\Extension;
13
14
use Twig_SimpleFilter;
15
use Twig_SimpleFunction;
16
use WBW\Bundle\SyntaxHighlighterBundle\API\SyntaxHighlighterConfig;
17
use WBW\Bundle\SyntaxHighlighterBundle\API\SyntaxHighlighterDefaults;
18
use WBW\Bundle\SyntaxHighlighterBundle\API\SyntaxHighlighterStrings;
19
use WBW\Library\Core\Exception\IO\FileNotFoundException;
20
use WBW\Library\Core\Utility\Argument\ArrayUtility;
21
use WBW\Library\Core\Utility\Argument\StringUtility;
22
use WBW\Library\Core\Utility\IO\FileUtility;
23
24
/**
25
 * SyntaxHighlighter Twig extension.
26
 *
27
 * @author webeweb <https://github.com/webeweb/>
28
 * @package WBW\Bundle\SyntaxHighlighterBundle\Twig\Extension
29
 */
30
class SyntaxHighlighterTwigExtension extends AbstractSyntaxHighlighterTwigExtension {
31
32
    /**
33
     * Service name.
34
     *
35
     * @var string
36
     */
37
    const SERVICE_NAME = "webeweb.bundle.syntaxhighlighterbundle.twig.extension.syntaxhighlighter";
38
39
    /**
40
     * Constructor.
41
     */
42
    public function __construct() {
43
        parent::__construct();
44
    }
45
46
    /**
47
     * Get the Twig filters.
48
     *
49
     * @return Twig_SimpleFilter[] Returns the Twig filters.
50
     */
51
    public function getFilters() {
52
        return [
53
            new Twig_SimpleFilter("syntaxHighlighterScript", [$this, "syntaxHighlighterScriptFilter"], ["is_safe" => ["html"]]),
54
        ];
55
    }
56
57
    /**
58
     * Get the Twig functions.
59
     *
60
     * @return Twig_SimpleFunction[] Returns the Twig functions.
61
     */
62
    public function getFunctions() {
63
        return [
64
            new Twig_SimpleFunction("syntaxHighlighterConfig", [$this, "syntaxHighlighterConfigFunction"], ["is_safe" => ["html"]]),
65
            new Twig_SimpleFunction("syntaxHighlighterContent", [$this, "syntaxHighlighterContentFunction"], ["is_safe" => ["html"]]),
66
            new Twig_SimpleFunction("syntaxHighlighterDefaults", [$this, "syntaxHighlighterDefaultsFunction"], ["is_safe" => ["html"]]),
67
            new Twig_SimpleFunction("syntaxHighlighterStrings", [$this, "syntaxHighlighterStringsFunction"], ["is_safe" => ["html"]]),
68
        ];
69
    }
70
71
    /**
72
     * Displays a SyntaxHighlighter content.
73
     *
74
     * @param array $args The arguments.
75
     * @return string Returns the SyntaxHighlighter content.
76
     * @throws FileNotFoundException Throws a file not found exception if the file does not exists.
77
     */
78
    public function syntaxHighlighterContentFunction(array $args = []) {
79
80
        // Get the parameters.
81
        $tag      = ArrayUtility::get($args, "tag", "pre");
82
        $language = ArrayUtility::get($args, "language", "php");
83
        $filename = ArrayUtility::get($args, "filename");
84
        $content  = ArrayUtility::get($args, "content");
85
86
        // Check the filename.
87
        if (null !== $filename) {
88
89
            // Get the file contents.
90
            $content = FileUtility::getContents($filename);
91
        }
92
93
        // Return the HTML.
94
        return $this->syntaxHighlighterContent($tag, $language, $content);
95
    }
96
97
    /**
98
     * Displays a SyntaxHighlighter config.
99
     *
100
     * @param SyntaxHighlighterConfig $config The SyntaxHighlighter config.
101
     * @return string Returns the SyntaxHighlighter config.
102
     */
103
    public function syntaxHighlighterConfigFunction(SyntaxHighlighterConfig $config) {
104
        return $this->syntaxHighlighterConfig($config);
105
    }
106
107
    /**
108
     * Displays a SyntaxHighlighter defaults.
109
     *
110
     * @param SyntaxHighlighterDefaults $defaults The SyntaxHighlighter defaults.
111
     * @return string Returns the SyntaxHighlighter defaults.
112
     */
113
    public function syntaxHighlighterDefaultsFunction(SyntaxHighlighterDefaults $defaults) {
114
        return $this->syntaxHighlighterDefaults($defaults);
115
    }
116
117
    /**
118
     * Displays a SyntaxHighlighter script.
119
     *
120
     * @param string $content The content.
121
     * @return string Returns the SyntaxHighlighter script.
122
     */
123
    public function syntaxHighlighterScriptFilter($content) {
124
125
        // Initialize the template.
126
        $template = "<script type=\"text/javascript\">\n%innerHTML%\n</script>";
127
128
        // Return the HTML.
129
        return StringUtility::replace($template, ["%innerHTML%"], [$content]);
130
    }
131
132
    /**
133
     * Displays a SyntaxHighlighter strings.
134
     *
135
     * @param SyntaxHighlighterStrings $strings The SyntaxHighlighter strings.
136
     * @return string Returns the SyntaxHighlighter strings.
137
     */
138
    public function syntaxHighlighterStringsFunction(SyntaxHighlighterStrings $strings) {
139
        return $this->syntaxHighlighterStrings($strings);
140
    }
141
142
}
143