sfGeneratorConfigHandler.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 13464 2008-11-28 15:45:07Z fabien $
  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'), self::getContent($generatorManager, $config['class'], $generatorParam));
  66. return $retval;
  67. }
  68. static public function getContent(sfGeneratorManager $generatorManager, $class, $parameters)
  69. {
  70. $data = '';
  71. // needed to maintain BC with the 1.0 admin generator
  72. $r = new ReflectionClass($class);
  73. if (
  74. (class_exists('sfPropelAdminGenerator') && ('sfPropelAdminGenerator' == $class || $r->isSubclassOf(new ReflectionClass('sfPropelAdminGenerator'))))
  75. ||
  76. (class_exists('sfDoctrineAdminGenerator') && ('sfDoctrineAdminGenerator' == $class || $r->isSubclassOf(new ReflectionClass('sfDoctrineAdminGenerator'))))
  77. )
  78. {
  79. $data .= "require sfConfig::get('sf_symfony_lib_dir').'/plugins/sfCompat10Plugin/config/config.php';\n";
  80. }
  81. $data .= $generatorManager->generate($class, $parameters);
  82. return $data;
  83. }
  84. /**
  85. * @see sfConfigHandler
  86. */
  87. static public function getConfiguration(array $configFiles)
  88. {
  89. return self::parseYamls($configFiles);
  90. }
  91. }