3 namespace CoreFramework\Loader;
5 // Import framework stuff
6 use CoreFramework\Configuration\FrameworkConfiguration;
7 use CoreFramework\EntryPoint\ApplicationEntryPoint;
8 use CoreFramework\Object\BaseFrameworkSystem;
11 use \InvalidArgumentException;
12 use \RecursiveDirectoryIterator;
13 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 - 2017 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 = array();
68 * List of loaded classes
70 private $loadedClasses = array();
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 = array();
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 * Filename for the list cache
106 private $listCacheFQFN = '';
109 * Cache for class content
111 private $classCacheFQFN = '';
114 * Counter for loaded include files
119 * By default the class loader is strict with naming-convention check
121 private static $strictNamingConventionCheck = TRUE;
124 * Framework/application paths for classes, etc.
126 private static $frameworkPaths = array(
127 'exceptions', // Exceptions
128 'interfaces', // Interfaces
129 'classes', // Classes
130 'middleware' // The middleware
134 * Registered paths where test classes can be found. These are all relative
137 private static $testPaths = array();
140 * The protected constructor. Please use the factory method below, or use
141 * getSelfInstance() for singleton
145 protected function __construct () {
146 // This is empty for now
150 * The destructor makes it sure all caches got flushed
154 public function __destruct () {
155 // Skip here if dev-mode
156 if (defined('DEVELOPER')) {
160 // Skip here if already cached
161 if ($this->listCached === FALSE) {
162 // Writes the cache file of our list away
163 $cacheContent = json_encode($this->foundClasses);
164 file_put_contents($this->listCacheFQFN, $cacheContent);
167 // Skip here if already cached
168 if ($this->classesCached === FALSE) {
169 // Generate a full-cache of all classes
171 foreach (array_keys($this->loadedClasses) as $fqfn) {
173 $cacheContent .= file_get_contents($fqfn);
177 file_put_contents($this->classCacheFQFN, $cacheContent);
182 * Creates an instance of this class loader for given configuration instance
184 * @param $configInstance Configuration class instance
187 public static final function createClassLoader (FrameworkConfiguration $configInstance) {
188 // Get a new instance
189 $loaderInstance = new ClassLoader();
192 $loaderInstance->initLoader($configInstance);
194 // Return the prepared instance
195 return $loaderInstance;
199 * Scans for all framework classes, exceptions and interfaces.
203 public static function scanFrameworkClasses () {
205 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
207 // Cache loader instance
208 $loaderInstance = self::getSelfInstance();
210 // Get config instance
211 $cfg = FrameworkConfiguration::getSelfInstance();
214 foreach (self::$frameworkPaths as $shortPath) {
216 //* NOISY-DEBUG: */ printf('[%s:%d]: shortPath=%s' . PHP_EOL, __METHOD__, __LINE__, $shortPath);
218 // Generate full path from it
219 $pathName = realpath(sprintf(
220 '%s/framework/main/%s/',
221 $cfg->getConfigEntry('base_path'),
226 //* NOISY-DEBUG: */ printf('[%s:%d]: pathName=%s' . PHP_EOL, __METHOD__, __LINE__, $pathName);
228 // Is it not FALSE and accessible?
229 if (is_bool($pathName)) {
232 } elseif (!is_readable($pathName)) {
233 // @TODO Throw exception instead of break
237 // Try to load the framework classes
238 $loaderInstance->scanClassPath($pathName);
242 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
246 * Scans for application's classes, etc.
250 public static function scanApplicationClasses () {
252 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
254 // Get config instance
255 $cfg = FrameworkConfiguration::getSelfInstance();
257 // Load all classes for the application
258 foreach (self::$frameworkPaths as $shortPath) {
260 //* NOISY-DEBUG: */ printf('[%s:%d]: shortPath=%s' . PHP_EOL, __METHOD__, __LINE__, $shortPath);
263 $pathName = realpath(sprintf(
265 $cfg->getConfigEntry('application_path'),
266 $cfg->getConfigEntry('app_name'),
271 //* NOISY-DEBUG: */ printf('[%s:%d]: pathName[%s]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype($pathName), $pathName);
273 // Is the path readable?
274 if (is_dir($pathName)) {
275 // Try to load the application classes
276 ClassLoader::getSelfInstance()->scanClassPath($pathName);
281 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
285 * Scans for test classes, etc.
289 public static function scanTestsClasses () {
291 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
293 // Get config instance
294 $cfg = FrameworkConfiguration::getSelfInstance();
296 // Load all classes for the application
297 foreach (self::$testPaths as $shortPath) {
299 //* NOISY-DEBUG: */ printf('[%s:%d]: shortPath=%s' . PHP_EOL, __METHOD__, __LINE__, $shortPath);
302 $pathName = realpath(sprintf(
304 $cfg->getConfigEntry('base_path'),
309 //* NOISY-DEBUG: */ printf('[%s:%d]: pathName[%s]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype($pathName), $pathName);
311 // Is the path readable?
312 if (is_dir($pathName)) {
313 // Try to load the application classes
314 ClassLoader::getSelfInstance()->scanClassPath($pathName);
319 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
323 * Enables or disables strict naming-convention tests on class loading
325 * @param $strictNamingConventionCheck Whether to strictly check naming-convention
328 public static function enableStrictNamingConventionCheck ($strictNamingConventionCheck = TRUE) {
329 self::$strictNamingConventionCheck = $strictNamingConventionCheck;
333 * Registeres given relative path where test classes reside. For regular
334 * framework uses, they should not be loaded (and used).
336 * @param $relativePath Relative path to test classes
339 public static function registerTestsPath ($relativePath) {
341 self::$testPaths[$relativePath] = $relativePath;
347 * @param $className Name of the class to load
350 public static function autoLoad ($className) {
351 // Try to include this class
352 self::getSelfInstance()->loadClassFile($className);
356 * Singleton getter for an instance of this class
358 * @return $selfInstance A singleton instance of this class
360 public static final function getSelfInstance () {
361 // Is the instance there?
362 if (is_null(self::$selfInstance)) {
364 self::$selfInstance = ClassLoader::createClassLoader(FrameworkConfiguration::getSelfInstance());
367 // Return the instance
368 return self::$selfInstance;
372 * Scans recursively a local path for class files which must have a prefix and a suffix as given by $this->suffix and $this->prefix
374 * @param $basePath The relative base path to 'base_path' constant for all classes
375 * @param $ignoreList An optional list (array forced) of directory and file names which shall be ignored
378 public function scanClassPath ($basePath, array $ignoreList = array() ) {
379 // Is a list has been restored from cache, don't read it again
380 if ($this->listCached === TRUE) {
385 // Keep it in class for later usage
386 $this->ignoreList = $ignoreList;
389 * Ignore .htaccess by default as it is for protection of directories
392 array_push($ignoreList, '.htaccess');
395 * Set base directory which holds all our classes, an absolute path
396 * should be used here so is_dir(), is_file() and so on will always
397 * find the correct files and dirs.
399 $basePath2 = realpath($basePath);
401 // If the basePath is FALSE it is invalid
402 if ($basePath2 === FALSE) {
403 /* @TODO: Do not exit here. */
404 exit(__METHOD__ . ': Cannot read ' . $basePath . ' !' . PHP_EOL);
407 $basePath = $basePath2;
410 // Get a new iterator
411 //* NOISY-DEBUG: */ printf('[%s:%d] basePath=%s' . PHP_EOL, __METHOD__, __LINE__, $basePath);
412 $iteratorInstance = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($basePath), RecursiveIteratorIterator::CHILD_FIRST);
415 while ($iteratorInstance->valid()) {
417 $currentEntry = $iteratorInstance->current();
419 // Get filename from iterator
420 $fileName = $currentEntry->getFileName();
422 // Get the "FQFN" (path and file name)
423 $fqfn = $currentEntry->getRealPath();
425 // Current entry must be a file, not smaller than 100 bytes and not on ignore list
426 if ((!$currentEntry->isFile()) || (in_array($fileName, $this->ignoreList)) || (filesize($fqfn) < 100)) {
427 // Advance to next entry
428 $iteratorInstance->next();
430 // Skip non-file entries
431 //* NOISY-DEBUG: */ printf('[%s:%d] SKIP: %s' . PHP_EOL, __METHOD__, __LINE__, $fileName);
435 // Is this file wanted?
436 //* NOISY-DEBUG: */ printf('[%s:%d] FOUND: %s' . PHP_EOL, __METHOD__, __LINE__, $fileName);
437 if ((substr($fileName, 0, strlen($this->prefix)) == $this->prefix) && (substr($fileName, -strlen($this->suffix), strlen($this->suffix)) == $this->suffix)) {
438 // Add it to the list
439 //* NOISY-DEBUG: */ printf('[%s:%d] ADD: %s,fqfn=%s' . PHP_EOL, __METHOD__, __LINE__, $fileName, $fqfn);
440 $this->foundClasses[$fileName] = $fqfn;
443 //* NOISY-DEBUG: */ printf('[%s:%d] NOT ADDED: %s,fqfn=%s' . PHP_EOL, __METHOD__, __LINE__, $fileName, $fqfn);
446 // Advance to next entry
447 //* NOISY-DEBUG: */ printf('[%s:%d] NEXT: %s' . PHP_EOL, __METHOD__, __LINE__, $fileName);
448 $iteratorInstance->next();
453 * Load extra config files
457 public function loadExtraConfigs () {
459 $oldPrefix = $this->prefix;
461 // Set new prefix (temporary!)
462 $this->prefix = 'config-';
464 // Set base directory
465 $basePath = $this->configInstance->getConfigEntry('base_path') . 'framework/config/';
467 // Load all classes from the config directory
468 $this->scanClassPath($basePath);
470 // Include these extra configs now
471 $this->includeExtraConfigs();
473 // Set back the old prefix
474 $this->prefix = $oldPrefix;
478 * Initializes our loader class
480 * @param $configInstance Configuration class instance
483 private function initLoader (FrameworkConfiguration $configInstance) {
484 // Set configuration instance
485 $this->configInstance = $configInstance;
487 // Construct the FQFN for the cache
488 if (!defined('DEVELOPER')) {
489 $this->listCacheFQFN = $this->configInstance->getConfigEntry('local_db_path') . 'list-' . $this->configInstance->getConfigEntry('app_name') . '.cache';
490 $this->classCacheFQFN = $this->configInstance->getConfigEntry('local_db_path') . 'class-' . $this->configInstance->getConfigEntry('app_name') . '.cache';
493 // Set suffix and prefix from configuration
494 $this->suffix = $configInstance->getConfigEntry('class_suffix');
495 $this->prefix = $configInstance->getConfigEntry('class_prefix');
498 self::$selfInstance = $this;
500 // Skip here if no dev-mode
501 if (defined('DEVELOPER')) {
505 // IS the cache there?
506 if (BaseFrameworkSystem::isReadableFile($this->listCacheFQFN)) {
508 $cacheContent = file_get_contents($this->listCacheFQFN);
511 $this->foundClasses = json_decode($cacheContent);
513 // List has been restored from cache!
514 $this->listCached = TRUE;
517 // Does the class cache exist?
518 if (BaseFrameworkSystem::isReadableFile($this->listCacheFQFN)) {
520 require($this->classCacheFQFN);
522 // Mark the class cache as loaded
523 $this->classesCached = TRUE;
528 * Tries to find the given class in our list. This method ignores silently
529 * missing classes or interfaces. So if you use class_exists() this method
530 * does not interrupt your program.
532 * @param $className The class that shall be loaded
534 * @throws InvalidArgumentException If strict-checking is enabled and class name is not following naming convention
536 private function loadClassFile ($className) {
538 //* NOISY-DEBUG: */ printf('[%s:%d] className=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $className);
540 // The class name should contain at least 2 back-slashes, so split at them
541 $classNameParts = explode("\\", $className);
543 // At least 3 parts should be there
544 if ((self::$strictNamingConventionCheck === TRUE) && (count($classNameParts) < 3)) {
545 // Namespace scheme is: Project\Package[\SubPackage...]
546 throw new InvalidArgumentException(sprintf('Class name "%s" is not conform to naming-convention: Project\Package[\SubPackage...]\SomeFooBar', $className));
550 $shortClassName = array_pop($classNameParts);
552 // Create a name with prefix and suffix
553 $fileName = $this->prefix . $shortClassName . $this->suffix;
555 // Now look it up in our index
556 //* NOISY-DEBUG: */ printf('[%s:%d] ISSET: %s' . PHP_EOL, __METHOD__, __LINE__, $fileName);
557 if ((isset($this->foundClasses[$fileName])) && (!isset($this->loadedClasses[$this->foundClasses[$fileName]]))) {
558 // File is found and not loaded so load it only once
559 //* NOISY-DEBUG: */ printf('[%s:%d] LOAD: %s - START' . PHP_EOL, __METHOD__, __LINE__, $fileName);
560 require($this->foundClasses[$fileName]);
561 //* NOISY-DEBUG: */ printf('[%s:%d] LOAD: %s - END' . PHP_EOL, __METHOD__, __LINE__, $fileName);
563 // Count this loaded class/interface/exception
566 // Mark this class as loaded for other purposes than loading it.
567 $this->loadedClasses[$this->foundClasses[$fileName]] = TRUE;
569 // Remove it from classes list so it won't be found twice.
570 //* NOISY-DEBUG: */ printf('[%s:%d] UNSET: %s' . PHP_EOL, __METHOD__, __LINE__, $fileName);
571 unset($this->foundClasses[$fileName]);
573 // Developer mode excludes caching (better debugging)
574 if (!defined('DEVELOPER')) {
576 //* NOISY-DEBUG: */ printf('[%s:%d] classesCached=FALSE' . PHP_EOL, __METHOD__, __LINE__);
577 $this->classesCached = FALSE;
581 //* NOISY-DEBUG: */ printf('[%s:%d] 404: %s' . PHP_EOL, __METHOD__, __LINE__, $fileName);
586 * Includes all extra config files
590 private function includeExtraConfigs () {
591 // Run through all class names (should not be much)
592 foreach ($this->foundClasses as $fileName => $fqfn) {
594 if (substr($fileName, 0, strlen($this->prefix)) == $this->prefix) {
596 //* NOISY-DEBUG: */ printf('[%s:%d] LOAD: %s - START' . PHP_EOL, __METHOD__, __LINE__, $fileName);
598 //* NOISY-DEBUG: */ printf('[%s:%d] LOAD: %s - END' . PHP_EOL, __METHOD__, __LINE__, $fileName);
600 // Remove it from the list
601 //* NOISY-DEBUG: */ printf('[%s:%d] UNSET: %s' . PHP_EOL, __METHOD__, __LINE__, $fileName);
602 unset($this->foundClasses[$fileName]);
608 * Getter for total include counter
610 * @return $total Total loaded include files
612 public final function getTotal () {
617 * Getter for a printable list of included main/interfaces/exceptions
619 * @param $includeList A printable include list
621 public function getPrintableIncludeList () {
624 foreach ($this->loadedClasses as $classFile) {
625 $includeList .= basename($classFile) . '<br />' . PHP_EOL;