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
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
 
11
/**
12
 * Configures the database connection.
13
 *
14
 * @package    symfony
15
 * @subpackage task
16
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17
 * @version    SVN: $Id: sfPropelConfigureDatabaseTask.class.php 21908 2009-09-11 12:06:21Z fabien $
18
 */
19
class sfPropelConfigureDatabaseTask extends sfBaseTask
20
{
21
  /**
22
   * @see sfTask
23
   */
24
  protected function configure()
25
  {
26
    $this->addArguments(array(
27
      new sfCommandArgument('dsn', sfCommandArgument::REQUIRED, 'The database dsn'),
28
      new sfCommandArgument('username', sfCommandArgument::OPTIONAL, 'The database username', 'root'),
29
      new sfCommandArgument('password', sfCommandArgument::OPTIONAL, 'The database password'),
30
    ));
31
 
32
    $this->addOptions(array(
33
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_OPTIONAL, 'The environment', 'all'),
34
      new sfCommandOption('name', null, sfCommandOption::PARAMETER_OPTIONAL, 'The connection name', 'propel'),
35
      new sfCommandOption('class', null, sfCommandOption::PARAMETER_OPTIONAL, 'The database class name', 'sfPropelDatabase'),
36
      new sfCommandOption('app', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', null),
37
    ));
38
 
39
    $this->namespace = 'configure';
40
    $this->name = 'database';
41
 
42
    $this->briefDescription = 'Configure database DSN';
43
 
44
    $this->detailedDescription = <<<EOF
45
The [configure:database|INFO] task configures the database DSN
46
for a project:
47
 
48
  [./symfony configure:database mysql:host=localhost;dbname=example root mYsEcret|INFO]
49
 
50
By default, the task change the configuration for all environment. If you want
51
to change the dsn for a specific environment, use the [env|COMMENT] option:
52
 
53
  [./symfony configure:database --env=dev mysql:host=localhost;dbname=example_dev root mYsEcret|INFO]
54
 
55
To change the configuration for a specific application, use the [app|COMMENT] option:
56
 
57
  [./symfony configure:database --app=frontend mysql:host=localhost;dbname=example root mYsEcret|INFO]
58
 
59
You can also specify the connection name and the database class name:
60
 
61
  [./symfony configure:database --name=main --class=ProjectDatabase mysql:host=localhost;dbname=example root mYsEcret|INFO]
62
 
63
WARNING: The [propel.ini|COMMENT] file is also updated when you use a [Propel|COMMENT] database
64
and configure for [all|COMMENT] environments with no [app|COMMENT].
65
EOF;
66
  }
67
 
68
  /**
69
   * @see sfTask
70
   */
71
  protected function execute($arguments = array(), $options = array())
72
  {
73
    // update databases.yml
74
    if (null !== $options['app'])
75
    {
76
      $file = sfConfig::get('sf_apps_dir').'/'.$options['app'].'/config/databases.yml';
77
    }
78
    else
79
    {
80
      $file = sfConfig::get('sf_config_dir').'/databases.yml';
81
    }
82
 
83
    $config = file_exists($file) ? sfYaml::load($file) : array();
84
 
85
    $config[$options['env']][$options['name']] = array(
86
      'class' => $options['class'],
87
      'param' => array_merge(isset($config[$options['env']][$options['name']]['param']) ? $config[$options['env']][$options['name']]['param'] : array(), array('dsn' => $arguments['dsn'], 'username' => $arguments['username'], 'password' => $arguments['password'])),
88
    );
89
 
90
    file_put_contents($file, sfYaml::dump($config, 4));
91
 
92
    // update propel.ini
93
    if (
94
      null === $options['app'] &&
95
      false !== strpos($options['class'], 'Propel') &&
96
      'all' == $options['env']
97
    )
98
    {
99
      $propelini = sfConfig::get('sf_config_dir').'/propel.ini';
100
      if (file_exists($propelini))
101
      {
102
        $content = file_get_contents($propelini);
103
        if (preg_match('/^(.+?):/', $arguments['dsn'], $match))
104
        {
105
          $content = preg_replace('/^propel\.database(\s*)=(\s*)(.+?)$/m', 'propel.database$1=${2}'.$match[1], $content);
106
          $content = preg_replace('/^propel\.database.driver(\s*)=(\s*)(.+?)$/m', 'propel.database.driver$1=${2}'.$match[1], $content);
107
          $content = preg_replace('/^propel\.database\.createUrl(\s*)=(\s*)(.+?)$/m', 'propel.database.createUrl$1=${2}'.$arguments['dsn'], $content);
108
          $content = preg_replace('/^propel\.database\.url(\s*)=(\s*)(.+?)$/m', 'propel.database.url$1=${2}'.$arguments['dsn'], $content);
109
 
110
          $content = preg_replace('/^propel\.database\.user(\s*)=(\s*)(.+?)$/m', 'propel.database.user$1=${2}'.$arguments['username'], $content);
111
          $content = preg_replace('/^propel\.database\.password(\s*)=(\s*)(.+?)$/m', 'propel.database.password$1=${2}'.$arguments['password'], $content);
112
 
113
          file_put_contents($propelini, $content);
114
        }
115
      }
116
    }
117
  }
118
}