Development

/branches/1.4/lib/database/sfDatabaseManager.class.php

You must first sign up to be able to contribute.

root/branches/1.4/lib/database/sfDatabaseManager.class.php

Revision 23810, 3.5 kB (checked in by Kris.Wallsmith, 4 years ago)

[1.3] set svn:eol-style property to native and svn:keywords property to Id on all .php files

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
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  * sfDatabaseManager allows you to setup your database connectivity before the
14  * request is handled. This eliminates the need for a filter to manage database
15  * connections.
16  *
17  * @package    symfony
18  * @subpackage database
19  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
20  * @author     Sean Kerr <sean@code-box.org>
21  * @version    SVN: $Id$
22  */
23 class sfDatabaseManager
24 {
25   protected
26     $configuration = null,
27     $databases     = array();
28
29   /**
30    * Class constructor.
31    *
32    * @see initialize()
33    */
34   public function __construct(sfProjectConfiguration $configuration, $options = array())
35   {
36     $this->initialize($configuration);
37
38     if (!isset($options['auto_shutdown']) || $options['auto_shutdown'])
39     {
40       register_shutdown_function(array($this, 'shutdown'));
41     }
42   }
43
44   /**
45    * Initializes this sfDatabaseManager object
46    *
47    * @param sfProjectConfiguration $configuration A sfProjectConfiguration instance
48    *
49    * @return bool true, if initialization completes successfully, otherwise false
50    *
51    * @throws <b>sfInitializationException</b> If an error occurs while initializing this sfDatabaseManager object
52    */
53   public function initialize(sfProjectConfiguration $configuration)
54   {
55     $this->configuration = $configuration;
56
57     $this->loadConfiguration();
58   }
59
60   /**
61    * Loads database configuration.
62    */
63   public function loadConfiguration()
64   {
65     if ($this->configuration instanceof sfApplicationConfiguration)
66     {
67       $databases = include($this->configuration->getConfigCache()->checkConfig('config/databases.yml'));
68     }
69     else
70     {
71       $configHandler = new sfDatabaseConfigHandler();
72       $databases = $configHandler->evaluate(array($this->configuration->getRootDir().'/config/databases.yml'));
73     }
74
75     foreach ($databases as $name => $database)
76     {
77       $this->setDatabase($name, $database);
78     }
79   }
80
81   /**
82    * Sets a database connection.
83    *
84    * @param string     $name     The database name
85    * @param sfDatabase $database A sfDatabase instance
86    */
87   public function setDatabase($name, sfDatabase $database)
88   {
89     $this->databases[$name] = $database;
90   }
91
92   /**
93    * Retrieves the database connection associated with this sfDatabase implementation.
94    *
95    * @param string $name A database name
96    *
97    * @return mixed A Database instance
98    *
99    * @throws <b>sfDatabaseException</b> If the requested database name does not exist
100    */
101   public function getDatabase($name = 'default')
102   {
103     if (isset($this->databases[$name]))
104     {
105       return $this->databases[$name];
106     }
107
108     // nonexistent database name
109     throw new sfDatabaseException(sprintf('Database "%s" does not exist.', $name));
110   }
111
112   /**
113    * Returns the names of all database connections.
114    *
115    * @return array An array containing all database connection names
116    */
117   public function getNames()
118   {
119     return array_keys($this->databases);
120   }
121
122   /**
123    * Executes the shutdown procedure
124    *
125    * @return void
126    *
127    * @throws <b>sfDatabaseException</b> If an error occurs while shutting down this DatabaseManager
128    */
129   public function shutdown()
130   {
131     // loop through databases and shutdown connections
132     foreach ($this->databases as $database)
133     {
134       $database->shutdown();
135     }
136   }
137 }
138
Note: See TracBrowser for help on using the browser.