Advertisement
Guest User

Untitled

a guest
Jul 26th, 2014
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.25 KB | None | 0 0
  1. /**
  2. * A class for lazy-loading other classes
  3. *
  4. * This class enables lazy-loading of php classes. The benefit of this is
  5. * three-fold. First, there is a memory benefit, since not all classes are
  6. * loaded until they are needed. Second, there is a time benefit, since not all
  7. * classes are loaded. Third, it produces cleaner code, since there is no need
  8. * to litter files with require_once() calls.
  9. *
  10. * @category Libraries
  11. * @package Libraries
  12. * @author Me
  13. */
  14. abstract class Loader
  15. {
  16.  
  17. /**
  18. * @var array An array of class to path mappings
  19. */
  20. protected static $classes = array();
  21.  
  22. /**
  23. * @var boolean Has the loader been initialized already
  24. */
  25. protected static $initialized = false;
  26.  
  27. /**
  28. * @var array An array of auto-search paths
  29. */
  30. protected static $namedPaths = array(
  31. 'exception',
  32. 'interface',
  33. 'iterator',
  34. );
  35.  
  36. /**
  37. * @var array An array of include paths to search
  38. */
  39. protected static $paths = array(
  40. PATH_LIBS,
  41. );
  42.  
  43. /**
  44. * Tell the auto-loader where to find an un-loaded class
  45. *
  46. * This can be used to "register" new classes that are unknown to the
  47. * system. It can also be used to "overload" a class (redefine it
  48. * elsewhere)
  49. *
  50. * @param string $class The class name to overload
  51. * @param string $path The path to the new class
  52. *
  53. * @throws InvalidArgumentException Upon an Invalid path submission
  54. * @return void
  55. */
  56. public static function _($class, $path)
  57. {
  58. $class = strtolower($class);
  59. if (!file_exists($path)) {
  60. throw new InvalidArgumentException('Invalid Path Specified');
  61. }
  62. self::$classes[$class] = $path;
  63. }
  64.  
  65. /**
  66. * Add a path to the include path list
  67. *
  68. * This adds a path to the list of paths to search for an included file.
  69. * This should not be used to overload classes, since the default include
  70. * directory will always be searched first. This can be used to extend
  71. * the search path to include new parts of the system
  72. *
  73. * @param string $path The path to add to the search list
  74. *
  75. * @throws InvalidArgumentException when an invalid path is specified
  76. * @return void
  77. */
  78. public static function addPath($path)
  79. {
  80. if (!is_dir($path)) {
  81. throw new InvalidArgumentException('Invalid Include Path Added');
  82. }
  83. $path = rtrim($path, DS);
  84. if (!in_array($path, self::$paths)) {
  85. self::$paths[] = $path;
  86. }
  87. }
  88.  
  89. /**
  90. * Add a path to the auto-search paths (for trailing extensions)
  91. *
  92. * The path should end with an 's'. Default files should not.
  93. *
  94. * @param string $path The name of the new auto-search path
  95. *
  96. * @return void
  97. */
  98. public static function addNamedPath($path)
  99. {
  100. $path = strtolower($path);
  101. if (substr($path, -1) == 's') {
  102. $path = substr($path, 0, -1);
  103. }
  104. if (!in_array($path, self::$namedPaths)) {
  105. self::$namedPaths[] = $path;
  106. }
  107. }
  108.  
  109. /**
  110. * Initialize and register the autoloader.
  111. *
  112. * This method will setup the autoloader. This should only be called once.
  113. *
  114. * @return void
  115. */
  116. public static function initialize()
  117. {
  118. if (!self::$initialized) {
  119. self::$initialized = true;
  120. spl_autoload_register(array('Loader', 'load'));
  121. }
  122. }
  123.  
  124. /**
  125. * The actual auto-loading function.
  126. *
  127. * This is automatically called by PHP whenever a class name is used that
  128. * doesn't exist yet. There should be no need to manually call this method.
  129. *
  130. * @param string $class The class name to load
  131. *
  132. * @return void
  133. */
  134. public static function load($class)
  135. {
  136. $className = strtolower($class);
  137. if (isset(self::$classes[$className])) {
  138. $file = self::$classes[$className];
  139. } else {
  140. $file = self::findFile($class);
  141. }
  142. if (file_exists($file)) {
  143. include_once $file;
  144. }
  145. }
  146.  
  147. /**
  148. * Find the file to include based upon its name
  149. *
  150. * This splits the class name by uppercase letter, and then rejoins them
  151. * to attain the file system path. So FooBarBaz will be turned into
  152. * foo/bar/baz. It then searches the include paths for that chain. If baz
  153. * is a directory, it searches that directory for a file called baz.php.
  154. * Otherwise, it looks for baz.php under the bar directory.
  155. *
  156. * @param string $class The name of the class to find
  157. *
  158. * @return string The path to the file defining that class
  159. */
  160. protected static function findFile($class)
  161. {
  162. $regex = '#([A-Z]{1}[a-z0-9_]+)#';
  163. $options = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE;
  164. $parts = preg_split($regex, $class, null, $options);
  165.  
  166. $subpath = '';
  167. $file = strtolower(end($parts));
  168. $test = strtolower(reset($parts));
  169. if ($test == 'is') {
  170. array_shift($parts);
  171. return self::findNamedFile($class, $parts, 'interface');
  172. }
  173. foreach ($parts as $part) {
  174. $subpath .= DS . strtolower($part);
  175. }
  176. foreach (self::$paths as $path) {
  177. $newpath = $path . $subpath;
  178. if (is_file($newpath . '.php')) {
  179. return $newpath . '.php';
  180. } elseif (is_file($newpath . DS . $file . '.php')) {
  181. return $newpath . DS . $file . '.php';
  182. }
  183. }
  184. if (in_array($file, self::$namedPaths)) {
  185. //Get rid of the trailing part
  186. array_pop($parts);
  187. return self::findNamedFile($class, $parts, $file);
  188. }
  189. return '';
  190. }
  191.  
  192. /**
  193. * Find a file for named directories (interfaces, exceptions, iterators, etc)
  194. *
  195. * @param string $class The class name of the exception to find
  196. * @param array $parts The parts of the class name pre-split
  197. * @param string $name The name of the named directory to search in
  198. *
  199. * @return string The found path, or '' if not found
  200. */
  201. protected static function findNamedFile($class, array $parts, $name)
  202. {
  203. if (empty($parts)) {
  204. return '';
  205. }
  206. $name = strtolower($name);
  207. //Add a trailing s, since individual files are not plural
  208. $filename = $name;
  209. $name .= 's';
  210. //Try the global path first
  211. $subpath = DS . $name . DS . strtolower(implode('', $parts)) . '.php';
  212. foreach (self::$paths as $path) {
  213. $newpath = $path . $subpath;
  214. if (is_file($newpath)) {
  215. return $newpath;
  216. }
  217. }
  218. //Try to build a full sub path for package specific named files
  219. $package = array_shift($parts);
  220. $subpath = DS . strtolower($package) . DS . $name . DS;
  221. if (!empty($parts)) {
  222. $subpath .= strtolower(implode('', $parts)) . '.php';
  223. } else {
  224. $subpath .= $filename . '.php';
  225. }
  226. foreach (self::$paths as $path) {
  227. $newpath = $path . $subpath;
  228. if (is_file($newpath)) {
  229. return $newpath;
  230. }
  231. }
  232. return '';
  233. }
  234. }
  235.  
  236. <Vendor Name>(<Namespace>)*<Class Name>
  237.  
  238. $classLoader = new SplClassLoader('DoctrineCommon', '/libs/doctrine');
  239. $classLoader->register();
  240.  
  241. $classLoader = new SplClassLoader('IrcmexellMyApplication', 'libs/internal');
  242. $classLoader->register();
  243.  
  244. pwd
  245. /var/www/project/app
  246. ls -lah PHPE*
  247. PHPExcel -> ../external/PHPExcel/PHPExcel
  248. PHPExcel.php -> ../external/PHPExcel/PHPExcel.php
  249.  
  250. set_include_path (PATH . 'app');
  251. require 'somepath/Autoloader.php';
  252. Autoloader::registerAutoload ();
  253.  
  254. class Autoloader
  255. {
  256.  
  257. public static function registerAutoload ()
  258. {
  259. spl_autoload_register (array (__CLASS__, 'autoload'));
  260. }
  261.  
  262. /**
  263. * @static
  264. * @param string $class
  265. * @return void
  266. */
  267. public static function autoload ($class)
  268. {
  269. require str_replace (array ('_', '\'), '/', $class) . '.php';
  270. }
  271.  
  272. public static function registerAutoload ()
  273. {
  274. spl_autoload_register (array (__CLASS__, 'autoload'));
  275. }
  276.  
  277. /**
  278. * @static
  279. * @param string $class
  280. * @return void
  281. */
  282. public static function autoload ($class)
  283. {
  284. require str_replace (array ('_', '\'), '/', $class) . '.php';
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement