[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 3 /* 4 * This file is part of the symfony package. 5 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> 6 * 7 * For the full copyright and license information, please view the LICENSE 8 * file that was distributed with this source code. 9 */ 10 11 /** 12 * core symfony class. 13 * 14 * @package symfony 15 * @author Fabien Potencier <fabien.potencier@symfony-project.com> 16 * @version SVN: $Id: sfCore.class.php 3128 2007-01-03 08:01:46Z fabien $ 17 */ 18 class sfCore 19 { 20 static protected 21 $autoloadCallables = array(), 22 $classes = array(); 23 24 static public function bootstrap($sf_symfony_lib_dir, $sf_symfony_data_dir) 25 { 26 require_once($sf_symfony_lib_dir.'/util/sfToolkit.class.php'); 27 require_once($sf_symfony_lib_dir.'/config/sfConfig.class.php'); 28 29 sfCore::initConfiguration($sf_symfony_lib_dir, $sf_symfony_data_dir); 30 if (sfConfig::get('sf_check_lock')) 31 { 32 sfCore::checkLock(); 33 } 34 if (sfConfig::get('sf_check_symfony_version')) 35 { 36 sfCore::checkSymfonyVersion(); 37 } 38 sfCore::initIncludePath(); 39 40 sfCore::callBootstrap(); 41 } 42 43 static public function callBootstrap() 44 { 45 $bootstrap = sfConfig::get('sf_config_cache_dir').'/config_bootstrap_compile.yml.php'; 46 if (is_readable($bootstrap)) 47 { 48 sfConfig::set('sf_in_bootstrap', true); 49 require($bootstrap); 50 } 51 else 52 { 53 require(sfConfig::get('sf_symfony_lib_dir').'/symfony.php'); 54 } 55 } 56 57 static public function initConfiguration($sf_symfony_lib_dir, $sf_symfony_data_dir, $test = false) 58 { 59 // start timer 60 if (SF_DEBUG) 61 { 62 sfConfig::set('sf_timer_start', microtime(true)); 63 } 64 65 // main configuration 66 sfConfig::add(array( 67 'sf_root_dir' => SF_ROOT_DIR, 68 'sf_app' => SF_APP, 69 'sf_environment' => SF_ENVIRONMENT, 70 'sf_debug' => SF_DEBUG, 71 'sf_symfony_lib_dir' => $sf_symfony_lib_dir, 72 'sf_symfony_data_dir' => $sf_symfony_data_dir, 73 'sf_test' => $test, 74 )); 75 76 // directory layout 77 include($sf_symfony_data_dir.'/config/constants.php'); 78 } 79 80 static public function initIncludePath() 81 { 82 set_include_path( 83 sfConfig::get('sf_lib_dir').PATH_SEPARATOR. 84 sfConfig::get('sf_root_dir').PATH_SEPARATOR. 85 sfConfig::get('sf_app_lib_dir').PATH_SEPARATOR. 86 sfConfig::get('sf_symfony_lib_dir').DIRECTORY_SEPARATOR.'vendor'.PATH_SEPARATOR. 87 get_include_path() 88 ); 89 } 90 91 // check to see if we're not in a cache cleaning process 92 static public function checkLock() 93 { 94 if (sfToolkit::hasLockFile(SF_ROOT_DIR.DIRECTORY_SEPARATOR.SF_APP.'_'.SF_ENVIRONMENT.'.lck', 5)) 95 { 96 // application is not available 97 $file = sfConfig::get('sf_web_dir').'/errors/unavailable.php'; 98 include(is_readable($file) ? $file : sfConfig::get('sf_symfony_data_dir').'/web/errors/unavailable.php'); 99 100 die(1); 101 } 102 } 103 104 static public function checkSymfonyVersion() 105 { 106 // recent symfony update? 107 $last_version = @file_get_contents(sfConfig::get('sf_config_cache_dir').'/VERSION'); 108 $current_version = trim(file_get_contents(sfConfig::get('sf_symfony_lib_dir').'/VERSION')); 109 if ($last_version != $current_version) 110 { 111 // clear cache 112 sfToolkit::clearDirectory(sfConfig::get('sf_config_cache_dir')); 113 } 114 } 115 116 static public function getClassPath($class) 117 { 118 return isset(self::$classes[$class]) ? self::$classes[$class] : null; 119 } 120 121 static public function addAutoloadCallable($callable) 122 { 123 self::$autoloadCallables[] = $callable; 124 125 if (function_exists('spl_autoload_register')) 126 { 127 spl_autoload_register($callable); 128 } 129 } 130 131 static public function getAutoloadCallables() 132 { 133 return self::$autoloadCallables; 134 } 135 136 /** 137 * Handles autoloading of classes that have been specified in autoload.yml. 138 * 139 * @param string A class name. 140 * 141 * @return boolean Returns true if the class has been loaded 142 */ 143 static public function splAutoload($class) 144 { 145 // load the list of autoload classes 146 if (!self::$classes) 147 { 148 $file = sfConfigCache::getInstance()->checkConfig(sfConfig::get('sf_app_config_dir_name').'/autoload.yml'); 149 self::$classes = include($file); 150 } 151 152 // class already exists 153 if (class_exists($class, false)) 154 { 155 return true; 156 } 157 158 // we have a class path, let's include it 159 if (isset(self::$classes[$class])) 160 { 161 require(self::$classes[$class]); 162 163 return true; 164 } 165 166 // see if the file exists in the current module lib directory 167 // must be in a module context 168 if (sfContext::hasInstance() && ($module = sfContext::getInstance()->getModuleName()) && isset(self::$classes[$module.'/'.$class])) 169 { 170 require(self::$classes[$module.'/'.$class]); 171 172 return true; 173 } 174 175 return false; 176 } 177 178 static public function initAutoload() 179 { 180 if (function_exists('spl_autoload_register')) 181 { 182 ini_set('unserialize_callback_func', 'spl_autoload_call'); 183 } 184 else if (!function_exists('__autoload')) 185 { 186 ini_set('unserialize_callback_func', '__autoload'); 187 188 function __autoload($class) 189 { 190 foreach (sfCore::getAutoloadCallables() as $callable) 191 { 192 if (call_user_func($callable, $class)) 193 { 194 return true; 195 } 196 } 197 198 // unspecified class 199 // do not print an error if the autoload came from class_exists 200 $trace = debug_backtrace(); 201 if (count($trace) < 1 || ($trace[1]['function'] != 'class_exists' && $trace[1]['function'] != 'is_a')) 202 { 203 $error = sprintf('Autoloading of class "%s" failed. Try to clear the symfony cache and refresh. [err0003]', $class); 204 $e = new sfAutoloadException($error); 205 206 $e->printStackTrace(); 207 } 208 } 209 } 210 211 self::addAutoloadCallable(array('sfCore', 'splAutoload')); 212 } 213 214 static public function splSimpleAutoload($class) 215 { 216 // class already exists 217 if (class_exists($class, false)) 218 { 219 return true; 220 } 221 222 // we have a class path, let's include it 223 if (isset(self::$classes[$class])) 224 { 225 require(self::$classes[$class]); 226 227 return true; 228 } 229 230 return false; 231 } 232 233 static public function initSimpleAutoload($dirs) 234 { 235 require_once(dirname(__FILE__).'/sfFinder.class.php'); 236 self::$classes = array(); 237 $finder = sfFinder::type('file')->ignore_version_control()->name('*.php'); 238 foreach ((array) $dirs as $dir) 239 { 240 $files = $finder->in(glob($dir)); 241 if (is_array($files)) 242 { 243 foreach ($files as $file) 244 { 245 preg_match_all('~^\s*(?:abstract\s+|final\s+)?(?:class|interface)\s+(\w+)~mi', file_get_contents($file), $classes); 246 foreach ($classes[1] as $class) 247 { 248 self::$classes[$class] = $file; 249 } 250 } 251 } 252 } 253 254 if (function_exists('spl_autoload_register')) 255 { 256 ini_set('unserialize_callback_func', 'spl_autoload_call'); 257 258 spl_autoload_register(array('sfCore', 'splSimpleAutoload')); 259 } 260 elseif (!function_exists('__autoload')) 261 { 262 ini_set('unserialize_callback_func', '__autoload'); 263 264 function __autoload($class) 265 { 266 return sfCore::splSimpleAutoload($class); 267 } 268 } 269 } 270 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |