PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/package/app/app/symfony/config/sfLoader.class.php

https://bitbucket.org/pandaos/kaltura
PHP | 375 lines | 209 code | 56 blank | 110 comment | 21 complexity | 78b49a998d5d1c2e61d37c806070b316 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. * sfLoader is a class which contains the logic to look for files/classes in symfony.
  11. *
  12. * @package symfony
  13. * @subpackage util
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfLoader.class.php 3313 2007-01-20 07:00:37Z fabien $
  16. */
  17. class sfLoader
  18. {
  19. /**
  20. * Gets directories where model classes are stored.
  21. *
  22. * @return array An array of directories
  23. */
  24. static public function getModelDirs()
  25. {
  26. $dirs = array(sfConfig::get('sf_lib_dir').'/model' ? sfConfig::get('sf_lib_dir').'/model' : 'lib/model'); // project
  27. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/lib/model'))
  28. {
  29. $dirs = array_merge($dirs, $pluginDirs); // plugins
  30. }
  31. return $dirs;
  32. }
  33. /**
  34. * Gets directories where controller classes are stored for a given module.
  35. *
  36. * @param string The module name
  37. *
  38. * @return array An array of directories
  39. */
  40. static public function getControllerDirs($moduleName)
  41. {
  42. $suffix = $moduleName.'/'.sfConfig::get('sf_app_module_action_dir_name');
  43. $dirs = array();
  44. foreach (sfConfig::get('sf_module_dirs', array()) as $key => $value)
  45. {
  46. $dirs[$key.'/'.$suffix] = $value;
  47. }
  48. $dirs[sfConfig::get('sf_app_module_dir').'/'.$suffix] = false; // application
  49. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/modules/'.$suffix))
  50. {
  51. $dirs = array_merge($dirs, array_combine($pluginDirs, array_fill(0, count($pluginDirs), true))); // plugins
  52. }
  53. $dirs[sfConfig::get('sf_symfony_data_dir').'/modules/'.$suffix] = true; // core modules
  54. return $dirs;
  55. }
  56. /**
  57. * Gets directories where template files are stored for a given module.
  58. *
  59. * @param string The module name
  60. *
  61. * @return array An array of directories
  62. */
  63. static public function getTemplateDirs($moduleName)
  64. {
  65. $suffix = $moduleName.'/'.sfConfig::get('sf_app_module_template_dir_name');
  66. $dirs = array();
  67. foreach (sfConfig::get('sf_module_dirs', array()) as $key => $value)
  68. {
  69. $dirs[] = $key.'/'.$suffix;
  70. }
  71. $dirs[] = sfConfig::get('sf_app_module_dir').'/'.$suffix; // application
  72. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/modules/'.$suffix))
  73. {
  74. $dirs = array_merge($dirs, $pluginDirs); // plugins
  75. }
  76. $dirs[] = sfConfig::get('sf_symfony_data_dir').'/modules/'.$suffix; // core modules
  77. $dirs[] = sfConfig::get('sf_module_cache_dir').'/auto'.ucfirst($suffix); // generated templates in cache
  78. return $dirs;
  79. }
  80. /**
  81. * Gets the template directory to use for a given module and template file.
  82. *
  83. * @param string The module name
  84. * @param string The template file
  85. *
  86. * @return string A template directory
  87. */
  88. static public function getTemplateDir($moduleName, $templateFile)
  89. {
  90. $dirs = self::getTemplateDirs($moduleName);
  91. foreach ($dirs as $dir)
  92. {
  93. if (is_readable($dir.'/'.$templateFile))
  94. {
  95. return $dir;
  96. }
  97. }
  98. return null;
  99. }
  100. /**
  101. * Gets the template to use for a given module and template file.
  102. *
  103. * @param string The module name
  104. * @param string The template file
  105. *
  106. * @return string A template path
  107. */
  108. static public function getTemplatePath($moduleName, $templateFile)
  109. {
  110. $dir = self::getTemplateDir($moduleName, $templateFile);
  111. return $dir ? $dir.'/'.$templateFile : null;
  112. }
  113. /**
  114. * Gets the i18n directory to use for a given module.
  115. *
  116. * @param string The module name
  117. *
  118. * @return string An i18n directory
  119. */
  120. static public function getI18NDir($moduleName)
  121. {
  122. $suffix = $moduleName.'/'.sfConfig::get('sf_app_module_i18n_dir_name');
  123. // application
  124. $dir = sfConfig::get('sf_app_module_dir').'/'.$suffix;
  125. if (is_dir($dir))
  126. {
  127. return $dir;
  128. }
  129. // plugins
  130. $dirs = glob(sfConfig::get('sf_plugins_dir').'/*/modules/'.$suffix);
  131. if (isset($dirs[0]))
  132. {
  133. return $dirs[0];
  134. }
  135. }
  136. /**
  137. * Gets directories where template files are stored for a generator class and a specific theme.
  138. *
  139. * @param string The generator class name
  140. * @param string The theme name
  141. *
  142. * @return array An array of directories
  143. */
  144. static public function getGeneratorTemplateDirs($class, $theme)
  145. {
  146. $dirs = array();
  147. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/data/generator/'.$class.'/'.$theme.'/template'))
  148. {
  149. $dirs = array_merge($dirs, $pluginDirs); // plugin
  150. }
  151. $dirs[] = sfConfig::get('sf_data_dir').'/generator/'.$class.'/'.$theme.'/template'; // project
  152. $dirs[] = sfConfig::get('sf_symfony_data_dir').'/generator/'.$class.'/default/template'; // default theme
  153. return $dirs;
  154. }
  155. /**
  156. * Gets directories where the skeleton is stored for a generator class and a specific theme.
  157. *
  158. * @param string The generator class name
  159. * @param string The theme name
  160. *
  161. * @return array An array of directories
  162. */
  163. static public function getGeneratorSkeletonDirs($class, $theme)
  164. {
  165. $dirs = array();
  166. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/data/generator/'.$class.'/'.$theme.'/skeleton'))
  167. {
  168. $dirs = array_merge($dirs, $pluginDirs); // plugin
  169. }
  170. $dirs[] = sfConfig::get('sf_data_dir').'/generator/'.$class.'/'.$theme.'/skeleton'; // project
  171. $dirs[] = sfConfig::get('sf_symfony_data_dir').'/generator/'.$class.'/default/skeleton'; // default theme
  172. return $dirs;
  173. }
  174. /**
  175. * Gets the template to use for a generator class.
  176. *
  177. * @param string The generator class name
  178. * @param string The theme name
  179. * @param string The template path
  180. *
  181. * @return string A template path
  182. *
  183. * @throws sfException
  184. */
  185. static public function getGeneratorTemplate($class, $theme, $path)
  186. {
  187. $dirs = self::getGeneratorTemplateDirs($class, $theme);
  188. foreach ($dirs as $dir)
  189. {
  190. if (is_readable($dir.'/'.$path))
  191. {
  192. return $dir.'/'.$path;
  193. }
  194. }
  195. throw new sfException(sprintf('Unable to load "%s" generator template in: %s', $path, implode(', ', $dirs)));
  196. }
  197. /**
  198. * Gets the configuration file paths for a given relative configuration path.
  199. *
  200. * @param string The configuration path
  201. *
  202. * @return array An array of paths
  203. */
  204. static public function getConfigPaths($configPath)
  205. {
  206. $globalConfigPath = basename(dirname($configPath)).'/'.basename($configPath);
  207. $files = array(
  208. sfConfig::get('sf_symfony_data_dir').'/'.$globalConfigPath, // symfony
  209. sfConfig::get('sf_symfony_data_dir').'/'.$configPath, // core modules
  210. );
  211. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/'.$globalConfigPath))
  212. {
  213. $files = array_merge($files, $pluginDirs); // plugins
  214. }
  215. $files = array_merge($files, array(
  216. sfConfig::get('sf_root_dir').'/'.$globalConfigPath, // project
  217. sfConfig::get('sf_root_dir').'/'.$configPath, // project
  218. sfConfig::get('sf_app_dir').'/'.$globalConfigPath, // application
  219. sfConfig::get('sf_cache_dir').'/'.$configPath, // generated modules
  220. ));
  221. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/'.$configPath))
  222. {
  223. $files = array_merge($files, $pluginDirs); // plugins
  224. }
  225. $files[] = sfConfig::get('sf_app_dir').'/'.$configPath; // module
  226. $configs = array();
  227. foreach (array_unique($files) as $file)
  228. {
  229. if (is_readable($file))
  230. {
  231. $configs[] = $file;
  232. }
  233. }
  234. return $configs;
  235. }
  236. /**
  237. * Gets the helper directories for a given module name.
  238. *
  239. * @param string The module name
  240. *
  241. * @return array An array of directories
  242. */
  243. static public function getHelperDirs($moduleName = '')
  244. {
  245. $dirs = array();
  246. if ($moduleName)
  247. {
  248. $dirs[] = sfConfig::get('sf_app_module_dir').'/'.$moduleName.'/'.sfConfig::get('sf_app_module_lib_dir_name').'/helper'; // module
  249. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/modules/'.$moduleName.'/lib/helper'))
  250. {
  251. $dirs = array_merge($dirs, $pluginDirs); // module plugins
  252. }
  253. }
  254. $dirs[] = sfConfig::get('sf_app_lib_dir').'/helper'; // application
  255. $dirs[] = sfConfig::get('sf_lib_dir').'/helper'; // project
  256. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/lib/helper'))
  257. {
  258. $dirs = array_merge($dirs, $pluginDirs); // plugins
  259. }
  260. $dirs[] = sfConfig::get('sf_symfony_lib_dir').'/helper'; // global
  261. return $dirs;
  262. }
  263. /**
  264. * Loads helpers.
  265. *
  266. * @param array An array of helpers to load
  267. * @param string A module name (optional)
  268. *
  269. * @throws sfViewException
  270. */
  271. static public function loadHelpers($helpers, $moduleName = '')
  272. {
  273. static $loaded = array();
  274. $dirs = self::getHelperDirs($moduleName);
  275. foreach ((array) $helpers as $helperName)
  276. {
  277. if (isset($loaded[$helperName]))
  278. {
  279. continue;
  280. }
  281. $fileName = $helperName.'Helper.php';
  282. foreach ($dirs as $dir)
  283. {
  284. $included = false;
  285. if (is_readable($dir.'/'.$fileName))
  286. {
  287. include($dir.'/'.$fileName);
  288. $included = true;
  289. break;
  290. }
  291. }
  292. if (!$included)
  293. {
  294. // search in the include path
  295. if ((@include('helper/'.$fileName)) != 1)
  296. {
  297. $dirs = array_merge($dirs, explode(PATH_SEPARATOR, get_include_path()));
  298. // remove sf_root_dir from dirs
  299. foreach ($dirs as &$dir)
  300. {
  301. $dir = str_replace('%SF_ROOT_DIR%', sfConfig::get('sf_root_dir'), $dir);
  302. }
  303. throw new sfViewException(sprintf('Unable to load "%sHelper.php" helper in: %s', $helperName, implode(', ', $dirs)));
  304. }
  305. }
  306. $loaded[$helperName] = true;
  307. }
  308. }
  309. static public function loadPluginConfig()
  310. {
  311. if ($pluginConfigs = glob(sfConfig::get('sf_plugins_dir').'/*/config/config.php'))
  312. {
  313. foreach ($pluginConfigs as $config)
  314. {
  315. include($config);
  316. }
  317. }
  318. }
  319. }