|
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
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private static $loads = [ |
|
26
|
|
|
'controllers' => [], |
|
27
|
|
|
'helpers' => [], |
|
28
|
|
|
'langs' => [], |
|
29
|
|
|
'libraries' => [], |
|
30
|
|
|
'models' => [], |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Charge un fichier d'aide |
|
35
|
|
|
* |
|
36
|
|
|
* @throws LoadException |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function helper(array|string $helpers) |
|
39
|
|
|
{ |
|
40
|
|
|
if ($helpers === '' || $helpers === '0' || $helpers === []) { |
|
|
|
|
|
|
41
|
34 |
|
throw new LoadException('Veuillez specifier le helper à charger'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
34 |
|
$helpers = (array) $helpers; |
|
45
|
|
|
|
|
46
|
|
|
foreach ($helpers as $helper) { |
|
47
|
34 |
|
FileLocator::helper($helper); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Charge un modele |
|
53
|
|
|
* |
|
54
|
|
|
* @return object|object[] |
|
55
|
|
|
* |
|
56
|
|
|
* @throws LoadException |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function model(array|string $model, ?ConnectionInterface $connection = null) |
|
59
|
|
|
{ |
|
60
|
|
|
if ($model === '' || $model === '0' || $model === []) { |
|
|
|
|
|
|
61
|
|
|
throw new LoadException('Veuillez specifier le modele à charger'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (is_array($model)) { |
|
|
|
|
|
|
65
|
|
|
$models = []; |
|
66
|
|
|
|
|
67
|
|
|
foreach ($model as $value) { |
|
68
|
|
|
$models[] = self::model($value, $connection); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $models; |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (! self::isLoaded('models', $model)) { |
|
75
|
|
|
self::loaded('models', $model, FileLocator::model($model, $connection)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return self::getLoaded('models', $model); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Verifie si un element est chargé dans la liste des modules |
|
83
|
|
|
* |
|
84
|
|
|
* @param mixed $element |
|
85
|
|
|
*/ |
|
86
|
|
|
private static function isLoaded(string $module, $element): bool |
|
87
|
|
|
{ |
|
88
|
|
|
if (! isset(self::$loads[$module]) || ! is_array(self::$loads[$module])) { |
|
89
|
|
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return in_array($element, self::$loads[$module], true); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Ajoute un element aux elements chargés |
|
97
|
|
|
* |
|
98
|
|
|
* @param mixed $value |
|
99
|
|
|
*/ |
|
100
|
|
|
private static function loaded(string $module, string $element, $value = null): void |
|
101
|
|
|
{ |
|
102
|
|
|
self::$loads[$module][$element] = $value; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Renvoie un element chargé |
|
107
|
|
|
* |
|
108
|
|
|
* @param mixed $element |
|
109
|
|
|
* |
|
110
|
|
|
* @return mixed |
|
111
|
|
|
*/ |
|
112
|
|
|
private static function getLoaded(string $module, $element) |
|
113
|
|
|
{ |
|
114
|
|
|
return self::$loads[$module][$element] ?? null; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|