Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/*
4
 * This file is part of the symfony package.
5
 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6
 * (c) Jonathan H. Wage <jonwage@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
require_once(dirname(__FILE__).'/sfDoctrineBaseTask.class.php');
13
 
14
/**
15
 * Inserts SQL for current model.
16
 *
17
 * @package    symfony
18
 * @subpackage doctrine
19
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
20
 * @author     Jonathan H. Wage <jonwage@gmail.com>
21
 * @version    SVN: $Id: sfDoctrineGenerateMigrationTask.class.php 24390 2009-11-25 18:21:06Z Kris.Wallsmith $
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->namespace = 'doctrine';
41
    $this->name = 'generate-migration';
42
    $this->briefDescription = 'Generate migration class';
43
 
44
    $this->detailedDescription = <<<EOF
45
The [doctrine:generate-migration|INFO] task generates migration template
46
 
47
  [./symfony doctrine:generate-migration AddUserEmailColumn|INFO]
48
 
49
You can provide an [--editor-cmd|COMMENT] option to open the new migration class in your
50
editor of choice upon creation:
51
 
52
  [./symfony doctrine:generate-migration AddUserEmailColumn --editor-cmd=mate|INFO]
53
EOF;
54
  }
55
 
56
  /**
57
   * @see sfTask
58
   */
59
  protected function execute($arguments = array(), $options = array())
60
  {
61
    $databaseManager = new sfDatabaseManager($this->configuration);
62
    $config = $this->getCliConfig();
63
 
64
    $this->logSection('doctrine', sprintf('generating migration class named "%s"', $arguments['name']));
65
 
66
    if (!is_dir($config['migrations_path']))
67
    {
68
      $this->getFilesystem()->mkdirs($config['migrations_path']);
69
    }
70
 
71
    $this->callDoctrineCli('generate-migration', array('name' => $arguments['name']));
72
 
73
    $finder = sfFinder::type('file')->sort_by_name()->name('*.php');
74
    if ($files = $finder->in($config['migrations_path']))
75
    {
76
      $file = array_pop($files);
77
 
78
      $contents = file_get_contents($file);
79
      $contents = strtr(sfToolkit::stripComments($contents), array(
80
        "{\n\n" => "{\n",
81
        "\n}"   => "\n}\n",
82
        '    '  => '  ',
83
      ));
84
      file_put_contents($file, $contents);
85
 
86
      if (isset($options['editor-cmd']))
87
      {
88
        $this->getFilesystem()->execute($options['editor-cmd'].' '.escapeshellarg($file));
89
      }
90
    }
91
  }
92
}