1 | <?php |
||||
2 | namespace Mezon\HtmlTemplate; |
||||
3 | |||||
4 | use Mezon\Conf\Conf; |
||||
5 | |||||
6 | /** |
||||
7 | * Class TemplateResources |
||||
8 | * |
||||
9 | * @package Mezon |
||||
10 | * @subpackage TemplateResources |
||||
11 | * @author Dodonov A.A. |
||||
12 | * @version v.1.0 (2019/08/17) |
||||
13 | * @copyright Copyright (c) 2019, aeon.org |
||||
14 | */ |
||||
15 | |||||
16 | /** |
||||
17 | * Class collects resources for page. |
||||
18 | * |
||||
19 | * Any including components can add to the page their own resources without having access to the application or template. |
||||
20 | */ |
||||
21 | class TemplateResources |
||||
22 | { |
||||
23 | |||||
24 | /** |
||||
25 | * Custom CSS files to be included |
||||
26 | */ |
||||
27 | private $cssFiles = false; |
||||
28 | |||||
29 | /** |
||||
30 | * Custom JS files to be included |
||||
31 | */ |
||||
32 | private $jsFiles = false; |
||||
33 | |||||
34 | /** |
||||
35 | * Constructor. |
||||
36 | */ |
||||
37 | function __construct() |
||||
38 | { |
||||
39 | if ($this->cssFiles === false) { |
||||
40 | $this->cssFiles = []; |
||||
41 | } |
||||
42 | if ($this->jsFiles === false) { |
||||
43 | $this->jsFiles = []; |
||||
44 | } |
||||
45 | } |
||||
46 | |||||
47 | /** |
||||
48 | * Additing single CSS file |
||||
49 | * |
||||
50 | * @param string $cssFile |
||||
51 | * CSS file |
||||
52 | */ |
||||
53 | function addCssFile(string $cssFile) |
||||
0 ignored issues
–
show
|
|||||
54 | { |
||||
55 | // additing only unique paths |
||||
56 | if (array_search($cssFile, $this->cssFiles) === false) { |
||||
0 ignored issues
–
show
$this->cssFiles of type boolean is incompatible with the type array expected by parameter $haystack of array_search() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
57 | $this->cssFiles[] = Conf::expandString($cssFile); |
||||
58 | } |
||||
59 | } |
||||
60 | |||||
61 | /** |
||||
62 | * Additing multyple CSS files |
||||
63 | * |
||||
64 | * @param array $cssFiles |
||||
65 | * CSS files |
||||
66 | */ |
||||
67 | function addCssFiles(array $cssFiles) |
||||
0 ignored issues
–
show
|
|||||
68 | { |
||||
69 | foreach ($cssFiles as $cssFile) { |
||||
70 | $this->addCssFile($cssFile); |
||||
71 | } |
||||
72 | } |
||||
73 | |||||
74 | /** |
||||
75 | * Method returning added CSS files |
||||
76 | */ |
||||
77 | function getCssFiles() |
||||
0 ignored issues
–
show
|
|||||
78 | { |
||||
79 | return $this->cssFiles; |
||||
80 | } |
||||
81 | |||||
82 | /** |
||||
83 | * Additing single CSS file |
||||
84 | * |
||||
85 | * @param string $jsFile |
||||
86 | * JS file |
||||
87 | */ |
||||
88 | function addJsFile($jsFile) |
||||
0 ignored issues
–
show
|
|||||
89 | { |
||||
90 | // additing only unique paths |
||||
91 | if (array_search($jsFile, $this->jsFiles) === false) { |
||||
0 ignored issues
–
show
$this->jsFiles of type boolean is incompatible with the type array expected by parameter $haystack of array_search() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
92 | $this->jsFiles[] = Conf::expandString($jsFile); |
||||
93 | } |
||||
94 | } |
||||
95 | |||||
96 | /** |
||||
97 | * Additing multyple CSS files |
||||
98 | * |
||||
99 | * @param array $jsFiles |
||||
100 | * JS files |
||||
101 | */ |
||||
102 | function addJsFiles(array $jsFiles) |
||||
0 ignored issues
–
show
|
|||||
103 | { |
||||
104 | foreach ($jsFiles as $jsFile) { |
||||
105 | $this->addJsFile($jsFile); |
||||
106 | } |
||||
107 | } |
||||
108 | |||||
109 | /** |
||||
110 | * Method returning added JS files. |
||||
111 | */ |
||||
112 | function getJsFiles() |
||||
0 ignored issues
–
show
|
|||||
113 | { |
||||
114 | return $this->jsFiles; |
||||
115 | } |
||||
116 | |||||
117 | /** |
||||
118 | * Method clears loaded resources. |
||||
119 | */ |
||||
120 | function clear() |
||||
0 ignored issues
–
show
|
|||||
121 | { |
||||
122 | $this->cssFiles = []; |
||||
123 | |||||
124 | $this->jsFiles = []; |
||||
125 | } |
||||
126 | |||||
127 | /** |
||||
128 | * Method returns compiled page resources |
||||
129 | * |
||||
130 | * @return string Compiled resources includers |
||||
131 | */ |
||||
132 | public function compileResources(): string |
||||
133 | { |
||||
134 | $content = ''; |
||||
135 | |||||
136 | foreach ($this->cssFiles as $cssFile) { |
||||
0 ignored issues
–
show
|
|||||
137 | $content .= ' |
||||
138 | <link href="' . $cssFile . '" rel="stylesheet" type="text/css">'; |
||||
139 | } |
||||
140 | |||||
141 | foreach ($this->jsFiles as $jsFile) { |
||||
0 ignored issues
–
show
|
|||||
142 | $content .= ' |
||||
143 | <script src="' . $jsFile . '"></script>'; |
||||
144 | } |
||||
145 | |||||
146 | return $content; |
||||
147 | } |
||||
148 | } |
||||
149 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.