1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file is part of Blitz PHP framework. |
||
5 | * |
||
6 | * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view |
||
9 | * the LICENSE file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace BlitzPHP\Loader; |
||
13 | |||
14 | use BlitzPHP\Contracts\Database\ConnectionInterface; |
||
15 | use BlitzPHP\Exceptions\LoadException; |
||
16 | |||
17 | class Load |
||
18 | { |
||
19 | /** |
||
20 | * Liste des elements deja chargés, |
||
21 | * Si un element est deja chargé, on le renvoie simplement sans avoir besoin de le construire à nouveau |
||
22 | */ |
||
23 | private static array $loads = [ |
||
24 | 'controllers' => [], |
||
25 | 'helpers' => [], |
||
26 | 'langs' => [], |
||
27 | 'libraries' => [], |
||
28 | 'models' => [], |
||
29 | ]; |
||
30 | |||
31 | /** |
||
32 | * Charge un fichier d'aide |
||
33 | * |
||
34 | * @throws LoadException |
||
35 | */ |
||
36 | public static function helper(array|string $helpers) |
||
37 | { |
||
38 | if ($helpers === '' || $helpers === '0' || $helpers === []) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
39 | 34 | throw new LoadException('Veuillez specifier le helper à charger'); |
|
40 | } |
||
41 | |||
42 | 34 | $helpers = (array) $helpers; |
|
43 | |||
44 | foreach ($helpers as $helper) { |
||
45 | 34 | FileLocator::helper($helper); |
|
46 | } |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Charge un modele |
||
51 | * |
||
52 | * @return list<object>|object |
||
0 ignored issues
–
show
The type
BlitzPHP\Loader\list was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
53 | * |
||
54 | * @throws LoadException |
||
55 | */ |
||
56 | public static function model(array|string $model, ?ConnectionInterface $connection = null) |
||
57 | { |
||
58 | if ($model === '' || $model === '0' || $model === []) { |
||
0 ignored issues
–
show
|
|||
59 | throw new LoadException('Veuillez specifier le modele à charger'); |
||
60 | } |
||
61 | |||
62 | if (is_array($model)) { |
||
0 ignored issues
–
show
|
|||
63 | $models = []; |
||
64 | |||
65 | foreach ($model as $value) { |
||
66 | $models[] = self::model($value, $connection); |
||
67 | } |
||
68 | |||
69 | return $models; |
||
0 ignored issues
–
show
|
|||
70 | } |
||
71 | |||
72 | if (! self::isLoaded('models', $model)) { |
||
73 | self::loaded('models', $model, FileLocator::model($model, $connection)); |
||
74 | } |
||
75 | |||
76 | return self::getLoaded('models', $model); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Verifie si un element est chargé dans la liste des modules |
||
81 | */ |
||
82 | private static function isLoaded(string $module, string $element): bool |
||
83 | { |
||
84 | if (! isset(self::$loads[$module]) || ! is_array(self::$loads[$module])) { |
||
85 | return false; |
||
86 | } |
||
87 | |||
88 | return in_array($element, self::$loads[$module], true); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Ajoute un element aux elements chargés |
||
93 | * |
||
94 | * @param mixed $value |
||
95 | */ |
||
96 | private static function loaded(string $module, string $element, $value = null): void |
||
97 | { |
||
98 | self::$loads[$module][$element] = $value; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Renvoie un element chargé |
||
103 | * |
||
104 | * @return mixed |
||
105 | */ |
||
106 | private static function getLoaded(string $module, string $element) |
||
107 | { |
||
108 | return self::$loads[$module][$element] ?? null; |
||
109 | } |
||
110 | } |
||
111 |