Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
Preloader.php
Go to the documentation of this file.
1<?php
2
3namespace Ubiquity\cache;
4
6
15class Preloader extends PreloaderInternalTrait {
16
22 public function __construct($appRoot) {
23 $this->vendorDir = $appRoot . './../vendor/';
24 $this->loader = require $this->vendorDir . 'autoload.php';
25 }
26
33 public function paths(string ...$paths): Preloader {
34 foreach ( $paths as $path ) {
35 $this->addDir ( $path );
36 }
37 return $this;
38 }
39
46 public function exclude(string ...$names): Preloader {
47 $this->excludeds = \array_merge ( $this->excludeds, $names );
48 return $this;
49 }
50
57 public function addClass(string $class): bool {
58 if (! $this->isExcluded ( $class )) {
59 if (! isset ( $this->classes [$class] )) {
60 $path = $this->getPathFromClass ( $class );
61 if (isset ( $path )) {
62 $this->classes [$class] = $path;
63 return true;
64 }
65 }
66 }
67 return false;
68 }
69
75 public function addClasses(array $classes) {
76 foreach ( $classes as $class ) {
77 $this->addClass ( $class );
78 }
79 }
80
84 public function load() {
85 foreach ( $this->classes as $class => $file ) {
86 if (! $this->isExcluded ( $class )) {
87 $this->loadClass ( $class, $file );
88 }
89 }
90 }
91
97 public function generateClassesFiles(): array {
98 $ret = [ ];
99 foreach ( $this->classes as $class => $file ) {
100 if (! $this->isExcluded ( $class )) {
101 $ret [$class] = \realpath ( $file );
102 }
103 }
104 return $ret;
105 }
106
114 public function generateToFile(string $filename, ?bool $preserve = true): int {
115 $array = [ ];
116 if ($preserve && \file_exists ( $filename )) {
117 $array = include $filename;
118 }
119 $array ['classes-files'] = $this->generateClassesFiles ();
120 $content = "<?php\nreturn " . $this->asPhpArray ( $array, 'array', 1, true ) . ";";
121 return \file_put_contents ( $filename, $content );
122 }
123
130 public function addDir(string $dirname): Preloader {
131 $files = $this->glob_recursive ( $dirname . DIRECTORY_SEPARATOR . '*.php' );
132 foreach ( $files as $file ) {
133 $class = $this->getClassFullNameFromFile ( $file );
134 if (isset ( $class )) {
135 $this->addClassFile ( $class, $file );
136 }
137 }
138 return $this;
139 }
140
149 public function addLibraryPart(string $library, ?string $part = ''): bool {
150 if (isset ( self::$libraries [$library] )) {
151 $dir = $this->vendorDir . self::$libraries [$library] . $part;
152 if (\file_exists ( $dir )) {
153 $this->addDir ( $dir );
154 return true;
155 }
156 }
157 return false;
158 }
159
165 public function addUbiquityControllers() {
166 $this->addLibraryPart ( 'ubiquity', 'controllers' );
167 $this->addClass ( 'Ubiquity\\events\\EventManager' );
168 $this->addClass ( 'Ubiquity\\log\\Logger' );
169 $this->exclude ( 'Ubiquity\\controllers\\crud', 'Ubiquity\\controllers\\rest', 'Ubiquity\\controllers\\seo', 'Ubiquity\\controllers\\auth' );
170 return $this;
171 }
172
178 public function addUbiquityCache() {
179 $this->addClass ( 'Ubiquity\\cache\\CacheManager' );
180 $this->addClass ( 'Ubiquity\\cache\\system\\ArrayCache' );
181 return $this;
182 }
183
189 public function addUbiquityPdo() {
190 $this->addClass ( 'Ubiquity\\db\\Database' );
191 $this->addClass ( 'Ubiquity\\cache\\database\\DbCache' );
192 $this->addClass ( 'Ubiquity\\db\\SqlUtils' );
193 $this->addLibraryPart ( 'ubiquity', 'db/providers' );
194 return $this;
195 }
196
202 public function addUbiquityORM() {
203 $this->addLibraryPart ( 'ubiquity', 'orm' );
204 $this->addClass ( 'Ubiquity\\events\\DAOEvents' );
205 return $this;
206 }
207
213 public function addUbiquityHttpUtils() {
214 $this->addClass ( 'Ubiquity\\utils\\http\\URequest' );
215 $this->addClass ( 'Ubiquity\\utils\\http\\UResponse' );
216 $this->addClass ( 'Ubiquity\\utils\\http\\foundation\\PhpHttp' );
217 return $this;
218 }
219
225 public function addUbiquityViews() {
226 $this->addClass ( 'Ubiquity\\views\\View' );
227 $this->addClass ( 'Ubiquity\\views\\engine\\micro\\MicroTemplateEngine' );
228 return $this;
229 }
230
236 public function addUbiquityTranslations() {
237 $this->addLibraryPart ( 'ubiquity', 'translation' );
238 return $this;
239 }
240
246 public function addUbiquityWorkerman() {
247 $this->addClasses ( [ 'Ubiquity\\servers\\workerman\\WorkermanServer','Ubiquity\\utils\\http\\foundation\\WorkermanHttp' ] );
248 return $this;
249 }
250
256 public function addUbiquitySwoole() {
257 $this->addClasses ( [ 'Ubiquity\\servers\\swoole\\SwooleServer','Ubiquity\\utils\\http\\foundation\\SwooleHttp' ] );
258 return $this;
259 }
260
267 public function addApplicationPart(?string $part = '') {
268 $this->addLibraryPart ( 'application', $part );
269 return $this;
270 }
271
277 public function addApplicationModels($dir = 'models') {
278 $this->addLibraryPart ( 'application', $dir );
279 return $this;
280 }
281
287 public function addApplicationCache($dir = 'cache') {
288 $this->addLibraryPart ( 'application', $dir );
289 return $this;
290 }
291
297 public function addApplicationControllers($dir = 'controllers') {
298 $this->addLibraryPart ( 'application', $dir );
299 $this->exclude ( $dir . '\\MaintenanceController', $dir . '\\Admin' );
300 return $this;
301 }
302
309 public function addUbiquityBasics($hasDatabase = true) {
310 $this->addUbiquityCache ();
311 $this->addUbiquityControllers ();
312 // $this->addUbiquityHttpUtils ();
313 if ($hasDatabase) {
314 $this->addUbiquityPdo ();
315 $this->addUbiquityORM ();
316 }
317 return $this;
318 }
319
325 public function addUbiquityTwig() {
326 $this->addClasses ( ['Ubiquity\\views\\engine\\twig\\Twig','Twig\\Cache\\FilesystemCache','Twig\\Extension\\CoreExtension','Twig\\Extension\\EscaperExtension','Twig\\Extension\\OptimizerExtension','Twig\\Extension\\StagingExtension','Twig\\ExtensionSet','Twig\\Template','Twig\\TemplateWrapper' ] );
327 return $this;
328 }
329
337 public static function fromFile(string $appRoot, string $filename): bool {
338 if (\file_exists ( $filename )) {
339 $array = include $filename;
340 return self::fromArray ( $appRoot, $array );
341 }
342 return false;
343 }
344
352 public static function fromArray(string $appRoot, array $array): bool {
353 $pre = new self ( $appRoot );
354 self::$count = 0;
355 if (isset ( $array ['classes-files'] )) {
356 $pre->classes = $array ['classes-files'];
357 }
358 if (isset ( $array ['excludeds'] )) {
359 $pre->excludeds = $array ['excludeds'];
360 }
361 if (isset ( $array ['paths'] )) {
362 foreach ( $array ['paths'] as $path ) {
363 $pre->addDir ( $path );
364 }
365 }
366 if (isset ( $array ['classes'] )) {
367 foreach ( $array ['classes'] as $class ) {
368 $pre->addClass ( $class );
369 }
370 }
371 if (isset ( $array ['libraries-parts'] )) {
372 foreach ( $array ['libraries-parts'] as $library => $parts ) {
373 foreach ( $parts as $part ) {
374 $pre->addLibraryPart ( $library, $part );
375 }
376 }
377 }
378 if (isset ( $array ['callback'] )) {
379 if (\is_callable ( $array ['callback'] )) {
380 $call = $array ['callback'];
381 $call ( $pre );
382 }
383 }
384 $pre->load ();
385 return self::$count > 0;
386 }
387
393 public function generateClassesFromRunning($resetExisting = true) {
394 $cache = \opcache_get_status ( true );
395 if ($resetExisting) {
396 $this->classes = [ ];
397 }
398 foreach ( $cache ['scripts'] as $script ) {
399 $path = $script ['full_path'];
400 $class = $this->getClassFullNameFromFile ( $path );
401 if (isset ( $class )) {
402 $this->addClassFile ( $class, $path );
403 }
404 }
405 }
406
412 public static function getLibraries() {
413 return \array_keys ( self::$libraries );
414 }
415}
416
Ubiquity\cache\preloading$PreloaderInternalTrait This class is part of Ubiquity.
Cache managment.
Definition CacheFile.php:3