PageRenderTime 60ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/symfony/config/sfLoader.class.php

http://pumukit.googlecode.com/
PHP | 377 lines | 208 code | 58 blank | 111 comment | 21 complexity | e6ffd146ef81b45080730f96bb21ed70 MD5 | raw file
Possible License(s): LGPL-2.1
  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 10701 2008-08-06 09:44:41Z hartym $
  16. */
  17. class sfLoader
  18. {
  19. /**
  20. * Gets directories where model classes are stored. The order of returned paths is lowest precedence
  21. * to highest precedence.
  22. *
  23. * @return array An array of directories
  24. */
  25. static public function getModelDirs()
  26. {
  27. $dirs = array(); // project
  28. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/lib/model'))
  29. {
  30. $dirs = array_merge($dirs, $pluginDirs); // plugins
  31. }
  32. $dirs[] = sfConfig::get('sf_lib_dir') ? sfConfig::get('sf_lib_dir').'/model' : 'lib/model';
  33. return $dirs;
  34. }
  35. /**
  36. * Gets directories where controller classes are stored for a given module.
  37. *
  38. * @param string The module name
  39. *
  40. * @return array An array of directories
  41. */
  42. static public function getControllerDirs($moduleName)
  43. {
  44. $suffix = $moduleName.'/'.sfConfig::get('sf_app_module_action_dir_name');
  45. $dirs = array();
  46. foreach (sfConfig::get('sf_module_dirs', array()) as $key => $value)
  47. {
  48. $dirs[$key.'/'.$suffix] = $value;
  49. }
  50. $dirs[sfConfig::get('sf_app_module_dir').'/'.$suffix] = false; // application
  51. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/modules/'.$suffix))
  52. {
  53. $dirs = array_merge($dirs, array_combine($pluginDirs, array_fill(0, count($pluginDirs), true))); // plugins
  54. }
  55. $dirs[sfConfig::get('sf_symfony_data_dir').'/modules/'.$suffix] = true; // core modules
  56. return $dirs;
  57. }
  58. /**
  59. * Gets directories where template files are stored for a given module.
  60. *
  61. * @param string The module name
  62. *
  63. * @return array An array of directories
  64. */
  65. static public function getTemplateDirs($moduleName)
  66. {
  67. $suffix = $moduleName.'/'.sfConfig::get('sf_app_module_template_dir_name');
  68. $dirs = array();
  69. foreach (sfConfig::get('sf_module_dirs', array()) as $key => $value)
  70. {
  71. $dirs[] = $key.'/'.$suffix;
  72. }
  73. $dirs[] = sfConfig::get('sf_app_module_dir').'/'.$suffix; // application
  74. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/modules/'.$suffix))
  75. {
  76. $dirs = array_merge($dirs, $pluginDirs); // plugins
  77. }
  78. $dirs[] = sfConfig::get('sf_symfony_data_dir').'/modules/'.$suffix; // core modules
  79. $dirs[] = sfConfig::get('sf_module_cache_dir').'/auto'.ucfirst($suffix); // generated templates in cache
  80. return $dirs;
  81. }
  82. /**
  83. * Gets the template directory to use for a given module and template file.
  84. *
  85. * @param string The module name
  86. * @param string The template file
  87. *
  88. * @return string A template directory
  89. */
  90. static public function getTemplateDir($moduleName, $templateFile)
  91. {
  92. $dirs = self::getTemplateDirs($moduleName);
  93. foreach ($dirs as $dir)
  94. {
  95. if (is_readable($dir.'/'.$templateFile))
  96. {
  97. return $dir;
  98. }
  99. }
  100. return null;
  101. }
  102. /**
  103. * Gets the template to use for a given module and template file.
  104. *
  105. * @param string The module name
  106. * @param string The template file
  107. *
  108. * @return string A template path
  109. */
  110. static public function getTemplatePath($moduleName, $templateFile)
  111. {
  112. $dir = self::getTemplateDir($moduleName, $templateFile);
  113. return $dir ? $dir.'/'.$templateFile : null;
  114. }
  115. /**
  116. * Gets the i18n directory to use for a given module.
  117. *
  118. * @param string The module name
  119. *
  120. * @return string An i18n directory
  121. */
  122. static public function getI18NDir($moduleName)
  123. {
  124. $suffix = $moduleName.'/'.sfConfig::get('sf_app_module_i18n_dir_name');
  125. // application
  126. $dir = sfConfig::get('sf_app_module_dir').'/'.$suffix;
  127. if (is_dir($dir))
  128. {
  129. return $dir;
  130. }
  131. // plugins
  132. $dirs = glob(sfConfig::get('sf_plugins_dir').'/*/modules/'.$suffix);
  133. if (isset($dirs[0]))
  134. {
  135. return $dirs[0];
  136. }
  137. }
  138. /**
  139. * Gets directories where template files are stored for a generator class and a specific theme.
  140. *
  141. * @param string The generator class name
  142. * @param string The theme name
  143. *
  144. * @return array An array of directories
  145. */
  146. static public function getGeneratorTemplateDirs($class, $theme)
  147. {
  148. $dirs = array(sfConfig::get('sf_data_dir').'/generator/'.$class.'/'.$theme.'/template'); // project
  149. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/data/generator/'.$class.'/'.$theme.'/template'))
  150. {
  151. $dirs = array_merge($dirs, $pluginDirs); // plugin
  152. }
  153. $dirs[] = sfConfig::get('sf_symfony_data_dir').'/generator/'.$class.'/default/template'; // default theme
  154. return $dirs;
  155. }
  156. /**
  157. * Gets directories where the skeleton is stored for a generator class and a specific theme.
  158. *
  159. * @param string The generator class name
  160. * @param string The theme name
  161. *
  162. * @return array An array of directories
  163. */
  164. static public function getGeneratorSkeletonDirs($class, $theme)
  165. {
  166. $dirs = array(sfConfig::get('sf_data_dir').'/generator/'.$class.'/'.$theme.'/skeleton'); // project
  167. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/data/generator/'.$class.'/'.$theme.'/skeleton'))
  168. {
  169. $dirs = array_merge($dirs, $pluginDirs); // plugin
  170. }
  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. }