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