1 | <?php |
||
13 | class Builder { |
||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $root; |
||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $target; |
||
22 | /** |
||
23 | * @param string $root |
||
24 | * @param string $target |
||
25 | */ |
||
26 | 14 | public function __construct ($root, $target) { |
|
32 | /** |
||
33 | * @param string[] $modules |
||
34 | * @param string[] $themes |
||
35 | * @param null|string $suffix |
||
36 | * |
||
37 | * @return string |
||
38 | * |
||
39 | * @throws \UnexpectedValueException |
||
40 | * @throws \BadMethodCallException |
||
41 | */ |
||
42 | 7 | public function core ($modules = [], $themes = [], $suffix = null) { |
|
43 | 7 | $suffix = $suffix ? "_$suffix" : ''; |
|
44 | 7 | $version = file_get_json("$this->root/modules/System/meta.json")['version']; |
|
45 | 7 | $target_file = "$this->target/CleverStyle_Framework_$version$suffix.phar.php"; |
|
46 | 7 | if (file_exists($target_file)) { |
|
47 | unlink($target_file); |
||
48 | } |
||
49 | 7 | $phar = new Phar($target_file); |
|
50 | 7 | unset($target_file); |
|
51 | 7 | $phar->startBuffering(); |
|
52 | 7 | $length = strlen("$this->root/"); |
|
53 | /** @noinspection ForeachSourceInspection */ |
||
54 | 7 | foreach (get_files_list("$this->root/install", false, 'f', '', true) as $file) { |
|
55 | 7 | $phar->addFile("$this->root/install/$file", $file); |
|
56 | } |
||
57 | 7 | unset($file); |
|
58 | 7 | $phar->addFile("$this->root/assets/img/logo.svg", 'logo.svg'); |
|
59 | /** |
||
60 | * Core files to be included into installation package |
||
61 | */ |
||
62 | 7 | $core_files = $this->get_core_files(); |
|
63 | /** |
||
64 | * Add modules that should be built-in into package |
||
65 | */ |
||
66 | 7 | $components_files = []; |
|
67 | 7 | $modules = $this->filter_and_add_components("$this->root/modules", $modules, $components_files); |
|
68 | 7 | $phar->addFromString('modules.json', _json_encode($modules)); |
|
69 | /** |
||
70 | * Add themes that should be built-in into package |
||
71 | */ |
||
72 | 7 | $themes = $this->filter_and_add_components("$this->root/themes", $themes, $components_files); |
|
73 | 7 | $phar->addFromString('themes.json', _json_encode($themes)); |
|
74 | /** |
||
75 | * Joining system and components files |
||
76 | */ |
||
77 | 7 | $core_files = array_merge($core_files, $components_files); |
|
78 | /** |
||
79 | * Addition of files into package |
||
80 | */ |
||
81 | 7 | foreach ($core_files as $index => &$file) { |
|
82 | 7 | $phar->addFile($file, "fs/$index"); |
|
83 | 7 | $file = substr($file, $length); |
|
84 | } |
||
85 | 7 | unset($index, $file); |
|
86 | /** |
||
87 | * Add license under the file `license-cleverstyle-framework.txt` rather than `license.txt`, so that license is not enforced on projects using it and is |
||
88 | * not overridden on framework update |
||
89 | */ |
||
90 | 7 | $phar->addFile("$this->root/license.txt", 'fs/'.count($core_files)); |
|
91 | 7 | $core_files[] = 'license-cleverstyle-framework.txt'; |
|
92 | /** |
||
93 | * Addition of separate files into package |
||
94 | */ |
||
95 | 7 | $phar->addFromString( |
|
96 | 7 | 'languages.json', |
|
97 | 7 | _json_encode( |
|
98 | 7 | array_merge( |
|
99 | 7 | _substr(get_files_list("$this->root/core/languages", '/^.*?\.php$/i', 'f'), 0, -4) ?: [], |
|
100 | 7 | _substr(get_files_list("$this->root/core/languages", '/^.*?\.json$/i', 'f'), 0, -5) ?: [] |
|
101 | ) |
||
102 | ) |
||
103 | ); |
||
104 | 7 | $phar->addFromString( |
|
105 | 7 | 'db_drivers.json', |
|
106 | 7 | _json_encode( |
|
107 | 7 | _substr(get_files_list("$this->root/core/drivers/DB", '/^[^_].*?\.php$/i', 'f'), 0, -4) |
|
108 | ) |
||
109 | ); |
||
110 | /** |
||
111 | * Fixation of system files list (without components files), it is needed for future system updating |
||
112 | */ |
||
113 | 7 | $phar->addFromString( |
|
114 | 7 | 'fs.json', |
|
115 | 7 | _json_encode( |
|
116 | 7 | array_flip( |
|
117 | 7 | array_diff( |
|
118 | 7 | $core_files, |
|
119 | 7 | _substr($components_files, $length) |
|
120 | ) |
||
121 | ) |
||
122 | ) |
||
123 | ); |
||
124 | 7 | unset($components_files, $length); |
|
125 | /** |
||
126 | * Addition of files, that are needed only for installation |
||
127 | */ |
||
128 | 7 | $phar->addFromString('fs/'.count($core_files), $this->get_htaccess()); |
|
129 | 7 | $core_files[] = '.htaccess'; |
|
130 | 7 | $phar->addFile("$this->root/config/main.php", 'fs/'.count($core_files)); |
|
131 | 7 | $core_files[] = 'config/main.php'; |
|
132 | 7 | $phar->addFile("$this->root/favicon.ico", 'fs/'.count($core_files)); |
|
133 | 7 | $core_files[] = 'favicon.ico'; |
|
134 | 7 | $phar->addFile("$this->root/.gitignore", 'fs/'.count($core_files)); |
|
135 | 7 | $core_files[] = '.gitignore'; |
|
136 | /** |
||
137 | * Flip array to have direct access to files by name during extracting and installation, and fixing of files list for installation |
||
138 | */ |
||
139 | 7 | $phar->addFromString( |
|
140 | 7 | 'fs_installer.json', |
|
141 | 7 | _json_encode( |
|
142 | 7 | array_flip($core_files) |
|
143 | ) |
||
144 | ); |
||
145 | 7 | unset($core_files); |
|
146 | /** |
||
147 | * Addition of supplementary files, that are needed directly for installation process: installer with GUI interface, readme, license, some additional |
||
148 | * information about available languages, themes, current version of system |
||
149 | */ |
||
150 | 7 | $phar->addFile("$this->root/license.txt", 'license.txt'); |
|
151 | 7 | $phar->addFile("$this->root/modules/System/meta.json", 'meta.json'); |
|
152 | 7 | $phar->setStub( |
|
153 | /** @lang PHP */ |
||
154 | <<<STUB |
||
155 | 7 | <?php |
|
156 | if (version_compare(PHP_VERSION, '7.0', '<')) { |
||
157 | echo 'CleverStyle Framework requires PHP 7.0 or higher'; |
||
158 | return; |
||
159 | } |
||
160 | |||
161 | if (php_sapi_name() == 'cli') { |
||
162 | Phar::mapPhar('cleverstyle_framework.phar'); |
||
163 | include 'phar://cleverstyle_framework.phar/cli.php'; |
||
164 | } else { |
||
165 | Phar::webPhar(null, 'web.php'); |
||
166 | } |
||
167 | __HALT_COMPILER(); |
||
168 | STUB |
||
169 | ); |
||
170 | 7 | $phar->stopBuffering(); |
|
171 | 7 | return "Done! CleverStyle Framework $version"; |
|
172 | } |
||
173 | /** |
||
174 | * Get array of files |
||
175 | * |
||
176 | * @return string[] |
||
177 | */ |
||
178 | 7 | protected function get_core_files () { |
|
179 | $files_to_include = [ |
||
180 | 7 | "$this->root/assets", |
|
181 | 7 | "$this->root/blocks/.gitkept", |
|
182 | 7 | "$this->root/core", |
|
183 | 7 | "$this->root/custom", |
|
184 | 7 | "$this->root/modules/System", |
|
185 | 7 | "$this->root/storage/.htaccess", |
|
186 | 7 | "$this->root/storage/public_cache/.htaccess", |
|
187 | 7 | "$this->root/storage/public/.htaccess", |
|
188 | 7 | "$this->root/storage/temp/.htaccess", |
|
189 | 7 | "$this->root/themes/CleverStyle", |
|
190 | 7 | "$this->root/bower.json", |
|
191 | 7 | "$this->root/cli", |
|
192 | 7 | "$this->root/composer.json", |
|
193 | 7 | "$this->root/composer.lock", |
|
194 | 7 | "$this->root/index.php", |
|
195 | 7 | "$this->root/package.json" |
|
196 | ]; |
||
197 | 7 | $files = []; |
|
198 | 7 | foreach ($files_to_include as $s) { |
|
199 | 7 | if (is_file($s)) { |
|
200 | 7 | $files[] = $s; |
|
201 | 7 | } elseif (is_dir($s)) { |
|
202 | /** @noinspection SlowArrayOperationsInLoopInspection */ |
||
203 | 7 | $files = array_merge( |
|
204 | 7 | $files, |
|
205 | 7 | get_files_list($s, false, 'f', true, true, false, false, true) |
|
206 | ); |
||
207 | } |
||
208 | } |
||
209 | 7 | return $files; |
|
210 | } |
||
211 | /** |
||
212 | * @param string $dir |
||
213 | * @param string[] $components |
||
214 | * @param string[] $components_files |
||
215 | * |
||
216 | * @return string[] |
||
217 | */ |
||
218 | 7 | protected function filter_and_add_components ($dir, $components, &$components_files) { |
|
219 | 7 | $components = array_filter( |
|
220 | 7 | $components, |
|
221 | 7 | function ($component) use ($dir, &$components_files) { |
|
222 | 1 | return $this->get_component_files("$dir/$component", $components_files); |
|
223 | 7 | } |
|
224 | ); |
||
225 | 7 | sort($components); |
|
226 | 7 | return $components; |
|
227 | } |
||
228 | /** |
||
229 | * @param string $component_root |
||
230 | * @param string[] $files Array, where new files will be appended |
||
231 | * |
||
232 | * @return bool |
||
233 | */ |
||
234 | 1 | protected function get_component_files ($component_root, &$files) { |
|
235 | /** |
||
236 | * Do not allow building System module and CleverStyle theme |
||
237 | */ |
||
238 | 1 | if (in_array(basename($component_root), ['System', 'CleverStyle'])) { |
|
239 | 1 | return false; |
|
240 | } |
||
241 | /** |
||
242 | * Components without meta.json also not allowed |
||
243 | */ |
||
244 | 1 | if (!file_exists("$component_root/meta.json")) { |
|
245 | 1 | return false; |
|
246 | } |
||
247 | 1 | @unlink("$component_root/fs.json"); |
|
248 | 1 | $local_files = get_files_list($component_root, false, 'f', true, true, false, false, true); |
|
249 | 1 | $files = array_merge($files, $local_files); |
|
250 | 1 | file_put_json( |
|
251 | 1 | "$component_root/fs.json", |
|
252 | 1 | array_values( |
|
253 | 1 | _substr( |
|
254 | 1 | $local_files, |
|
255 | 1 | strlen("$component_root/") |
|
256 | ) |
||
257 | ) |
||
258 | ); |
||
259 | 1 | $files[] = "$component_root/fs.json"; |
|
260 | 1 | return true; |
|
261 | } |
||
262 | /** |
||
263 | * @return string |
||
264 | */ |
||
265 | 7 | protected function get_htaccess () { |
|
298 | /** |
||
299 | * @param string $module |
||
300 | * @param null|string $suffix |
||
301 | * |
||
302 | * @return string |
||
303 | */ |
||
304 | 4 | public function module ($module, $suffix = null) { |
|
310 | /** |
||
311 | * @param string $theme |
||
312 | * @param null|string $suffix |
||
313 | * |
||
314 | * @return string |
||
315 | */ |
||
316 | 3 | public function theme ($theme, $suffix = null) { |
|
322 | /** |
||
323 | * @param string $source_dir |
||
324 | * @param null|string $suffix |
||
325 | * |
||
326 | * @return string |
||
327 | */ |
||
328 | 5 | protected function generic_package_creation ($source_dir, $suffix = null) { |
|
391 | } |
||
392 |