sfDatabaseConfigHandler.class.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 12159 2008-10-13 07:37:30Z fabien $
  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. list($includes, $data) = $this->parse($configFiles);
  36. foreach ($includes as $i => $include)
  37. {
  38. $includes[$i] = sprintf("require_once('%s');", $include);
  39. }
  40. foreach ($data as $name => $database)
  41. {
  42. $data[$name] = sprintf("\n'%s' => new %s(%s),", $name, $database[0], var_export($database[1], true));
  43. }
  44. // compile data
  45. return sprintf("<?php\n".
  46. "// auto-generated by sfDatabaseConfigHandler\n".
  47. "// date: %s\n%s\nreturn array(%s);\n",
  48. date('Y/m/d H:i:s'), implode("\n", $includes), implode("\n", $data));
  49. }
  50. public function evaluate($configFiles)
  51. {
  52. list($includes, $data) = $this->parse($configFiles);
  53. foreach ($includes as $i => $include)
  54. {
  55. require_once($include);
  56. }
  57. $databases = array();
  58. foreach ($data as $name => $database)
  59. {
  60. $databases[$name] = new $database[0]($database[1]);
  61. }
  62. return $databases;
  63. }
  64. protected function parse($configFiles)
  65. {
  66. // parse the yaml
  67. $config = self::getConfiguration($configFiles);
  68. // init our data and includes arrays
  69. $data = array();
  70. $databases = array();
  71. $includes = array();
  72. // get a list of database connections
  73. foreach ($config as $name => $dbConfig)
  74. {
  75. // is this category already registered?
  76. if (in_array($name, $databases))
  77. {
  78. // this category is already registered
  79. throw new sfParseException(sprintf('Configuration file "%s" specifies previously registered category "%s".', $configFiles[0], $name));
  80. }
  81. // add this database
  82. $databases[] = $name;
  83. // let's do our fancy work
  84. if (!isset($dbConfig['class']))
  85. {
  86. // missing class key
  87. throw new sfParseException(sprintf('Configuration file "%s" specifies category "%s" with missing class key.', $configFiles[0], $name));
  88. }
  89. if (isset($dbConfig['file']))
  90. {
  91. // we have a file to include
  92. if (!is_readable($dbConfig['file']))
  93. {
  94. // database file doesn't exist
  95. throw new sfParseException(sprintf('Configuration file "%s" specifies class "%s" with nonexistent or unreadable file "%s".', $configFiles[0], $dbConfig['class'], $dbConfig['file']));
  96. }
  97. // append our data
  98. $includes[] = $dbConfig['file'];
  99. }
  100. // parse parameters
  101. $parameters = array();
  102. if (isset($dbConfig['param']))
  103. {
  104. $parameters = $dbConfig['param'];
  105. }
  106. $parameters['name'] = $name;
  107. // append new data
  108. $data[$name] = array($dbConfig['class'], $parameters);
  109. }
  110. return array($includes, $data);
  111. }
  112. /**
  113. * @see sfConfigHandler
  114. */
  115. static public function getConfiguration(array $configFiles)
  116. {
  117. $config = self::replaceConstants(self::flattenConfigurationWithEnvironment(self::parseYamls($configFiles)));
  118. foreach ($config as $name => $dbConfig)
  119. {
  120. if (isset($dbConfig['file']))
  121. {
  122. $config[$name]['file'] = self::replacePath($dbConfig['file']);
  123. }
  124. }
  125. return $config;
  126. }
  127. }