sfGeneratorConfigHandler.class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfGeneratorConfigHandler.
  11. *
  12. * @package symfony
  13. * @subpackage config
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfGeneratorConfigHandler.class.php 9771 2008-06-23 03:56:59Z dwhittle $
  16. */
  17. class sfGeneratorConfigHandler extends sfYamlConfigHandler
  18. {
  19. /**
  20. * Executes this configuration handler.
  21. *
  22. * @param array $configFiles An array of absolute filesystem path to a configuration file
  23. *
  24. * @return string Data to be written to a cache file
  25. *
  26. * @throws sfConfigurationException If a requested configuration file does not exist or is not readable
  27. * @throws sfParseException If a requested configuration file is improperly formatted
  28. * @throws sfInitializationException If a generator.yml key check fails
  29. */
  30. public function execute($configFiles)
  31. {
  32. // parse the yaml
  33. $config = self::getConfiguration($configFiles);
  34. if (!$config)
  35. {
  36. return '';
  37. }
  38. if (!isset($config['generator']))
  39. {
  40. throw new sfParseException(sprintf('Configuration file "%s" must specify a generator section.', isset($configFiles[1]) ? $configFiles[1] : $configFiles[0]));
  41. }
  42. $config = $config['generator'];
  43. if (!isset($config['class']))
  44. {
  45. throw new sfParseException(sprintf('Configuration file "%s" must specify a generator class section under the generator section.', isset($configFiles[1]) ? $configFiles[1] : $configFiles[0]));
  46. }
  47. foreach (array('fields', 'list', 'edit') as $section)
  48. {
  49. if (isset($config[$section]))
  50. {
  51. 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));
  52. }
  53. }
  54. // generate class and add a reference to it
  55. $generatorManager = new sfGeneratorManager(sfContext::getInstance()->getConfiguration());
  56. // generator parameters
  57. $generatorParam = (isset($config['param']) ? $config['param'] : array());
  58. // hack to find the module name (look for the last /modules/ in path)
  59. preg_match('#.*/modules/([^/]+)/#', $configFiles[0], $match);
  60. $generatorParam['moduleName'] = $match[1];
  61. // compile data
  62. $retval = "<?php\n".
  63. "// auto-generated by sfGeneratorConfigHandler\n".
  64. "// date: %s\n%s\n";
  65. $retval = sprintf($retval, date('Y/m/d H:i:s'), $this->getContent($generatorManager, $config['class'], $generatorParam));
  66. return $retval;
  67. }
  68. static public function getContent(sfGeneratorManager $generatorManager, $class, $parameters)
  69. {
  70. $data = '';
  71. // TODO: remove when the admin generator is moved to the new form framework
  72. $r = new ReflectionClass($class);
  73. if ('sfPropelAdminGenerator' == $class || $r->isSubclassOf(new ReflectionClass('sfPropelAdminGenerator')))
  74. {
  75. $data .= <<<EOF
  76. sfConfig::set('sf_compat_10', true);
  77. require sfConfig::get('sf_symfony_lib_dir').'/plugins/sfCompat10Plugin/config/config.php';
  78. EOF;
  79. }
  80. $data .= $generatorManager->generate($class, $parameters);
  81. return $data;
  82. }
  83. /**
  84. * @see sfConfigHandler
  85. */
  86. static public function getConfiguration(array $configFiles)
  87. {
  88. return self::parseYamls($configFiles);
  89. }
  90. }