|
1
|
|
|
<?php |
|
2
|
|
|
namespace Mouf\Html\Utils\WebLibraryManager; |
|
3
|
|
|
|
|
4
|
|
|
use Mouf\Html\HtmlElement\HtmlElementInterface; |
|
5
|
|
|
use Mouf\Html\Renderer\RendererInterface; |
|
6
|
|
|
use Mouf\Html\HtmlElement\HtmlString; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* This class is in charge of including and keeping track of Javascript and CSS libraries into an HTML page. |
|
10
|
|
|
* <p>JS and CSS files are grouped into <b>WebLibraries</b>. If you want to add a new library, just create an instance |
|
11
|
|
|
* of the <b>WebLibrary</b> class, and add it to the <b>WebLibraryManager</b>.</p> |
|
12
|
|
|
* <p>You can use the <b>WebLibraryManager</b> class to add JS/CSS libraries. It will keep track of dependencies and ensure each file is included |
|
13
|
|
|
* only once.</p> |
|
14
|
|
|
|
|
15
|
|
|
* <p>If you have specific needs and don't want to use the <b>WebLibrary</b> class, you can either create your own class |
|
16
|
|
|
* that implements the WebLibraryInterface, or provide your own "renderer".</p> |
|
17
|
|
|
* |
|
18
|
|
|
* |
|
19
|
|
|
* @author David Négrier |
|
20
|
|
|
*/ |
|
21
|
|
|
class WebLibraryManager implements HtmlElementInterface { |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The array of all included libraries. |
|
25
|
|
|
* |
|
26
|
|
|
* @var WebLibraryInterface[] |
|
27
|
|
|
*/ |
|
28
|
|
|
private $webLibraries = array(); |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* false if the toHtml method has not yet been called, true if it has been called. |
|
32
|
|
|
* @var boolean |
|
33
|
|
|
*/ |
|
34
|
|
|
private $rendered = false; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The renderer used by the application. Usually, this points to the 'defaultRenderer' instance. |
|
38
|
|
|
* |
|
39
|
|
|
* @var RendererInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $renderer; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* |
|
45
|
|
|
* @param RendererInterface $renderer The renderer used by the application. Usually, this points to the 'defaultRenderer' instance. |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(RendererInterface $renderer) { |
|
48
|
|
|
$this->renderer = $renderer; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Adds a library to the list of libraries that should be loaded in the web page. |
|
53
|
|
|
* <p>The function will also load the dependencies (if any) and will have no effect if the library has already been loaded.</p> |
|
54
|
|
|
* |
|
55
|
|
|
* @param WebLibraryInterface $library |
|
56
|
|
|
*/ |
|
57
|
|
|
public function addLibrary(WebLibraryInterface $library) { |
|
58
|
|
|
if ($this->rendered) { |
|
59
|
|
|
throw new WebLibraryException("The libraries have already been rendered. This call to addLibrary should be performed BEFORE the toHtml method of WebLibraryManager is called."); |
|
60
|
|
|
} |
|
61
|
|
|
if (array_search($library, $this->webLibraries) === false) { |
|
62
|
|
|
// Let's start by adding dependencies. |
|
63
|
|
|
$dependencies = $library->getDependencies(); |
|
64
|
|
|
if ($dependencies) { |
|
|
|
|
|
|
65
|
|
|
foreach ($dependencies as $dependency) { |
|
66
|
|
|
/* @var $dependency WebLibraryInterface */ |
|
67
|
|
|
$this->addLibrary($dependency); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$this->webLibraries[] = $library; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* The list of all libraries that should be loaded in the web page. |
|
77
|
|
|
* <p>If you do not pass all dependencies of a library, the dependencies will be loaded automatically.</p> |
|
78
|
|
|
* |
|
79
|
|
|
* @param array<WebLibraryInterface> $libraries |
|
80
|
|
|
*/ |
|
81
|
|
|
public function setWebLibraries($libraries) { |
|
82
|
|
|
foreach ($libraries as $library) { |
|
83
|
|
|
$this->addLibrary($library); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Renders the HTML in charge of loading CSS and JS files. |
|
89
|
|
|
* The Html is echoed directly into the output. |
|
90
|
|
|
* This function should be called within the head tag. |
|
91
|
|
|
* |
|
92
|
|
|
*/ |
|
93
|
|
|
public function toHtml() { |
|
94
|
|
|
/*if ($this->rendered) { |
|
95
|
|
|
throw new WebLibraryException("The library has already been rendered."); |
|
96
|
|
|
}*/ |
|
97
|
|
|
|
|
98
|
|
|
echo $this->getCssHtml(); |
|
99
|
|
|
echo $this->getJsHtml(); |
|
100
|
|
|
echo $this->getAdditionalHtml(); |
|
101
|
|
|
|
|
102
|
|
|
$this->rendered = true; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Adds a single JS file to the list of files to be included in the <head> tag. |
|
107
|
|
|
* <p>If you don't specify http:// or https:// and if the file does not start with /, the file is considered to be relative to ROOT_URL.</p> |
|
108
|
|
|
* <div class="info">It is a good practice to make sure the file does not start with /, http:// or https:// (unless you are using a CDN).</div> |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $jsFile |
|
111
|
|
|
*/ |
|
112
|
|
|
public function addJsFile($jsFile) { |
|
113
|
|
|
$this->webLibraries[] = new WebLibrary([$jsFile]); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Adds a single CSS file to the list of files to be included in the <head> tag. |
|
118
|
|
|
* <p>If you don't specify http:// or https:// and if the file does not start with /, the file is considered to be relative to ROOT_URL.</p> |
|
119
|
|
|
* <div class="info">It is a good practice to make sure the file does not start with /, http:// or https:// (unless you are using a CDN).</div> |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $cssFile |
|
122
|
|
|
*/ |
|
123
|
|
|
public function addCssFile($cssFile) { |
|
124
|
|
|
$this->webLibraries[] = new WebLibrary([], [$cssFile]); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Adds an additional script at the end of the <head> tag. |
|
129
|
|
|
* The provided script can either be a string or an object implementing HtmlElementInterface. |
|
130
|
|
|
* |
|
131
|
|
|
* @param string|HtmlElementInterface $additionalScript |
|
132
|
|
|
*/ |
|
133
|
|
|
public function addAdditionalScript($additionalScript) { |
|
134
|
|
|
if (!$additionalScript instanceof HtmlElementInterface) { |
|
135
|
|
|
$additionalScript = new HtmlString($additionalScript); |
|
136
|
|
|
} |
|
137
|
|
|
$this->webLibraries[] = new InlineWebLibrary(null, null, $additionalScript); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @return string |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getCssHtml() { |
|
144
|
|
|
ob_start(); |
|
145
|
|
|
foreach ($this->webLibraries as $library) { |
|
146
|
|
|
$this->renderer->render($library, 'css'); |
|
147
|
|
|
} |
|
148
|
|
|
return ob_get_clean(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @return string |
|
153
|
|
|
*/ |
|
154
|
|
|
public function getJsHtml() { |
|
155
|
|
|
ob_start(); |
|
156
|
|
|
foreach ($this->webLibraries as $library) { |
|
157
|
|
|
$this->renderer->render($library, 'js'); |
|
158
|
|
|
} |
|
159
|
|
|
return ob_get_clean(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @return string |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getAdditionalHtml() { |
|
166
|
|
|
ob_start(); |
|
167
|
|
|
foreach ($this->webLibraries as $library) { |
|
168
|
|
|
$this->renderer->render($library, 'additional'); |
|
169
|
|
|
} |
|
170
|
|
|
return ob_get_clean(); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.