| 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 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 |
$myConfig = $this->parseYamls($configFiles); |
|---|
| 39 |
|
|---|
| 40 |
$myConfig = sfToolkit::arrayDeepMerge( |
|---|
| 41 |
isset($myConfig['default']) && is_array($myConfig['default']) ? $myConfig['default'] : array(), |
|---|
| 42 |
isset($myConfig['all']) && is_array($myConfig['all']) ? $myConfig['all'] : array(), |
|---|
| 43 |
isset($myConfig[sfConfig::get('sf_environment')]) && is_array($myConfig[sfConfig::get('sf_environment')]) ? $myConfig[sfConfig::get('sf_environment')] : array() |
|---|
| 44 |
); |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
$data = array(); |
|---|
| 48 |
$databases = array(); |
|---|
| 49 |
$includes = array(); |
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
foreach ($myConfig as $key => $dbConfig) |
|---|
| 53 |
{ |
|---|
| 54 |
|
|---|
| 55 |
if (in_array($key, $databases)) |
|---|
| 56 |
{ |
|---|
| 57 |
|
|---|
| 58 |
$error = sprintf('Configuration file "%s" specifies previously registered category "%s"', $configFiles[0], $key); |
|---|
| 59 |
throw new sfParseException($error); |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
$databases[] = $key; |
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
if (!isset($dbConfig['class'])) |
|---|
| 67 |
{ |
|---|
| 68 |
|
|---|
| 69 |
$error = sprintf('Configuration file "%s" specifies category "%s" with missing class key', $configFiles[0], $key); |
|---|
| 70 |
throw new sfParseException($error); |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
if (isset($dbConfig['file'])) |
|---|
| 74 |
{ |
|---|
| 75 |
|
|---|
| 76 |
$file = $this->replaceConstants($dbConfig['file']); |
|---|
| 77 |
$file = $this->replacePath($file); |
|---|
| 78 |
|
|---|
| 79 |
if (!is_readable($file)) |
|---|
| 80 |
{ |
|---|
| 81 |
|
|---|
| 82 |
$error = sprintf('Configuration file "%s" specifies class "%s" with nonexistent or unreadable file "%s"', $configFiles[0], $dbConfig['class'], $file); |
|---|
| 83 |
throw new sfParseException($error); |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
$includes[] = sprintf("require_once('%s');", $file); |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
if (isset($dbConfig['param'])) |
|---|
| 92 |
{ |
|---|
| 93 |
foreach ($dbConfig['param'] as &$value) |
|---|
| 94 |
{ |
|---|
| 95 |
$value = $this->replaceConstants($value); |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
$parameters = var_export($dbConfig['param'], true); |
|---|
| 99 |
} |
|---|
| 100 |
else |
|---|
| 101 |
{ |
|---|
| 102 |
$parameters = 'null'; |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
$data[] = sprintf("\n\$database = new %s();\n". |
|---|
| 107 |
"\$database->initialize(%s, '%s');\n". |
|---|
| 108 |
"\$this->databases['%s'] = \$database;", |
|---|
| 109 |
$dbConfig['class'], $parameters, $key, $key); |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
$retval = sprintf("<?php\n". |
|---|
| 114 |
"// auto-generated by sfDatabaseConfigHandler\n". |
|---|
| 115 |
"// date: %s%s\n%s\n", |
|---|
| 116 |
date('Y/m/d H:i:s'), implode("\n", $includes), implode("\n", $data)); |
|---|
| 117 |
|
|---|
| 118 |
return $retval; |
|---|
| 119 |
} |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|