xphp::get_models()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 23
c 0
b 0
f 0
rs 9.0444
cc 6
nc 5
nop 2
1
<?php
2
3
/**
4
 * © 2018 - Phoponent
5
 * Author: Nicolas Choquet
6
 * Email: [email protected]
7
 * LICENSE GPL ( GNU General Public License )
8
 */
9
10
namespace phoponent\framework\static_classe;
11
12
use phoponent\framework\classe\xphp_tag;
13
use phoponent\framework\traits\static_class;
14
use phoponent\loading\Auto;
15
16
class xphp {
17
    use static_class;
18
19
    public static function get_models($component, $type = 'core') {
20
        $models = [];
21
        $dir = opendir("phoponent/app/components/{$type}/{$component}/models");
22
        while (($file = readdir($dir)) !== false) {
0 ignored issues
show
Bug introduced by
It seems like $dir can also be of type false; however, parameter $dir_handle of readdir() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

22
        while (($file = readdir(/** @scrutinizer ignore-type */ $dir)) !== false) {
Loading history...
23
            if($file !== '.' && $file !== '..') {
24
                if(is_file("phoponent/app/components/custom/{$component}/models/{$file}")) {
25
                    require_once "phoponent/app/components/core/{$component}/models/{$file}";
26
                    require_once "phoponent/app/components/custom/{$component}/models/{$file}";
27
                    $class_tag = "\phoponent\app\component\custom\{$component}";
28
                    $model = str_replace('.php', '', $file);
29
                    $model_class = "\\phoponent\\app\\component\\custom\\$component\\mvc\\model\\$model";
30
                    $models[$model] = new $model_class(self::get_services($class_tag), $component);
31
                }
32
                elseif(is_file("phoponent/app/components/core/{$component}/models/{$file}")) {
33
                    require_once "phoponent/app/components/core/{$component}/models/{$file}";
34
                    $class_tag = "\phoponent\app\component\core\{$component}";
35
                    $model = str_replace('.php', '', $file);
36
                    $model_class = "\\phoponent\\app\\component\\core\\$component\\mvc\\model\\$model";
37
                    $models[$model] = new $model_class(self::get_services($class_tag), $component);
38
                }
39
            }
40
        }
41
        return $models;
42
    }
43
44
    public static function get_views($component, $type = 'core') {
45
        $views = [];
46
        $dir = opendir("phoponent/app/components/{$type}/{$component}/views");
47
        while (($file = readdir($dir)) !== false) {
0 ignored issues
show
Bug introduced by
It seems like $dir can also be of type false; however, parameter $dir_handle of readdir() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
        while (($file = readdir(/** @scrutinizer ignore-type */ $dir)) !== false) {
Loading history...
48
            if($file !== '.' && $file !== '..') {
49
                if(is_file("phoponent/app/components/custom/{$component}/views/{$file}")) {
50
                    require_once "phoponent/app/components/core/{$component}/views/{$file}";
51
                    require_once "phoponent/app/components/custom/{$component}/views/{$file}";
52
                    $view = str_replace('.view.php', '', $file);
53
                    $view_class = "\\phoponent\\app\\component\\custom\\$component\\mvc\\view\\$view";
54
                    $view_parent_class = "\\phoponent\\app\\component\\core\\$component\\mvc\\view\\$view";
55
                    $views[$view] = new $view_class("phoponent/app/components/custom/{$component}/views");
56
                    $views[$view]->set_parent(new $view_parent_class("phoponent/app/components/core/{$component}/views"));
57
                }
58
                elseif(is_file("phoponent/app/components/core/{$component}/views/{$file}")) {
59
                    require_once "phoponent/app/components/core/{$component}/views/{$file}";
60
                    $view = str_replace('.view.php', '', $file);
61
                    $view_class = "\\phoponent\\app\\component\\core\\$component\\mvc\\view\\$view";
62
                    $views[$view] = new $view_class("phoponent/app/components/core/{$component}/views");
63
                }
64
            }
65
        }
66
        return $views;
67
    }
68
69
    public static function get_services($component) {
70
		$services = [];
71
		$component = str_replace(['{', '}'], '', $component);
72
        foreach ($component::load_services() as $name => $service_to_load) {
73
            if($service_to_load !== null) {
74
                if (is_file($service_to_load['path'])) {
75
                    Auto::dependencie('services', explode('\\', $service_to_load['class'])[count(explode('\\', $service_to_load['class'])) - 1]);
76
                    $class = '\\' . $service_to_load['class'];
77
                    $services[$name] = new $class();
78
                }
79
            }
80
		}
81
		return $services;
82
	}
83
84
    private static function clean_path($path) {
85
		if(substr($path, 0, 11) !== '/phoponent' && substr($path, 0, 11) !== 'phoponent/') {
86
			$path = str_replace('index.php/', '', $path);
87
			if (strstr($path, 0, 1) === '/') {
88
				$path = 'phoponent/app' . $path;
89
			} elseif (strstr($path, 0, 1) !== '/') {
90
				$path = 'phoponent/app/' . $path;
91
			}
92
93
			if (!strstr($path, '.') && substr($path, strlen($path) - 1, 1) !== '/') {
94
				$path .= '/index.html';
95
			} elseif (!strstr($path, '.') && substr($path, strlen($path) - 1, 1) === '/') {
96
				$path .= 'index.html';
97
			}
98
		}
99
		return $path;
100
	}
101
102
	public static function parse($path) {
103
        $path = self::clean_path($path);
104
105
		if(is_file($path)) {
106
            $file_content = file_get_contents($path);
107
		    if(explode('.', $path)[count(explode('.', $path))-1] === 'php') {
108
		        $file_content = include $path;
109
            }
110
            self::parse_template_content($file_content);
111
			return $file_content;
112
		}
113
		return '';
114
	}
115
116
	public static function parse_template_content(&$template) {
117
		// balise banales avec contenu
118
		// sans attributs
119
		preg_replace_callback(regexp::get_regexp_for_no_autoclosed_tags_with_content_and_not_arguments(), function ($matches) use (&$template) {
120
			$class = str_replace('-', '_', $matches[1]);
121
			if(is_file("phoponent/app/components/custom/{$class}/{$class}.php")) {
122
			    require_once "phoponent/app/components/core/{$class}/{$class}.php";
123
				require_once "phoponent/app/components/custom/{$class}/{$class}.php";
124
                $class_tag = "\\phoponent\\app\\component\\custom\\$class";
125
				/**
126
				 * @var xphp_tag $tag
127
				 */
128
				$tag = new $class_tag(self::get_models($class), self::get_views($class), self::get_services($class_tag), $template);
129
				$tag->value($matches[2]);
130
				$template = str_replace($matches[0], $tag->render(), $template);
131
			}
132
			elseif(is_file("phoponent/app/components/core/{$class}/{$class}.php")) {
133
                require_once "phoponent/app/components/core/{$class}/{$class}.php";
134
                $class_tag = "\\phoponent\\app\\component\\core\\$class";
135
                /**
136
                 * @var xphp_tag $tag
137
                 */
138
                $tag = new $class_tag(self::get_models($class), self::get_views($class), self::get_services($class_tag), $template);
139
                $tag->value($matches[2]);
140
                $template = str_replace($matches[0], $tag->render(), $template);
141
            }
142
		}, $template);
143
		// sans attributs mais avec un espace
144
		preg_replace_callback(regexp::get_regexp_for_no_autoclosed_tags_with_content_and_not_arguments_and_spaces(), function ($matches) use (&$template) {
145
			$class = str_replace('-', '_', $matches[1]);
146
			if(is_file("phoponent/app/components/custom/{$class}/{$class}.php")) {
147
				require_once "phoponent/app/components/core/{$class}/{$class}.php";
148
                require_once "phoponent/app/components/custom/{$class}/{$class}.php";
149
                $class_tag = "\\phoponent\\app\\component\\custom\\$class";
150
				/**
151
				 * @var xphp_tag $tag
152
				 */
153
				$tag = new $class_tag(self::get_models($class), self::get_views($class), self::get_services($class_tag), $template);
154
				$tag->value($matches[2]);
155
				$template = str_replace($matches[0], $tag->render(), $template);
156
			}
157
			elseif (is_file("phoponent/app/components/core/{$class}/{$class}.php")) {
158
                require_once "phoponent/app/components/core/{$class}/{$class}.php";
159
                $class_tag = "\\phoponent\\app\\component\\core\\$class";
160
                /**
161
                 * @var xphp_tag $tag
162
                 */
163
                $tag = new $class_tag(self::get_models($class), self::get_views($class), self::get_services($class_tag), $template);
164
                $tag->value($matches[2]);
165
                $template = str_replace($matches[0], $tag->render(), $template);
166
            }
167
		}, $template);
168
		// avec attributs
169
		preg_replace_callback(regexp::get_regexp_for_no_autoclosed_tags_with_content_and_arguments(), function ($matches) use (&$template) {
170
			$class = str_replace('-', '_', $matches[1]);
171
172
			$arguments = str_replace(["\n", "\t"], '', $matches[2]);
173
            $arguments_array = [];
174
            preg_replace_callback(regexp::regexp_for_parse_attributs(), function ($matches) use (&$arguments_array) {
175
                $arguments_array[] = $matches[1];
176
            }, $arguments);
177
            preg_replace_callback(regexp::regexp_for_parse_attributs_with_only_integers(), function ($matches) use (&$arguments_array) {
178
                $arguments_array[] = $matches[1];
179
            }, $arguments);
180
            foreach ($arguments_array as $id => $arg) {
181
                $arg_local = explode('=', $arg);
182
                $argument = $arg_local[0];
183
                $valeur = str_replace('\"', 'µ', $arg_local[1]);
184
                $valeur = str_replace('"', '', $valeur);
185
                $valeur = str_replace('µ', '"', $valeur);
186
187
                $arguments_array[$argument] = $valeur;
188
                unset($arguments_array[$id]);
189
            }
190
            $arguments_array['value'] = $matches[3];
191
            $arguments = $arguments_array;
192
193
            if(is_file("phoponent/app/components/custom/{$class}/{$class}.php")) {
194
                require_once "phoponent/app/components/core/{$class}/{$class}.php";
195
                require_once "phoponent/app/components/custom/{$class}/{$class}.php";
196
                $class_tag = "\\phoponent\\app\\component\\custom\\$class";
197
                /**
198
                 * @var xphp_tag $tag
199
                 */
200
                $tag = new $class_tag(self::get_models($class), self::get_views($class), self::get_services($class_tag), $template);
201
                foreach ($arguments as $argument => $valeur) {
202
                    if ($argument === 'value') {
203
                        $tag->value($valeur);
204
                    } else {
205
                        $tag->attribute($argument, $valeur);
206
                    }
207
                }
208
                $template = str_replace($matches[0], $tag->render(), $template);
209
            }
210
            elseif (is_file("phoponent/app/components/core/{$class}/{$class}.php")) {
211
                require_once "phoponent/app/components/core/{$class}/{$class}.php";
212
                $class_tag = "\\phoponent\\app\\component\\core\\$class";
213
                /**
214
                 * @var xphp_tag $tag
215
                 */
216
                $tag = new $class_tag(self::get_models($class), self::get_views($class), self::get_services($class_tag), $template);
217
                foreach ($arguments as $argument => $valeur) {
218
                    if ($argument === 'value') {
219
                        $tag->value($valeur);
220
                    } else {
221
                        $tag->attribute($argument, $valeur);
222
                    }
223
                }
224
                $template = str_replace($matches[0], $tag->render(), $template);
225
            }
226
		}, $template);
227
228
		// balises banales sans contenu
229
		// sans attributs
230
		preg_replace_callback(regexp::get_regexp_for_no_autoclosed_tags_without_content_and_arguments(), function ($matches) use (&$template) {
231
			$class = str_replace('-', '_', $matches[1]);
232
			if(is_file("phoponent/app/components/custom/{$class}/{$class}.php")) {
233
				require_once "phoponent/app/components/core/{$class}/{$class}.php";
234
                require_once "phoponent/app/components/custom/{$class}/{$class}.php";
235
                $class_tag = "\\phoponent\\app\\component\\custom\\$class";
236
				/**
237
				 * @var xphp_tag $tag
238
				 */
239
				$tag = new $class_tag(self::get_models($class), self::get_views($class), self::get_services($class_tag), $template);
240
				$template = str_replace($matches[0], $tag->render(), $template);
241
			}
242
			elseif(is_file("phoponent/app/components/core/{$class}/{$class}.php")) {
243
                require_once "phoponent/app/components/core/{$class}/{$class}.php";
244
                $class_tag = "\\phoponent\\app\\component\\core\\$class";
245
                /**
246
                 * @var xphp_tag $tag
247
                 */
248
                $tag = new $class_tag(self::get_models($class), self::get_views($class), self::get_services($class_tag), $template);
249
                $template = str_replace($matches[0], $tag->render(), $template);
250
            }
251
		}, $template);
252
		// sans attributs mais avec un espace
253
		preg_replace_callback(regexp::get_regexp_for_no_autoclosed_tags_without_content_and_arguments_and_with_spaces(), function ($matches) use (&$template) {
254
			$class = str_replace('-', '_', $matches[1]);
255
			if(is_file("phoponent/app/components/custom/{$class}/{$class}.php")) {
256
				require_once "phoponent/app/components/core/{$class}/{$class}.php";
257
                require_once "phoponent/app/components/custom/{$class}/{$class}.php";
258
                $class_tag = "\\phoponent\\app\\component\\custom\\$class";
259
				/**
260
				 * @var xphp_tag $tag
261
				 */
262
				$tag = new $class_tag(self::get_models($class), self::get_views($class), self::get_services($class_tag), $template);
263
				$template = str_replace($matches[0], $tag->render(), $template);
264
			}
265
			elseif(is_file("phoponent/app/components/core/{$class}/{$class}.php")) {
266
                require_once "phoponent/app/components/core/{$class}/{$class}.php";
267
                $class_tag = "\\phoponent\\app\\component\\core\\$class";
268
                /**
269
                 * @var xphp_tag $tag
270
                 */
271
                $tag = new $class_tag(self::get_models($class), self::get_views($class), self::get_services($class_tag), $template);
272
                $template = str_replace($matches[0], $tag->render(), $template);
273
            }
274
		}, $template);
275
		// avec attributs
276
		preg_replace_callback(regexp::get_regexp_for_no_autoclosed_tags_without_content_and_with_arguments(), function ($matches) use (&$template) {
277
			$class = str_replace('-', '_', $matches[1]);
278
			$arguments = str_replace(["\n", "\t"], '', $matches[2]);
279
            $arguments_array = [];
280
            preg_replace_callback(regexp::regexp_for_parse_attributs(), function ($matches) use (&$arguments_array) {
281
                $arguments_array[] = $matches[1];
282
            }, $arguments);
283
284
            preg_replace_callback(regexp::regexp_for_parse_attributs_with_only_integers(), function ($matches) use (&$arguments_array) {
285
                $arguments_array[] = $matches[1];
286
            }, $arguments);
287
288
            foreach ($arguments_array as $id => $arg) {
289
                $arg_local = explode('=', $arg);
290
                $argument = $arg_local[0];
291
                $valeur = str_replace('\"', 'µ', $arg_local[1]);
292
                $valeur = str_replace('"', '', $valeur);
293
                $valeur = str_replace('µ', '"', $valeur);
294
295
                $arguments_array[$argument] = $valeur;
296
                unset($arguments_array[$id]);
297
            }
298
299
            $arguments = $arguments_array;
300
            if(is_file("phoponent/app/components/custom/{$class}/{$class}.php")) {
301
                require_once "phoponent/app/components/core/{$class}/{$class}.php";
302
                require_once "phoponent/app/components/custom/{$class}/{$class}.php";
303
                $class_tag = "\\phoponent\\app\\component\\custom\\$class";
304
                /**
305
                 * @var xphp_tag $tag
306
                 */
307
                $tag = new $class_tag(self::get_models($class), self::get_views($class), self::get_services($class_tag), $template);
308
                foreach ($arguments as $argument => $valeur) {
309
                    if ($argument === 'value') {
310
                        $tag->value($valeur);
311
                    } else {
312
                        $tag->attribute($argument, $valeur);
313
                    }
314
                }
315
                $template = str_replace($matches[0], $tag->render(), $template);
316
            }
317
            elseif(is_file("phoponent/app/components/core/{$class}/{$class}.php")) {
318
                require_once "phoponent/app/components/core/{$class}/{$class}.php";
319
                $class_tag = "\\phoponent\\app\\component\\core\\class";
320
                /**
321
                 * @var xphp_tag $tag
322
                 */
323
                $tag = new $class_tag(self::get_models($class), self::get_views($class), self::get_services($class_tag), $template);
324
                foreach ($arguments as $argument => $valeur) {
325
                    if ($argument === 'value') {
326
                        $tag->value($valeur);
327
                    } else {
328
                        $tag->attribute($argument, $valeur);
329
                    }
330
                }
331
                $template = str_replace($matches[0], $tag->render(), $template);
332
            }
333
		}, $template);
334
335
		// balises autofermantes
336
		// sans attributs
337
		preg_replace_callback(regexp::get_regexp_for_autoclosed_tags_without_arguments(), function ($matches) use (&$template) {
338
			$class = str_replace('-', '_', $matches[1]);
339
			if(is_file("phoponent/app/components/custom/{$class}/{$class}.php")) {
340
				require_once "phoponent/app/components/core/{$class}/{$class}.php";
341
                require_once "phoponent/app/components/custom/{$class}/{$class}.php";
342
                $class_tag = "\\phoponent\\app\\component\\custom\\$class";
343
				/**
344
				 * @var xphp_tag $tag
345
				 */
346
				$tag = new $class_tag(self::get_models($class), self::get_views($class), self::get_services($class_tag), $template);
347
				$template = str_replace($matches[0], $tag->render(), $template);
348
			}
349
			elseif(is_file("phoponent/app/components/core/{$class}/{$class}.php")) {
350
                require_once "phoponent/app/components/core/{$class}/{$class}.php";
351
                $class_tag = "\\phoponent\\app\\component\\core\\$class";
352
                /**
353
                 * @var xphp_tag $tag
354
                 */
355
                $tag = new $class_tag(self::get_models($class), self::get_views($class), self::get_services($class_tag), $template);
356
                $template = str_replace($matches[0], $tag->render(), $template);
357
            }
358
		}, $template);
359
		// sans attributs mais avec un espace
360
		preg_replace_callback(regexp::get_regexp_for_autoclosed_tags_without_arguments_and_spaces(), function ($matches) use (&$template) {
361
			$class = str_replace('-', '_', $matches[1]);
362
			if(is_file("phoponent/app/components/custom/{$class}/{$class}.php")) {
363
				require_once "phoponent/app/components/core/{$class}/{$class}.php";
364
                require_once "phoponent/app/components/custom/{$class}/{$class}.php";
365
                $class_tag = "\\phoponent\\app\\component\\custom\\$class";
366
				/**
367
				 * @var xphp_tag $tag
368
				 */
369
				$tag = new $class_tag(self::get_models($class), self::get_views($class), self::get_services($class_tag), $template);
370
				$template = str_replace($matches[0], $tag->render(), $template);
371
			}
372
			elseif(is_file("phoponent/app/components/core/{$class}/{$class}.php")) {
373
                require_once "phoponent/app/components/core/{$class}/{$class}.php";
374
                $class_tag = "\\phoponent\\app\\component\\core\\$class";
375
                /**
376
                 * @var xphp_tag $tag
377
                 */
378
                $tag = new $class_tag(self::get_models($class), self::get_views($class), self::get_services($class_tag), $template);
379
                $template = str_replace($matches[0], $tag->render(), $template);
380
            }
381
		}, $template);
382
		// avec attributs
383
		preg_replace_callback(regexp::get_regexp_for_autoclosed_tags_with_arguments(), function ($matches) use (&$template) {
384
			$class = str_replace('-', '_', $matches[1]);
385
			$arguments = str_replace(["\n", "\t"], '', $matches[2]);
386
            $arguments_array = [];
387
            preg_replace_callback(regexp::regexp_for_parse_attributs(), function ($matches) use (&$arguments_array) {
388
                $arguments_array[] = $matches[1];
389
            }, $arguments);
390
391
            preg_replace_callback(regexp::regexp_for_parse_attributs_with_only_integers(), function ($matches) use (&$arguments_array) {
392
                $arguments_array[] = $matches[1];
393
            }, $arguments);
394
395
            foreach ($arguments_array as $id => $arg) {
396
                $arg_local = explode('=', $arg);
397
                $argument = $arg_local[0];
398
                $valeur = str_replace('\"', 'µ', $arg_local[1]);
399
                $valeur = str_replace('"', '', $valeur);
400
                $valeur = str_replace('µ', '"', $valeur);
401
402
                $arguments_array[$argument] = $valeur;
403
                unset($arguments_array[$id]);
404
            }
405
406
            $arguments = $arguments_array;
407
			if(is_file("phoponent/app/components/custom/{$class}/{$class}.php")) {
408
				require_once "phoponent/app/components/core/{$class}/{$class}.php";
409
                require_once "phoponent/app/components/custom/{$class}/{$class}.php";
410
                $class_tag = "\\phoponent\\app\\component\\custom\\$class";
411
				/**
412
				 * @var xphp_tag $tag
413
				 */
414
				$tag = new $class_tag(self::get_models($class), self::get_views($class), self::get_services($class_tag), $template);
415
				foreach ($arguments as $argument => $valeur) {
416
					if ($argument === 'value') {
417
					    $tag->value($valeur);
418
					} else {
419
					    $tag->attribute($argument, $valeur);
420
					}
421
				}
422
				$template = str_replace($matches[0], $tag->render(), $template);
423
			}
424
			elseif(is_file("phoponent/app/components/core/{$class}/{$class}.php")) {
425
                require_once "phoponent/app/components/core/{$class}/{$class}.php";
426
                $class_tag = "\\phoponent\\app\\component\\core\\$class";
427
                /**
428
                 * @var xphp_tag $tag
429
                 */
430
                $tag = new $class_tag(self::get_models($class), self::get_views($class), self::get_services($class_tag), $template);
431
                foreach ($arguments as $argument => $valeur) {
432
                    if ($argument === 'value') {
433
                        $tag->value($valeur);
434
                    } else {
435
                        $tag->attribute($argument, $valeur);
436
                    }
437
                }
438
                $template = str_replace($matches[0], $tag->render(), $template);
439
            }
440
		}, $template);
441
	}
442
}