svn: /pear2/Autoload/trunk/src/ Autoload.php

From: Date: Tue, 10 Nov 2009 12:29:33 +0000
Subject: svn: /pear2/Autoload/trunk/src/ Autoload.php
Groups: php.pear.cvs php.pear.core 
Request: Send a blank email to pear-core+get-9021@lists.php.net to get a copy of this message
saltybeagle Tue, 10 Nov 2009 12:29:33 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=290473 Log: Move autoloader into a class. Use a static function to sandbox setup so we don't need $__________var. Allow the Autoload.php file to be included more than once, just add the Pyrus controlled registry path to the include path each time. Changed paths: U pear2/Autoload/trunk/src/Autoload.php Modified: pear2/Autoload/trunk/src/Autoload.php =================================================================== --- pear2/Autoload/trunk/src/Autoload.php 2009-11-10 11:27:34 UTC (rev 290472) +++ pear2/Autoload/trunk/src/Autoload.php 2009-11-10 12:29:33 UTC (rev 290473) @@ -1,59 +1,120 @@ <?php namespace pear2; -function Autoload($class) -{ - if ((substr($class, 0, 6) !== 'PEAR2_') && (substr($class, 0, 6) !== 'pear2\\')) { - return false; - } - $fp = @fopen(str_replace('_', '/', $class) . '.php', 'r', true); - if ($fp) { - fclose($fp); - require str_replace('_', '/', $class) . '.php'; - if (!class_exists($class, false) && !interface_exists($class, false)) { - die(new Exception('Class ' . $class . ' was not present in ' . - str_replace('_', '/', $class) . '.php (include_path="' . get_include_path() . - '") [PEAR2_Autoload version 1.0]')); +if (!class_exists('\pear2\Autoload', false)) { + class Autoload + { + /** + * Whether the autoload class has been spl_autoload_register-ed + * + * @var bool + */ + protected static $registered = false; + + /** + * Array of PEAR2 autoload paths registered + * + * @var array + */ + protected static $paths = array(); + + /** + * Initialize the PEAR2 autoloader + * + * @param string $path Directory path to register + * + * @return void + */ + static function initialize($path) + { + self::register(); + self::addPath($path); } - return true; - } - $e = new \Exception('Class ' . $class . ' could not be loaded from ' . - str_replace('_', '/', $class) . '.php, file does not exist (include_path="' . get_include_path() . - '") [PEAR2_Autoload version 1.0]'); - $trace = $e->getTrace(); - if (isset($trace[2]) && isset($trace[2]['function']) && - in_array($trace[2]['function'], array('class_exists', 'interface_exists'))) { - return false; - } - if (isset($trace[1]) && isset($trace[1]['function']) && - in_array($trace[1]['function'], array('class_exists', 'interface_exists'))) { - return false; - } - die ((string) $e); -} -// set up __autoload -if (!($_____t = spl_autoload_functions()) || !in_array('pear2\Autoload', spl_autoload_functions())) { - spl_autoload_register('pear2\Autoload'); - if (function_exists('__autoload') && ($_____t === false)) { - // __autoload() was being used, but now would be ignored, add - // it to the autoload stack - spl_autoload_register('__autoload'); - } -} -unset($_____t); + /** + * Register the PEAR2 autoload class with spl_autoload_register + * + * @return void + */ + protected static function register() + { + if (!self::$registered) { + // set up __autoload + $autoload = spl_autoload_functions(); + spl_autoload_register('pear2\Autoload::load'); + if (function_exists('__autoload') && ($autoload === false)) { + // __autoload() was being used, but now would be ignored, add + // it to the autoload stack + spl_autoload_register('__autoload'); + } + } + self::$registered = true; + } -// set up include_path if it doesn't register our current location -$____paths = explode(PATH_SEPARATOR, get_include_path()); -$____found = false; -foreach ($____paths as $____path) { - if ($____path == dirname(dirname(__FILE__))) { - $____found = true; - break; + /** + * Add a path + * + * @param string $path The directory to add to the include path + * + * @return void + */ + protected static function addPath($path) + { + if (!in_array($path, self::$paths)) { + $paths = explode(PATH_SEPARATOR, get_include_path()); + if (!in_array($path, $paths)) { + set_include_path(get_include_path() . PATH_SEPARATOR . $path); + } + self::$paths[] = $path; + } + } + + /** + * Load a PEAR2 class + * + * @param string $class The class to load + * + * @return bool + */ + static function load($class) + { + if ((substr($class, 0, 6) !== 'PEAR2_') && (substr($class, 0, 6) !== 'pear2\\')) { + return false; + } + $fp = @fopen(str_replace('_', '/', $class) . '.php', 'r', true); + if ($fp) { + fclose($fp); + require str_replace('_', '/', $class) . '.php'; + if (!class_exists($class, false) && !interface_exists($class, false)) { + die(new Exception('Class ' . $class . ' was not present in ' . + str_replace('_', '/', $class) . '.php (include_path="' . get_include_path() . + '") [PEAR2_Autoload version 1.0]')); + } + return true; + } + $e = new \Exception('Class ' . $class . ' could not be loaded from ' . + str_replace('_', '/', $class) . '.php, file does not exist (include_path="' . get_include_path() . + '") [PEAR2_Autoload version 1.0]'); + $trace = $e->getTrace(); + if (isset($trace[2]) && isset($trace[2]['function']) && + in_array($trace[2]['function'], array('class_exists', 'interface_exists'))) { + return false; + } + if (isset($trace[1]) && isset($trace[1]['function']) && + in_array($trace[1]['function'], array('class_exists', 'interface_exists'))) { + return false; + } + die ((string) $e); + } + + /** + * return the array of paths PEAR2 autoload has registered + * + * @return array + */ + static function getPaths() + { + return self::$paths; + } } } -if (!$____found) { - set_include_path(get_include_path() . PATH_SEPARATOR . dirname(dirname(__FILE__))); -} -unset($____paths); -unset($____path); -unset($____found); +Autoload::initialize(dirname(__DIR__)); \ No newline at end of file

« previous php.pear.core (#9021) next »