[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/include/Zend/ -> Loader.php (source)

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


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1