PageRenderTime 24ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/task/dinDoctrineGenerateModuleTask.class.php

https://github.com/relo-san/dinDoctrineExtraPlugin
PHP | 284 lines | 173 code | 47 blank | 64 comment | 8 complexity | 25bb5ea0f371e29a7575ea2f51f054c2 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the dinDoctrineExtraPlugin package.
  4. * (c) DineCat, 2010 http://dinecat.com/
  5. *
  6. * For the full copyright and license information, please view the LICENSE file,
  7. * that was distributed with this package, or see http://www.dinecat.com/din/license.html
  8. */
  9. require_once sfConfig::get( 'sf_symfony_lib_dir' ) . '/plugins/sfDoctrinePlugin/lib/task/sfDoctrineBaseTask.class.php';
  10. /**
  11. * Generate doctrine module
  12. *
  13. * @package dinDoctrineExtraPlugin.lib.task
  14. * @signed 5
  15. * @signer relo_san
  16. * @author relo_san [http://relo-san.com/]
  17. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  18. * @since february 26, 2010
  19. * @version SVN: $Id: dinDoctrineGenerateModuleTask.class.php 49 2010-07-01 18:29:15Z relo_san $
  20. */
  21. class dinDoctrineGenerateModuleTask extends sfDoctrineBaseTask
  22. {
  23. /**
  24. * Configure task
  25. *
  26. * @return void
  27. * @author relo_san
  28. * @since february 26, 2010
  29. */
  30. protected function configure()
  31. {
  32. $this->addArguments( array(
  33. new sfCommandArgument( 'application', sfCommandArgument::REQUIRED, 'The application name' ),
  34. new sfCommandArgument( 'module', sfCommandArgument::REQUIRED, 'The module name' ),
  35. new sfCommandArgument( 'model', sfCommandArgument::REQUIRED, 'The model class name' )
  36. ) );
  37. $this->addOptions(array(
  38. new sfCommandOption( 'theme', null, sfCommandOption::PARAMETER_REQUIRED, 'The theme name', 'default' ),
  39. new sfCommandOption( 'generate-in-cache', null, sfCommandOption::PARAMETER_NONE, 'Generate the module in cache' ),
  40. new sfCommandOption( 'non-verbose-templates', null, sfCommandOption::PARAMETER_NONE, 'Generate non verbose templates' ),
  41. new sfCommandOption( 'with-show', null, sfCommandOption::PARAMETER_NONE, 'Generate a show method' ),
  42. new sfCommandOption( 'singular', null, sfCommandOption::PARAMETER_REQUIRED, 'The singular name', null ),
  43. new sfCommandOption( 'plural', null, sfCommandOption::PARAMETER_REQUIRED, 'The plural name', null ),
  44. new sfCommandOption( 'plugin', null, sfCommandOption::PARAMETER_REQUIRED, 'The plugin name', null ),
  45. new sfCommandOption( 'route-prefix', null, sfCommandOption::PARAMETER_REQUIRED, 'The route prefix', null ),
  46. new sfCommandOption( 'with-doctrine-route', null, sfCommandOption::PARAMETER_NONE, 'Whether you will use a Doctrine route' ),
  47. new sfCommandOption( 'env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev' ),
  48. new sfCommandOption( 'actions-base-class', null, sfCommandOption::PARAMETER_REQUIRED, 'The base class for the actions', 'sfActions' )
  49. ) );
  50. $this->namespace = 'doctrine-ext';
  51. $this->name = 'generate-module';
  52. $this->briefDescription = 'Generates a Doctrine module';
  53. $d[] = 'The [doctrine:generate-module|INFO] task generates a Doctrine module:';
  54. $d[] = '';
  55. $d[] = ' [./symfony doctrine:generate-module frontend article Article|INFO]';
  56. $d[] = '';
  57. $d[] = 'The task creates a [%module%|COMMENT] module in the [%application%|COMMENT] application';
  58. $d[] = 'for the model class [%model%|COMMENT].';
  59. $d[] = '';
  60. $d[] = 'You can also create an empty module that inherits its actions and templates from';
  61. $d[] = 'a runtime generated module in [%sf_app_cache_dir%/modules/auto%module%|COMMENT] by';
  62. $d[] = 'using the [--generate-in-cache|COMMENT] option:';
  63. $d[] = '';
  64. $d[] = ' [./symfony doctrine:generate-module --generate-in-cache frontend article Article|INFO]';
  65. $d[] = '';
  66. $d[] = 'The generator can use a customized theme by using the [--theme|COMMENT] option:';
  67. $d[] = '';
  68. $d[] = ' [./symfony doctrine:generate-module --theme="custom" frontend article Article|INFO]';
  69. $d[] = '';
  70. $d[] = 'This way, you can create your very own module generator with your own conventions.';
  71. $d[] = '';
  72. $d[] = 'You can also change the default actions base class (default to sfActions) of';
  73. $d[] = 'the generated modules:';
  74. $d[] = '';
  75. $d[] = ' [./symfony doctrine:generate-module --actions-base-class="ProjectActions" frontend article Article|INFO]';
  76. $this->detailedDescription = implode( "\n", $d );
  77. } // dinDoctrineGenerateModuleTask::configure()
  78. /**
  79. * Execute task
  80. *
  81. * @param array $arguments Task arguments [optional]
  82. * @param array $options Task options [optional]
  83. * @return void
  84. * @author relo_san
  85. * @since february 20, 2010
  86. */
  87. protected function execute( $arguments = array(), $options = array() )
  88. {
  89. $databaseManager = new sfDatabaseManager( $this->configuration );
  90. $properties = parse_ini_file(sfConfig::get('sf_config_dir').'/properties.ini', true);
  91. $this->constants = array(
  92. 'PROJECT_NAME' => dinGeneratorSigner::getProjectName(),
  93. 'APP_NAME' => $arguments['application'],
  94. 'MODULE_NAME' => $arguments['module'],
  95. 'UC_MODULE_NAME' => ucfirst( $arguments['module'] ),
  96. 'MODEL_CLASS' => $arguments['model'],
  97. 'PLUGIN_NAME' => $options['plugin'],
  98. 'PHP' => '<?php',
  99. 'AUTHOR' => dinGeneratorSigner::getAuthor(),
  100. 'DATE' => strtolower( date( 'F d, Y' ) ),
  101. 'AUTHOR_NAME' => dinGeneratorSigner::getAuthor(),
  102. );
  103. $method = $options['generate-in-cache'] ? 'executeInit' : 'executeGenerate';
  104. $this->$method( $arguments, $options );
  105. } // dinDoctrineGenerateModuleTask::execute()
  106. protected function executeGenerate( $arguments = array(), $options = array() )
  107. {
  108. // generate module
  109. $tmpDir = sfConfig::get('sf_cache_dir') . DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.md5(uniqid(rand(), true));
  110. $generatorManager = new sfGeneratorManager($this->configuration, $tmpDir);
  111. $generatorManager->generate( 'sfDoctrineGenerator', array(
  112. 'model_class' => $arguments['model'],
  113. 'moduleName' => $arguments['module'],
  114. 'theme' => $options['theme'],
  115. 'non_verbose_templates' => $options['non-verbose-templates'],
  116. 'with_show' => $options['with-show'],
  117. 'singular' => $options['singular'] ? $options['singular'] : sfInflector::underscore($arguments['model']),
  118. 'plural' => $options['plural'] ? $options['plural'] : sfInflector::underscore($arguments['model'].'s'),
  119. 'route_prefix' => $options['route-prefix'],
  120. 'with_doctrine_route' => $options['with-doctrine-route'],
  121. 'actions_base_class' => $options['actions-base-class'],
  122. ) );
  123. $moduleDir = $options['plugin'] ? sfConfig::get( 'sf_plugins_dir' ) . '/' . $options['plugin'] . '/modules/' . $arguments['module'] : sfConfig::get( 'sf_app_module_dir' ) . '/' . $arguments['module'];
  124. // copy our generated module
  125. $this->getFilesystem()->mirror( $tmpDir . DIRECTORY_SEPARATOR . 'auto' . ucfirst( $arguments['module'] ), $moduleDir, sfFinder::type( 'any' ) );
  126. if ( !$options['with-show'] )
  127. {
  128. $this->getFilesystem()->remove( $moduleDir . '/templates/showSuccess.php' );
  129. }
  130. // create module definition
  131. //$this->getFilesystem()->copy( $moduleDir . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'moduleDefinition.php', $moduleDir . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'Plugin' . ucfirst( $arguments['module'] ) . 'ModuleDefinition.class.php' );
  132. //$this->getFilesystem()->remove( $moduleDir . '/config/moduleDefinition.php' );
  133. // change module name
  134. $finder = sfFinder::type( 'file' )->name( '*.php' );
  135. $this->getFilesystem()->replaceTokens( $finder->in( $moduleDir ), '', '', array( 'auto' . ucfirst( $arguments['module'] ) => $arguments['module'] ) );
  136. // customize php and yml files
  137. $finder = sfFinder::type( 'file' )->name( '*.php', '*.yml' );
  138. $this->getFilesystem()->replaceTokens( $finder->in( $moduleDir ), '##', '##', $this->constants );
  139. // create basic test
  140. $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' );
  141. // customize test file
  142. $this->getFilesystem()->replaceTokens( sfConfig::get( 'sf_test_dir' ) . DIRECTORY_SEPARATOR . 'functional' . DIRECTORY_SEPARATOR . $arguments['application'] . DIRECTORY_SEPARATOR . $arguments['module'] . 'ActionsTest.php', '##', '##', $this->constants );
  143. // delete temp files
  144. $this->getFilesystem()->remove( sfFinder::type( 'any' )->in( $tmpDir ) );
  145. } // dinDoctrineGenerateModuleTask::executeGenerate()
  146. protected function executeInit( $arguments = array(), $options = array() )
  147. {
  148. $moduleDir = $options['plugin'] ? sfConfig::get( 'sf_plugins_dir' ) . '/' . $options['plugin'] . '/modules/' . $arguments['module'] : sfConfig::get( 'sf_app_module_dir' ) . '/' . $arguments['module'];
  149. // create basic application structure
  150. $finder = sfFinder::type( 'any' )->discard( '.sf' );
  151. $dirs = $this->configuration->getGeneratorSkeletonDirs( 'sfDoctrineModule', $options['theme'] );
  152. foreach ( $dirs as $dir )
  153. {
  154. if ( is_dir( $dir ) )
  155. {
  156. $this->getFilesystem()->mirror( $dir, $moduleDir, $finder );
  157. break;
  158. }
  159. }
  160. // move configuration file
  161. if ( file_exists( $config = $moduleDir . '/lib/configuration.php' ) )
  162. {
  163. if ( file_exists( $target = $moduleDir . '/lib/' . $arguments['module'] . 'GeneratorConfiguration.php' ) )
  164. {
  165. $this->getFilesystem()->remove( $config );
  166. }
  167. else
  168. {
  169. $this->getFilesystem()->rename( $config, $target );
  170. }
  171. }
  172. // move helper file
  173. if ( file_exists( $config = $moduleDir . '/lib/helper.php' ) )
  174. {
  175. if ( file_exists( $target = $moduleDir . '/lib/' . $arguments['module'] . 'GeneratorHelper.php' ) )
  176. {
  177. $this->getFilesystem()->remove( $config );
  178. }
  179. else
  180. {
  181. $this->getFilesystem()->rename( $config, $target );
  182. }
  183. }
  184. // create module definition
  185. //if ( file_exists( $config = $moduleDir . '/config/moduleDefinition.php' ) )
  186. //{
  187. // if ( file_exists( $target = $moduleDir . '/config/Plugin' . ucfirst( $arguments['module'] ) . 'ModuleDefinition.class.php' ) )
  188. // {
  189. // $this->getFilesystem()->remove( $config );
  190. // }
  191. // else
  192. // {
  193. // $this->getFilesystem()->rename( $config, $target );
  194. // }
  195. //}
  196. // create lang file
  197. if ( file_exists( $config = $moduleDir . '/i18n/lang.ru.xml' ) )
  198. {
  199. if ( file_exists( $target = $moduleDir . '/i18n/' . $arguments['module'] . '.ru.xml' ) )
  200. {
  201. $this->getFilesystem()->remove( $config );
  202. }
  203. else
  204. {
  205. $this->getFilesystem()->rename( $config, $target );
  206. }
  207. }
  208. // create basic test
  209. $this->getFilesystem()->copy(
  210. sfConfig::get( 'sf_symfony_lib_dir' ) . DIRECTORY_SEPARATOR . 'task'
  211. . DIRECTORY_SEPARATOR . 'generator' . DIRECTORY_SEPARATOR . 'skeleton'
  212. . DIRECTORY_SEPARATOR . 'module' . DIRECTORY_SEPARATOR . 'test' . DIRECTORY_SEPARATOR
  213. . 'actionsTest.php',
  214. sfConfig::get( 'sf_test_dir' ) . DIRECTORY_SEPARATOR . 'functional'
  215. . DIRECTORY_SEPARATOR . $arguments['application'] . DIRECTORY_SEPARATOR
  216. . $arguments['module'] . 'ActionsTest.php'
  217. );
  218. // customize test file
  219. $this->getFilesystem()->replaceTokens(
  220. sfConfig::get( 'sf_test_dir' ) . DIRECTORY_SEPARATOR . 'functional'
  221. . DIRECTORY_SEPARATOR . $arguments['application'] . DIRECTORY_SEPARATOR
  222. . $arguments['module'] . 'ActionsTest.php', '##', '##', $this->constants
  223. );
  224. // customize php and yml files
  225. $finder = sfFinder::type( 'file' )->name( '*.php', '*.yml', '*.xml' );
  226. $this->constants['WITH_SHOW'] = $options['with-show'] ? 'true' : 'false';
  227. $this->constants['ROUTE_PREFIX'] = $options['route-prefix'] ? "'" . $options['route-prefix'] . "'" : 'null';
  228. $this->constants['SINGULAR'] = $options['singular'] ? "'" . $options['singular'] . "'" : 'null';
  229. $this->constants['PLURAL'] = $options['plural'] ? "'" . $options['plural'] . "'" : 'null';
  230. $this->constants['THEME'] = $options['theme'];
  231. $this->constants['VERBOSE'] = $options['non-verbose-templates'] ? 'true' : 'false';
  232. $this->constants['WITH_ROUTE'] = $options['with-doctrine-route'] ? 'true' : 'false';
  233. $this->constants['BASE_ACTION'] = $options['actions-base-class'];
  234. $this->getFilesystem()->replaceTokens( $finder->in( $moduleDir ), '##', '##', $this->constants );
  235. } // dinDoctrineGenerateModuleTask
  236. } // dinDoctrineGenerateModuleTask
  237. //EOF