sfDatabaseConfigHandler.class.php 4.28 KB
Newer Older
Игорь's avatar
init    
Игорь committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php

/*
 * This file is part of the symfony package.
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
 * (c) 2004-2006 Sean Kerr <sean@code-box.org>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * sfDatabaseConfigHandler allows you to setup database connections in a
 * configuration file that will be created for you automatically upon first
 * request.
 *
 * @package    symfony
 * @subpackage config
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @author     Sean Kerr <sean@code-box.org>
 * @version    SVN: $Id: sfDatabaseConfigHandler.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class sfDatabaseConfigHandler extends sfYamlConfigHandler
{
  /**
   * Executes this configuration handler.
   *
   * @param array $configFiles An array of absolute filesystem path to a configuration file
   *
   * @return string Data to be written to a cache file
   *
   * @throws sfConfigurationException If a requested configuration file does not exist or is not readable
   * @throws sfParseException If a requested configuration file is improperly formatted
   */
  public function execute($configFiles)
  {
    list($includes, $data) = $this->parse($configFiles);

    foreach ($includes as $i => $include)
    {
      $includes[$i] = sprintf("require_once('%s');", $include);
    }

    foreach ($data as $name => $database)
    {
      $data[$name] = sprintf("\n'%s' => new %s(%s),", $name, $database[0], var_export($database[1], true));
    }

    // compile data
    return sprintf("<?php\n".
                      "// auto-generated by sfDatabaseConfigHandler\n".
                      "// date: %s\n%s\nreturn array(%s);\n",
                      date('Y/m/d H:i:s'), implode("\n", $includes), implode("\n", $data));
  }

  public function evaluate($configFiles)
  {
    list($includes, $data) = $this->parse($configFiles);

    foreach ($includes as $i => $include)
    {
      require_once($include);
    }

    $databases = array();
    foreach ($data as $name => $database)
    {
      $databases[$name] = new $database[0]($database[1]);
    }

    return $databases;
  }

  protected function parse($configFiles)
  {
    // parse the yaml
    $config = self::getConfiguration($configFiles);

    // init our data and includes arrays
    $data      = array();
    $databases = array();
    $includes  = array();

    // get a list of database connections
    foreach ($config as $name => $dbConfig)
    {
      // is this category already registered?
      if (in_array($name, $databases))
      {
        // this category is already registered
        throw new sfParseException(sprintf('Configuration file "%s" specifies previously registered category "%s".', $configFiles[0], $name));
      }

      // add this database
      $databases[] = $name;

      // let's do our fancy work
      if (!isset($dbConfig['class']))
      {
        // missing class key
        throw new sfParseException(sprintf('Configuration file "%s" specifies category "%s" with missing class key.', $configFiles[0], $name));
      }

      if (isset($dbConfig['file']))
      {
        // we have a file to include
        if (!is_readable($dbConfig['file']))
        {
          // database file doesn't exist
          throw new sfParseException(sprintf('Configuration file "%s" specifies class "%s" with nonexistent or unreadable file "%s".', $configFiles[0], $dbConfig['class'], $dbConfig['file']));
        }

        // append our data
        $includes[] = $dbConfig['file'];
      }

      // parse parameters
      $parameters = array();
      if (isset($dbConfig['param']))
      {
        $parameters = $dbConfig['param'];
      }
      $parameters['name'] = $name;

      // append new data
      $data[$name] = array($dbConfig['class'], $parameters);
    }

    return array($includes, $data);
  }

  /**
   * @see sfConfigHandler
   */
  static public function getConfiguration(array $configFiles)
  {
    $config = self::replaceConstants(self::flattenConfigurationWithEnvironment(self::parseYamls($configFiles)));

    foreach ($config as $name => $dbConfig)
    {
      if (isset($dbConfig['file']))
      {
        $config[$name]['file'] = self::replacePath($dbConfig['file']);
      }
    }

    return $config;
  }
}