/lib/config/ysfApplicationConfiguration.class.php

https://github.com/remialvado/ysfDimensionPlugin · PHP · 618 lines · 409 code · 79 blank · 130 comment · 47 complexity · 32af7da1d504fa400475ddacc62b40da MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. * Copyright (c) 2008 Yahoo! Inc. All rights reserved.
  5. * The copyrights embodied in the content in this file are licensed
  6. * under the MIT open source license.
  7. *
  8. * For the full copyright and license information, please view the LICENSE.yahoo
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * ysfApplicationConfiguration represents a configuration for a symfony application.
  13. *
  14. * @package ysymfony
  15. * @subpackage config
  16. * @author Dustin Whittle <dustin.whittle@symfony-project.com>
  17. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  18. * @version SVN: $Id: sfApplicationConfiguration.class.php 7618 2008-02-27 00:02:41Z dwhittle $
  19. */
  20. abstract class ysfApplicationConfiguration extends sfApplicationConfiguration
  21. {
  22. /**
  23. * Configures the current configuration.
  24. */
  25. public function configure()
  26. {
  27. parent::configure();
  28. if ($this->hasDimension())
  29. {
  30. $this->setCacheDir($this->getRootDir().'/cache/'.$this->dimension->getName());
  31. }
  32. }
  33. /**
  34. * Initialized the current configuration.
  35. */
  36. public function initialize()
  37. {
  38. parent::initialize();
  39. }
  40. /**
  41. * Gets directories where controller classes are stored for a given module.
  42. *
  43. * @param string The module name
  44. *
  45. * @return array An array of directories
  46. */
  47. public function getControllerDirs($moduleName)
  48. {
  49. // if there is a configuration dimension
  50. if ($this->hasDimension())
  51. {
  52. $cacheKey = sprintf('sf_controller_dirs_%s', $moduleName);
  53. // if there is a cache return it
  54. if($this->dimension->getCache()->has($cacheKey))
  55. {
  56. $dirs = $this->dimension->getCache()->get($cacheKey);
  57. }
  58. else
  59. {
  60. $dimensions = $this->dimension->getCascade();
  61. // otherwise create and store
  62. $dirs = array();
  63. if (is_readable(sfConfig::get('sf_app_module_dir').'/'.$moduleName.'/actions'))
  64. {
  65. foreach ($dimensions as $dimension)
  66. {
  67. if(is_readable(sfConfig::get('sf_app_module_dir').'/'.$moduleName.'/actions/'.$dimension))
  68. {
  69. $dirs[sfConfig::get('sf_app_module_dir').'/'.$moduleName.'/actions/'.$dimension] = false;
  70. }
  71. }
  72. $dirs[sfConfig::get('sf_app_module_dir').'/'.$moduleName.'/actions'] = false; // application
  73. }
  74. foreach (sfConfig::get('sf_module_dirs', array()) as $key => $value)
  75. {
  76. if(is_readable($key.'/'.$moduleName.'/actions'))
  77. {
  78. // extend base dirs and add dimension cascade + checking dir exists
  79. foreach ($dimensions as $dimension)
  80. {
  81. if(is_readable($key.'/'.$moduleName.'/actions/'.$dimension))
  82. {
  83. $dirs[$key.'/'.$moduleName.'/actions/'.$dimension] = $value;
  84. }
  85. }
  86. $dirs[$key.'/'.$moduleName.'/actions'] = $value;
  87. }
  88. }
  89. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/modules/'.$moduleName.'/actions'))
  90. {
  91. foreach ($pluginDirs as $dir)
  92. {
  93. // extend base dirs and add dimension cascade + checking dir exists
  94. foreach ($dimensions as $dimension)
  95. {
  96. if(is_readable($dir.'/'.$dimension))
  97. {
  98. array_push($pluginDirs, $dir.'/'.$dimension);
  99. }
  100. }
  101. }
  102. $dirs = array_merge($dirs, array_combine($pluginDirs, array_fill(0, count($pluginDirs), true))); // plugins
  103. }
  104. if (is_readable(sfConfig::get('sf_symfony_lib_dir').'/controller/'.$moduleName.'/actions'))
  105. {
  106. $dirs[sfConfig::get('sf_symfony_lib_dir').'/controller/'.$moduleName.'/actions'] = true; // core modules
  107. }
  108. // save cache
  109. $this->dimension->getCache()->set($cacheKey, $dirs);
  110. }
  111. }
  112. else
  113. {
  114. $dirs = parent::getControllerDirs($moduleName);
  115. }
  116. return $dirs;
  117. }
  118. /**
  119. * Gets directories where template files are stored for a given module.
  120. *
  121. * @param string The module name
  122. *
  123. * @return array An array of directories
  124. */
  125. public function getTemplateDirs($moduleName)
  126. {
  127. // if there is a configuration dimension
  128. if ($this->hasDimension())
  129. {
  130. $cacheKey = sprintf('sf_template_dirs_%s', $moduleName);
  131. // if there is a cache return it
  132. if($this->dimension->getCache()->has($cacheKey))
  133. {
  134. $dirs = $this->dimension->getCache()->get($cacheKey);
  135. }
  136. else
  137. {
  138. $dirs = array();
  139. $dimensions = $this->dimension->getCascade();
  140. foreach (sfConfig::get('sf_module_dirs', array()) as $key => $value)
  141. {
  142. if (is_readable($key.'/'.$moduleName.'/templates'))
  143. {
  144. foreach ($dimensions as $dimension)
  145. {
  146. if (is_readable($key.'/'.$moduleName.'/templates/'.$dimension))
  147. {
  148. array_push($dirs, $key.'/'.$moduleName.'/templates/'.$dimension);
  149. }
  150. }
  151. array_push($dirs, $key.'/'.$moduleName.'/templates');
  152. }
  153. }
  154. if(is_readable(sfConfig::get('sf_app_module_dir').'/'.$moduleName.'/templates'))
  155. {
  156. foreach ($dimensions as $dimension)
  157. {
  158. if(is_readable(sfConfig::get('sf_app_module_dir').'/'.$moduleName.'/templates/'.$dimension))
  159. {
  160. array_push($dirs, sfConfig::get('sf_app_module_dir').'/'.$moduleName.'/templates/'.$dimension);
  161. }
  162. }
  163. array_push($dirs, sfConfig::get('sf_app_module_dir').'/'.$moduleName.'/templates'); // application module
  164. }
  165. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/modules/'.$moduleName.'/templates'))
  166. {
  167. foreach ($pluginDirs as $dir)
  168. {
  169. foreach($dimensions as $dimension)
  170. {
  171. if (is_readable($dir.'/'.$dimension))
  172. {
  173. array_push($dirs, $dir.'/'.$dimension);
  174. }
  175. }
  176. array_push($dirs, $dir); // plugins
  177. }
  178. }
  179. $dirs[] = sfConfig::get('sf_module_cache_dir').'/auto'.ucfirst($moduleName.'/templates'); // generated templates in cache
  180. $dirs[] = sfConfig::get('sf_symfony_lib_dir').'/controller/'.$moduleName.'/templates'; // core modules
  181. // save cache
  182. $this->dimension->getCache()->set($cacheKey, $dirs);
  183. }
  184. }
  185. else
  186. {
  187. $dirs = parent::getTemplateDirs($moduleName);
  188. }
  189. return $dirs;
  190. }
  191. /**
  192. * Gets the template directory to use for a given module and template file.
  193. *
  194. * @param string The module name
  195. * @param string The template file
  196. *
  197. * @return string A template directory
  198. */
  199. public function getTemplateDir($moduleName, $templateFile)
  200. {
  201. // if there is a configuration dimension
  202. if ($this->hasDimension())
  203. {
  204. $cacheKey = sprintf('sf_template_dirs_%s_%s', $moduleName, $templateFile);
  205. // if there is a cache return it
  206. if($this->dimension->getCache()->has($cacheKey))
  207. {
  208. $dirs = $this->dimension->getCache()->get($cacheKey);
  209. }
  210. else
  211. {
  212. $dirs = null;
  213. $paths = $this->getTemplateDirs($moduleName);
  214. foreach ($paths as $dir)
  215. {
  216. if (is_readable($dir.'/'.$templateFile))
  217. {
  218. $dirs = $dir;
  219. break;
  220. }
  221. }
  222. /*
  223. if($dirs === null)
  224. {
  225. throw new sfException(sprintf('Could not find template "%s" in paths "%s"', $templateFile, var_export($paths, true)));
  226. }
  227. */
  228. // save cache
  229. $this->dimension->getCache()->set($cacheKey, $dirs);
  230. }
  231. }
  232. else
  233. {
  234. $dirs = parent::getTemplateDir($moduleName, $templateFile);
  235. }
  236. return $dirs;
  237. }
  238. /**
  239. * Gets the template to use for a given module and template file.
  240. *
  241. * @param string The module name
  242. * @param string The template file
  243. *
  244. * @return string A template path
  245. */
  246. public function getTemplatePath($moduleName, $templateFile)
  247. {
  248. $dir = $this->getTemplateDir($moduleName, $templateFile);
  249. return $dir ? $dir.'/'.$templateFile : null;
  250. }
  251. /**
  252. * Gets the decorator directories.
  253. *
  254. * @param string The template file
  255. *
  256. * @return array An array of the decorator directories
  257. *
  258. */
  259. public function getDecoratorDirs()
  260. {
  261. // if there is a configuration dimension
  262. if ($this->hasDimension())
  263. {
  264. $cacheKey = 'sf_decorator_dirs';
  265. // if there is a cache return it
  266. if($this->dimension->getCache()->has($cacheKey))
  267. {
  268. $dirs = $this->dimension->getCache()->get($cacheKey);
  269. }
  270. else
  271. {
  272. $dirs = array();
  273. $dimensions = $this->dimension->getCascade();
  274. $dir = sfConfig::get('sf_app_template_dir');
  275. foreach ($dimensions as $dimension)
  276. {
  277. if (is_readable($dir.'/'.$dimension))
  278. {
  279. array_push($dirs, $dir.'/'.$dimension);
  280. }
  281. }
  282. array_push($dirs, $dir);
  283. // save cache
  284. $this->dimension->getCache()->set($cacheKey, $dirs);
  285. }
  286. }
  287. else
  288. {
  289. $dirs = parent::getDecoratorDirs();
  290. }
  291. return $dirs;
  292. }
  293. /**
  294. * Gets the decorator directory for a given template.
  295. *
  296. * @param string The template file
  297. *
  298. * @return string A template directory
  299. */
  300. public function getDecoratorDir($template)
  301. {
  302. // if there is a configuration dimension
  303. if ($this->hasDimension())
  304. {
  305. $cacheKey = sprintf('sf_decorator_dir_%s', $template);
  306. // if there is a cache return it
  307. if($this->dimension->getCache()->has($cacheKey))
  308. {
  309. $decoratorDir = $this->dimension->getCache()->get($cacheKey);
  310. }
  311. else
  312. {
  313. $dirs = $this->getDecoratorDirs();
  314. foreach ($dirs as $dir)
  315. {
  316. if (is_readable($dir.'/'.$template))
  317. {
  318. $decoratorDir = $dir;
  319. break; // find most specific and then break
  320. }
  321. }
  322. // save cache
  323. $this->dimension->getCache()->set($cacheKey, $decoratorDir);
  324. }
  325. }
  326. else
  327. {
  328. $decoratorDir = parent::getDecoratorDir($template);
  329. }
  330. return $decoratorDir;
  331. }
  332. /**
  333. * Gets the i18n directories to use globally.
  334. *
  335. * @return array An array of i18n directories
  336. */
  337. public function getI18NGlobalDirs()
  338. {
  339. // if there is a configuration dimension
  340. if ($this->hasDimension())
  341. {
  342. $cacheKey = 'sf_i18n_global_dirs';
  343. // if there is a cache return it
  344. if($this->dimension->getCache()->has($cacheKey))
  345. {
  346. $dirs = $this->dimension->getCache()->get($cacheKey);
  347. }
  348. else
  349. {
  350. $dirs = array();
  351. // application
  352. if (is_dir($dir = sfConfig::get('sf_app_dir').'/i18n'))
  353. {
  354. array_push($dirs, $dir);
  355. }
  356. // plugins
  357. $pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/i18n');
  358. if (isset($pluginDirs[0]))
  359. {
  360. array_push($dirs, $pluginDirs[0]);
  361. }
  362. // save cache
  363. $this->dimension->getCache()->set($cacheKey, $dirs);
  364. }
  365. }
  366. else
  367. {
  368. $dirs = parent::getI18NGlobalDirs();
  369. }
  370. return $dirs;
  371. }
  372. /**
  373. * Gets the i18n directories to use for a given module.
  374. *
  375. * @param string The module name
  376. *
  377. * @return array An array of i18n directories
  378. */
  379. public function getI18NDirs($moduleName)
  380. {
  381. // if there is a configuration dimension
  382. if ($this->hasDimension())
  383. {
  384. $cacheKey = sprintf('sf_i18n_dirs_%s', $moduleName);
  385. // if there is a cache return it
  386. if($this->dimension->getCache()->has($cacheKey))
  387. {
  388. $dirs = $this->dimension->getCache()->get($cacheKey);
  389. }
  390. else
  391. {
  392. $dirs = array();
  393. // module
  394. if (is_dir($dir = sfConfig::get('sf_app_module_dir').'/'.$moduleName.'/i18n'))
  395. {
  396. array_push($dirs, $dir);
  397. }
  398. // application
  399. if (is_dir($dir = sfConfig::get('sf_app_dir').'/i18n'))
  400. {
  401. array_push($dirs, $dir);
  402. }
  403. // module in plugins
  404. $pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/modules/'.$moduleName.'/i18n');
  405. if (isset($pluginDirs[0]))
  406. {
  407. array_push($dirs, $pluginDirs[0]);
  408. }
  409. // plugins
  410. $pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/i18n');
  411. if (isset($pluginDirs[0]))
  412. {
  413. array_push($dirs, $pluginDirs[0]);
  414. }
  415. // save cache
  416. $this->dimension->getCache()->set($cacheKey, $dirs);
  417. }
  418. }
  419. else
  420. {
  421. $dirs = parent::getI18NDirs($moduleName);
  422. }
  423. return $dirs;
  424. }
  425. /**
  426. * Gets the configuration file paths for a given relative configuration path.
  427. *
  428. * @param string The configuration path
  429. *
  430. * @return array An array of paths
  431. */
  432. public function getConfigPaths($configPath)
  433. {
  434. // returned in reverse order cascade
  435. // if there is a configuration dimension
  436. if ($this->hasDimension())
  437. {
  438. $cacheKey = sprintf('sf_config_dirs_%s', $configPath);
  439. // if there is a cache return it
  440. if($this->dimension->getCache()->has($cacheKey))
  441. {
  442. $configs = $this->dimension->getCache()->get($cacheKey);
  443. }
  444. else
  445. {
  446. // reverse cascade
  447. $dimensions = array_reverse($this->dimension->getCascade());
  448. // $configPath = modules/blah/config/config.yml | config/config.yml
  449. $configDirName = dirname($configPath);
  450. $configFileName = basename($configPath);
  451. $globalConfigPath = basename($configDirName).'/'.$configFileName; // config/config.yml
  452. $files = array(
  453. sfConfig::get('sf_symfony_lib_dir').'/config/'.$globalConfigPath, // symfony
  454. );
  455. if ($bundledPluginConfigs = glob(sfConfig::get('sf_symfony_lib_dir').'/plugins/*/'.$globalConfigPath))
  456. {
  457. $files = array_merge($files, $bundledPluginConfigs); // bundled plugins
  458. }
  459. if ($pluginConfigs = glob(sfConfig::get('sf_plugins_dir').'/*/'.$globalConfigPath))
  460. {
  461. foreach ($pluginConfigs as $pluginConfig)
  462. {
  463. $configDir = dirname($pluginConfig);
  464. array_push($files, $pluginConfig); // plugins
  465. foreach ($dimensions as $dimension)
  466. {
  467. array_push($files, $configDir.'/'.$dimension.'/'.basename($pluginConfig)); // plugin dimensions
  468. }
  469. }
  470. }
  471. if (is_readable(sfConfig::get('sf_root_dir').'/'.$globalConfigPath))
  472. {
  473. $configDir = dirname(sfConfig::get('sf_root_dir').'/'.$globalConfigPath);
  474. array_push($files, sfConfig::get('sf_root_dir').'/'.$globalConfigPath); // project
  475. foreach ($dimensions as $dimension)
  476. {
  477. array_push($files, $configDir.'/'.$dimension.'/'.basename($globalConfigPath)); // project dimensions
  478. }
  479. }
  480. if (is_readable(sfConfig::get('sf_root_dir').'/'.$configPath))
  481. {
  482. $configDir = dirname(sfConfig::get('sf_root_dir').'/'.$configPath);
  483. array_push($files, sfConfig::get('sf_root_dir').'/'.$configPath); // project
  484. foreach ($dimensions as $dimension)
  485. {
  486. array_push($files, $configDir.'/'.$dimension.'/'.basename($configPath)); // project dimensions
  487. }
  488. }
  489. if (is_readable(sfConfig::get('sf_app_dir').'/'.$globalConfigPath))
  490. {
  491. $configDir = dirname(sfConfig::get('sf_app_dir').'/'.$globalConfigPath);
  492. array_push($files, sfConfig::get('sf_app_dir').'/'.$globalConfigPath); // application
  493. foreach ($dimensions as $dimension)
  494. {
  495. array_push($files, $configDir.'/'.$dimension.'/'.basename($globalConfigPath)); // application dimensions
  496. }
  497. }
  498. array_push($files, sfConfig::get('sf_app_cache_dir').'/'.$configPath); // generated modules
  499. if ($pluginConfigs = glob(sfConfig::get('sf_plugins_dir').'/*/'.$configPath))
  500. {
  501. foreach ($pluginConfigs as $pluginConfig)
  502. {
  503. $configDir = dirname($pluginConfig);
  504. array_push($files, $pluginConfig); // plugins
  505. foreach ($dimensions as $dimension)
  506. {
  507. array_push($files, $configDir.'/'.$dimension.'/'.basename($pluginConfig)); // plugin dimensions
  508. }
  509. }
  510. }
  511. if (is_readable(sfConfig::get('sf_app_dir').'/'.$configPath))
  512. {
  513. $configDir = dirname(sfConfig::get('sf_app_dir').'/'.$configPath);
  514. array_push($files, sfConfig::get('sf_app_dir').'/'.$configPath); // module
  515. foreach ($dimensions as $dimension)
  516. {
  517. array_push($files, $configDir.'/'.$dimension.'/'.basename($configPath)); // module
  518. }
  519. }
  520. $configs = array();
  521. $files = array_unique($files);
  522. foreach ($files as $file)
  523. {
  524. if (is_readable($file))
  525. {
  526. $configs[] = $file;
  527. }
  528. }
  529. // save cache
  530. $this->dimension->getCache()->set($cacheKey, $configs);
  531. }
  532. }
  533. else
  534. {
  535. $configs = parent::getConfigPaths($configPath);
  536. }
  537. return $configs;
  538. }
  539. }