Connections.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\data;
  9. use lithium\core\Libraries;
  10. /**
  11. * The `Connections` class manages a list of named configurations that connect to external
  12. * resources. Connections are usually comprised of a type (i.e. `'database'` or `'http'`), a
  13. * reference to an adapter class (i.e. `'MySql'` or `'CouchDb'`), and authentication credentials.
  14. *
  15. * While connections can be added and removed dynamically during the course of your application
  16. * (using `Connections::add()`), it is most typical to define all connections at once, in
  17. * `config/bootstrap/connections.php`.
  18. *
  19. * The `Connections` class handles adapter classes efficiently by only loading adapter classes and
  20. * creating instances when they are requested (using `Connections::get()`).
  21. *
  22. * Adapters are usually subclasses of `lithium\data\Source`.
  23. *
  24. * @see lithium\data\Source
  25. */
  26. class Connections extends \lithium\core\Adaptable {
  27. /**
  28. * A Collection of the configurations you add through Connections::add().
  29. *
  30. * @var `lithium\util\Collection`
  31. */
  32. protected static $_configurations = array();
  33. /**
  34. * Libraries::locate() compatible path to adapters for this class.
  35. *
  36. * @var string Dot-delimited path.
  37. */
  38. protected static $_adapters = 'data.source';
  39. /**
  40. * Add connection configurations to your app in `config/bootstrap/connections.php`
  41. *
  42. * For example:
  43. * {{{
  44. * Connections::add('default', array(
  45. * 'type' => 'database',
  46. * 'adapter' => 'MySql',
  47. * 'host' => 'localhost',
  48. * 'login' => 'root',
  49. * 'password' => '',
  50. * 'database' => 'my_blog'
  51. * ));
  52. * }}}
  53. *
  54. * or
  55. *
  56. * {{{
  57. * Connections::add('couch', array(
  58. * 'type' => 'http', 'adapter' => 'CouchDb', 'host' => '127.0.0.1', 'port' => 5984
  59. * ));
  60. * }}}
  61. *
  62. * or
  63. *
  64. * {{{
  65. * Connections::add('mongo', array('type' => 'MongoDb', 'database' => 'my_app'));
  66. * }}}
  67. *
  68. * @see lithium\data\Model::$_meta
  69. * @param string $name The name by which this connection is referenced. Use this name to
  70. * retrieve the connection again using `Connections::get()`, or to bind a model to it
  71. * using `Model::$_meta['connection']`.
  72. * @param array $config Contains all additional configuration information used by the
  73. * connection, including the name of the adapter class where applicable (i.e. `MySql`),
  74. * the server name and port or socket to connect to, and (typically) the name of the
  75. * database or other entity to use. Each adapter has its own specific configuration
  76. * settings for handling things like connection persistence, data encoding, etc. See the
  77. * individual adapter or data source class for more information on what configuration
  78. * settings it supports. Basic / required options supported by most adapters:
  79. * - `'type'` _string_: The type of data source that defines this connection; typically a
  80. * class or namespace name. Relational database data sources, use `'database'`, while
  81. * CouchDB and other HTTP-related data sources use `'http'`, etc. For classes which
  82. * directly extend `lithium\data\Source`, and do not use an adapter, simply use the
  83. * name of the class, i.e. `'MongoDb'`.
  84. * - `'adapter'` _string_: For `type`s such as `'database'` which are adapter-driven,
  85. * provides the name of the adapter associated with this configuration.
  86. * - `'host'` _string_: The host name that the database should connect to. Typically
  87. * defaults to `'localhost'`.
  88. * - `'login'` _string_: If the connection requires authentication, specifies the login
  89. * name to use.
  90. * - `'password'` _string_: If the connection requires authentication, specifies the
  91. * password to use.
  92. * @return array Returns the final post-processed connection information, as stored in the
  93. * internal configuration array used by `Connections`.
  94. */
  95. public static function add($name, array $config = array()) {
  96. $defaults = array(
  97. 'type' => null,
  98. 'adapter' => null,
  99. 'login' => '',
  100. 'password' => ''
  101. );
  102. return static::$_configurations[$name] = $config + $defaults;
  103. }
  104. /**
  105. * Read the configuration or access the connections you have set up.
  106. *
  107. * Usage:
  108. * {{{
  109. * // Gets the names of all available configurations
  110. * $configurations = Connections::get();
  111. *
  112. * // Gets the configuration array for the connection named 'db'
  113. * $config = Connections::get('db', array('config' => true));
  114. *
  115. * // Gets the instance of the connection object, configured with the settings defined for
  116. * // this object in Connections::add()
  117. * $dbConnection = Connections::get('db');
  118. *
  119. * // Gets the connection object, but only if it has already been built.
  120. * // Otherwise returns null.
  121. * $dbConnection = Connections::get('db', array('autoCreate' => false));
  122. * }}}
  123. *
  124. * @param string $name The name of the connection to get, as defined in the first parameter of
  125. * `add()`, when the connection was initially created.
  126. * @param array $options Options to use when returning the connection:
  127. * - `'autoCreate'`: If `false`, the connection object is only returned if it has
  128. * already been instantiated by a previous call.
  129. * - `'config'`: If `true`, returns an array representing the connection's internal
  130. * configuration, instead of the connection itself.
  131. * @return mixed A configured instance of the connection, or an array of the configuration used.
  132. */
  133. public static function get($name = null, array $options = array()) {
  134. static $mockAdapter;
  135. $defaults = array('config' => false, 'autoCreate' => true);
  136. $options += $defaults;
  137. if ($name === false) {
  138. if (!$mockAdapter) {
  139. $class = Libraries::locate('data.source', 'Mock');
  140. $mockAdapter = new $class();
  141. }
  142. return $mockAdapter;
  143. }
  144. if (!$name) {
  145. return array_keys(static::$_configurations);
  146. }
  147. if (!isset(static::$_configurations[$name])) {
  148. return null;
  149. }
  150. if ($options['config']) {
  151. return static::_config($name);
  152. }
  153. $settings = static::$_configurations[$name];
  154. if (!isset($settings[0]['object']) && !$options['autoCreate']) {
  155. return null;
  156. }
  157. return static::adapter($name);
  158. }
  159. /**
  160. * Constructs a data source or adapter object instance from a configuration array.
  161. *
  162. * @param array $config
  163. * @param array $paths
  164. * @return object
  165. */
  166. protected static function _class($config, $paths = array()) {
  167. if (!$config['adapter']) {
  168. $config['adapter'] = $config['type'];
  169. } else {
  170. $paths = array_merge(array("adapter.data.source.{$config['type']}"), (array) $paths);
  171. }
  172. return parent::_class($config, $paths);
  173. }
  174. }
  175. ?>