PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/util/sfCore.class.php

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