Development

/branches/1.0/lib/config/sfDatabaseConfigHandler.class.php

You must first sign up to be able to contribute.

root/branches/1.0/lib/config/sfDatabaseConfigHandler.class.php

Revision 7791, 3.9 kB (checked in by fabien, 7 years ago)

updated Sean Kerr email address

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
Line 
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$
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     // parse the yaml
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     // init our data and includes arrays
47     $data      = array();
48     $databases = array();
49     $includes  = array();
50
51     // get a list of database connections
52     foreach ($myConfig as $key => $dbConfig)
53     {
54       // is this category already registered?
55       if (in_array($key, $databases))
56       {
57         // this category is already registered
58         $error = sprintf('Configuration file "%s" specifies previously registered category "%s"', $configFiles[0], $key);
59         throw new sfParseException($error);
60       }
61
62       // add this database
63       $databases[] = $key;
64
65       // let's do our fancy work
66       if (!isset($dbConfig['class']))
67       {
68         // missing class key
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         // we have a file to include
76         $file = $this->replaceConstants($dbConfig['file']);
77         $file = $this->replacePath($file);
78
79         if (!is_readable($file))
80         {
81           // database file doesn't exist
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         // append our data
87         $includes[] = sprintf("require_once('%s');", $file);
88       }
89
90       // parse parameters
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       // append new data
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     // compile data
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
Note: See TracBrowser for help on using the browser.