| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
require_once(dirname(__FILE__).'/sfDoctrineBaseTask.class.php'); |
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
class sfDoctrineGenerateMigrationTask extends sfDoctrineBaseTask |
|---|
| 24 |
{ |
|---|
| 25 |
|
|---|
| 26 |
* @see sfTask |
|---|
| 27 |
*/ |
|---|
| 28 |
protected function configure() |
|---|
| 29 |
{ |
|---|
| 30 |
$this->addArguments(array( |
|---|
| 31 |
new sfCommandArgument('name', sfCommandArgument::REQUIRED, 'The name of the migration'), |
|---|
| 32 |
)); |
|---|
| 33 |
|
|---|
| 34 |
$this->addOptions(array( |
|---|
| 35 |
new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', true), |
|---|
| 36 |
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'), |
|---|
| 37 |
new sfCommandOption('editor-cmd', null, sfCommandOption::PARAMETER_REQUIRED, 'Open script with this command upon creation'), |
|---|
| 38 |
)); |
|---|
| 39 |
|
|---|
| 40 |
$this->aliases = array('doctrine-generate-migration'); |
|---|
| 41 |
$this->namespace = 'doctrine'; |
|---|
| 42 |
$this->name = 'generate-migration'; |
|---|
| 43 |
$this->briefDescription = 'Generate migration class'; |
|---|
| 44 |
|
|---|
| 45 |
$this->detailedDescription = <<<EOF |
|---|
| 46 |
The [doctrine:generate-migration|INFO] task generates migration template |
|---|
| 47 |
|
|---|
| 48 |
[./symfony doctrine:generate-migration AddUserEmailColumn|INFO] |
|---|
| 49 |
|
|---|
| 50 |
You can provide an [--editor-cmd|COMMENT] option to open the new migration class in your |
|---|
| 51 |
editor of choice upon creation: |
|---|
| 52 |
|
|---|
| 53 |
[./symfony doctrine:generate-migration AddUserEmailColumn --editor-cmd=mate|INFO] |
|---|
| 54 |
EOF; |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
* @see sfTask |
|---|
| 59 |
*/ |
|---|
| 60 |
protected function execute($arguments = array(), $options = array()) |
|---|
| 61 |
{ |
|---|
| 62 |
$databaseManager = new sfDatabaseManager($this->configuration); |
|---|
| 63 |
$config = $this->getCliConfig(); |
|---|
| 64 |
|
|---|
| 65 |
$this->logSection('doctrine', sprintf('generating migration class named "%s"', $arguments['name'])); |
|---|
| 66 |
|
|---|
| 67 |
if (!is_dir($config['migrations_path'])) |
|---|
| 68 |
{ |
|---|
| 69 |
$this->getFilesystem()->mkdirs($config['migrations_path']); |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
$this->callDoctrineCli('generate-migration', array('name' => $arguments['name'])); |
|---|
| 73 |
|
|---|
| 74 |
$finder = sfFinder::type('file')->sort_by_name()->name('*.php'); |
|---|
| 75 |
if ($files = $finder->in($config['migrations_path'])) |
|---|
| 76 |
{ |
|---|
| 77 |
$file = array_pop($files); |
|---|
| 78 |
|
|---|
| 79 |
$contents = file_get_contents($file); |
|---|
| 80 |
$contents = strtr(sfToolkit::stripComments($contents), array( |
|---|
| 81 |
"{\n\n" => "{\n", |
|---|
| 82 |
"\n}" => "\n}\n", |
|---|
| 83 |
' ' => ' ', |
|---|
| 84 |
)); |
|---|
| 85 |
file_put_contents($file, $contents); |
|---|
| 86 |
|
|---|
| 87 |
if (isset($options['editor-cmd'])) |
|---|
| 88 |
{ |
|---|
| 89 |
$this->getFilesystem()->execute($options['editor-cmd'].' '.escapeshellarg($file)); |
|---|
| 90 |
} |
|---|
| 91 |
} |
|---|
| 92 |
} |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|