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