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

Debug toolbar