web/lib/Zend/Loader.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    12  * obtain it through the world-wide-web, please send an email
    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.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Loader
    16  * @package    Zend_Loader
    17  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    17  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @version    $Id: Loader.php 22019 2010-04-27 16:33:31Z matthew $
    19  * @version    $Id: Loader.php 24593 2012-01-05 20:35:02Z matthew $
    20  */
    20  */
    21 
    21 
    22 /**
    22 /**
    23  * Static methods for loading classes and files.
    23  * Static methods for loading classes and files.
    24  *
    24  *
    25  * @category   Zend
    25  * @category   Zend
    26  * @package    Zend_Loader
    26  * @package    Zend_Loader
    27  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    27  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    28  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    28  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    29  */
    29  */
    30 class Zend_Loader
    30 class Zend_Loader
    31 {
    31 {
    32     /**
    32     /**
    58         if ((null !== $dirs) && !is_string($dirs) && !is_array($dirs)) {
    58         if ((null !== $dirs) && !is_string($dirs) && !is_array($dirs)) {
    59             require_once 'Zend/Exception.php';
    59             require_once 'Zend/Exception.php';
    60             throw new Zend_Exception('Directory argument must be a string or an array');
    60             throw new Zend_Exception('Directory argument must be a string or an array');
    61         }
    61         }
    62 
    62 
    63         // Autodiscover the path from the class name
    63         $file = self::standardiseFile($class);
    64         // Implementation is PHP namespace-aware, and based on 
       
    65         // Framework Interop Group reference implementation:
       
    66         // http://groups.google.com/group/php-standards/web/psr-0-final-proposal
       
    67         $className = ltrim($class, '\\');
       
    68         $file      = '';
       
    69         $namespace = '';
       
    70         if ($lastNsPos = strripos($className, '\\')) {
       
    71             $namespace = substr($className, 0, $lastNsPos);
       
    72             $className = substr($className, $lastNsPos + 1);
       
    73             $file      = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
       
    74         }
       
    75         $file .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
       
    76 
    64 
    77         if (!empty($dirs)) {
    65         if (!empty($dirs)) {
    78             // use the autodiscovered path
    66             // use the autodiscovered path
    79             $dirPath = dirname($file);
    67             $dirPath = dirname($file);
    80             if (is_string($dirs)) {
    68             if (is_string($dirs)) {
   172      * @return boolean
   160      * @return boolean
   173      */
   161      */
   174     public static function isReadable($filename)
   162     public static function isReadable($filename)
   175     {
   163     {
   176         if (is_readable($filename)) {
   164         if (is_readable($filename)) {
   177             // Return early if the filename is readable without needing the 
   165             // Return early if the filename is readable without needing the
   178             // include_path
   166             // include_path
   179             return true;
   167             return true;
   180         }
   168         }
   181 
   169 
   182         if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN'
   170         if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN'
   183             && preg_match('/^[a-z]:/i', $filename)
   171             && preg_match('/^[a-z]:/i', $filename)
   184         ) {
   172         ) {
   185             // If on windows, and path provided is clearly an absolute path, 
   173             // If on windows, and path provided is clearly an absolute path,
   186             // return false immediately
   174             // return false immediately
   187             return false;
   175             return false;
   188         }
   176         }
   189 
   177 
   190         foreach (self::explodeIncludePath() as $path) {
   178         foreach (self::explodeIncludePath() as $path) {
   205     /**
   193     /**
   206      * Explode an include path into an array
   194      * Explode an include path into an array
   207      *
   195      *
   208      * If no path provided, uses current include_path. Works around issues that
   196      * If no path provided, uses current include_path. Works around issues that
   209      * occur when the path includes stream schemas.
   197      * occur when the path includes stream schemas.
   210      * 
   198      *
   211      * @param  string|null $path 
   199      * @param  string|null $path
   212      * @return array
   200      * @return array
   213      */
   201      */
   214     public static function explodeIncludePath($path = null)
   202     public static function explodeIncludePath($path = null)
   215     {
   203     {
   216         if (null === $path) {
   204         if (null === $path) {
   217             $path = get_include_path();
   205             $path = get_include_path();
   218         }
   206         }
   219 
   207 
   220         if (PATH_SEPARATOR == ':') {
   208         if (PATH_SEPARATOR == ':') {
   221             // On *nix systems, include_paths which include paths with a stream 
   209             // On *nix systems, include_paths which include paths with a stream
   222             // schema cannot be safely explode'd, so we have to be a bit more
   210             // schema cannot be safely explode'd, so we have to be a bit more
   223             // intelligent in the approach.
   211             // intelligent in the approach.
   224             $paths = preg_split('#:(?!//)#', $path);
   212             $paths = preg_split('#:(?!//)#', $path);
   225         } else {
   213         } else {
   226             $paths = explode(PATH_SEPARATOR, $path);
   214             $paths = explode(PATH_SEPARATOR, $path);
   324             return include_once $filespec;
   312             return include_once $filespec;
   325         } else {
   313         } else {
   326             return include $filespec ;
   314             return include $filespec ;
   327         }
   315         }
   328     }
   316     }
       
   317 
       
   318     /**
       
   319      * Standardise the filename.
       
   320      *
       
   321      * Convert the supplied filename into the namespace-aware standard,
       
   322      * based on the Framework Interop Group reference implementation:
       
   323      * http://groups.google.com/group/php-standards/web/psr-0-final-proposal
       
   324      *
       
   325      * The filename must be formatted as "$file.php".
       
   326      *
       
   327      * @param string $file - The file name to be loaded.
       
   328      * @return string
       
   329      */
       
   330     public static function standardiseFile($file)
       
   331     {
       
   332         $fileName = ltrim($file, '\\');
       
   333         $file      = '';
       
   334         $namespace = '';
       
   335         if ($lastNsPos = strripos($fileName, '\\')) {
       
   336             $namespace = substr($fileName, 0, $lastNsPos);
       
   337             $fileName = substr($fileName, $lastNsPos + 1);
       
   338             $file      = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
       
   339         }
       
   340         $file .= str_replace('_', DIRECTORY_SEPARATOR, $fileName) . '.php';
       
   341         return $file;    
       
   342     }
   329 }
   343 }