1. sfGeneratorConfigHandler.class.php
  2. /** * sfGeneratorConfigHandler. * * @package symfony * @subpackage config * @author Fabien Potencier * @version SVN: $Id: sfGeneratorConfigHandler.class.php 23932 2009-11-14 16:32:53Z fabien $ */
  3. class sfGeneratorConfigHandler extends sfYamlConfigHandler
  4. {
  5. /**
  6. * Executes this configuration handler.
  7. *
  8. * @param array $configFiles An array of 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. * @throws sfInitializationException If a generator.yml key check fails
  15. */
  16. public function execute($configFiles)
  17. {
  18. // parse the yaml
  19. $config = self::getConfiguration($configFiles);
  20. if (!$config)
  21. {
  22. return '';
  23. }
  24. if (!isset($config['generator']))
  25. {
  26. throw new sfParseException(sprintf('Configuration file "%s" must specify a generator section.', isset($configFiles[1]) ? $configFiles[1] : $configFiles[0]));
  27. }
  28. $config = $config['generator'];
  29. if (!isset($config['class']))
  30. {
  31. throw new sfParseException(sprintf('Configuration file "%s" must specify a generator class section under the generator section.', isset($configFiles[1]) ? $configFiles[1] : $configFiles[0]));
  32. }
  33. foreach (array('fields', 'list', 'edit') as $section)
  34. {
  35. if (isset($config[$section]))
  36. {
  37. throw new sfParseException(sprintf('Configuration file "%s" can specify a "%s" section but only under the param section.', isset($configFiles[1]) ? $configFiles[1] : $configFiles[0], $section));
  38. }
  39. }
  40. // generate class and add a reference to it
  41. $generatorManager = new sfGeneratorManager(sfContext::getInstance()->getConfiguration());
  42. // generator parameters
  43. $generatorParam = (isset($config['param']) ? $config['param'] : array());
  44. // hack to find the module name (look for the last /modules/ in path)
  45. preg_match('#.*/modules/([^/]+)/#', $configFiles[0], $match);
  46. $generatorParam['moduleName'] = $match[1];
  47. // compile data
  48. $retval = "<?php\n".
  49. "// auto-generated by sfGeneratorConfigHandler\n".
  50. "// date: %s\n%s\n";
  51. $retval = sprintf($retval, date('Y/m/d H:i:s'), self::getContent($generatorManager, $config['class'], $generatorParam));
  52. return $retval;
  53. }
  54. static public function getContent(sfGeneratorManager $generatorManager, $class, $parameters)
  55. {
  56. return $generatorManager->generate($class, $parameters);
  57. }
  58. /**
  59. * @see sfConfigHandler
  60. */
  61. static public function getConfiguration(array $configFiles)
  62. {
  63. return self::parseYamls($configFiles);
  64. }
  65. }

Debug toolbar