Development

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

You must first sign up to be able to contribute.

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

Revision 9400, 3.5 kB (checked in by dwhittle, 6 years ago)

1.1: fixed file includes in config handlers (closes #3669)

  • 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 $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     // parse the yaml
38     $config = self::getConfiguration($configFiles);
39
40     // init our data and includes arrays
41     $data      = array();
42     $databases = array();
43     $includes  = array();
44
45     // get a list of database connections
46     foreach ($config as $name => $dbConfig)
47     {
48       // is this category already registered?
49       if (in_array($name, $databases))
50       {
51         // this category is already registered
52         throw new sfParseException(sprintf('Configuration file "%s" specifies previously registered category "%s".', $configFiles[0], $name));
53       }
54
55       // add this database
56       $databases[] = $name;
57
58       // let's do our fancy work
59       if (!isset($dbConfig['class']))
60       {
61         // missing class key
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         // we have a file to include
68         if (!is_readable($dbConfig['file']))
69         {
70           // database file doesn't exist
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         // append our data
75         $includes[] = sprintf("require_once('%s');", $dbConfig['file']);
76       }
77
78       // parse parameters
79       $parameters = array();
80       if (isset($dbConfig['param']))
81       {
82         $parameters = $dbConfig['param'];
83       }
84       $parameters['name'] = $name;
85
86       // append new data
87       $data[] = sprintf("\n\$this->setDatabase('%s', new %s(%s));", $name, $dbConfig['class'], var_export($parameters, true));
88     }
89
90     // compile data
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
Note: See TracBrowser for help on using the browser.