sfConfigureDatabaseTask.class.php 4.6 KB

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