sfPhpExcelConfigHandler.class.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. class sfPhpExcelConfigHandler extends sfYamlConfigHandler
  3. {
  4. public function execute($configFiles)
  5. {
  6. // get our prefix
  7. $prefix = strtolower($this->getParameterHolder()->get('prefix', ''));
  8. // add dynamic prefix if needed
  9. if ($this->getParameterHolder()->get('module', false))
  10. {
  11. $prefix .= "'.strtolower(\$moduleName).'_";
  12. }
  13. // parse the yaml
  14. $myConfig = $this->parseYamls($configFiles);
  15. $values = array();
  16. foreach ($myConfig as $category => $keys)
  17. {
  18. $values = array_merge($values, $this->getValues($prefix, $category, $keys));
  19. }
  20. $data = '';
  21. foreach ($values as $key => $value)
  22. {
  23. $data .= sprintf(" '%s' => %s,\n", $key, var_export($value, true));
  24. }
  25. // compile data
  26. $retval = '';
  27. if ($values)
  28. {
  29. $retval = "<?php\n".
  30. "// auto-generated by sfPhpExcelConfigHandler\n".
  31. "// date: %s\nsfConfig::add(array(\n%s));\n";
  32. $retval = sprintf($retval, date('Y/m/d H:i:s'), $data);
  33. }
  34. return $retval;
  35. }
  36. /**
  37. * Gets values from the configuration array.
  38. *
  39. * @param string The prefix name
  40. * @param string The category name
  41. * @param mixed The key/value array
  42. *
  43. * @param array The new key/value array
  44. */
  45. protected function getValues($prefix, $category, $keys)
  46. {
  47. if (!is_array($keys))
  48. {
  49. list($key, $value) = $this->fixCategoryValue($prefix.strtolower($category), '', $keys);
  50. return array($key => $value);
  51. }
  52. $values = array();
  53. $category = $this->fixCategoryName($category, $prefix);
  54. // loop through all key/value pairs
  55. foreach ($keys as $key => $value)
  56. {
  57. list($key, $value) = $this->fixCategoryValue($category, $key, $value);
  58. $values[$key] = $value;
  59. }
  60. return $values;
  61. }
  62. /**
  63. * Fixes the category name and replaces constants in the value.
  64. *
  65. * @param string The category name
  66. * @param string The key name
  67. * @param string The value
  68. *
  69. * @param string Return the new key and value
  70. */
  71. protected function fixCategoryValue($category, $key, $value)
  72. {
  73. // prefix the key
  74. $key = $category.$key;
  75. // replace constant values
  76. $value = $this->replaceConstants($value);
  77. return array($key, $value);
  78. }
  79. /**
  80. * Fixes the category name.
  81. *
  82. * @param string The category name
  83. * @param string The prefix
  84. *
  85. * @return string The fixed category name
  86. */
  87. protected function fixCategoryName($category, $prefix)
  88. {
  89. // categories starting without a period will be prepended to the key
  90. if ($category[0] != '.')
  91. {
  92. $category = $prefix.$category.'_';
  93. }
  94. else
  95. {
  96. $category = $prefix;
  97. }
  98. return $category;
  99. }
  100. }
  101. ?>