PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lithium/libraries/lithium/data/Source.php

https://github.com/brtriver/sukonv
PHP | 280 lines | 72 code | 29 blank | 179 comment | 4 complexity | cebb20410c81cff76cb908838bc9320f MD5 | raw file
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2010, 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\NetworkException;
  10. /**
  11. * This is the base class for Lithium's data abstraction layer.
  12. *
  13. * In addition to utility methods and standardized properties, it defines the implementation tasks
  14. * for all Lithium classes that work with external data, such as connections to remote resources
  15. * (`connect()` and `disconnect()`), introspecting available data objects (`entities()` and
  16. * `describe()`), and a standard read/write interface (`create()`, `read()`, `update()` and
  17. * `delete()`).
  18. *
  19. * Subclasses may implement any other non-standard functionality, but the above methods define the
  20. * requirements for interacting with `Model` objects, and other classes within `lithium\data`.
  21. */
  22. abstract class Source extends \lithium\core\Object {
  23. /**
  24. * The list of object properties to be automatically assigned from configuration passed to
  25. * `__construct()`.
  26. *
  27. * @var array
  28. */
  29. protected $_autoConfig = array('classes' => 'merge');
  30. /**
  31. * Default entity and set classes used by subclasses of `Source`.
  32. *
  33. * @var array
  34. */
  35. protected $_classes = array(
  36. 'entity' => 'lithium\data\Entity',
  37. 'set' => 'lithium\data\Collection',
  38. 'relationship' => 'lithium\data\model\Relationship'
  39. );
  40. /**
  41. * Stores a connection to a remote resource. Usually a database connection (`resource` type),
  42. * or an HTTP connection object ('object' type).
  43. *
  44. * @var mixed
  45. */
  46. public $connection = null;
  47. /**
  48. * Stores the status of this object's connection. Updated when `connect()` or `disconnect()` are
  49. * called, or if an error occurs that closes the object's connection.
  50. *
  51. * @var boolean
  52. */
  53. protected $_isConnected = false;
  54. /**
  55. * Constructor. Sets defaults and returns object.
  56. *
  57. * Options defined:
  58. * - 'autoConnect' `boolean` If true, a connection is made on initialisation. Defaults to true.
  59. *
  60. * @param array $config
  61. * @return Source object
  62. */
  63. public function __construct(array $config = array()) {
  64. $defaults = array('autoConnect' => true);
  65. parent::__construct($config + $defaults);
  66. }
  67. /**
  68. * Ensures the connection is closed, before the object is destroyed.
  69. *
  70. * @return void
  71. */
  72. public function __destruct() {
  73. if ($this->isConnected()) {
  74. $this->disconnect();
  75. }
  76. }
  77. protected function _init() {
  78. parent::_init();
  79. if ($this->_config['autoConnect']) {
  80. $this->connect();
  81. }
  82. }
  83. /**
  84. * Checks the connection status of this data source. If the `'autoConnect'` option is set to
  85. * true and the source connection is not currently active, a connection attempt will be made
  86. * before returning the result of the connection status.
  87. *
  88. * @param array $options The options available for this method:
  89. * - 'autoConnect': If true, and the connection is not currently active, calls
  90. * `connect()` on this object. Defaults to `false`.
  91. * @return boolean Returns the current value of `$_isConnected`, indicating whether or not
  92. * the object's connection is currently active. This value may not always be accurate,
  93. * as the connection could have timed out or otherwise been dropped by the remote
  94. * resource during the course of the request.
  95. */
  96. public function isConnected(array $options = array()) {
  97. $defaults = array('autoConnect' => false);
  98. $options += $defaults;
  99. if (!$this->_isConnected && $options['autoConnect']) {
  100. try {
  101. $this->connect();
  102. } catch (NetworkException $e) {
  103. $this->_isConnected = false;
  104. }
  105. }
  106. return $this->_isConnected;
  107. }
  108. /**
  109. * Quotes data-source-native identifiers, where applicable.
  110. *
  111. * @param string $name Identifier name.
  112. * @return string Returns `$name`, quoted if applicable.
  113. */
  114. public function name($name) {
  115. return $name;
  116. }
  117. /**
  118. * Abstract. Must be defined by child classes.
  119. */
  120. abstract public function connect();
  121. /**
  122. * Abstract. Must be defined by child classes.
  123. */
  124. abstract public function disconnect();
  125. /**
  126. * Returns a list of objects (entities) that models can bind to, i.e. a list of tables in the
  127. * case of a database, or REST collections, in the case of a web service.
  128. *
  129. * @param string $model The fully-name-spaced class name of the object making the request.
  130. * @return array Returns an array of objects to which models can connect.
  131. */
  132. abstract public function entities($class = null);
  133. /**
  134. * Gets the column schema for a given entity (such as a database table).
  135. *
  136. * @param mixed $entity Specifies the table name for which the schema should be returned, or
  137. * the class name of the model object requesting the schema, in which case the model
  138. * class will be queried for the correct table name.
  139. * @param array $meta
  140. * @return array Returns an associative array describing the given table's schema, where the
  141. * array keys are the available fields, and the values are arrays describing each
  142. * field, containing the following keys:
  143. * - `'type'`: The field type name
  144. */
  145. abstract public function describe($entity, array $meta = array());
  146. /**
  147. * Defines or modifies the default settings of a relationship between two models.
  148. *
  149. * @return array Returns an array containing the configuration for a model relationship.
  150. */
  151. abstract public function relationship($class, $type, $name, array $options = array());
  152. /**
  153. * Abstract. Must be defined by child classes.
  154. *
  155. * @param mixed $query
  156. * @param array $options
  157. * @return boolean Returns true if the operation was a success, otherwise false.
  158. */
  159. abstract public function create($query, array $options = array());
  160. /**
  161. * Abstract. Must be defined by child classes.
  162. *
  163. * @param mixed $query
  164. * @param array $options
  165. * @return boolean Returns true if the operation was a success, otherwise false.
  166. */
  167. abstract public function read($query, array $options = array());
  168. /**
  169. * Updates a set of records in a concrete data store.
  170. *
  171. * @param mixed $query An object which defines the update operation(s) that should be performed
  172. * against the data store. This can be a `Query`, a `RecordSet`, a `Record`, or a
  173. * subclass of one of the three. Alternatively, `$query` can be an adapter-specific
  174. * query string.
  175. * @param array $options Options to execute, which are defined by the concrete implementation.
  176. * @return boolean Returns true if the update operation was a success, otherwise false.
  177. */
  178. abstract public function update($query, array $options = array());
  179. /**
  180. * Abstract. Must be defined by child classes.
  181. *
  182. * @param mixed $query
  183. * @param array $options
  184. * @return boolean Returns true if the operation was a success, otherwise false.
  185. */
  186. abstract public function delete($query, array $options = array());
  187. /**
  188. * Casts data into proper format when added to a collection or entity object.
  189. *
  190. * @param mixed $entity The entity or collection for which data is being cast, or the name of
  191. * the model class to which the entity/collection is bound.
  192. * @param array $data An array of data being assigned.
  193. * @param array $options Any associated options with, for example, instantiating new objects in
  194. * which to wrap the data. Options implemented by `cast()` itself:
  195. * - `first` _boolean_: Used when only one value is passed to `cast()`. Even though
  196. * that value must be wrapped in an array, setting the `'first'` option to `true`
  197. * causes only that one value to be returned.
  198. * @return mixed Returns the value of `$data`, cast to the proper format according to the schema
  199. * definition of the model class specified by `$model`.
  200. */
  201. public function cast($entity, array $data, array $options = array()) {
  202. $defaults = array('first' => false);
  203. $options += $defaults;
  204. return $options['first'] ? reset($data) : $data;
  205. }
  206. /**
  207. * Returns the list of methods which format values imported from `Query` objects. Should be
  208. * overridden in subclasses.
  209. *
  210. * @see lithium\data\model\Query
  211. * @return array
  212. */
  213. public function methods() {
  214. return get_class_methods($this);
  215. }
  216. /**
  217. * A method which can be optionally implemented to configure a model class.
  218. *
  219. * @see lithium\data\Model::$_meta
  220. * @see lithium\data\Model::$_finders
  221. * @see lithium\data\Model::$_classes
  222. * @param string $class The name of the model class to be configured.
  223. * @return array This method should return an array one or more of the following keys: `'meta'`,
  224. * `'classes'` or `'finders'`. These keys maps to the three corresponding properties in
  225. * `lithium\data\Model`, and are used to override the base-level default settings and
  226. * dependencies.
  227. */
  228. public function configureClass($class) {
  229. return array();
  230. }
  231. /**
  232. * This method is responsible for factorying a new instance of a single entity object of correct
  233. * type, matching the current data source class.
  234. *
  235. * @param string $model A fully-namespaced class name representing the model class to which the
  236. * `Entity` object will be bound.
  237. * @param array $data The default data with which the new `Entity` should be populated.
  238. * @param array $options Any additional options to pass to the `Entity`'s constructor
  239. * @return object Returns a new, un-saved `Entity` object bound to the model class specified
  240. * in `$model`.
  241. */
  242. public function item($model, array $data = array(), array $options = array()) {
  243. $defaults = array('class' => 'entity');
  244. $options += $defaults;
  245. $type = $options['class'];
  246. $class = isset($this->_classes[$type]) ? $this->_classes[$type] : $type;
  247. unset($options['class']);
  248. return new $class(compact('model', 'data') + $options);
  249. }
  250. }
  251. ?>