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
|
2 |
|
public function getName() |
42
|
|
|
{ |
43
|
2 |
|
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
|
2 |
|
public function getFunctions() |
52
|
|
|
{ |
53
|
|
|
return [ |
54
|
|
|
new SimpleFunction('url', function($url) { |
|
|
|
|
55
|
|
|
$args = func_get_args(); |
56
|
|
|
return call_user_func_array([$this, 'getUrl'], $args); |
57
|
2 |
|
}), |
58
|
2 |
|
new SimpleFunction( |
59
|
2 |
|
'addCss', |
60
|
|
|
function($file, $path='/css', $attr = []) { |
61
|
|
|
return $this->addCss($file, $path, $attr); |
62
|
|
|
} |
63
|
2 |
|
), |
64
|
2 |
|
new SimpleFunction( |
65
|
2 |
|
'addJs', |
66
|
|
|
function($file, $path='/javascripts') { |
67
|
|
|
return $this->addJs($file, $path); |
68
|
|
|
} |
69
|
2 |
|
) |
70
|
2 |
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get request |
75
|
|
|
* @return Request |
76
|
|
|
*/ |
77
|
2 |
|
public function getRequest() |
78
|
|
|
{ |
79
|
2 |
|
if (null == $this->request) { |
80
|
2 |
|
$this->setRequest(Application::container()->get('request')); |
81
|
2 |
|
} |
82
|
2 |
|
return $this->request; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set HTML request message |
87
|
|
|
* |
88
|
|
|
* @param Request $request |
89
|
|
|
* |
90
|
|
|
* @return $this |
91
|
|
|
*/ |
92
|
10 |
|
public function setRequest(Request $request) |
93
|
|
|
{ |
94
|
10 |
|
$this->request = $request; |
95
|
10 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Creates an HTML style tag |
100
|
|
|
* |
101
|
|
|
* @param string $file |
102
|
|
|
* @param string $path |
103
|
|
|
* @param array $attr |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
2 |
|
public function addCss($file, $path='/css', $attr = []) |
108
|
|
|
{ |
109
|
2 |
|
$attr = array_merge(['rel' => 'stylesheet'], $attr); |
110
|
2 |
|
$output = []; |
111
|
2 |
|
foreach ($attr as $name => $value) { |
112
|
2 |
|
$output[] = "{$name}=\"{$value}\""; |
113
|
2 |
|
} |
114
|
2 |
|
$attr = implode(' ', $output); |
115
|
2 |
|
$file = str_replace('//', '', "{$path}/{$file}"); |
116
|
2 |
|
return sprintf('<link href="%s" %s>', $file, $attr); |
117
|
|
|
} |
118
|
|
|
|
119
|
2 |
|
public function addJs($file, $path='/javascripts') |
120
|
|
|
{ |
121
|
2 |
|
$file = str_replace('//', '', "{$path}/{$file}"); |
122
|
2 |
|
return sprintf('<script src="%s"></script>', $file); |
123
|
|
|
} |
124
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.