sfRootConfigHandler.class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. * sfRootConfigHandler allows you to specify configuration handlers for the
  11. * application or on a module level.
  12. *
  13. * @package symfony
  14. * @subpackage config
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. * @version SVN: $Id: sfRootConfigHandler.class.php 13547 2008-11-30 14:05:44Z Kris.Wallsmith $
  17. */
  18. class sfRootConfigHandler extends sfYamlConfigHandler
  19. {
  20. /**
  21. * Executes this configuration handler
  22. *
  23. * @param array $configFiles An array of absolute filesystem path to a configuration file
  24. *
  25. * @return string Data to be written to a cache file
  26. *
  27. * @throws sfConfigurationException If a requested configuration file does not exist or is not readable
  28. * @throws sfParseException If a requested configuration file is improperly formatted
  29. */
  30. public function execute($configFiles)
  31. {
  32. // parse the yaml
  33. $config = self::getConfiguration($configFiles);
  34. // determine if we're loading the system config_handlers.yml or a module config_handlers.yml
  35. $moduleLevel = ($this->getParameterHolder()->get('module_level') === true) ? true : false;
  36. if ($moduleLevel)
  37. {
  38. // get the current module name
  39. $moduleName = $this->getParameterHolder()->get('module_name');
  40. }
  41. // init our data and includes arrays
  42. $data = array();
  43. $includes = array();
  44. // let's do our fancy work
  45. foreach ($config as $category => $keys)
  46. {
  47. if ($moduleLevel)
  48. {
  49. // module-level registration, so we must prepend the module
  50. // root to the category
  51. $category = 'modules/'.$moduleName.'/'.$category;
  52. }
  53. if (!isset($keys['class']))
  54. {
  55. // missing class key
  56. throw new sfParseException(sprintf('Configuration file "%s" specifies category "%s" with missing class key.', $configFiles[0], $category));
  57. }
  58. $class = $keys['class'];
  59. if (isset($keys['file']))
  60. {
  61. if (!is_readable($keys['file']))
  62. {
  63. // handler file doesn't exist
  64. throw new sfParseException(sprintf('Configuration file "%s" specifies class "%s" with nonexistent or unreadable file "%s".', $configFiles[0], $class, $keys['file']));
  65. }
  66. // append our data
  67. $includes[] = sprintf("require_once('%s');", $keys['file']);
  68. }
  69. // parse parameters
  70. $parameters = (isset($keys['param']) ? var_export($keys['param'], true) : null);
  71. // append new data
  72. $data[] = sprintf("\$this->handlers['%s'] = new %s(%s);", $category, $class, $parameters);
  73. }
  74. // compile data
  75. $retval = sprintf("<?php\n" .
  76. "// auto-generated by sfRootConfigHandler\n".
  77. "// date: %s\n%s\n%s\n",
  78. date('Y/m/d H:i:s'), implode("\n", $includes), implode("\n", $data));
  79. return $retval;
  80. }
  81. /**
  82. * @see sfConfigHandler
  83. */
  84. static public function getConfiguration(array $configFiles)
  85. {
  86. $config = self::replaceConstants(self::parseYamls($configFiles));
  87. foreach ($config as $category => $keys)
  88. {
  89. if (isset($keys['file']))
  90. {
  91. $config[$category]['file'] = self::replacePath($keys['file']);
  92. }
  93. }
  94. return $config;
  95. }
  96. }