/lib/config/sfDefineEnvironmentConfigHandler.class.php

https://github.com/bheneka/gitta · PHP · 148 lines · 70 code · 18 blank · 60 comment · 5 complexity · f362b68337d3bec16d56a478c4d4d5f9 MD5 · raw file

  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$
  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 module prefix if needed
  33. if ($this->getParameterHolder()->get('module', false))
  34. {
  35. $wildcardValues = $this->getParameterHolder()->get('wildcardValues');
  36. // either the module name is in wildcard values, or it needs to be inserted on runtime
  37. $moduleName = $wildcardValues ? strtolower($wildcardValues[0]) : "'.strtolower(\$moduleName).'";
  38. $prefix .= $moduleName."_";
  39. }
  40. // parse the yaml
  41. $config = self::getConfiguration($configFiles);
  42. $values = array();
  43. foreach ($config as $category => $keys)
  44. {
  45. $values = array_merge($values, $this->getValues($prefix, $category, $keys));
  46. }
  47. $data = '';
  48. foreach ($values as $key => $value)
  49. {
  50. $data .= sprintf(" '%s' => %s,\n", $key, var_export($value, true));
  51. }
  52. // compile data
  53. $retval = '';
  54. if ($values)
  55. {
  56. $retval = "<?php\n".
  57. "// auto-generated by sfDefineEnvironmentConfigHandler\n".
  58. "// date: %s\nsfConfig::add(array(\n%s));\n";
  59. $retval = sprintf($retval, date('Y/m/d H:i:s'), $data);
  60. }
  61. return $retval;
  62. }
  63. /**
  64. * Gets values from the configuration array.
  65. *
  66. * @param string $prefix The prefix name
  67. * @param string $category The category name
  68. * @param mixed $keys The key/value array
  69. *
  70. * @return array The new key/value array
  71. */
  72. protected function getValues($prefix, $category, $keys)
  73. {
  74. if (!is_array($keys))
  75. {
  76. list($key, $value) = $this->fixCategoryValue($prefix.strtolower($category), '', $keys);
  77. return array($key => $value);
  78. }
  79. $values = array();
  80. $category = $this->fixCategoryName($category, $prefix);
  81. // loop through all key/value pairs
  82. foreach ($keys as $key => $value)
  83. {
  84. list($key, $value) = $this->fixCategoryValue($category, $key, $value);
  85. $values[$key] = $value;
  86. }
  87. return $values;
  88. }
  89. /**
  90. * Fixes the category name and replaces constants in the value.
  91. *
  92. * @param string $category The category name
  93. * @param string $key The key name
  94. * @param string $value The value
  95. *
  96. * @return string Return the new key and value
  97. */
  98. protected function fixCategoryValue($category, $key, $value)
  99. {
  100. return array($category.$key, $value);
  101. }
  102. /**
  103. * Fixes the category name.
  104. *
  105. * @param string $category The category name
  106. * @param string $prefix The prefix
  107. *
  108. * @return string The fixed category name
  109. */
  110. protected function fixCategoryName($category, $prefix)
  111. {
  112. // categories starting without a period will be prepended to the key
  113. if ($category[0] != '.')
  114. {
  115. $category = $prefix.$category.'_';
  116. }
  117. else
  118. {
  119. $category = $prefix;
  120. }
  121. return $category;
  122. }
  123. /**
  124. * @see sfConfigHandler
  125. */
  126. static public function getConfiguration(array $configFiles)
  127. {
  128. return self::replaceConstants(self::flattenConfigurationWithEnvironment(self::parseYamls($configFiles)));
  129. }
  130. }