3 namespace Org\Mxchange\CoreFramework\Loader;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Configuration\FrameworkConfiguration;
10 use \InvalidArgumentException;
11 use \RecursiveDirectoryIterator;
12 use \RecursiveIteratorIterator;
16 * This class loads class include files with a specific prefix and suffix
18 * @author Roland Haeder <webmaster@shipsimu.org>
20 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
21 * @license GNU GPL 3.0 or any newer version
22 * @link http://www.shipsimu.org
24 * This program is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 * ----------------------------------
39 * - Namespace scheme Project\Package[\SubPackage...] is fully supported and
40 * throws an InvalidArgumentException if not present. The last part will be
41 * always the class' name.
43 * - Some comments improved, other minor improvements
45 * - Constructor is now empty and factory method 'createClassLoader' is created
46 * - renamed loadClasses to scanClassPath
47 * - Added initLoader()
49 * - ClassLoader rewritten to PHP SPL's own RecursiveIteratorIterator class
51 * - loadClasses rewritten to fix some notices
54 * ----------------------------------
58 * Instance of this class
60 private static $selfInstance = NULL;
63 * Array with all found classes
65 private $foundClasses = [];
68 * List of loaded classes
70 private $loadedClasses = [];
73 * Suffix with extension for all class files
75 private $prefix = 'class_';
78 * Suffix with extension for all class files
80 private $suffix = '.php';
83 * A list for directory names (no leading/trailing slashes!) which not be scanned by the path scanner
86 private $ignoreList = [];
89 * Debug this class loader? (true = yes, false = no)
91 private $debug = false;
94 * Whether the file list is cached
96 private $listCached = false;
99 * Wethe class content has been cached
101 private $classesCached = false;
104 * SplFileInfo for the list cache
106 private $listCacheFile = NULL;
109 * SplFileInfo for class content
111 private $classCacheFile = NULL;
114 * Counter for loaded include files
119 * By default the class loader is strict with naming-convention check
121 private static $strictNamingConvention = true;
124 * Framework/application paths for classes, etc.
126 private static $frameworkPaths = [
127 'classes', // Classes
128 'exceptions', // Exceptions
129 'interfaces', // Interfaces
130 'middleware', // The middleware
135 * Registered paths where test classes can be found. These are all relative
138 private static $testPaths = [];
141 * The protected constructor. Please use the factory method below, or use
142 * getSelfInstance() for singleton
146 protected function __construct () {
147 // This is empty for now
151 * The destructor makes it sure all caches got flushed
155 public function __destruct () {
156 // Skip here if dev-mode
157 if (defined('DEVELOPER')) {
161 // Skip here if already cached
162 if ($this->listCached === false) {
163 // Writes the cache file of our list away
164 $cacheContent = json_encode($this->foundClasses);
166 // Open cache instance
167 $fileObject = $this->listCacheFile->openFile('w');
169 // And write whole list
170 $fileObject->fwrite($cacheContent);
173 // Skip here if already cached
174 if ($this->classesCached === false) {
175 // Generate a full-cache of all classes
177 foreach (array_keys($this->loadedClasses) as $fileInstance) {
179 $fileObject = $fileInstance->openFile('r');
182 // @TODO Add some uglifying code (compress) here
183 $cacheContent .= $fileObject->fread($fileInstance->getSize());
187 $fileObject = $this->classCacheFile->openFile('w');
190 $fileObject->fwrite($cacheContent);
195 * Creates an instance of this class loader for given configuration instance
197 * @param $configInstance Configuration class instance
200 public static final function createClassLoader (FrameworkConfiguration $configInstance) {
201 // Get a new instance
202 $loaderInstance = new ClassLoader();
205 $loaderInstance->initLoader($configInstance);
207 // Return the prepared instance
208 return $loaderInstance;
212 * Scans for all framework classes, exceptions and interfaces.
216 public static function scanFrameworkClasses () {
218 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
220 // Get loader instance
221 $loaderInstance = self::getSelfInstance();
223 // Get config instance
224 $configInstance = FrameworkBootstrap::getConfigurationInstance();
227 foreach (self::$frameworkPaths as $shortPath) {
229 //* NOISY-DEBUG: */ printf('[%s:%d]: shortPath=%s' . PHP_EOL, __METHOD__, __LINE__, $shortPath);
231 // Generate full path from it
232 $realPathName = realpath(sprintf(
234 $configInstance->getConfigEntry('framework_base_path'),
241 //* NOISY-DEBUG: */ printf('[%s:%d]: realPathName=%s' . PHP_EOL, __METHOD__, __LINE__, $realPathName);
243 // Is it not false and accessible?
244 if (is_bool($realPathName)) {
247 } elseif (!is_readable($realPathName)) {
248 // @TODO Throw exception instead of break
252 // Try to load the framework classes
253 $loaderInstance->scanClassPath($realPathName);
257 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
261 * Scans for application's classes, etc.
265 public static function scanApplicationClasses () {
267 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
269 // Get loader instance
270 $loaderInstance = self::getSelfInstance();
272 // Get config instance
273 $configInstance = FrameworkBootstrap::getConfigurationInstance();
275 // Load all classes for the application
276 foreach (self::$frameworkPaths as $shortPath) {
278 //* NOISY-DEBUG: */ printf('[%s:%d]: shortPath=%s' . PHP_EOL, __METHOD__, __LINE__, $shortPath);
281 $pathName = realpath(sprintf(
283 $configInstance->getConfigEntry('application_base_path'),
285 $configInstance->getConfigEntry('detected_app_name'),
291 //* NOISY-DEBUG: */ printf('[%s:%d]: pathName[%s]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype($pathName), $pathName);
293 // Is the path readable?
294 if (is_dir($pathName)) {
295 // Try to load the application classes
296 $loaderInstance->scanClassPath($pathName);
301 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
305 * Scans for test classes, etc.
309 public static function scanTestsClasses () {
311 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
313 // Get config instance
314 $configInstance = FrameworkBootstrap::getConfigurationInstance();
316 // Load all classes for the application
317 foreach (self::$testPaths as $shortPath) {
319 //* NOISY-DEBUG: */ printf('[%s:%d]: shortPath=%s' . PHP_EOL, __METHOD__, __LINE__, $shortPath);
321 // Construct path name
324 $configInstance->getConfigEntry('root_base_path'),
330 //* NOISY-DEBUG: */ printf('[%s:%d]: pathName[%s]=%s - BEFORE!' . PHP_EOL, __METHOD__, __LINE__, gettype($pathName), $pathName);
333 $realPathName = realpath($pathName);
336 //* NOISY-DEBUG: */ printf('[%s:%d]: realPathName[%s]=%s - AFTER!' . PHP_EOL, __METHOD__, __LINE__, gettype($realPathName), $realPathName);
338 // Is the path readable?
339 if ((is_dir($realPathName)) && (is_readable($realPathName))) {
340 // Try to load the application classes
341 ClassLoader::getSelfInstance()->scanClassPath($realPathName);
346 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
350 * Enables or disables strict naming-convention tests on class loading
352 * @param $strictNamingConvention Whether to strictly check naming-convention
355 public static function enableStrictNamingConventionCheck (bool $strictNamingConvention = true) {
356 self::$strictNamingConvention = $strictNamingConvention;
360 * Registeres given relative path where test classes reside. For regular
361 * framework uses, they should not be loaded (and used).
363 * @param $relativePath Relative path to test classes
366 public static function registerTestsPath (string $relativePath) {
368 //* NOISY-DEBUG: */ printf('[%s:%d]: relativePath=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $relativePath);
371 self::$testPaths[$relativePath] = $relativePath;
374 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
380 * @param $className Name of the class to load
383 public static function autoLoad (string $className) {
385 //* NOISY-DEBUG: */ printf('[%s:%d]: className=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $className);
387 // Try to include this class
388 self::getSelfInstance()->loadClassFile($className);
391 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
395 * Singleton getter for an instance of this class
397 * @return $selfInstance A singleton instance of this class
399 public static final function getSelfInstance () {
400 // Is the instance there?
401 if (is_null(self::$selfInstance)) {
403 self::$selfInstance = ClassLoader::createClassLoader(FrameworkBootstrap::getConfigurationInstance());
406 // Return the instance
407 return self::$selfInstance;
411 * Scans recursively a local path for class files which must have a prefix and a suffix as given by $this->suffix and $this->prefix
413 * @param $basePath The relative base path to 'framework_base_path' constant for all classes
414 * @param $ignoreList An optional list (array forced) of directory and file names which shall be ignored
417 public function scanClassPath (string $basePath, array $ignoreList = [] ) {
418 // Is a list has been restored from cache, don't read it again
419 if ($this->listCached === true) {
424 // Keep it in class for later usage
425 $this->ignoreList = $ignoreList;
428 * Ignore .htaccess by default as it is for protection of directories
431 array_push($ignoreList, '.htaccess');
434 * Set base directory which holds all our classes, an absolute path
435 * should be used here so is_dir(), is_file() and so on will always
436 * find the correct files and dirs.
438 $basePath2 = realpath($basePath);
440 // If the basePath is false it is invalid
441 if ($basePath2 === false) {
442 /* @TODO: Do not exit here. */
443 exit(__METHOD__ . ': Cannot read ' . $basePath . ' !' . PHP_EOL);
446 $basePath = $basePath2;
449 // Get a new iterator
450 //* NOISY-DEBUG: */ printf('[%s:%d] basePath=%s' . PHP_EOL, __METHOD__, __LINE__, $basePath);
451 $iteratorInstance = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($basePath), RecursiveIteratorIterator::CHILD_FIRST);
454 while ($iteratorInstance->valid()) {
456 $currentEntry = $iteratorInstance->current();
458 // Get filename from iterator which is the class' name (according naming-convention)
459 $fileName = $currentEntry->getFilename();
461 // Current entry must be a file, not smaller than 100 bytes and not on ignore list
462 if ((!$currentEntry->isFile()) || (in_array($fileName, $this->ignoreList)) || ($currentEntry->getSize() < 100)) {
463 // Advance to next entry
464 $iteratorInstance->next();
466 // Skip non-file entries
467 //* NOISY-DEBUG: */ printf('[%s:%d] SKIP: %s' . PHP_EOL, __METHOD__, __LINE__, $fileName);
471 // Is this file wanted?
472 //* NOISY-DEBUG: */ printf('[%s:%d] FOUND: %s' . PHP_EOL, __METHOD__, __LINE__, $fileName);
473 if ((substr($fileName, 0, strlen($this->prefix)) == $this->prefix) && (substr($fileName, -strlen($this->suffix), strlen($this->suffix)) == $this->suffix)) {
474 // Add it to the list
475 //* NOISY-DEBUG: */ printf('[%s:%d] ADD: %s,currentEntry=%s' . PHP_EOL, __METHOD__, __LINE__, $fileName, $currentEntry);
476 $this->foundClasses[$fileName] = $currentEntry;
479 //* NOISY-DEBUG: */ printf('[%s:%d] NOT ADDED: %s,currentEntry=%s' . PHP_EOL, __METHOD__, __LINE__, $fileName, $currentEntry);
482 // Advance to next entry
483 //* NOISY-DEBUG: */ printf('[%s:%d] NEXT: %s' . PHP_EOL, __METHOD__, __LINE__, $fileName);
484 $iteratorInstance->next();
489 * Getter for total include counter
491 * @return $total Total loaded include files
493 public final function getTotal () {
498 * Getter for a printable list of included main/interfaces/exceptions
500 * @param $includeList A printable include list
502 public function getPrintableIncludeList () {
505 foreach ($this->loadedClasses as $classFile) {
506 $includeList .= basename($classFile) . '<br />' . PHP_EOL;
514 * Initializes our loader class
516 * @param $configInstance Configuration class instance
519 private function initLoader (FrameworkConfiguration $configInstance) {
520 // Set configuration instance
521 $this->configInstance = $configInstance;
523 // Construct the FQFN for the cache
524 if (!defined('DEVELOPER')) {
525 $this->listCacheFile = new SplFileInfo($this->configInstance->getConfigEntry('local_database_path') . 'list-' . $this->configInstance->getConfigEntry('detected_app_name') . '.cache');
526 $this->classCacheFile = new SplFileInfo($this->configInstance->getConfigEntry('local_database_path') . 'class-' . $this->configInstance->getConfigEntry('detected_app_name') . '.cache');
529 // Set suffix and prefix from configuration
530 $this->suffix = $configInstance->getConfigEntry('class_suffix');
531 $this->prefix = $configInstance->getConfigEntry('class_prefix');
534 self::$selfInstance = $this;
536 // Skip here if no dev-mode
537 if (defined('DEVELOPER')) {
541 // Is the cache there?
542 if (FrameworkBootstrap::isReadableFile($this->listCacheFile)) {
543 // Load and convert it
544 $this->foundClasses = json_decode(file_get_contents($this->listCacheFile->getPathname()));
546 // List has been restored from cache!
547 $this->listCached = true;
550 // Does the class cache exist?
551 if (FrameworkBootstrap::isReadableFile($this->classCacheFile)) {
553 FrameworkBootstrap::loadInclude($this->classCacheFile);
555 // Mark the class cache as loaded
556 $this->classesCached = true;
561 * Tries to find the given class in our list. This method ignores silently
562 * missing classes or interfaces. So if you use class_exists() this method
563 * does not interrupt your program.
565 * @param $className The class that shall be loaded
567 * @throws InvalidArgumentException If strict-checking is enabled and class name is not following naming-convention
569 private function loadClassFile (string $className) {
570 // The class name should contain at least 2 back-slashes, so split at them
571 //* NOISY-DEBUG: */ printf('[%s:%d] className=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $className);
572 $classNameParts = explode("\\", $className);
574 // At least 3 parts should be there
575 if ((self::$strictNamingConvention === true) && (count($classNameParts) < 5)) {
576 // Namespace scheme is: Project\Package[\SubPackage...]
577 throw new InvalidArgumentException(sprintf('Class name "%s" is not conform to naming-convention: Tld\Domain\Project\Package[\SubPackage...]\SomeFooBar', $className));
581 $shortClassName = array_pop($classNameParts);
583 // Create a name with prefix and suffix
584 $fileName = sprintf('%s%s%s', $this->prefix, $shortClassName, $this->suffix);
586 // Now look it up in our index
587 //* NOISY-DEBUG: */ printf('[%s:%d] ISSET: %s' . PHP_EOL, __METHOD__, __LINE__, $fileName);
588 if ((isset($this->foundClasses[$fileName])) && (!isset($this->loadedClasses[$this->foundClasses[$fileName]->getPathname()]))) {
589 // File is found and not loaded so load it only once
590 //* NOISY-DEBUG: */ printf('[%s:%d] LOAD: %s - START' . PHP_EOL, __METHOD__, __LINE__, $fileName);
591 FrameworkBootstrap::loadInclude($this->foundClasses[$fileName]);
592 //* NOISY-DEBUG: */ printf('[%s:%d] LOAD: %s - END' . PHP_EOL, __METHOD__, __LINE__, $fileName);
594 // Count this loaded class/interface/exception
597 // Mark this class as loaded for other purposes than loading it.
598 $this->loadedClasses[$this->foundClasses[$fileName]->getPathname()] = true;
600 // Remove it from classes list so it won't be found twice.
601 //* NOISY-DEBUG: */ printf('[%s:%d] UNSET: %s' . PHP_EOL, __METHOD__, __LINE__, $fileName);
602 unset($this->foundClasses[$fileName]);
604 // Developer mode excludes caching (better debugging)
605 if (!defined('DEVELOPER')) {
607 //* NOISY-DEBUG: */ printf('[%s:%d] classesCached=false' . PHP_EOL, __METHOD__, __LINE__);
608 $this->classesCached = false;
612 //* NOISY-DEBUG: */ printf('[%s:%d] 404: %s' . PHP_EOL, __METHOD__, __LINE__, $fileName);