| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
class sfGenerateTaskTask extends sfBaseTask |
|---|
| 19 |
{ |
|---|
| 20 |
|
|---|
| 21 |
* @see sfTask |
|---|
| 22 |
*/ |
|---|
| 23 |
protected function configure() |
|---|
| 24 |
{ |
|---|
| 25 |
$this->addArguments(array( |
|---|
| 26 |
new sfCommandArgument('task_name', sfCommandArgument::REQUIRED, 'The task name (can contain namespace)'), |
|---|
| 27 |
)); |
|---|
| 28 |
|
|---|
| 29 |
$this->addOptions(array( |
|---|
| 30 |
new sfCommandOption('dir', null, sfCommandOption::PARAMETER_OPTIONAL, 'The directory to create the task in', 'lib/task'), |
|---|
| 31 |
new sfCommandOption('use-database', 'db', sfCommandOption::PARAMETER_OPTIONAL, 'Whether the task needs model initialization to access database', 'true'), |
|---|
| 32 |
new sfCommandOption('brief-description', 'bd', sfCommandOption::PARAMETER_OPTIONAL, 'A brief task description (appears in task list)', ''), |
|---|
| 33 |
)); |
|---|
| 34 |
|
|---|
| 35 |
$this->namespace = 'generate'; |
|---|
| 36 |
$this->name = 'task'; |
|---|
| 37 |
$this->briefDescription = 'Creates a skeleton class for a new task'; |
|---|
| 38 |
|
|---|
| 39 |
$this->detailedDescription = <<<EOF |
|---|
| 40 |
The [generate:task|INFO] creates a new Task class based on the name passed as argument: |
|---|
| 41 |
[./symfony generate:task namespace:name|INFO] |
|---|
| 42 |
|
|---|
| 43 |
The `fooBarTask.class.php` skeleton task is created under the `lib/task/` directory. Note that the namespace is optional. |
|---|
| 44 |
If you want to create the file in another directory (relative to the project root folder), pass it in the [dir|INFO] option: |
|---|
| 45 |
[./symfony generate:task namespace:name --dir=plugins/myPlugin/lib/task|INFO] |
|---|
| 46 |
|
|---|
| 47 |
If the task doesn't need database access, you can remove the database initialization code with the [use-database|INFO] option: |
|---|
| 48 |
[./symfony generate:task namespace:name --use-database=false|INFO] |
|---|
| 49 |
|
|---|
| 50 |
You can also specify a description: |
|---|
| 51 |
[./symfony generate:task namespace:name --briefDescription='Does interesting things' --detailedDescription='Usage tutorial'|INFO] |
|---|
| 52 |
EOF; |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
* @see sfTask |
|---|
| 57 |
*/ |
|---|
| 58 |
protected function execute($arguments = array(), $options = array()) |
|---|
| 59 |
{ |
|---|
| 60 |
$taskName = $arguments['task_name']; |
|---|
| 61 |
$taskNameComponents = split(':', $taskName); |
|---|
| 62 |
$namespace = isset($taskNameComponents[1]) ? $taskNameComponents[0] : ''; |
|---|
| 63 |
$name = isset($taskNameComponents[1]) ? $taskNameComponents[1] : $taskNameComponents[0]; |
|---|
| 64 |
$taskClassName = str_replace('-', '', ($namespace ? $namespace.ucfirst($name) : $name)).'Task'; |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $taskClassName)) |
|---|
| 68 |
{ |
|---|
| 69 |
throw new sfCommandException(sprintf('The task class name "%s" is invalid.', $taskClassName)); |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
$briefDescription = $options['brief-description']; |
|---|
| 73 |
$detailedDescription = <<<HED |
|---|
| 74 |
The [$taskName|INFO] task does things. |
|---|
| 75 |
Call it with: |
|---|
| 76 |
|
|---|
| 77 |
[php symfony $taskName|INFO] |
|---|
| 78 |
HED; |
|---|
| 79 |
|
|---|
| 80 |
if($options['use-database'] != 'true') |
|---|
| 81 |
{ |
|---|
| 82 |
$content = <<<HED |
|---|
| 83 |
<?php |
|---|
| 84 |
|
|---|
| 85 |
class $taskClassName extends sfPropelBaseTask |
|---|
| 86 |
{ |
|---|
| 87 |
protected function configure() |
|---|
| 88 |
{ |
|---|
| 89 |
\$this->namespace = '$namespace'; |
|---|
| 90 |
\$this->name = '$name'; |
|---|
| 91 |
\$this->briefDescription = '$briefDescription'; |
|---|
| 92 |
\$this->detailedDescription = <<<EOF |
|---|
| 93 |
$detailedDescription |
|---|
| 94 |
EOF; |
|---|
| 95 |
\$this->addArgument('application', sfCommandArgument::REQUIRED, 'The application name'); |
|---|
| 96 |
// add other arguments here |
|---|
| 97 |
\$this->addOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'); |
|---|
| 98 |
\$this->addOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'propel'); |
|---|
| 99 |
// add other options here |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
protected function execute(\$arguments = array(), \$options = array()) |
|---|
| 103 |
{ |
|---|
| 104 |
// Database initialization |
|---|
| 105 |
\$databaseManager = new sfDatabaseManager(\$this->configuration); |
|---|
| 106 |
\$connection = Propel::getConnection(\$options['connection'] ? \$options['connection'] : ''); |
|---|
| 107 |
// add code here |
|---|
| 108 |
|
|---|
| 109 |
} |
|---|
| 110 |
} |
|---|
| 111 |
HED; |
|---|
| 112 |
} |
|---|
| 113 |
else |
|---|
| 114 |
{ |
|---|
| 115 |
$content = <<<HED |
|---|
| 116 |
<?php |
|---|
| 117 |
|
|---|
| 118 |
class $taskClassName extends sfBaseTask |
|---|
| 119 |
{ |
|---|
| 120 |
protected function configure() |
|---|
| 121 |
{ |
|---|
| 122 |
\$this->namespace = '$namespace'; |
|---|
| 123 |
\$this->name = '$name'; |
|---|
| 124 |
\$this->briefDescription = '$briefDescription'; |
|---|
| 125 |
\$this->detailedDescription = <<<EOF |
|---|
| 126 |
$detailedDescription |
|---|
| 127 |
EOF; |
|---|
| 128 |
// add arguments here, like the following: |
|---|
| 129 |
//\$this->addArgument('application', sfCommandArgument::REQUIRED, 'The application name'); |
|---|
| 130 |
// add options here, like the following: |
|---|
| 131 |
//\$this->addOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'); |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
protected function execute(\$arguments = array(), \$options = array()) |
|---|
| 135 |
{ |
|---|
| 136 |
// add code here |
|---|
| 137 |
} |
|---|
| 138 |
} |
|---|
| 139 |
HED; |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
if (!is_readable(sfConfig::get('sf_root_dir').'/'.$options['dir'])) |
|---|
| 144 |
{ |
|---|
| 145 |
$this->getFilesystem()->mkdirs(str_replace('/', DIRECTORY_SEPARATOR, $options['dir'])); |
|---|
| 146 |
} |
|---|
| 147 |
|
|---|
| 148 |
$taskFile = sfConfig::get('sf_root_dir').'/'.$options['dir'].'/'.$taskClassName.'.class.php'; |
|---|
| 149 |
if (is_readable($taskFile)) |
|---|
| 150 |
{ |
|---|
| 151 |
throw new sfCommandException(sprintf('A "%s" task already exists in "%s".', $taskName, $taskFile)); |
|---|
| 152 |
} |
|---|
| 153 |
|
|---|
| 154 |
$this->logSection('task', sprintf('Creating "%s" task file', $taskFile)); |
|---|
| 155 |
file_put_contents($taskFile, $content); |
|---|
| 156 |
} |
|---|
| 157 |
} |
|---|
| 158 |
|
|---|