PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/oiclient/data/symfony/plugins/sfPropelPlugin/lib/task/sfPropelGenerateCrudTask.class.php

http://openirudi.googlecode.com/
PHP | 165 lines | 101 code | 32 blank | 32 comment | 3 complexity | 1512153ad99c02836e4fc206452005d3 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  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. require_once(dirname(__FILE__).'/sfPropelBaseTask.class.php');
  10. /**
  11. * Generates a Propel CRUD module.
  12. *
  13. * @package symfony
  14. * @subpackage command
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. * @version SVN: $Id: sfPropelGenerateCrudTask.class.php 10512 2008-07-30 15:27:39Z nicolas $
  17. */
  18. class sfPropelGenerateCrudTask extends sfPropelBaseTask
  19. {
  20. /**
  21. * @see sfTask
  22. */
  23. protected function configure()
  24. {
  25. $this->addArguments(array(
  26. new sfCommandArgument('application', sfCommandArgument::REQUIRED, 'The application name'),
  27. new sfCommandArgument('module', sfCommandArgument::REQUIRED, 'The module name'),
  28. new sfCommandArgument('model', sfCommandArgument::REQUIRED, 'The model class name'),
  29. ));
  30. $this->addOptions(array(
  31. new sfCommandOption('theme', null, sfCommandOption::PARAMETER_REQUIRED, 'The theme name', 'default'),
  32. new sfCommandOption('generate-in-cache', null, sfCommandOption::PARAMETER_NONE, 'Generate the module in cache'),
  33. new sfCommandOption('non-atomic-actions', null, sfCommandOption::PARAMETER_NONE, 'Generate non atomic actions'),
  34. new sfCommandOption('non-verbose-templates', null, sfCommandOption::PARAMETER_NONE, 'Generate non verbose templates'),
  35. new sfCommandOption('with-show', null, sfCommandOption::PARAMETER_NONE, 'Generate a show method'),
  36. new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
  37. ));
  38. $this->aliases = array('propel-generate-crud');
  39. $this->namespace = 'propel';
  40. $this->name = 'generate-crud';
  41. $this->briefDescription = 'Generates a Propel CRUD module';
  42. $this->detailedDescription = <<<EOF
  43. The [propel:generate-crud|INFO] task generates a Propel CRUD module:
  44. [./symfony propel:generate-crud frontend article Article|INFO]
  45. The task creates a [%module%|COMMENT] module in the [%application%|COMMENT] application
  46. for the model class [%model%|COMMENT].
  47. You can also create an empty module that inherits its actions and templates from
  48. a runtime generated module in [%sf_app_cache_dir%/modules/auto%module%|COMMENT] by
  49. using the [--generate-in-cache|COMMENT] option:
  50. [./symfony propel:generate-crud --generate-in-cache frontend article Article|INFO]
  51. The generator can use a customized theme by using the [--theme|COMMENT] option:
  52. [./symfony propel:generate-crud --theme="custom" frontend article Article|INFO]
  53. This way, you can create your very own CRUD generator with your own conventions.
  54. EOF;
  55. }
  56. /**
  57. * @see sfTask
  58. */
  59. protected function execute($arguments = array(), $options = array())
  60. {
  61. $databaseManager = new sfDatabaseManager($this->configuration);
  62. $properties = parse_ini_file(sfConfig::get('sf_config_dir').'/properties.ini', true);
  63. $this->constants = array(
  64. 'PROJECT_NAME' => isset($properties['symfony']['name']) ? $properties['symfony']['name'] : 'symfony',
  65. 'APP_NAME' => $arguments['application'],
  66. 'MODULE_NAME' => $arguments['module'],
  67. 'MODEL_CLASS' => $arguments['model'],
  68. 'AUTHOR_NAME' => isset($properties['symfony']['author']) ? $properties['symfony']['author'] : 'Your name here',
  69. );
  70. $method = $options['generate-in-cache'] ? 'executeInit' : 'executeGenerate';
  71. $this->$method($arguments, $options);
  72. }
  73. protected function executeGenerate($arguments = array(), $options = array())
  74. {
  75. // generate module
  76. $tmpDir = sfConfig::get('sf_cache_dir').DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.md5(uniqid(rand(), true));
  77. sfConfig::set('sf_module_cache_dir', $tmpDir);
  78. $generatorManager = new sfGeneratorManager($this->configuration);
  79. $generatorManager->generate('sfPropelCrudGenerator', array(
  80. 'model_class' => $arguments['model'],
  81. 'moduleName' => $arguments['module'],
  82. 'theme' => $options['theme'],
  83. 'non_atomic_actions' => $options['non-atomic-actions'],
  84. 'non_verbose_templates' => $options['non-verbose-templates'],
  85. 'with_show' => $options['with-show'],
  86. ));
  87. $moduleDir = sfConfig::get('sf_app_module_dir').'/'.$arguments['module'];
  88. // copy our generated module
  89. $this->getFilesystem()->mirror($tmpDir.'/auto'.ucfirst($arguments['module']), $moduleDir, sfFinder::type('any'));
  90. if (!$options['with-show'])
  91. {
  92. $this->getFilesystem()->remove($moduleDir.'/templates/showSuccess.php');
  93. }
  94. // change module name
  95. $this->getFilesystem()->replaceTokens($moduleDir.'/actions/actions.class.php', '', '', array('auto'.ucfirst($arguments['module']) => $arguments['module']));
  96. // customize php and yml files
  97. $finder = sfFinder::type('file')->name('*.php', '*.yml');
  98. $this->getFilesystem()->replaceTokens($finder->in($moduleDir), '##', '##', $this->constants);
  99. // create basic test
  100. $this->getFilesystem()->copy(sfConfig::get('sf_symfony_lib_dir').'/task/generator/skeleton/module/test/actionsTest.php', sfConfig::get('sf_test_dir').'/functional/'.$arguments['application'].'/'.$arguments['module'].'ActionsTest.php');
  101. // customize test file
  102. $this->getFilesystem()->replaceTokens(sfConfig::get('sf_test_dir').'/functional/'.$arguments['application'].DIRECTORY_SEPARATOR.$arguments['module'].'ActionsTest.php', '##', '##', $this->constants);
  103. // delete temp files
  104. $this->getFilesystem()->remove(sfFinder::type('any')->in($tmpDir));
  105. }
  106. protected function executeInit($arguments = array(), $options = array())
  107. {
  108. $moduleDir = sfConfig::get('sf_app_module_dir').'/'.$arguments['module'];
  109. // create basic application structure
  110. $finder = sfFinder::type('any')->discard('.sf');
  111. $dirs = $this->configuration->getGeneratorSkeletonDirs('sfPropelCrud', $options['theme']);
  112. foreach ($dirs as $dir)
  113. {
  114. if (is_dir($dir))
  115. {
  116. $this->getFilesystem()->mirror($dir, $moduleDir, $finder);
  117. break;
  118. }
  119. }
  120. // create basic test
  121. $this->getFilesystem()->copy(sfConfig::get('sf_symfony_lib_dir').'/task/generator/skeleton/module/test/actionsTest.php', sfConfig::get('sf_test_dir').'/functional/'.$arguments['application'].'/'.$arguments['module'].'ActionsTest.php');
  122. // customize test file
  123. $this->getFilesystem()->replaceTokens(sfConfig::get('sf_test_dir').'/functional/'.$arguments['application'].DIRECTORY_SEPARATOR.$arguments['module'].'ActionsTest.php', '##', '##', $this->constants);
  124. // customize php and yml files
  125. $finder = sfFinder::type('file')->name('*.php', '*.yml');
  126. $this->constants['CONFIG'] = sprintf(" non_atomic_actions: %s\n non_verbose_templates: %s\n with_show: %s",
  127. $options['non-atomic-actions'] ? 'true' : 'false',
  128. $options['non-verbose-templates'] ? 'true' : 'false',
  129. $options['with-show'] ? 'true' : 'false'
  130. );
  131. $this->getFilesystem()->replaceTokens($finder->in($moduleDir), '##', '##', $this->constants);
  132. }
  133. }