File indexing completed on 2024-11-17 05:19:46
0001 <?php 0002 0003 /** 0004 * Flooer Framework 0005 * 0006 * LICENSE: BSD License (2 Clause) 0007 * 0008 * @category Flooer 0009 * @package Flooer_Autoload 0010 * @author Akira Ohgaki <akiraohgaki@gmail.com> 0011 * @copyright Akira Ohgaki 0012 * @license https://opensource.org/licenses/BSD-2-Clause BSD License (2 Clause) 0013 * @link https://github.com/akiraohgaki/flooer 0014 */ 0015 0016 /** 0017 * Usage 0018 * 0019 * $autoload = new Flooer_Autoload(); 0020 * $autoload->register(); 0021 * $autoload->setExtensions('.php,.inc'); 0022 */ 0023 0024 /** 0025 * Class autoloader class 0026 * 0027 * @category Flooer 0028 * @package Flooer_Autoload 0029 * @author Akira Ohgaki <akiraohgaki@gmail.com> 0030 */ 0031 class Flooer_Autoload 0032 { 0033 0034 /** 0035 * Configuration options 0036 * 0037 * @var array 0038 */ 0039 protected $_config = array( 0040 'register' => false, 0041 'extensions' => null 0042 ); 0043 0044 /** 0045 * Constructor 0046 * 0047 * @param array $config 0048 * @return void 0049 */ 0050 public function __construct(array $config = null) 0051 { 0052 if ($config) { 0053 $this->_config = $config + $this->_config; 0054 } 0055 if ($this->_config['register']) { 0056 $this->register(); 0057 } 0058 if ($this->_config['extensions']) { 0059 $this->setExtensions($this->_config['extensions']); 0060 } 0061 } 0062 0063 /** 0064 * Autoload a class 0065 * 0066 * @param string $class 0067 * @return void 0068 */ 0069 public function autoload($class) 0070 { 0071 $extensions = explode( 0072 ',', 0073 str_replace(' ', '', spl_autoload_extensions()) 0074 ); 0075 $directories = explode( 0076 PATH_SEPARATOR, 0077 get_include_path() 0078 ); 0079 $pearStyleFile = null; 0080 if (strpos($class, '_')) { 0081 $pearStyleFile = str_replace('_', '/', $class); 0082 } 0083 $lowerCaseFile = strtolower($class); 0084 foreach ($extensions as $extension) { 0085 foreach ($directories as $directory) { 0086 if ($pearStyleFile 0087 && is_file($directory . '/' . $pearStyleFile . $extension) 0088 ) { 0089 include_once $directory . '/' . $pearStyleFile . $extension; 0090 break 2; 0091 } 0092 if (is_file($directory . '/' . $lowerCaseFile . $extension)) { 0093 include_once $directory . '/' . $lowerCaseFile . $extension; 0094 break 2; 0095 } 0096 if (is_file($directory . '/' . $class . $extension)) { 0097 include_once $directory . '/' . $class . $extension; 0098 break 2; 0099 } 0100 } 0101 } 0102 } 0103 0104 /** 0105 * Register the autoload function 0106 * 0107 * @return bool 0108 */ 0109 public function register() 0110 { 0111 return spl_autoload_register(array($this, 'autoload')); 0112 } 0113 0114 /** 0115 * Unregister the autoload function 0116 * 0117 * @return bool 0118 */ 0119 public function unregister() 0120 { 0121 return spl_autoload_unregister(array($this, 'autoload')); 0122 } 0123 0124 /** 0125 * Get a registered functions 0126 * 0127 * @return array|false 0128 */ 0129 public function getFunctions() 0130 { 0131 return spl_autoload_functions(); 0132 } 0133 0134 /** 0135 * Set a file extensions 0136 * 0137 * @param string $extensions Comma-separated list 0138 * @return void 0139 */ 0140 public function setExtensions($extensions) 0141 { 0142 spl_autoload_extensions($extensions); 0143 } 0144 0145 /** 0146 * Get a file extensions 0147 * 0148 * @return string 0149 */ 0150 public function getExtensions() 0151 { 0152 return spl_autoload_extensions(); 0153 } 0154 0155 }