Blame view

administrator/components/com_jmap/framework/loader.php 9.36 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
<?php
// namespace administrator\components\com_jmap\libraries\framework;
/**
 *
 * @package JMAP::administrator::components::com_jmap
 * @subpackage framework
 * @author Joomla! Extensions Store
 * @copyright (C) 2015 - Joomla! Extensions Store
 * @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html
 */
defined ( '_JEXEC' ) or die ( 'Restricted access' );

/**
 * Framework autoloader based on camel case and prefix namespacing
 *
 * @package JMAP::administrator::components::com_jmap
 * @subpackage framework
 * @since 2.0
 */
abstract class JMapLoader {
	/**
	 * Container for already imported library paths.
	 *
	 * @var array
	 */
	protected static $classes = array ();
	
	/**
	 * Container for already imported library paths.
	 *
	 * @var array
	 */
	protected static $imported = array ();
	
	/**
	 * Container for registered library class prefixes and path lookups.
	 *
	 * @var array
	 */
	protected static $prefixes = array ();
	
	/**
	 * Method to discover classes of a given type in a given path.
	 *
	 * @param string $classPrefix
	 *        	The class name prefix to use for discovery.
	 * @param string $parentPath
	 *        	Full path to the parent folder for the classes to discover.
	 * @param boolean $force
	 *        	True to overwrite the autoload path value for the class if it already exists.
	 * @param boolean $recurse
	 *        	Recurse through all child directories as well as the parent path.
	 *        	
	 * @return void
	 */
	public static function discover($classPrefix, $parentPath, $force = true, $recurse = false) {
		try {
			if ($recurse) {
				$iterator = new RecursiveIteratorIterator ( new RecursiveDirectoryIterator ( $parentPath ), RecursiveIteratorIterator::SELF_FIRST );
			} else {
				$iterator = new DirectoryIterator ( $parentPath );
			}
			
			foreach ( $iterator as $file ) {
				$fileName = $file->getFilename ();
				
				// Only load for php files.
				// Note: DirectoryIterator::getExtension only available PHP >= 5.3.6
				if ($file->isFile () && substr ( $fileName, strrpos ( $fileName, '.' ) + 1 ) == 'php') {
					// Get the class name and full path for each file.
					$class = strtolower ( $classPrefix . preg_replace ( '#\.php$#', '', $fileName ) );
					
					// Register the class with the autoloader if not already registered or the force flag is set.
					if (empty ( self::$classes [$class] ) || $force) {
						self::register ( $class, $file->getPath () . '/' . $fileName );
					}
				}
			}
		} catch ( UnexpectedValueException $e ) {
			// Exception will be thrown if the path is not a directory. Ignore it.
		}
	}
	
	/**
	 * Method to get the list of registered classes and their respective file paths for the autoloader.
	 *
	 * @return array The array of class => path values for the autoloader.
	 */
	public static function getClassList() {
		return self::$classes;
	}
	
	/**
	 * Loads a class from specified directories.
	 *
	 * @param string $key
	 *        	The class name to look for (dot notation).
	 * @param string $base
	 *        	Search this directory for the class.
	 *        	
	 * @return boolean True on success.
	 */
	public static function import($key, $base = null) {
		// Only import the library if not already attempted.
		if (! isset ( self::$imported [$key] )) {
			// Setup some variables.
			$success = false;
			$parts = explode ( '.', $key );
			$class = array_pop ( $parts );
			$base = (! empty ( $base )) ? $base : dirname ( __FILE__ );
			$path = str_replace ( '.', DIRECTORY_SEPARATOR, $key );
			
			// Handle special case for helper classes.
			if ($class == 'helper') {
				$class = ucfirst ( array_pop ( $parts ) ) . ucfirst ( $class );
			} 			// Standard class.
			else {
				$class = ucfirst ( $class );
			}
			
			// If we are importing a library from the Joomla namespace set the class to autoload.
			if (strpos ( $path, 'joomla' ) === 0) {
				// Since we are in the Joomla namespace prepend the classname with J.
				$class = 'J' . $class;
				
				// Only register the class for autoloading if the file exists.
				if (is_file ( $base . '/' . $path . '.php' )) {
					self::$classes [strtolower ( $class )] = $base . '/' . $path . '.php';
					$success = true;
				}
			} 			/*
			 * If we are not importing a library from the Joomla namespace directly include the file since we cannot assert the file/folder naming conventions.
			 */
			else {
				// If the file exists attempt to include it.
				if (is_file ( $base . '/' . $path . '.php' )) {
					$success = ( bool ) include_once $base . '/' . $path . '.php';
				}
			}
			
			// Add the import key to the memory cache container.
			self::$imported [$key] = $success;
		}
		
		return self::$imported [$key];
	}
	
	/**
	 * Load the file for a class.
	 *
	 * @param string $class
	 *        	The class to be loaded.
	 *        	
	 * @return boolean True on success
	 */
	public static function load($class) {
		// Sanitize class name.
		$class = strtolower ( $class );
		
		// If the class already exists do nothing.
		if (class_exists ( $class )) {
			return true;
		}
		
		// If the class is registered include the file.
		if (isset ( self::$classes [$class] )) {
			include_once self::$classes [$class];
			return true;
		}
		
		return false;
	}
	
	/**
	 * Directly register a class to the autoload list.
	 *
	 * @param string $class
	 *        	The class name to register.
	 * @param string $path
	 *        	Full path to the file that holds the class to register.
	 * @param boolean $force
	 *        	True to overwrite the autoload path value for the class if it already exists.
	 *        	
	 * @return void
	 */
	public static function register($class, $path, $force = true) {
		// Sanitize class name.
		$class = strtolower ( $class );
		
		// Only attempt to register the class if the name and file exist.
		if (! empty ( $class ) && is_file ( $path )) {
			// Register the class with the autoloader if not already registered or the force flag is set.
			if (empty ( self::$classes [$class] ) || $force) {
				self::$classes [$class] = $path;
			}
		}
	}
	
	/**
	 * Register a class prefix with lookup path.
	 * This will allow developers to register library
	 * packages with different class prefixes to the system autoloader. More than one lookup path
	 * may be registered for the same class prefix, but if this method is called with the reset flag
	 * set to true then any registered lookups for the given prefix will be overwritten with the current
	 * lookup path.
	 *
	 * @param string $prefix
	 *        	The class prefix to register.
	 * @param string $path
	 *        	Absolute file path to the library root where classes with the given prefix can be found.
	 * @param boolean $reset
	 *        	True to reset the prefix with only the given lookup path.
	 *        	
	 * @return void
	 */
	public static function registerPrefix($prefix, $path, $reset = false) {
		// Verify the library path exists.
		if (! file_exists ( $path )) {
			throw new RuntimeException ( 'Library path ' . $path . ' cannot be found.', 500 );
		}
		
		// If the prefix is not yet registered or we have an explicit reset flag then set set the path.
		if (! isset ( self::$prefixes [$prefix] ) || $reset) {
			self::$prefixes [$prefix] = array (
					$path 
			);
		} 		// Otherwise we want to simply add the path to the prefix.
		else {
			self::$prefixes [$prefix] [] = $path;
		}
	}
	
	/**
	 * Method to setup the autoloaders for the Joomla Platform.
	 * Since the SPL autoloaders are
	 * called in a queue we will add our explicit, class-registration based loader first, then
	 * fall back on the autoloader based on conventions. This will allow people to register a
	 * class in a specific location and override platform libraries as was previously possible.
	 *
	 * @return void
	 */
	public static function setup() {
		// Register the autoloader functions.
		spl_autoload_register ( array (
				'JMapLoader',
				'load' 
		) );
		spl_autoload_register ( array (
				'JMapLoader',
				'_autoload' 
		) );
	}
	
	/**
	 * Autoload a class based on name.
	 *
	 * @param string $class
	 *        	The class to be loaded.
	 *        	
	 * @return void
	 */
	private static function _autoload($class) {
		foreach ( self::$prefixes as $prefix => $lookup ) {
			if (strpos ( $class, $prefix ) === 0) {
				return self::_load ( substr ( $class, strlen ( $prefix ) ), $lookup );
			}
		}
	}
	
	/**
	 * Load a class based on name and lookup array.
	 *
	 * @param string $class
	 *        	The class to be loaded (wihtout prefix).
	 * @param array $lookup
	 *        	The array of base paths to use for finding the class file.
	 *        	
	 * @return void
	 */
	private static function _load($class, $lookup) {
		// Split the class name into parts separated by camelCase.
		$parts = preg_split ( '/(?<=[a-z0-9])(?=[A-Z])/x', $class );
		
		// If there is only one part we want to duplicate that part for generating the path.
		$parts = (count ( $parts ) === 1) ? array (
				$parts [0],
				$parts [0] 
		) : $parts;
		
		foreach ( $lookup as $base ) {
			// Generate the path based on the class name parts.
			$path = $base . '/' . implode ( '/', array_map ( 'strtolower', $parts ) ) . '.php';
			
			// Load the file if it exists.
			if (file_exists ( $path )) {
				return include $path;
			} else {
				// Try if an exact folder/name match
				// Generate the path based on the class name parts.
				$parts[] = $parts[count($parts) - 1];
				$path = $base . '/' . implode ( '/', array_map ( 'strtolower', $parts ) ) . '.php';
				if (file_exists ( $path )) {
					return include $path;
				}
			}
		}
	}
}