Passed
Push — master ( 231b74...b288a1 )
by WEBEWEB
04:16
created

syntaxHighlighterFunction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 16
rs 9.4285
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_SimpleFunction;
15
use WBW\Library\Core\Exception\IO\FileNotFoundException;
16
use WBW\Library\Core\Utility\Argument\ArrayUtility;
17
use WBW\Library\Core\Utility\IO\FileUtility;
18
19
/**
20
 * SyntaxHighlighter Twig extension.
21
 *
22
 * @author webeweb <https://github.com/webeweb/>
23
 * @package WBW\Bundle\SyntaxHighlighterBundle\Twig\Extension
24
 */
25
class SyntaxHighlighterTwigExtension extends AbstractSyntaxHighlighterTwigExtension {
26
27
    /**
28
     * Service name.
29
     *
30
     * @var string
31
     */
32
    const SERVICE_NAME = "webeweb.bundle.syntaxhighlighterbundle.twig.extension.syntaxhighlighter";
33
34
    /**
35
     * Constructor.
36
     */
37
    public function __construct() {
38
        parent::__construct();
39
    }
40
41
    /**
42
     * Displays a SyntaxHighlighter.
43
     *
44
     * @param array $args The arguments.
45
     * @return string Returns the SyntaxHighlighter.
46
     * @throws FileNotFoundException Throws a file not found exception if the file does not exists.
47
     */
48
    public function syntaxHighlighterFunction(array $args = []) {
49
50
        // Get the parameters.
51
        $language = ArrayUtility::get($args, "language", "php");
52
        $filename = ArrayUtility::get($args, "filename");
53
        $content  = ArrayUtility::get($args, "content");
54
55
        // Check the filename.
56
        if (null !== $filename) {
57
58
            // Get the file contents.
59
            $content = FileUtility::getContents($filename);
60
        }
61
62
        // Return the HTML.
63
        return $this->syntaxHighlighter($language, $content);
64
    }
65
66
    /**
67
     * Get the Twig functions.
68
     *
69
     * @return Twig_SimpleFunction[] Returns the Twig functions.
70
     */
71
    public function getFunctions() {
72
        return [
73
            new Twig_SimpleFunction("syntaxHighlighter", [$this, "syntaxHighlighterFunction"], ["is_safe" => ["html"]]),
74
        ];
75
    }
76
77
}
78