sfDatabaseConfigHandler.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. * (c) 2004-2006 Sean Kerr <sean@code-box.org>
  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. * sfDatabaseConfigHandler allows you to setup database connections in a
  12. * configuration file that will be created for you automatically upon first
  13. * request.
  14. *
  15. * @package symfony
  16. * @subpackage config
  17. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  18. * @author Sean Kerr <sean@code-box.org>
  19. * @version SVN: $Id: sfDatabaseConfigHandler.class.php 9400 2008-06-02 00:47:11Z dwhittle $
  20. */
  21. class sfDatabaseConfigHandler extends sfYamlConfigHandler
  22. {
  23. /**
  24. * Executes this configuration handler.
  25. *
  26. * @param array $configFiles An array of absolute filesystem path to a configuration file
  27. *
  28. * @return string Data to be written to a cache file
  29. *
  30. * @throws sfConfigurationException If a requested configuration file does not exist or is not readable
  31. * @throws sfParseException If a requested configuration file is improperly formatted
  32. */
  33. public function execute($configFiles)
  34. {
  35. // parse the yaml
  36. $config = self::getConfiguration($configFiles);
  37. // init our data and includes arrays
  38. $data = array();
  39. $databases = array();
  40. $includes = array();
  41. // get a list of database connections
  42. foreach ($config as $name => $dbConfig)
  43. {
  44. // is this category already registered?
  45. if (in_array($name, $databases))
  46. {
  47. // this category is already registered
  48. throw new sfParseException(sprintf('Configuration file "%s" specifies previously registered category "%s".', $configFiles[0], $name));
  49. }
  50. // add this database
  51. $databases[] = $name;
  52. // let's do our fancy work
  53. if (!isset($dbConfig['class']))
  54. {
  55. // missing class key
  56. throw new sfParseException(sprintf('Configuration file "%s" specifies category "%s" with missing class key.', $configFiles[0], $name));
  57. }
  58. if (isset($dbConfig['file']))
  59. {
  60. // we have a file to include
  61. if (!is_readable($dbConfig['file']))
  62. {
  63. // database file doesn't exist
  64. throw new sfParseException(sprintf('Configuration file "%s" specifies class "%s" with nonexistent or unreadable file "%s".', $configFiles[0], $dbConfig['class'], $dbConfig['file']));
  65. }
  66. // append our data
  67. $includes[] = sprintf("require_once('%s');", $dbConfig['file']);
  68. }
  69. // parse parameters
  70. $parameters = array();
  71. if (isset($dbConfig['param']))
  72. {
  73. $parameters = $dbConfig['param'];
  74. }
  75. $parameters['name'] = $name;
  76. // append new data
  77. $data[] = sprintf("\n\$this->setDatabase('%s', new %s(%s));", $name, $dbConfig['class'], var_export($parameters, true));
  78. }
  79. // compile data
  80. return sprintf("<?php\n".
  81. "// auto-generated by sfDatabaseConfigHandler\n".
  82. "// date: %s\n%s\n%s\n",
  83. date('Y/m/d H:i:s'), implode("\n", $includes), implode("\n", $data));
  84. }
  85. /**
  86. * @see sfConfigHandler
  87. */
  88. static public function getConfiguration(array $configFiles)
  89. {
  90. $config = self::replaceConstants(self::flattenConfigurationWithEnvironment(self::parseYamls($configFiles)));
  91. foreach ($config as $name => $dbConfig)
  92. {
  93. if (isset($dbConfig['file']))
  94. {
  95. $config[$name]['file'] = self::replacePath($dbConfig['file']);
  96. }
  97. }
  98. return $config;
  99. }
  100. }