sfDefineEnvironmentConfigHandler.class.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. *
  11. * @package symfony
  12. * @subpackage config
  13. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  14. * @version SVN: $Id: sfDefineEnvironmentConfigHandler.class.php 17858 2009-05-01 21:22:50Z FabianLange $
  15. */
  16. class sfDefineEnvironmentConfigHandler extends sfYamlConfigHandler
  17. {
  18. /**
  19. * Executes this configuration handler.
  20. *
  21. * @param string $configFiles An absolute filesystem path to a configuration file
  22. *
  23. * @return string Data to be written to a cache file
  24. *
  25. * @throws sfConfigurationException If a requested configuration file does not exist or is not readable
  26. * @throws sfParseException If a requested configuration file is improperly formatted
  27. */
  28. public function execute($configFiles)
  29. {
  30. // get our prefix
  31. $prefix = strtolower($this->getParameterHolder()->get('prefix', ''));
  32. // add dynamic prefix if needed
  33. if ($this->getParameterHolder()->get('module', false))
  34. {
  35. $prefix .= "'.strtolower(\$moduleName).'_";
  36. }
  37. // parse the yaml
  38. $config = self::getConfiguration($configFiles);
  39. $values = array();
  40. foreach ($config as $category => $keys)
  41. {
  42. $values = array_merge($values, $this->getValues($prefix, $category, $keys));
  43. }
  44. $data = '';
  45. foreach ($values as $key => $value)
  46. {
  47. $data .= sprintf(" '%s' => %s,\n", $key, var_export($value, true));
  48. }
  49. // compile data
  50. $retval = '';
  51. if ($values)
  52. {
  53. $retval = "<?php\n".
  54. "// auto-generated by sfDefineEnvironmentConfigHandler\n".
  55. "// date: %s\nsfConfig::add(array(\n%s));\n";
  56. $retval = sprintf($retval, date('Y/m/d H:i:s'), $data);
  57. }
  58. return $retval;
  59. }
  60. /**
  61. * Gets values from the configuration array.
  62. *
  63. * @param string $prefix The prefix name
  64. * @param string $category The category name
  65. * @param mixed $keys The key/value array
  66. *
  67. * @return array The new key/value array
  68. */
  69. protected function getValues($prefix, $category, $keys)
  70. {
  71. if (!is_array($keys))
  72. {
  73. list($key, $value) = $this->fixCategoryValue($prefix.strtolower($category), '', $keys);
  74. return array($key => $value);
  75. }
  76. $values = array();
  77. $category = $this->fixCategoryName($category, $prefix);
  78. // loop through all key/value pairs
  79. foreach ($keys as $key => $value)
  80. {
  81. list($key, $value) = $this->fixCategoryValue($category, $key, $value);
  82. $values[$key] = $value;
  83. }
  84. return $values;
  85. }
  86. /**
  87. * Fixes the category name and replaces constants in the value.
  88. *
  89. * @param string $category The category name
  90. * @param string $key The key name
  91. * @param string $value The value
  92. *
  93. * @return string Return the new key and value
  94. */
  95. protected function fixCategoryValue($category, $key, $value)
  96. {
  97. return array($category.$key, $value);
  98. }
  99. /**
  100. * Fixes the category name.
  101. *
  102. * @param string $category The category name
  103. * @param string $prefix The prefix
  104. *
  105. * @return string The fixed category name
  106. */
  107. protected function fixCategoryName($category, $prefix)
  108. {
  109. // categories starting without a period will be prepended to the key
  110. if ($category[0] != '.')
  111. {
  112. $category = $prefix.$category.'_';
  113. }
  114. else
  115. {
  116. $category = $prefix;
  117. }
  118. return $category;
  119. }
  120. /**
  121. * @see sfConfigHandler
  122. */
  123. static public function getConfiguration(array $configFiles)
  124. {
  125. return self::replaceConstants(self::flattenConfigurationWithEnvironment(self::parseYamls($configFiles)));
  126. }
  127. }