PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/task/sfPropelGenerateModuleTask.class.php

https://github.com/mregis/procine
PHP | 226 lines | 153 code | 38 blank | 35 comment | 7 complexity | aba8effded22083f9a31bfcd16d6a975 MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-3.0, ISC
  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 module.
  12. *
  13. * @package symfony
  14. * @subpackage propel
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. * @version SVN: $Id: sfPropelGenerateModuleTask.class.php 23947 2009-11-14 20:02:28Z FabianLange $
  17. */
  18. class sfPropelGenerateModuleTask 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-verbose-templates', null, sfCommandOption::PARAMETER_NONE, 'Generate non verbose templates'),
  34. new sfCommandOption('with-show', null, sfCommandOption::PARAMETER_NONE, 'Generate a show method'),
  35. new sfCommandOption('singular', null, sfCommandOption::PARAMETER_REQUIRED, 'The singular name', null),
  36. new sfCommandOption('plural', null, sfCommandOption::PARAMETER_REQUIRED, 'The plural name', null),
  37. new sfCommandOption('route-prefix', null, sfCommandOption::PARAMETER_REQUIRED, 'The route prefix', null),
  38. new sfCommandOption('with-propel-route', null, sfCommandOption::PARAMETER_NONE, 'Whether you will use a Propel route'),
  39. new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
  40. new sfCommandOption('actions-base-class', null, sfCommandOption::PARAMETER_REQUIRED, 'The base class for the actions', 'sfActions'),
  41. ));
  42. $this->namespace = 'propel';
  43. $this->name = 'generate-module';
  44. $this->briefDescription = 'Generates a Propel module';
  45. $this->detailedDescription = <<<EOF
  46. The [propel:generate-module|INFO] task generates a Propel module:
  47. [./symfony propel:generate-module frontend article Article|INFO]
  48. The task creates a [%module%|COMMENT] module in the [%application%|COMMENT] application
  49. for the model class [%model%|COMMENT].
  50. You can also create an empty module that inherits its actions and templates from
  51. a runtime generated module in [%sf_app_cache_dir%/modules/auto%module%|COMMENT] by
  52. using the [--generate-in-cache|COMMENT] option:
  53. [./symfony propel:generate-module --generate-in-cache frontend article Article|INFO]
  54. The generator can use a customized theme by using the [--theme|COMMENT] option:
  55. [./symfony propel:generate-module --theme="custom" frontend article Article|INFO]
  56. This way, you can create your very own module generator with your own conventions.
  57. You can also change the default actions base class (default to sfActions) of
  58. the generated modules:
  59. [./symfony propel:generate-module --actions-base-class="ProjectActions" frontend article Article|INFO]
  60. EOF;
  61. }
  62. /**
  63. * @see sfTask
  64. */
  65. protected function execute($arguments = array(), $options = array())
  66. {
  67. $databaseManager = new sfDatabaseManager($this->configuration);
  68. $properties = parse_ini_file(sfConfig::get('sf_config_dir').'/properties.ini', true);
  69. $this->constants = array(
  70. 'PROJECT_NAME' => isset($properties['symfony']['name']) ? $properties['symfony']['name'] : 'symfony',
  71. 'APP_NAME' => $arguments['application'],
  72. 'MODULE_NAME' => $arguments['module'],
  73. 'UC_MODULE_NAME' => ucfirst($arguments['module']),
  74. 'MODEL_CLASS' => $arguments['model'],
  75. 'AUTHOR_NAME' => isset($properties['symfony']['author']) ? $properties['symfony']['author'] : 'Your name here',
  76. );
  77. $method = $options['generate-in-cache'] ? 'executeInit' : 'executeGenerate';
  78. // for backwarads compatibility symfony uses the model name as singular and plural form if none specified (#5640)
  79. $options['singular'] = $options['singular'] ? $options['singular'] : $arguments['model'];
  80. $options['plural'] = $options['plural'] ? $options['plural'] : $arguments['model'].'s';
  81. $this->$method($arguments, $options);
  82. }
  83. protected function executeGenerate($arguments = array(), $options = array())
  84. {
  85. // generate module
  86. $tmpDir = sfConfig::get('sf_cache_dir').DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.md5(uniqid(rand(), true));
  87. $generatorManager = new sfGeneratorManager($this->configuration, $tmpDir);
  88. $generatorManager->generate('sfPropelGenerator', array(
  89. 'model_class' => $arguments['model'],
  90. 'moduleName' => $arguments['module'],
  91. 'theme' => $options['theme'],
  92. 'non_verbose_templates' => $options['non-verbose-templates'],
  93. 'with_show' => $options['with-show'],
  94. 'singular' => $options['singular'],
  95. 'plural' => $options['plural'],
  96. 'route_prefix' => $options['route-prefix'],
  97. 'with_propel_route' => $options['with-propel-route'],
  98. 'actions_base_class' => $options['actions-base-class'],
  99. ));
  100. $moduleDir = sfConfig::get('sf_app_module_dir').'/'.$arguments['module'];
  101. // copy our generated module
  102. $this->getFilesystem()->mirror($tmpDir.DIRECTORY_SEPARATOR.'auto'.ucfirst($arguments['module']), $moduleDir, sfFinder::type('any'));
  103. if (!$options['with-show'])
  104. {
  105. $this->getFilesystem()->remove($moduleDir.'/templates/showSuccess.php');
  106. }
  107. // change module name
  108. $finder = sfFinder::type('file')->name('*.php');
  109. $this->getFilesystem()->replaceTokens($finder->in($moduleDir), '', '', array('auto'.ucfirst($arguments['module']) => $arguments['module']));
  110. // customize php and yml files
  111. $finder = sfFinder::type('file')->name('*.php', '*.yml');
  112. $this->getFilesystem()->replaceTokens($finder->in($moduleDir), '##', '##', $this->constants);
  113. // create basic test
  114. $this->getFilesystem()->copy(sfConfig::get('sf_symfony_lib_dir').DIRECTORY_SEPARATOR.'task'.DIRECTORY_SEPARATOR.'generator'.DIRECTORY_SEPARATOR.'skeleton'.DIRECTORY_SEPARATOR.'module'.DIRECTORY_SEPARATOR.'test'.DIRECTORY_SEPARATOR.'actionsTest.php', sfConfig::get('sf_test_dir').DIRECTORY_SEPARATOR.'functional'.DIRECTORY_SEPARATOR.$arguments['application'].DIRECTORY_SEPARATOR.$arguments['module'].'ActionsTest.php');
  115. // customize test file
  116. $this->getFilesystem()->replaceTokens(sfConfig::get('sf_test_dir').DIRECTORY_SEPARATOR.'functional'.DIRECTORY_SEPARATOR.$arguments['application'].DIRECTORY_SEPARATOR.$arguments['module'].'ActionsTest.php', '##', '##', $this->constants);
  117. // delete temp files
  118. $this->getFilesystem()->remove(sfFinder::type('any')->in($tmpDir));
  119. }
  120. protected function executeInit($arguments = array(), $options = array())
  121. {
  122. $moduleDir = sfConfig::get('sf_app_module_dir').'/'.$arguments['module'];
  123. // create basic application structure
  124. $finder = sfFinder::type('any')->discard('.sf');
  125. $dirs = $this->configuration->getGeneratorSkeletonDirs('sfPropelModule', $options['theme']);
  126. foreach ($dirs as $dir)
  127. {
  128. if (is_dir($dir))
  129. {
  130. $this->getFilesystem()->mirror($dir, $moduleDir, $finder);
  131. break;
  132. }
  133. }
  134. // move configuration file
  135. if (file_exists($config = $moduleDir.'/lib/configuration.php'))
  136. {
  137. if (file_exists($target = $moduleDir.'/lib/'.$arguments['module'].'GeneratorConfiguration.class.php'))
  138. {
  139. $this->getFilesystem()->remove($config);
  140. }
  141. else
  142. {
  143. $this->getFilesystem()->rename($config, $target);
  144. }
  145. }
  146. // move helper file
  147. if (file_exists($config = $moduleDir.'/lib/helper.php'))
  148. {
  149. if (file_exists($target = $moduleDir.'/lib/'.$arguments['module'].'GeneratorHelper.class.php'))
  150. {
  151. $this->getFilesystem()->remove($config);
  152. }
  153. else
  154. {
  155. $this->getFilesystem()->rename($config, $target);
  156. }
  157. }
  158. // create basic test
  159. $this->getFilesystem()->copy(sfConfig::get('sf_symfony_lib_dir').DIRECTORY_SEPARATOR.'task'.DIRECTORY_SEPARATOR.'generator'.DIRECTORY_SEPARATOR.'skeleton'.DIRECTORY_SEPARATOR.'module'.DIRECTORY_SEPARATOR.'test'.DIRECTORY_SEPARATOR.'actionsTest.php', sfConfig::get('sf_test_dir').DIRECTORY_SEPARATOR.'functional'.DIRECTORY_SEPARATOR.$arguments['application'].DIRECTORY_SEPARATOR.$arguments['module'].'ActionsTest.php');
  160. // customize test file
  161. $this->getFilesystem()->replaceTokens(sfConfig::get('sf_test_dir').DIRECTORY_SEPARATOR.'functional'.DIRECTORY_SEPARATOR.$arguments['application'].DIRECTORY_SEPARATOR.$arguments['module'].'ActionsTest.php', '##', '##', $this->constants);
  162. // customize php and yml files
  163. $finder = sfFinder::type('file')->name('*.php', '*.yml');
  164. $this->constants['CONFIG'] = sprintf(<<<EOF
  165. model_class: %s
  166. theme: %s
  167. non_verbose_templates: %s
  168. with_show: %s
  169. singular: %s
  170. plural: %s
  171. route_prefix: %s
  172. with_propel_route: %s
  173. actions_base_class: %s
  174. EOF
  175. ,
  176. $arguments['model'],
  177. $options['theme'],
  178. $options['non-verbose-templates'] ? 'true' : 'false',
  179. $options['with-show'] ? 'true' : 'false',
  180. $options['singular'] ? $options['singular'] : '~',
  181. $options['plural'] ? $options['plural'] : '~',
  182. $options['route-prefix'] ? $options['route-prefix'] : '~',
  183. $options['with-propel-route'] ? $options['with-propel-route'] : 'false',
  184. $options['actions-base-class']
  185. );
  186. $this->getFilesystem()->replaceTokens($finder->in($moduleDir), '##', '##', $this->constants);
  187. }
  188. }