| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfConfigureDatabaseTask 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 |
)); |
|---|
| 29 |
|
|---|
| 30 |
$this->addOptions(array( |
|---|
| 31 |
new sfCommandOption('env', null, sfCommandOption::PARAMETER_OPTIONAL, 'The environment', 'all'), |
|---|
| 32 |
new sfCommandOption('name', null, sfCommandOption::PARAMETER_OPTIONAL, 'The connection name', 'propel'), |
|---|
| 33 |
new sfCommandOption('class', null, sfCommandOption::PARAMETER_OPTIONAL, 'The database class name', 'sfPropelDatabase'), |
|---|
| 34 |
new sfCommandOption('app', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', null), |
|---|
| 35 |
)); |
|---|
| 36 |
|
|---|
| 37 |
$this->namespace = 'configure'; |
|---|
| 38 |
$this->name = 'database'; |
|---|
| 39 |
|
|---|
| 40 |
$this->briefDescription = 'Configure database DSN'; |
|---|
| 41 |
|
|---|
| 42 |
$this->detailedDescription = <<<EOF |
|---|
| 43 |
The [configure:database|INFO] task configures the database DSN |
|---|
| 44 |
for a project: |
|---|
| 45 |
|
|---|
| 46 |
[./symfony configure:database mysql://root:mYsEcret@localhost/dbname|INFO] |
|---|
| 47 |
|
|---|
| 48 |
By default, the task change the configuration for all environment. If you want |
|---|
| 49 |
to change the dsn for a specific environment, use the [env|COMMENT] option: |
|---|
| 50 |
|
|---|
| 51 |
[./symfony configure:database --env=dev mysql://root@localhost/dbname_test|INFO] |
|---|
| 52 |
|
|---|
| 53 |
To change the configuration for a specific application, use the [app|COMMENT] option: |
|---|
| 54 |
|
|---|
| 55 |
[./symfony configure:database --app=frontend mysql://root@localhost/dbname|INFO] |
|---|
| 56 |
|
|---|
| 57 |
You can also specify the connection name and the database class name: |
|---|
| 58 |
|
|---|
| 59 |
[./symfony configure:database --name=main --class=sfDoctrineDatabase mysql://root@localhost/dbname|INFO] |
|---|
| 60 |
|
|---|
| 61 |
WARNING: The [propel.ini|COMMENT] file is also updated when you use a [Propel|COMMENT] database |
|---|
| 62 |
and configure for [all|COMMENT] environments with no [app|COMMENT]. |
|---|
| 63 |
EOF; |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
* @see sfTask |
|---|
| 68 |
*/ |
|---|
| 69 |
protected function execute($arguments = array(), $options = array()) |
|---|
| 70 |
{ |
|---|
| 71 |
|
|---|
| 72 |
if (!is_null($options['app'])) |
|---|
| 73 |
{ |
|---|
| 74 |
$file = sfConfig::get('sf_apps_dir').'/'.$options['app'].'/config/databases.yml'; |
|---|
| 75 |
} |
|---|
| 76 |
else |
|---|
| 77 |
{ |
|---|
| 78 |
$file = sfConfig::get('sf_config_dir').'/databases.yml'; |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
$config = file_exists($file) ? sfYaml::load($file) : array(); |
|---|
| 82 |
|
|---|
| 83 |
$config[$options['env']][$options['name']] = array( |
|---|
| 84 |
'class' => $options['class'], |
|---|
| 85 |
'param' => array_merge(isset($config[$options['env']][$options['name']]['param']) ? $config[$options['env']][$options['name']]['param'] : array(), array('dsn' => $arguments['dsn'])), |
|---|
| 86 |
); |
|---|
| 87 |
|
|---|
| 88 |
file_put_contents($file, sfYaml::dump($config, 4)); |
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
if ( |
|---|
| 92 |
is_null($options['app']) && |
|---|
| 93 |
false !== strpos($options['class'], 'Propel') && |
|---|
| 94 |
'all' == $options['env'] |
|---|
| 95 |
) |
|---|
| 96 |
{ |
|---|
| 97 |
$propelini = sfConfig::get('sf_config_dir').'/propel.ini'; |
|---|
| 98 |
if (file_exists($propelini)) |
|---|
| 99 |
{ |
|---|
| 100 |
$content = file_get_contents($propelini); |
|---|
| 101 |
if (preg_match('/^(.+?):\/\//', $arguments['dsn'], $match)) |
|---|
| 102 |
{ |
|---|
| 103 |
$content = preg_replace('/^propel\.database(\s*)=(\s*)(.+?)$/m', 'propel.database$1=$2'.$match[1], $content); |
|---|
| 104 |
$content = preg_replace('/^propel\.database\.createUrl(\s*)=(\s*)(.+?)$/m', 'propel.database.createUrl$1=$2'.$arguments['dsn'], $content); |
|---|
| 105 |
$content = preg_replace('/^propel\.database\.url(\s*)=(\s*)(.+?)$/m', 'propel.database.url$1=$2'.$arguments['dsn'], $content); |
|---|
| 106 |
|
|---|
| 107 |
file_put_contents($propelini, $content); |
|---|
| 108 |
} |
|---|
| 109 |
} |
|---|
| 110 |
} |
|---|
| 111 |
} |
|---|
| 112 |
} |
|---|
| 113 |
|
|---|