| Total Complexity | 59 |
| Total Lines | 388 |
| Duplicated Lines | 0 % |
| Changes | 10 | ||
| Bugs | 1 | Features | 2 |
Complex classes like Preloader often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Preloader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Preloader { |
||
| 16 | use PreloaderInternalTrait; |
||
|
1 ignored issue
–
show
|
|||
| 17 | |||
| 18 | /** |
||
| 19 | * Creates a new loader instance for this application. |
||
| 20 | * |
||
| 21 | * @param string $appRoot The app root |
||
| 22 | */ |
||
| 23 | public function __construct($appRoot) { |
||
| 24 | $this->vendorDir = $appRoot . './../vendor/'; |
||
| 25 | $this->loader = require $this->vendorDir . 'autoload.php'; |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Adds paths to be scanned during preloading. |
||
| 30 | * |
||
| 31 | * @param string ...$paths |
||
| 32 | * @return Preloader |
||
| 33 | */ |
||
| 34 | public function paths(string ...$paths): Preloader { |
||
| 35 | foreach ( $paths as $path ) { |
||
| 36 | $this->addDir ( $path ); |
||
| 37 | } |
||
| 38 | return $this; |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Adds namespaces to exclude from preloading. |
||
| 43 | * |
||
| 44 | * @param string ...$names |
||
| 45 | * @return Preloader |
||
| 46 | */ |
||
| 47 | public function exclude(string ...$names): Preloader { |
||
| 48 | $this->excludeds = \array_merge ( $this->excludeds, $names ); |
||
| 49 | return $this; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Adds a class to preload. |
||
| 54 | * |
||
| 55 | * @param string $class |
||
| 56 | * @return bool |
||
| 57 | */ |
||
| 58 | public function addClass(string $class): bool { |
||
| 59 | if (! $this->isExcluded ( $class )) { |
||
| 60 | if (! isset ( $this->classes [$class] )) { |
||
| 61 | $path = $this->getPathFromClass ( $class ); |
||
| 62 | if (isset ( $path )) { |
||
| 63 | $this->classes [$class] = $path; |
||
| 64 | return true; |
||
| 65 | } |
||
| 66 | } |
||
| 67 | } |
||
| 68 | return false; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Adds an array of classes to preload. |
||
| 73 | * |
||
| 74 | * @param array $classes |
||
| 75 | */ |
||
| 76 | public function addClasses(array $classes) { |
||
| 77 | foreach ( $classes as $class ) { |
||
| 78 | $this->addClass ( $class ); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Preload all added classes. |
||
| 84 | */ |
||
| 85 | public function load() { |
||
| 89 | } |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Returns a generated associative array of classes to preload (key: class, value: file). |
||
| 95 | * |
||
| 96 | * @return array |
||
| 97 | */ |
||
| 98 | public function generateClassesFiles(): array { |
||
| 99 | $ret = [ ]; |
||
| 100 | foreach ( $this->classes as $class => $file ) { |
||
| 101 | if (! $this->isExcluded ( $class )) { |
||
| 102 | $ret [$class] = \realpath ( $file ); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | return $ret; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Generate a file containing the associative array of classes to preload (classes-files=>[key: class, value: file}). |
||
| 110 | * |
||
| 111 | * @param string $filename |
||
| 112 | * @param ?bool $preserve |
||
| 113 | * @return int |
||
| 114 | */ |
||
| 115 | public function generateToFile(string $filename, ?bool $preserve = true): int { |
||
| 116 | $array = [ ]; |
||
| 117 | if ($preserve && \file_exists ( $filename )) { |
||
| 118 | $array = include $filename; |
||
| 119 | } |
||
| 120 | $array ['classes-files'] = $this->generateClassesFiles (); |
||
| 121 | $content = "<?php\nreturn " . $this->asPhpArray ( $array, 'array', 1, true ) . ";"; |
||
| 122 | return \file_put_contents ( $filename, $content ); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Adds a directory to be scanned during preloading. |
||
| 127 | * |
||
| 128 | * @param string $dirname |
||
| 129 | * @return \Ubiquity\cache\Preloader |
||
| 130 | */ |
||
| 131 | public function addDir(string $dirname): Preloader { |
||
| 132 | $files = $this->glob_recursive ( $dirname . DIRECTORY_SEPARATOR . '*.php' ); |
||
| 133 | foreach ( $files as $file ) { |
||
| 134 | $class = $this->getClassFullNameFromFile ( $file ); |
||
| 135 | if (isset ( $class )) { |
||
| 136 | $this->addClassFile ( $class, $file ); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | return $this; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Adds a part of an existing library to be preloaded. |
||
| 144 | * The available libraries can be obtained with the getLibraries method. |
||
| 145 | * |
||
| 146 | * @param string $library |
||
| 147 | * @param ?string $part |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | public function addLibraryPart(string $library, ?string $part = ''): bool { |
||
| 151 | if (isset ( self::$libraries [$library] )) { |
||
| 152 | $dir = $this->vendorDir . self::$libraries [$library] . $part; |
||
| 153 | if (\file_exists ( $dir )) { |
||
| 154 | $this->addDir ( $dir ); |
||
| 155 | return true; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | return false; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Adds Ubiquity framework controller and routing classes preload. |
||
| 163 | * |
||
| 164 | * @return \Ubiquity\cache\Preloader |
||
| 165 | */ |
||
| 166 | public function addUbiquityControllers() { |
||
| 167 | $this->addLibraryPart ( 'ubiquity', 'controllers' ); |
||
| 168 | $this->addClass ( 'Ubiquity\\events\\EventManager' ); |
||
| 169 | $this->addClass ( 'Ubiquity\\log\\Logger' ); |
||
| 170 | $this->exclude ( 'Ubiquity\\controllers\\crud', 'Ubiquity\\controllers\\rest', 'Ubiquity\\controllers\\seo', 'Ubiquity\\controllers\\auth' ); |
||
| 171 | return $this; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Adds Ubiquity framework cache system classes to preload. |
||
| 176 | * |
||
| 177 | * @return \Ubiquity\cache\Preloader |
||
| 178 | */ |
||
| 179 | public function addUbiquityCache() { |
||
| 180 | $this->addClass ( 'Ubiquity\\cache\\CacheManager' ); |
||
| 181 | $this->addClass ( 'Ubiquity\\cache\\system\\ArrayCache' ); |
||
| 182 | return $this; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Adds Ubiquity framework PDO classes to preload. |
||
| 187 | * |
||
| 188 | * @return \Ubiquity\cache\Preloader |
||
| 189 | */ |
||
| 190 | public function addUbiquityPdo() { |
||
| 191 | $this->addClass ( 'Ubiquity\\db\\Database' ); |
||
| 192 | $this->addClass ( 'Ubiquity\\cache\\database\\DbCache' ); |
||
| 193 | $this->addClass ( 'Ubiquity\\db\\SqlUtils' ); |
||
| 194 | $this->addLibraryPart ( 'ubiquity', 'db/providers' ); |
||
| 195 | return $this; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Adds Ubiquity framework ORM classes to preload. |
||
| 200 | * |
||
| 201 | * @return \Ubiquity\cache\Preloader |
||
| 202 | */ |
||
| 203 | public function addUbiquityORM() { |
||
| 204 | $this->addLibraryPart ( 'ubiquity', 'orm' ); |
||
| 205 | $this->addClass ( 'Ubiquity\\events\\DAOEvents' ); |
||
| 206 | return $this; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Adds Ubiquity framework Http classes to preload. |
||
| 211 | * |
||
| 212 | * @return \Ubiquity\cache\Preloader |
||
| 213 | */ |
||
| 214 | public function addUbiquityHttpUtils() { |
||
| 215 | $this->addClass ( 'Ubiquity\\utils\\http\\URequest' ); |
||
| 216 | $this->addClass ( 'Ubiquity\\utils\\http\\UResponse' ); |
||
| 217 | $this->addClass ( 'Ubiquity\\utils\\http\\foundation\\PhpHttp' ); |
||
| 218 | return $this; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Adds Ubiquity framework MicroTemplateEngine classes to preload. |
||
| 223 | * |
||
| 224 | * @return \Ubiquity\cache\Preloader |
||
| 225 | */ |
||
| 226 | public function addUbiquityViews() { |
||
| 227 | $this->addClass ( 'Ubiquity\\views\\View' ); |
||
| 228 | $this->addClass ( 'Ubiquity\\views\\engine\\micro\\MicroTemplateEngine' ); |
||
| 229 | return $this; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Adds Ubiquity framework Translations classes to preload. |
||
| 234 | * |
||
| 235 | * @return \Ubiquity\cache\Preloader |
||
| 236 | */ |
||
| 237 | public function addUbiquityTranslations() { |
||
| 238 | $this->addLibraryPart ( 'ubiquity', 'translation' ); |
||
| 239 | return $this; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Adds Ubiquity-workerman classes to preload. |
||
| 244 | * |
||
| 245 | * @return \Ubiquity\cache\Preloader |
||
| 246 | */ |
||
| 247 | public function addUbiquityWorkerman() { |
||
| 248 | $this->addLibraryPart ( 'ubiquity-workerman' ); |
||
| 249 | return $this; |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Adds classes from an application part (app folder) to preload. |
||
| 254 | * |
||
| 255 | * @param string $part |
||
| 256 | * @return \Ubiquity\cache\Preloader |
||
| 257 | */ |
||
| 258 | public function addApplicationPart(?string $part = '') { |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * |
||
| 265 | * @param string $dir |
||
| 266 | * @return \Ubiquity\cache\Preloader |
||
| 267 | */ |
||
| 268 | public function addApplicationModels($dir = 'models') { |
||
| 269 | $this->addLibraryPart ( 'application', $dir ); |
||
| 270 | return $this; |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * |
||
| 275 | * @param string $dir |
||
| 276 | * @return \Ubiquity\cache\Preloader |
||
| 277 | */ |
||
| 278 | public function addApplicationCache($dir = 'cache') { |
||
| 279 | $this->addLibraryPart ( 'application', $dir ); |
||
| 280 | return $this; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * |
||
| 285 | * @param string $dir |
||
| 286 | * @return \Ubiquity\cache\Preloader |
||
| 287 | */ |
||
| 288 | public function addApplicationControllers($dir = 'controllers') { |
||
| 289 | $this->addLibraryPart ( 'application', $dir ); |
||
| 290 | $this->exclude ( $dir . '\\MaintenanceController', $dir . '\\Admin' ); |
||
| 291 | return $this; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Add Ubiquity hot classes for preloading |
||
| 296 | * |
||
| 297 | * @param boolean $hasDatabase |
||
| 298 | */ |
||
| 299 | public function addUbiquityBasics($hasDatabase = true) { |
||
| 300 | $this->addUbiquityCache (); |
||
| 301 | $this->addUbiquityControllers (); |
||
| 302 | $this->addUbiquityHttpUtils (); |
||
| 303 | if ($hasDatabase) { |
||
| 304 | $this->addUbiquityPdo (); |
||
| 305 | $this->addUbiquityORM (); |
||
| 306 | } |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Adds Twig templating system classes to preload. |
||
| 311 | * |
||
| 312 | * @return \Ubiquity\cache\Preloader |
||
| 313 | */ |
||
| 314 | public function addUbiquityTwig() { |
||
| 315 | $this->addClasses ( [ 'Ubiquity\\views\\engine\\Twig','Twig\\Cache\\FilesystemCache','Twig\\Extension\\CoreExtension','Twig\\Extension\\EscaperExtension','Twig\\Extension\\OptimizerExtension','Twig\\Extension\\StagingExtension','Twig\\ExtensionSet','Twig\\Template','Twig\\TemplateWrapper' ] ); |
||
| 316 | return $this; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Defines classes to be preloaded in a file returning an associative array keys : (classes-files, excludeds, paths, classes, libraries-parts, callback). |
||
| 321 | * |
||
| 322 | * @param string $appRoot |
||
| 323 | * @param string $filename |
||
| 324 | * @return bool |
||
| 325 | */ |
||
| 326 | public static function fromFile(string $appRoot, string $filename): bool { |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Defines classes to be preloaded with an associative array keys : (classes-files, excludeds, paths, classes, libraries-parts, callback). |
||
| 336 | * |
||
| 337 | * @param string $appRoot |
||
| 338 | * @param array $array |
||
| 339 | * @return bool |
||
| 340 | */ |
||
| 341 | public static function fromArray(string $appRoot, array $array): bool { |
||
| 342 | $pre = new self ( $appRoot ); |
||
| 343 | self::$count = 0; |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Generates a preload classes-files array from cached files |
||
| 379 | * |
||
| 380 | * @param boolean $resetExisting |
||
| 381 | */ |
||
| 382 | public function generateClassesFromRunning($resetExisting = true) { |
||
| 383 | $cache = \opcache_get_status ( true ); |
||
| 384 | if ($resetExisting) { |
||
| 385 | $this->classes = [ ]; |
||
| 386 | } |
||
| 387 | foreach ( $cache ['scripts'] as $script ) { |
||
| 388 | $path = $script ['full_path']; |
||
| 389 | $class = $this->getClassFullNameFromFile ( $path ); |
||
| 390 | if (isset ( $class )) { |
||
| 391 | $this->addClassFile ( $class, $path ); |
||
| 392 | } |
||
| 393 | } |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Returns an array of available libraries to be preloaded |
||
| 398 | * |
||
| 399 | * @return array |
||
| 400 | */ |
||
| 401 | public static function getLibraries() { |
||
| 403 | } |
||
| 404 | } |
||
| 405 | |||
| 406 |