1
|
|
|
<?php |
2
|
|
|
namespace Mouf\Html\Utils\WebLibraryManager; |
3
|
|
|
|
4
|
|
|
use Mouf\MoufManager; |
5
|
|
|
|
6
|
|
|
use Mouf\Html\HtmlElement\HtmlElementInterface; |
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
|
|
|
* @Component |
21
|
|
|
*/ |
22
|
|
|
class WebLibraryManager implements HtmlElementInterface { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The array of all included libraries. |
26
|
|
|
* |
27
|
|
|
* @var array<WebLibraryInterface> |
28
|
|
|
*/ |
29
|
|
|
private $webLibraries = array(); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* false if the toHtml method has not yet been called, true if it has been called. |
33
|
|
|
* @var boolean |
34
|
|
|
*/ |
35
|
|
|
private $rendered = false; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Adds a library to the list of libraries that should be loaded in the web page. |
39
|
|
|
* <p>The function will also load the dependencies (if any) and will have no effect if the library has already been loaded.</p> |
40
|
|
|
* |
41
|
|
|
* @param WebLibraryInterface $library |
42
|
|
|
*/ |
43
|
|
|
public function addLibrary(WebLibraryInterface $library) { |
44
|
|
|
if ($this->rendered) { |
45
|
|
|
throw new WebLibraryException("The libraries have already been rendered. This call to addLibrary should be performed BEFORE the toHtml method of WebLibraryManager is called."); |
46
|
|
|
} |
47
|
|
|
if (array_search($library, $this->webLibraries) === false) { |
48
|
|
|
// Let's start by adding dependencies. |
49
|
|
|
$dependencies = $library->getDependencies(); |
50
|
|
|
if ($dependencies) { |
|
|
|
|
51
|
|
|
foreach ($dependencies as $dependency) { |
52
|
|
|
/* @var $dependency WebLibraryInterface */ |
53
|
|
|
$this->addLibrary($dependency); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->webLibraries[] = $library; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// TODO: add: addJs and addCss file |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* The list of all libraries that should be loaded in the web page. |
65
|
|
|
* <p>If you do not pass all dependencies of a library, the dependencies will be loaded automatically.</p> |
66
|
|
|
* |
67
|
|
|
* @Property |
68
|
|
|
* @param array<WebLibraryInterface> $libraries |
69
|
|
|
*/ |
70
|
|
|
public function setWebLibraries($libraries) { |
71
|
|
|
foreach ($libraries as $library) { |
72
|
|
|
$this->addLibrary($library); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Renders the HTML in charge of loading CSS and JS files. |
78
|
|
|
* The Html is echoed directly into the output. |
79
|
|
|
* This function should be called within the head tag. |
80
|
|
|
* |
81
|
|
|
*/ |
82
|
|
|
public function toHtml() |
83
|
|
|
{ |
84
|
|
|
/*if ($this->rendered) { |
85
|
|
|
throw new WebLibraryException("The library has already been rendered."); |
86
|
|
|
}*/ |
87
|
|
|
|
88
|
|
|
$this->cssToHtml(); |
89
|
|
|
$this->jsToHtml(); |
90
|
|
|
$this->additionalToHtml(); |
91
|
|
|
|
92
|
|
|
$this->rendered = true; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Renders the HTML in charge of loading CSS files. |
97
|
|
|
* The Html is echoed directly into the output. |
98
|
|
|
* |
99
|
|
|
*/ |
100
|
|
View Code Duplication |
public function cssToHtml() |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$defaultWebLibraryRenderer = null; |
103
|
|
|
|
104
|
|
|
foreach ($this->webLibraries as $library) { |
105
|
|
|
/* @var $library WebLibraryInterface */ |
106
|
|
|
$renderer = $library->getRenderer(); |
107
|
|
|
if ($renderer == null) { |
108
|
|
|
if ($defaultWebLibraryRenderer == null) { |
109
|
|
|
$defaultWebLibraryRenderer = MoufManager::getMoufManager()->getInstance( |
110
|
|
|
'defaultWebLibraryRenderer' |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
$renderer = $defaultWebLibraryRenderer; |
114
|
|
|
} |
115
|
|
|
/* @var $renderer WebLibraryRendererInterface */ |
116
|
|
|
$renderer->toCssHtml($library); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Renders the HTML in charge of loading JS files. |
122
|
|
|
* The Html is echoed directly into the output. |
123
|
|
|
* |
124
|
|
|
*/ |
125
|
|
View Code Duplication |
public function jsToHtml() |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$defaultWebLibraryRenderer = null; |
128
|
|
|
|
129
|
|
|
foreach ($this->webLibraries as $library) { |
130
|
|
|
/* @var $library WebLibraryInterface */ |
131
|
|
|
$renderer = $library->getRenderer(); |
132
|
|
|
if ($renderer == null) { |
133
|
|
|
if ($defaultWebLibraryRenderer == null) { |
134
|
|
|
$defaultWebLibraryRenderer = MoufManager::getMoufManager()->getInstance( |
135
|
|
|
'defaultWebLibraryRenderer' |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
$renderer = $defaultWebLibraryRenderer; |
139
|
|
|
} |
140
|
|
|
/* @var $renderer WebLibraryRendererInterface */ |
141
|
|
|
$renderer->toJsHtml($library); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Renders the HTML in charge of loading additional files. |
147
|
|
|
* The Html is echoed directly into the output. |
148
|
|
|
* |
149
|
|
|
*/ |
150
|
|
View Code Duplication |
public function additionalToHtml() |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
$defaultWebLibraryRenderer = null; |
153
|
|
|
|
154
|
|
|
foreach ($this->webLibraries as $library) { |
155
|
|
|
/* @var $library WebLibraryInterface */ |
156
|
|
|
$renderer = $library->getRenderer(); |
157
|
|
|
if ($renderer == null) { |
158
|
|
|
if ($defaultWebLibraryRenderer == null) { |
159
|
|
|
$defaultWebLibraryRenderer = MoufManager::getMoufManager()->getInstance( |
160
|
|
|
'defaultWebLibraryRenderer' |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
$renderer = $defaultWebLibraryRenderer; |
164
|
|
|
} |
165
|
|
|
/* @var $renderer WebLibraryRendererInterface */ |
166
|
|
|
$renderer->toAdditionalHtml($library); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
?> |
|
|
|
|
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.