| 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: sfDoctrineConfigureDatabaseTask.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
|
|
|
18 |
*/
|
|
|
19 |
class sfDoctrineConfigureDatabaseTask 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', 'doctrine'),
|
|
|
35 |
new sfCommandOption('class', null, sfCommandOption::PARAMETER_OPTIONAL, 'The database class name', 'sfDoctrineDatabase'),
|
|
|
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 |
EOF;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* @see sfTask
|
|
|
67 |
*/
|
|
|
68 |
protected function execute($arguments = array(), $options = array())
|
|
|
69 |
{
|
|
|
70 |
// update databases.yml
|
|
|
71 |
if (null !== $options['app'])
|
|
|
72 |
{
|
|
|
73 |
$file = sfConfig::get('sf_apps_dir').'/'.$options['app'].'/config/databases.yml';
|
|
|
74 |
}
|
|
|
75 |
else
|
|
|
76 |
{
|
|
|
77 |
$file = sfConfig::get('sf_config_dir').'/databases.yml';
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
$config = file_exists($file) ? sfYaml::load($file) : array();
|
|
|
81 |
|
|
|
82 |
$config[$options['env']][$options['name']] = array(
|
|
|
83 |
'class' => $options['class'],
|
|
|
84 |
'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'])),
|
|
|
85 |
);
|
|
|
86 |
|
|
|
87 |
file_put_contents($file, sfYaml::dump($config, 4));
|
|
|
88 |
}
|
|
|
89 |
}
|