15class Preloader
extends PreloaderInternalTrait {
22 public function __construct($appRoot) {
23 $this->vendorDir = $appRoot .
'./../vendor/';
24 $this->loader = require $this->vendorDir .
'autoload.php';
33 public function paths(
string ...$paths): Preloader {
34 foreach ( $paths as $path ) {
35 $this->addDir ( $path );
46 public function exclude(
string ...$names): Preloader {
47 $this->excludeds = \array_merge ( $this->excludeds, $names );
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;
75 public function addClasses(array $classes) {
76 foreach ( $classes as $class ) {
77 $this->addClass ( $class );
84 public function load() {
85 foreach ( $this->classes as $class => $file ) {
86 if (! $this->isExcluded ( $class )) {
87 $this->loadClass ( $class, $file );
97 public function generateClassesFiles(): array {
99 foreach ( $this->classes as $class => $file ) {
100 if (! $this->isExcluded ( $class )) {
101 $ret [$class] = \realpath ( $file );
114 public function generateToFile(
string $filename, ?
bool $preserve =
true): int {
116 if ($preserve && \file_exists ( $filename )) {
117 $array = include $filename;
119 $array [
'classes-files'] = $this->generateClassesFiles ();
120 $content =
"<?php\nreturn " . $this->asPhpArray ( $array,
'array', 1,
true ) .
";";
121 return \file_put_contents ( $filename, $content );
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 );
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 );
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' );
178 public function addUbiquityCache() {
179 $this->addClass (
'Ubiquity\\cache\\CacheManager' );
180 $this->addClass (
'Ubiquity\\cache\\system\\ArrayCache' );
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' );
202 public function addUbiquityORM() {
203 $this->addLibraryPart (
'ubiquity',
'orm' );
204 $this->addClass (
'Ubiquity\\events\\DAOEvents' );
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' );
225 public function addUbiquityViews() {
226 $this->addClass (
'Ubiquity\\views\\View' );
227 $this->addClass (
'Ubiquity\\views\\engine\\micro\\MicroTemplateEngine' );
236 public function addUbiquityTranslations() {
237 $this->addLibraryPart (
'ubiquity',
'translation' );
246 public function addUbiquityWorkerman() {
247 $this->addClasses ( [
'Ubiquity\\servers\\workerman\\WorkermanServer',
'Ubiquity\\utils\\http\\foundation\\WorkermanHttp' ] );
256 public function addUbiquitySwoole() {
257 $this->addClasses ( [
'Ubiquity\\servers\\swoole\\SwooleServer',
'Ubiquity\\utils\\http\\foundation\\SwooleHttp' ] );
267 public function addApplicationPart(?
string $part =
'') {
268 $this->addLibraryPart (
'application', $part );
277 public function addApplicationModels($dir =
'models') {
278 $this->addLibraryPart (
'application', $dir );
287 public function addApplicationCache($dir =
'cache') {
288 $this->addLibraryPart (
'application', $dir );
297 public function addApplicationControllers($dir =
'controllers') {
298 $this->addLibraryPart (
'application', $dir );
299 $this->exclude ( $dir .
'\\MaintenanceController', $dir .
'\\Admin' );
309 public function addUbiquityBasics($hasDatabase =
true) {
310 $this->addUbiquityCache ();
311 $this->addUbiquityControllers ();
314 $this->addUbiquityPdo ();
315 $this->addUbiquityORM ();
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' ] );
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 );
352 public static function fromArray(
string $appRoot, array $array): bool {
353 $pre = new self ( $appRoot );
355 if (isset ( $array [
'classes-files'] )) {
356 $pre->classes = $array [
'classes-files'];
358 if (isset ( $array [
'excludeds'] )) {
359 $pre->excludeds = $array [
'excludeds'];
361 if (isset ( $array [
'paths'] )) {
362 foreach ( $array [
'paths'] as $path ) {
363 $pre->addDir ( $path );
366 if (isset ( $array [
'classes'] )) {
367 foreach ( $array [
'classes'] as $class ) {
368 $pre->addClass ( $class );
371 if (isset ( $array [
'libraries-parts'] )) {
372 foreach ( $array [
'libraries-parts'] as $library => $parts ) {
373 foreach ( $parts as $part ) {
374 $pre->addLibraryPart ( $library, $part );
378 if (isset ( $array [
'callback'] )) {
379 if (\is_callable ( $array [
'callback'] )) {
380 $call = $array [
'callback'];
385 return self::$count > 0;
393 public function generateClassesFromRunning($resetExisting =
true) {
394 $cache = \opcache_get_status (
true );
395 if ($resetExisting) {
396 $this->classes = [ ];
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 );
412 public static function getLibraries() {
413 return \array_keys ( self::$libraries );
Ubiquity\cache\preloading$PreloaderInternalTrait This class is part of Ubiquity.