PageRenderTime 57ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/package/app/app/symfony/util/sfCore.class.php

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