sfLoader.class.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 9085 2008-05-20 01:53:23Z Carl.Vondrick $
  16. */
  17. class sfLoader
  18. {
  19. /**
  20. * Gets the helper directories for a given module name.
  21. *
  22. * @param string $moduleName The module name
  23. *
  24. * @return array An array of directories
  25. */
  26. static public function getHelperDirs($moduleName = '')
  27. {
  28. $dirs = array();
  29. if ($moduleName)
  30. {
  31. $dirs[] = sfConfig::get('sf_app_module_dir').'/'.$moduleName.'/lib/helper'; // module
  32. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/modules/'.$moduleName.'/lib/helper'))
  33. {
  34. $dirs = array_merge($dirs, $pluginDirs); // module plugins
  35. }
  36. }
  37. $dirs[] = sfConfig::get('sf_app_lib_dir').'/helper'; // application
  38. $dirs[] = sfConfig::get('sf_lib_dir').'/helper'; // project
  39. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/lib/helper'))
  40. {
  41. $dirs = array_merge($dirs, $pluginDirs); // plugins
  42. }
  43. $dirs[] = sfConfig::get('sf_symfony_lib_dir').'/helper'; // symfony
  44. return $dirs;
  45. }
  46. /**
  47. * Loads helpers.
  48. *
  49. * @param array $helpers An array of helpers to load
  50. * @param string $moduleName A module name (optional)
  51. *
  52. * @throws sfViewException
  53. */
  54. static public function loadHelpers($helpers, $moduleName = '')
  55. {
  56. static $loaded = array();
  57. $dirs = self::getHelperDirs($moduleName);
  58. foreach ((array) $helpers as $helperName)
  59. {
  60. if (isset($loaded[$helperName]))
  61. {
  62. continue;
  63. }
  64. $fileName = $helperName.'Helper.php';
  65. foreach ($dirs as $dir)
  66. {
  67. $included = false;
  68. if (is_readable($dir.'/'.$fileName))
  69. {
  70. include_once($dir.'/'.$fileName);
  71. $included = true;
  72. break;
  73. }
  74. }
  75. if (!$included)
  76. {
  77. // search in the include path
  78. if ((@include_once('helper/'.$fileName)) != 1)
  79. {
  80. $dirs = array_merge($dirs, explode(PATH_SEPARATOR, get_include_path()));
  81. // remove sf_root_dir from dirs
  82. foreach ($dirs as &$dir)
  83. {
  84. $dir = str_replace('%SF_ROOT_DIR%', sfConfig::get('sf_root_dir'), $dir);
  85. }
  86. throw new sfViewException(sprintf('Unable to load "%sHelper.php" helper in: %s.', $helperName, implode(', ', $dirs)));
  87. }
  88. }
  89. $loaded[$helperName] = true;
  90. }
  91. }
  92. }