| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the symfony package.
|
|
|
5 |
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
6 |
* (c) 2004-2006 Sean Kerr <sean@code-box.org>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
/**
|
|
|
13 |
* sfDatabaseConfigHandler allows you to setup database connections in a
|
|
|
14 |
* configuration file that will be created for you automatically upon first
|
|
|
15 |
* request.
|
|
|
16 |
*
|
|
|
17 |
* @package symfony
|
|
|
18 |
* @subpackage config
|
|
|
19 |
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
20 |
* @author Sean Kerr <sean@code-box.org>
|
|
|
21 |
* @version SVN: $Id: sfDatabaseConfigHandler.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
|
|
|
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 |
list($includes, $data) = $this->parse($configFiles);
|
|
|
38 |
|
|
|
39 |
foreach ($includes as $i => $include)
|
|
|
40 |
{
|
|
|
41 |
$includes[$i] = sprintf("require_once('%s');", $include);
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
foreach ($data as $name => $database)
|
|
|
45 |
{
|
|
|
46 |
$data[$name] = sprintf("\n'%s' => new %s(%s),", $name, $database[0], var_export($database[1], true));
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
// compile data
|
|
|
50 |
return sprintf("<?php\n".
|
|
|
51 |
"// auto-generated by sfDatabaseConfigHandler\n".
|
|
|
52 |
"// date: %s\n%s\nreturn array(%s);\n",
|
|
|
53 |
date('Y/m/d H:i:s'), implode("\n", $includes), implode("\n", $data));
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
public function evaluate($configFiles)
|
|
|
57 |
{
|
|
|
58 |
list($includes, $data) = $this->parse($configFiles);
|
|
|
59 |
|
|
|
60 |
foreach ($includes as $i => $include)
|
|
|
61 |
{
|
|
|
62 |
require_once($include);
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
$databases = array();
|
|
|
66 |
foreach ($data as $name => $database)
|
|
|
67 |
{
|
|
|
68 |
$databases[$name] = new $database[0]($database[1]);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
return $databases;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
protected function parse($configFiles)
|
|
|
75 |
{
|
|
|
76 |
// parse the yaml
|
|
|
77 |
$config = self::getConfiguration($configFiles);
|
|
|
78 |
|
|
|
79 |
// init our data and includes arrays
|
|
|
80 |
$data = array();
|
|
|
81 |
$databases = array();
|
|
|
82 |
$includes = array();
|
|
|
83 |
|
|
|
84 |
// get a list of database connections
|
|
|
85 |
foreach ($config as $name => $dbConfig)
|
|
|
86 |
{
|
|
|
87 |
// is this category already registered?
|
|
|
88 |
if (in_array($name, $databases))
|
|
|
89 |
{
|
|
|
90 |
// this category is already registered
|
|
|
91 |
throw new sfParseException(sprintf('Configuration file "%s" specifies previously registered category "%s".', $configFiles[0], $name));
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
// add this database
|
|
|
95 |
$databases[] = $name;
|
|
|
96 |
|
|
|
97 |
// let's do our fancy work
|
|
|
98 |
if (!isset($dbConfig['class']))
|
|
|
99 |
{
|
|
|
100 |
// missing class key
|
|
|
101 |
throw new sfParseException(sprintf('Configuration file "%s" specifies category "%s" with missing class key.', $configFiles[0], $name));
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
if (isset($dbConfig['file']))
|
|
|
105 |
{
|
|
|
106 |
// we have a file to include
|
|
|
107 |
if (!is_readable($dbConfig['file']))
|
|
|
108 |
{
|
|
|
109 |
// database file doesn't exist
|
|
|
110 |
throw new sfParseException(sprintf('Configuration file "%s" specifies class "%s" with nonexistent or unreadable file "%s".', $configFiles[0], $dbConfig['class'], $dbConfig['file']));
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
// append our data
|
|
|
114 |
$includes[] = $dbConfig['file'];
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
// parse parameters
|
|
|
118 |
$parameters = array();
|
|
|
119 |
if (isset($dbConfig['param']))
|
|
|
120 |
{
|
|
|
121 |
$parameters = $dbConfig['param'];
|
|
|
122 |
}
|
|
|
123 |
$parameters['name'] = $name;
|
|
|
124 |
|
|
|
125 |
// append new data
|
|
|
126 |
$data[$name] = array($dbConfig['class'], $parameters);
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
return array($includes, $data);
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* @see sfConfigHandler
|
|
|
134 |
*/
|
|
|
135 |
static public function getConfiguration(array $configFiles)
|
|
|
136 |
{
|
|
|
137 |
$config = self::replaceConstants(self::flattenConfigurationWithEnvironment(self::parseYamls($configFiles)));
|
|
|
138 |
|
|
|
139 |
foreach ($config as $name => $dbConfig)
|
|
|
140 |
{
|
|
|
141 |
if (isset($dbConfig['file']))
|
|
|
142 |
{
|
|
|
143 |
$config[$name]['file'] = self::replacePath($dbConfig['file']);
|
|
|
144 |
}
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
return $config;
|
|
|
148 |
}
|
|
|
149 |
}
|