sfGenerateModuleTask.class.php 3.9 KB

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