1. sfGenerateModuleTask.class.php
  2. /** * Generates a new module. * * @package symfony * @subpackage task * @author Fabien Potencier * @version SVN: $Id: sfGenerateModuleTask.class.php 23922 2009-11-14 14:58:38Z fabien $ */
  3. class sfGenerateModuleTask extends sfGeneratorBaseTask
  4. {
  5. /**
  6. * @see sfTask
  7. */
  8. protected function configure()
  9. {
  10. $this->addArguments(array(
  11. new sfCommandArgument('application', sfCommandArgument::REQUIRED, 'The application name'),
  12. new sfCommandArgument('module', sfCommandArgument::REQUIRED, 'The module name'),
  13. ));
  14. $this->namespace = 'generate';
  15. $this->name = 'module';
  16. $this->briefDescription = 'Generates a new module';
  17. $this->detailedDescription = <<<EOF
  18. The [generate:module|INFO] task creates the basic directory structure
  19. for a new module in an existing application:
  20. [./symfony generate:module frontend article|INFO]
  21. The task can also change the author name found in the [actions.class.php|COMMENT]
  22. if you have configure it in [config/properties.ini|COMMENT]:
  23. [[symfony]
  24. name=blog
  25. author=Fabien Potencier <fabien.potencier@sensio.com>|INFO]
  26. You can customize the default skeleton used by the task by creating a
  27. [%sf_data_dir%/skeleton/module|COMMENT] directory.
  28. The task also creates a functional test stub named
  29. [%sf_test_dir%/functional/%application%/%module%ActionsTest.class.php|COMMENT]
  30. that does not pass by default.
  31. If a module with the same name already exists in the application,
  32. it throws a [sfCommandException|COMMENT].
  33. EOF;
  34. }
  35. /**
  36. * @see sfTask
  37. */
  38. protected function execute($arguments = array(), $options = array())
  39. {
  40. $app = $arguments['application'];
  41. $module = $arguments['module'];
  42. // Validate the module name
  43. if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $module))
  44. {
  45. throw new sfCommandException(sprintf('The module name "%s" is invalid.', $module));
  46. }
  47. $moduleDir = sfConfig::get('sf_app_module_dir').'/'.$module;
  48. if (is_dir($moduleDir))
  49. {
  50. throw new sfCommandException(sprintf('The module "%s" already exists in the "%s" application.', $moduleDir, $app));
  51. }
  52. $properties = parse_ini_file(sfConfig::get('sf_config_dir').'/properties.ini', true);
  53. $constants = array(
  54. 'PROJECT_NAME' => isset($properties['symfony']['name']) ? $properties['symfony']['name'] : 'symfony',
  55. 'APP_NAME' => $app,
  56. 'MODULE_NAME' => $module,
  57. 'AUTHOR_NAME' => isset($properties['symfony']['author']) ? $properties['symfony']['author'] : 'Your name here',
  58. );
  59. if (is_readable(sfConfig::get('sf_data_dir').'/skeleton/module'))
  60. {
  61. $skeletonDir = sfConfig::get('sf_data_dir').'/skeleton/module';
  62. }
  63. else
  64. {
  65. $skeletonDir = dirname(__FILE__).'/skeleton/module';
  66. }
  67. // create basic application structure
  68. $finder = sfFinder::type('any')->discard('.sf');
  69. $this->getFilesystem()->mirror($skeletonDir.'/module', $moduleDir, $finder);
  70. // create basic test
  71. $this->getFilesystem()->copy($skeletonDir.'/test/actionsTest.php', sfConfig::get('sf_test_dir').'/functional/'.$app.'/'.$module.'ActionsTest.php');
  72. // customize test file
  73. $this->getFilesystem()->replaceTokens(sfConfig::get('sf_test_dir').'/functional/'.$app.DIRECTORY_SEPARATOR.$module.'ActionsTest.php', '##', '##', $constants);
  74. // customize php and yml files
  75. $finder = sfFinder::type('file')->name('*.php', '*.yml');
  76. $this->getFilesystem()->replaceTokens($finder->in($moduleDir), '##', '##', $constants);
  77. }
  78. }

Debug toolbar