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