123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- class sfPhpExcelConfigHandler extends sfYamlConfigHandler
- {
- public function execute($configFiles)
- {
- // get our prefix
- $prefix = strtolower($this->getParameterHolder()->get('prefix', ''));
- // add dynamic prefix if needed
- if ($this->getParameterHolder()->get('module', false))
- {
- $prefix .= "'.strtolower(\$moduleName).'_";
- }
- // parse the yaml
- $myConfig = $this->parseYamls($configFiles);
- $values = array();
- foreach ($myConfig as $category => $keys)
- {
- $values = array_merge($values, $this->getValues($prefix, $category, $keys));
- }
- $data = '';
- foreach ($values as $key => $value)
- {
- $data .= sprintf(" '%s' => %s,\n", $key, var_export($value, true));
- }
- // compile data
- $retval = '';
- if ($values)
- {
- $retval = "<?php\n".
- "// auto-generated by sfPhpExcelConfigHandler\n".
- "// date: %s\nsfConfig::add(array(\n%s));\n";
- $retval = sprintf($retval, date('Y/m/d H:i:s'), $data);
- }
- return $retval;
- }
- /**
- * Gets values from the configuration array.
- *
- * @param string The prefix name
- * @param string The category name
- * @param mixed The key/value array
- *
- * @param array The new key/value array
- */
- protected function getValues($prefix, $category, $keys)
- {
- if (!is_array($keys))
- {
- list($key, $value) = $this->fixCategoryValue($prefix.strtolower($category), '', $keys);
- return array($key => $value);
- }
- $values = array();
- $category = $this->fixCategoryName($category, $prefix);
- // loop through all key/value pairs
- foreach ($keys as $key => $value)
- {
- list($key, $value) = $this->fixCategoryValue($category, $key, $value);
- $values[$key] = $value;
- }
- return $values;
- }
- /**
- * Fixes the category name and replaces constants in the value.
- *
- * @param string The category name
- * @param string The key name
- * @param string The value
- *
- * @param string Return the new key and value
- */
- protected function fixCategoryValue($category, $key, $value)
- {
- // prefix the key
- $key = $category.$key;
- // replace constant values
- $value = $this->replaceConstants($value);
- return array($key, $value);
- }
- /**
- * Fixes the category name.
- *
- * @param string The category name
- * @param string The prefix
- *
- * @return string The fixed category name
- */
- protected function fixCategoryName($category, $prefix)
- {
- // categories starting without a period will be prepended to the key
- if ($category[0] != '.')
- {
- $category = $prefix.$category.'_';
- }
- else
- {
- $category = $prefix;
- }
- return $category;
- }
- }
- ?>
|