Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
25 / 25 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
Preloader | |
100.00% |
25 / 25 |
|
100.00% |
5 / 5 |
16 | |
100.00% |
1 / 1 |
ignore | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
includePath | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
load | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
5 | |||
loadDir | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
6 | |||
loadFile | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | /** |
3 | * Jingga |
4 | * |
5 | * PHP Version 8.1 |
6 | * |
7 | * @package phpOMS |
8 | * @copyright Dennis Eichhorn |
9 | * @license OMS License 2.0 |
10 | * @version 1.0.0 |
11 | * @link https://jingga.app |
12 | */ |
13 | declare(strict_types=1); |
14 | |
15 | namespace phpOMS; |
16 | |
17 | /** |
18 | * Preloader class. |
19 | * |
20 | * @package phpOMS |
21 | * @license OMS License 2.0 |
22 | * @link https://jingga.app |
23 | * @since 1.0.0 |
24 | */ |
25 | final class Preloader |
26 | { |
27 | /** |
28 | * Files and paths |
29 | * |
30 | * @var string[] |
31 | * @since 1.0.0 |
32 | */ |
33 | private array $includes = []; |
34 | |
35 | /** |
36 | * Ignored files and paths |
37 | * |
38 | * @var string[] |
39 | * @since 1.0.0 |
40 | */ |
41 | private array $ignores = ['.', '..']; |
42 | |
43 | /** |
44 | * Ignore a path or file from preloading |
45 | * |
46 | * @param string $path Path to prevent preloading |
47 | * |
48 | * @return Preloader |
49 | * |
50 | * @since 1.0.0 |
51 | */ |
52 | public function ignore(string $path) : self |
53 | { |
54 | $this->ignores[] = $path; |
55 | |
56 | return $this; |
57 | } |
58 | |
59 | /** |
60 | * Add a path to preload |
61 | * |
62 | * @param string $path Path to preload |
63 | * |
64 | * @return Preloader |
65 | * |
66 | * @since 1.0.0 |
67 | */ |
68 | public function includePath(string $path) : self |
69 | { |
70 | $this->includes[] = $path; |
71 | |
72 | return $this; |
73 | } |
74 | |
75 | /** |
76 | * Load paths |
77 | * |
78 | * @return void |
79 | * |
80 | * @since 1.0.0 |
81 | */ |
82 | public function load() : void |
83 | { |
84 | foreach ($this->includes as $include) { |
85 | if (\in_array($include, $this->ignores)) { |
86 | continue; |
87 | } |
88 | |
89 | if (\is_dir($include)) { |
90 | $this->loadDir($include); |
91 | } elseif (\is_file($include)) { |
92 | $this->loadFile($include); |
93 | } |
94 | } |
95 | } |
96 | |
97 | /** |
98 | * Load directory paths |
99 | * |
100 | * @param string $path Path to load |
101 | * |
102 | * @return void |
103 | * |
104 | * @since 1.0.0 |
105 | */ |
106 | private function loadDir(string $path) : void |
107 | { |
108 | $fh = \opendir($path); |
109 | |
110 | if ($fh === false) { |
111 | return; // @codeCoverageIgnore |
112 | } |
113 | |
114 | while ($file = \readdir($fh)) { |
115 | if (\in_array($file, $this->ignores)) { |
116 | continue; |
117 | } |
118 | |
119 | if (\is_dir($path . '/' . $file)) { |
120 | $this->loadDir($path . '/' . $file); |
121 | } elseif (\is_file($path . '/' . $file)) { |
122 | $this->loadFile($path . '/' . $file); |
123 | } |
124 | } |
125 | |
126 | \closedir($fh); |
127 | } |
128 | |
129 | /** |
130 | * Load file |
131 | * |
132 | * @param string $path Path to load |
133 | * |
134 | * @return void |
135 | * |
136 | * @since 1.0.0 |
137 | */ |
138 | private function loadFile(string $path) : void |
139 | { |
140 | if (\in_array($path, $this->ignores) |
141 | || \substr($path, -\strlen('.php')) !== '.php' |
142 | ) { |
143 | return; |
144 | } |
145 | |
146 | require_once($path); |
147 | } |
148 | } |