<?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;
    }
}
?>