Completed
Push — master ( 8e9f7c...f37cad )
by Filipe
07:19
created

HtmlExtension::getFunctions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 21
ccs 0
cts 19
cp 0
rs 9.3142
cc 1
eloc 13
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * This file is part of slick/mvc package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Mvc\Renderer;
11
12
use Slick\Http\PhpEnvironment\Request;
13
use Slick\Mvc\Application;
14
use Slick\Mvc\Controller\UrlUtils;
15
use Slick\Template\EngineExtensionInterface;
16
use Slick\Template\Extension\AbstractTwigExtension;
17
use Twig_SimpleFunction as SimpleFunction;
18
19
/**
20
 * HTML twig extension
21
 *
22
 * @package Slick\Mvc\Renderer
23
 */
24
class HtmlExtension extends AbstractTwigExtension implements EngineExtensionInterface
25
{
26
    /**
27
     * For url parse
28
     */
29
    use UrlUtils;
30
31
    /**
32
     * @var Request
33
     */
34
    protected $request;
35
36
    /**
37
     * Returns the name of the extension.
38
     *
39
     * @return string The extension name
40
     */
41
    public function getName()
42
    {
43
        return "HTML helper extension";
44
    }
45
46
    /**
47
     * Returns a list of functions to add to the existing list.
48
     *
49
     * @return array An array of functions
50
     */
51
    public function getFunctions()
52
    {
53
        return [
54
            new SimpleFunction('url', function($url) {
0 ignored issues
show
Unused Code introduced by
The parameter $url is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
                $args = func_get_args();
56
                return call_user_func_array([$this, 'getUrl'], $args);
57
            }),
58
            new SimpleFunction(
59
                'addCss',
60
                function($file, $path='/css', $attr = []) {
61
                    return $this->addCss($file, $path, $attr);
62
                }
63
            ),
64
            new SimpleFunction(
65
                'addJs',
66
                function($file, $path='/stylesheets') {
67
                    return $this->addJs($file, $path);
68
                }
69
            )
70
        ];
71
    }
72
73
    /**
74
     * @return Request
75
     */
76
    public function getRequest()
77
    {
78
        if (null == $this->request) {
79
            $this->request = Application::container()->get('request');
80
        }
81
        return $this->request;
82
    }
83
84
    /**
85
     * Creates an HTML style tag
86
     *
87
     * @param string $file
88
     * @param string $path
89
     * @param array $attr
90
     *
91
     * @return string
92
     */
93
    public function addCss($file, $path='/css', $attr = [])
94
    {
95
        $attr = array_merge(['rel' => 'stylesheet'], $attr);
96
        $output = [];
97
        foreach ($attr as $name => $value) {
98
            $output[] = "{$name}=\"{$value}\"";
99
        }
100
        $attr = implode(' ', $output);
101
        $file = str_replace('//', '', "{$path}/{$file}");
102
        return sprintf('<link href="%s" %s>', $file, $attr);
103
    }
104
105
    public function addJs($file, $path='/stylesheets')
106
    {
107
        $file = str_replace('//', '', "{$path}/{$file}");
108
        return sprintf('<script src="%s"></script>', $file);
109
    }
110
}