| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
class sfDatabaseConfigHandler extends sfYamlConfigHandler |
|---|
| 24 |
{ |
|---|
| 25 |
|
|---|
| 26 |
* Executes this configuration handler. |
|---|
| 27 |
* |
|---|
| 28 |
* @param array $configFiles An array of absolute filesystem path to a configuration file |
|---|
| 29 |
* |
|---|
| 30 |
* @return string Data to be written to a cache file |
|---|
| 31 |
* |
|---|
| 32 |
* @throws sfConfigurationException If a requested configuration file does not exist or is not readable |
|---|
| 33 |
* @throws sfParseException If a requested configuration file is improperly formatted |
|---|
| 34 |
*/ |
|---|
| 35 |
public function execute($configFiles) |
|---|
| 36 |
{ |
|---|
| 37 |
|
|---|
| 38 |
$config = self::getConfiguration($configFiles); |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
$data = array(); |
|---|
| 42 |
$databases = array(); |
|---|
| 43 |
$includes = array(); |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
foreach ($config as $name => $dbConfig) |
|---|
| 47 |
{ |
|---|
| 48 |
|
|---|
| 49 |
if (in_array($name, $databases)) |
|---|
| 50 |
{ |
|---|
| 51 |
|
|---|
| 52 |
throw new sfParseException(sprintf('Configuration file "%s" specifies previously registered category "%s".', $configFiles[0], $name)); |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
$databases[] = $name; |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
if (!isset($dbConfig['class'])) |
|---|
| 60 |
{ |
|---|
| 61 |
|
|---|
| 62 |
throw new sfParseException(sprintf('Configuration file "%s" specifies category "%s" with missing class key.', $configFiles[0], $name)); |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
if (isset($dbConfig['file'])) |
|---|
| 66 |
{ |
|---|
| 67 |
|
|---|
| 68 |
if (!is_readable($dbConfig['file'])) |
|---|
| 69 |
{ |
|---|
| 70 |
|
|---|
| 71 |
throw new sfParseException(sprintf('Configuration file "%s" specifies class "%s" with nonexistent or unreadable file "%s".', $configFiles[0], $dbConfig['class'], $dbConfig['file'])); |
|---|
| 72 |
} |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
$includes[] = sprintf("require_once('%s');", $dbConfig['file']); |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
$parameters = array(); |
|---|
| 80 |
if (isset($dbConfig['param'])) |
|---|
| 81 |
{ |
|---|
| 82 |
$parameters = $dbConfig['param']; |
|---|
| 83 |
} |
|---|
| 84 |
$parameters['name'] = $name; |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
$data[] = sprintf("\n\$this->setDatabase('%s', new %s(%s));", $name, $dbConfig['class'], var_export($parameters, true)); |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
return sprintf("<?php\n". |
|---|
| 92 |
"// auto-generated by sfDatabaseConfigHandler\n". |
|---|
| 93 |
"// date: %s\n%s\n%s\n", |
|---|
| 94 |
date('Y/m/d H:i:s'), implode("\n", $includes), implode("\n", $data)); |
|---|
| 95 |
} |
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
* @see sfConfigHandler |
|---|
| 99 |
*/ |
|---|
| 100 |
static public function getConfiguration(array $configFiles) |
|---|
| 101 |
{ |
|---|
| 102 |
$config = self::replaceConstants(self::flattenConfigurationWithEnvironment(self::parseYamls($configFiles))); |
|---|
| 103 |
|
|---|
| 104 |
foreach ($config as $name => $dbConfig) |
|---|
| 105 |
{ |
|---|
| 106 |
if (isset($dbConfig['file'])) |
|---|
| 107 |
{ |
|---|
| 108 |
$config[$name]['file'] = self::replacePath($dbConfig['file']); |
|---|
| 109 |
} |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
return $config; |
|---|
| 113 |
} |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|