PageRenderTime 58ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Loader.php

https://gitlab.com/blingbang2016/shop
PHP | 343 lines | 148 code | 23 blank | 172 comment | 40 complexity | 817eebfff3c19cf057a8bd92f0c9c679 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Loader
  17. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * Static methods for loading classes and files.
  23. *
  24. * @category Zend
  25. * @package Zend_Loader
  26. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Loader
  30. {
  31. /**
  32. * Loads a class from a PHP file. The filename must be formatted
  33. * as "$class.php".
  34. *
  35. * If $dirs is a string or an array, it will search the directories
  36. * in the order supplied, and attempt to load the first matching file.
  37. *
  38. * If $dirs is null, it will split the class name at underscores to
  39. * generate a path hierarchy (e.g., "Zend_Example_Class" will map
  40. * to "Zend/Example/Class.php").
  41. *
  42. * If the file was not found in the $dirs, or if no $dirs were specified,
  43. * it will attempt to load it from PHP's include_path.
  44. *
  45. * @param string $class - The full class name of a Zend component.
  46. * @param string|array $dirs - OPTIONAL Either a path or an array of paths
  47. * to search.
  48. * @return void
  49. * @throws Zend_Exception
  50. */
  51. public static function loadClass($class, $dirs = null)
  52. {
  53. if (class_exists($class, false) || interface_exists($class, false)) {
  54. return;
  55. }
  56. if ((null !== $dirs) && !is_string($dirs) && !is_array($dirs)) {
  57. #require_once 'Zend/Exception.php';
  58. throw new Zend_Exception('Directory argument must be a string or an array');
  59. }
  60. $file = self::standardiseFile($class);
  61. if (!empty($dirs)) {
  62. // use the autodiscovered path
  63. $dirPath = dirname($file);
  64. if (is_string($dirs)) {
  65. $dirs = explode(PATH_SEPARATOR, $dirs);
  66. }
  67. foreach ($dirs as $key => $dir) {
  68. if ($dir == '.') {
  69. $dirs[$key] = $dirPath;
  70. } else {
  71. $dir = rtrim($dir, '\\/');
  72. $dirs[$key] = $dir . DIRECTORY_SEPARATOR . $dirPath;
  73. }
  74. }
  75. $file = basename($file);
  76. self::loadFile($file, $dirs, true);
  77. } else {
  78. self::loadFile($file, null, true);
  79. }
  80. if (!class_exists($class, false) && !interface_exists($class, false)) {
  81. #require_once 'Zend/Exception.php';
  82. throw new Zend_Exception("File \"$file\" does not exist or class \"$class\" was not found in the file");
  83. }
  84. }
  85. /**
  86. * Loads a PHP file. This is a wrapper for PHP's include() function.
  87. *
  88. * $filename must be the complete filename, including any
  89. * extension such as ".php". Note that a security check is performed that
  90. * does not permit extended characters in the filename. This method is
  91. * intended for loading Zend Framework files.
  92. *
  93. * If $dirs is a string or an array, it will search the directories
  94. * in the order supplied, and attempt to load the first matching file.
  95. *
  96. * If the file was not found in the $dirs, or if no $dirs were specified,
  97. * it will attempt to load it from PHP's include_path.
  98. *
  99. * If $once is TRUE, it will use include_once() instead of include().
  100. *
  101. * @param string $filename
  102. * @param string|array $dirs - OPTIONAL either a path or array of paths
  103. * to search.
  104. * @param boolean $once
  105. * @return boolean
  106. * @throws Zend_Exception
  107. */
  108. public static function loadFile($filename, $dirs = null, $once = false)
  109. {
  110. self::_securityCheck($filename);
  111. /**
  112. * Search in provided directories, as well as include_path
  113. */
  114. $incPath = false;
  115. if (!empty($dirs) && (is_array($dirs) || is_string($dirs))) {
  116. if (is_array($dirs)) {
  117. $dirs = implode(PATH_SEPARATOR, $dirs);
  118. }
  119. $incPath = get_include_path();
  120. set_include_path($dirs . PATH_SEPARATOR . $incPath);
  121. }
  122. /**
  123. * Try finding for the plain filename in the include_path.
  124. */
  125. if ($once) {
  126. include_once $filename;
  127. } else {
  128. include $filename;
  129. }
  130. /**
  131. * If searching in directories, reset include_path
  132. */
  133. if ($incPath) {
  134. set_include_path($incPath);
  135. }
  136. return true;
  137. }
  138. /**
  139. * Returns TRUE if the $filename is readable, or FALSE otherwise.
  140. * This function uses the PHP include_path, where PHP's is_readable()
  141. * does not.
  142. *
  143. * Note from ZF-2900:
  144. * If you use custom error handler, please check whether return value
  145. * from error_reporting() is zero or not.
  146. * At mark of fopen() can not suppress warning if the handler is used.
  147. *
  148. * @param string $filename
  149. * @return boolean
  150. */
  151. public static function isReadable($filename)
  152. {
  153. if (is_readable($filename)) {
  154. // Return early if the filename is readable without needing the
  155. // include_path
  156. return true;
  157. }
  158. if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN'
  159. && preg_match('/^[a-z]:/i', $filename)
  160. ) {
  161. // If on windows, and path provided is clearly an absolute path,
  162. // return false immediately
  163. return false;
  164. }
  165. foreach (self::explodeIncludePath() as $path) {
  166. if ($path == '.') {
  167. if (is_readable($filename)) {
  168. return true;
  169. }
  170. continue;
  171. }
  172. $file = $path . '/' . $filename;
  173. if (is_readable($file)) {
  174. return true;
  175. }
  176. }
  177. return false;
  178. }
  179. /**
  180. * Explode an include path into an array
  181. *
  182. * If no path provided, uses current include_path. Works around issues that
  183. * occur when the path includes stream schemas.
  184. *
  185. * @param string|null $path
  186. * @return array
  187. */
  188. public static function explodeIncludePath($path = null)
  189. {
  190. if (null === $path) {
  191. $path = get_include_path();
  192. }
  193. if (PATH_SEPARATOR == ':') {
  194. // On *nix systems, include_paths which include paths with a stream
  195. // schema cannot be safely explode'd, so we have to be a bit more
  196. // intelligent in the approach.
  197. $paths = preg_split('#:(?!//)#', $path);
  198. } else {
  199. $paths = explode(PATH_SEPARATOR, $path);
  200. }
  201. return $paths;
  202. }
  203. /**
  204. * spl_autoload() suitable implementation for supporting class autoloading.
  205. *
  206. * Attach to spl_autoload() using the following:
  207. * <code>
  208. * spl_autoload_register(array('Zend_Loader', 'autoload'));
  209. * </code>
  210. *
  211. * @deprecated Since 1.8.0
  212. * @param string $class
  213. * @return string|false Class name on success; false on failure
  214. */
  215. public static function autoload($class)
  216. {
  217. trigger_error(__CLASS__ . '::' . __METHOD__ . ' is deprecated as of 1.8.0 and will be removed with 2.0.0; use Zend_Loader_Autoloader instead', E_USER_NOTICE);
  218. try {
  219. @self::loadClass($class);
  220. return $class;
  221. } catch (Exception $e) {
  222. return false;
  223. }
  224. }
  225. /**
  226. * Register {@link autoload()} with spl_autoload()
  227. *
  228. * @deprecated Since 1.8.0
  229. * @param string $class (optional)
  230. * @param boolean $enabled (optional)
  231. * @return void
  232. * @throws Zend_Exception if spl_autoload() is not found
  233. * or if the specified class does not have an autoload() method.
  234. */
  235. public static function registerAutoload($class = 'Zend_Loader', $enabled = true)
  236. {
  237. trigger_error(__CLASS__ . '::' . __METHOD__ . ' is deprecated as of 1.8.0 and will be removed with 2.0.0; use Zend_Loader_Autoloader instead', E_USER_NOTICE);
  238. #require_once 'Zend/Loader/Autoloader.php';
  239. $autoloader = Zend_Loader_Autoloader::getInstance();
  240. $autoloader->setFallbackAutoloader(true);
  241. if ('Zend_Loader' != $class) {
  242. self::loadClass($class);
  243. $methods = get_class_methods($class);
  244. if (!in_array('autoload', (array) $methods)) {
  245. #require_once 'Zend/Exception.php';
  246. throw new Zend_Exception("The class \"$class\" does not have an autoload() method");
  247. }
  248. $callback = array($class, 'autoload');
  249. if ($enabled) {
  250. $autoloader->pushAutoloader($callback);
  251. } else {
  252. $autoloader->removeAutoloader($callback);
  253. }
  254. }
  255. }
  256. /**
  257. * Ensure that filename does not contain exploits
  258. *
  259. * @param string $filename
  260. * @return void
  261. * @throws Zend_Exception
  262. */
  263. protected static function _securityCheck($filename)
  264. {
  265. /**
  266. * Security check
  267. */
  268. if (preg_match('/[^a-z0-9\\/\\\\_.:-]/i', $filename)) {
  269. #require_once 'Zend/Exception.php';
  270. throw new Zend_Exception('Security check: Illegal character in filename');
  271. }
  272. }
  273. /**
  274. * Attempt to include() the file.
  275. *
  276. * include() is not prefixed with the @ operator because if
  277. * the file is loaded and contains a parse error, execution
  278. * will halt silently and this is difficult to debug.
  279. *
  280. * Always set display_errors = Off on production servers!
  281. *
  282. * @param string $filespec
  283. * @param boolean $once
  284. * @return boolean
  285. * @deprecated Since 1.5.0; use loadFile() instead
  286. */
  287. protected static function _includeFile($filespec, $once = false)
  288. {
  289. if ($once) {
  290. return include_once $filespec;
  291. } else {
  292. return include $filespec ;
  293. }
  294. }
  295. /**
  296. * Standardise the filename.
  297. *
  298. * Convert the supplied filename into the namespace-aware standard,
  299. * based on the Framework Interop Group reference implementation:
  300. * http://groups.google.com/group/php-standards/web/psr-0-final-proposal
  301. *
  302. * The filename must be formatted as "$file.php".
  303. *
  304. * @param string $file - The file name to be loaded.
  305. * @return string
  306. */
  307. public static function standardiseFile($file)
  308. {
  309. $fileName = ltrim($file, '\\');
  310. $file = '';
  311. $namespace = '';
  312. if ($lastNsPos = strripos($fileName, '\\')) {
  313. $namespace = substr($fileName, 0, $lastNsPos);
  314. $fileName = substr($fileName, $lastNsPos + 1);
  315. $file = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
  316. }
  317. $file .= str_replace('_', DIRECTORY_SEPARATOR, $fileName) . '.php';
  318. return $file;
  319. }
  320. }