Development

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

You must first sign up to be able to contribute.

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

Revision 9086, 3.1 kB (checked in by Carl.Vondrick, 6 years ago)

1.1: fixed @param phpdoc to fit specs in database (refs #2991)

  • 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  * 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(sfApplicationConfiguration $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  sfApplicationConfiguration $configuration A sfApplicationConfiguration 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(sfApplicationConfiguration $configuration)
54   {
55     $this->configuration = $configuration;
56
57     $this->loadConfiguration();
58   }
59
60   /**
61    * Loads database configuration.
62    */
63   public function loadConfiguration()
64   {
65     require($this->configuration->getConfigCache()->checkConfig('config/databases.yml'));
66   }
67
68   /**
69    * Sets a database connection.
70    *
71    * @param string     $name        The database name
72    * @param sfDatabase $sfDatabase  A sfDatabase instance
73    */
74   public function setDatabase($name, sfDatabase $database)
75   {
76     $this->databases[$name] = $database;
77   }
78
79   /**
80    * Retrieves the database connection associated with this sfDatabase implementation.
81    *
82    * @param string $name  A database name
83    *
84    * @return mixed A Database instance
85    *
86    * @throws <b>sfDatabaseException</b> If the requested database name does not exist
87    */
88   public function getDatabase($name = 'default')
89   {
90     if (isset($this->databases[$name]))
91     {
92       return $this->databases[$name];
93     }
94
95     // nonexistent database name
96     throw new sfDatabaseException(sprintf('Database "%s" does not exist.', $name));
97   }
98
99   /**
100    * Returns the names of all database connections.
101    *
102    * @return array An array containing all database connection names
103    */
104   public function getNames()
105   {
106     return array_keys($this->databases);
107   }
108
109   /**
110    * Executes the shutdown procedure
111    *
112    * @return void
113    *
114    * @throws <b>sfDatabaseException</b> If an error occurs while shutting down this DatabaseManager
115    */
116   public function shutdown()
117   {
118     // loop through databases and shutdown connections
119     foreach ($this->databases as $database)
120     {
121       $database->shutdown();
122     }
123   }
124 }
125
Note: See TracBrowser for help on using the browser.