In addition to utility methods and standardized properties, it defines the implementation tasks for all Lithium classes that work with external data, such as connections to remote resources (connect() and disconnect()), introspecting available data objects (entities() and describe()), and a standard read/write interface (create(), read(), update() and delete()). Subclasses may implement any other non-standard functionality, but the above methods define the requirements for interacting with Model objects, and other classes within lithium\data.
Inheritance: extends lithium\core\Object
Ejemplo n.º 1
0
 protected function _init()
 {
     $config = $this->_config;
     unset($config['type']);
     $this->connection = $this->_instance('service', $config);
     parent::_init();
 }
Ejemplo n.º 2
0
 /**
  * Helper method used by `export()` which delegate the query generation to the datasource.
  *
  * @param object $source Instance of the data source (`lithium\data\Source`) to use for
  *        conversion.
  */
 public function applyStrategy(Source $source)
 {
     if ($this->_built) {
         return;
     }
     $this->_built = true;
     if (!$this->_config['with']) {
         return;
     }
     $options = array();
     if (isset($this->_config['strategy'])) {
         $options['strategy'] = $this->_config['strategy'];
     }
     $source->applyStrategy($options, $this);
 }
Ejemplo n.º 3
0
 /**
  *
  */
 public function __construct(array $config = array())
 {
     $defaults = array('host' => 'localhost', 'port' => '6379', 'timeout' => 3, 'retry_interval' => 100, 'persistent' => false, 'persistent_id' => null, 'retries' => null, 'password' => null, 'database' => null, 'lazy_connect' => true);
     parent::__construct($config + $defaults);
 }
Ejemplo n.º 4
0
 /**
  * Creates the database object and set default values for it.
  *
  * Options defined:
  *  - 'database' _string_ Name of the database to use. Defaults to `null`.
  *  - 'host' _string_ Name/address of server to connect to. Defaults to 'localhost'.
  *  - 'login' _string_ Username to use when connecting to server. Defaults to 'root'.
  *  - 'password' _string_ Password to use when connecting to server. Defaults to `''`.
  *  - 'persistent' _boolean_ If true a persistent connection will be attempted, provided the
  *    adapter supports it. Defaults to `true`.
  *
  * @param $config array Array of configuration options.
  * @return Database object.
  */
 public function __construct(array $config = array())
 {
     $defaults = array('persistent' => true, 'host' => 'localhost', 'login' => 'root', 'password' => '', 'database' => null);
     $this->_strings += array('read' => 'SELECT {:fields} FROM {:source} {:alias} {:joins} {:conditions} {:group} ' . '{:order} {:limit};{:comment}');
     parent::__construct($config + $defaults);
 }
Ejemplo n.º 5
0
 /**
  * Determines if a given method can be called.
  *
  * @param string $method Name of the method.
  * @param boolean $internal Provide `true` to perform check from inside the
  *                class/object. When `false` checks also for public visibility;
  *                defaults to `false`.
  * @return boolean Returns `true` if the method can be called, `false` otherwise.
  */
 public function respondsTo($method, $internal = false)
 {
     $childRespondsTo = is_object($this->server) && is_callable(array($this->server, $method));
     return parent::respondsTo($method, $internal) || $childRespondsTo;
 }
Ejemplo n.º 6
0
 /**
  * Determines if a given method can be called.
  *
  * @param string $method Name of the method.
  * @param boolean $internal Provide `true` to perform check from inside the
  *                class/object. When `false` checks also for public visibility;
  *                defaults to `false`.
  * @return boolean Returns `true` if the method can be called, `false` otherwise.
  */
 public function respondsTo($method, $internal = false)
 {
     return isset($this->_methods[$method]) || parent::respondsTo($method, $internal);
 }
Ejemplo n.º 7
0
 /**
  * Returns a newly-created `Document` object, bound to a model and populated with default data
  * and options.
  *
  * @param string $model A fully-namespaced class name representing the model class to which the
  *               `Document` object will be bound.
  * @param array $data The default data with which the new `Document` should be populated.
  * @param array $options Any additional options to pass to the `Record`'s constructor.
  * @return object Returns a new, un-saved `Document` object bound to the model class specified
  *         in `$model`.
  */
 public function item($model, array $data = array(), array $options = array())
 {
     return parent::item($model, $data, array('handle' => $this) + $options);
 }
Ejemplo n.º 8
0
 protected function _init()
 {
     parent::_init();
     $this->_operators += array('like' => function ($key, $value) {
         return new MongoRegex($value);
     });
     $this->_handlers += array('id' => function ($v) {
         return is_string($v) && preg_match('/^[0-9a-f]{24}$/', $v) ? new MongoId($v) : $v;
     }, 'date' => function ($v) {
         return new MongoDate(is_numeric($v) ? intval($v) : strtotime($v));
     }, 'regex' => function ($v) {
         return new MongoRegex($v);
     }, 'integer' => function ($v) {
         return (int) $v;
     }, 'float' => function ($v) {
         return (double) $v;
     }, 'boolean' => function ($v) {
         return (bool) $v;
     });
 }
Ejemplo n.º 9
0
 public function result($type, $resource, $context)
 {
     if (!is_object($resource)) {
         return null;
     }
     switch ($type) {
         case 'next':
             $result = $resource->hasNext() ? $resource->getNext() : null;
             break;
         case 'close':
             unset($resource);
             $result = null;
             break;
         default:
             $result = parent::result($type, $resource, $context);
             break;
     }
     return $result;
 }
Ejemplo n.º 10
0
 /**
  * Convert the query's properties to the data sources' syntax and return it as an array.
  *
  * @param object $dataSource Instance of `lithium\data\Source` to use for conversion.
  * @param array $options Options to use when exporting the data.
  * @return array Returns an array containing a data source-specific representation of a query.
  */
 public function export(Source $dataSource, array $options = array())
 {
     $defaults = array('keys' => array());
     $options += $defaults;
     $keys = $options['keys'] ?: array_keys($this->_config);
     $methods = $dataSource->methods();
     $results = array('type' => $this->_type);
     $apply = array_intersect($keys, $methods);
     $copy = array_diff($keys, $apply);
     foreach ($apply as $item) {
         $results[$item] = $dataSource->{$item}($this->{$item}(), $this);
     }
     foreach ($copy as $item) {
         if (in_array($item, $keys)) {
             $results[$item] = $this->_config[$item];
         }
     }
     if (in_array('data', $keys)) {
         $results['data'] = $this->_exportData();
     }
     if (isset($results['source'])) {
         $results['source'] = $dataSource->name($results['source']);
     }
     if (!isset($results['fields'])) {
         return $results;
     }
     $created = array('fields', 'values');
     if (is_array($results['fields']) && array_keys($results['fields']) == $created) {
         $results = $results['fields'] + $results;
     }
     return $results;
 }
Ejemplo n.º 11
0
	public function cast($entity, array $data, array $options = array()) {
		$defaults = array('schema' => null, 'first' => false);
		$options += $defaults;
		$model = null;
		$exists = false;

		if (!$data) {
			return $data;
		}

		if (is_string($entity)) {
			$model = $entity;
			$entity = null;
			$options['schema'] = $options['schema'] ?: $model::schema();
		} elseif ($entity) {
			$options['schema'] = $options['schema'] ?: $entity->schema();
			$model = $entity->model();

			if (is_a($entity, $this->_classes['entity'])) {
				$exists = $entity->exists();
			}
		}
		$schema = $options['schema'] ?: array('_id' => array('type' => 'id'));
		unset($options['schema']);

		$exporter = $this->_classes['exporter'];
		$options += compact('model', 'exists') + array('handlers' => $this->_handlers);
		return parent::cast($entity, $exporter::cast($data, $schema, $this, $options), $options);
	}
Ejemplo n.º 12
0
 /**
  * Convert the query's properties to the data sources' syntax and return it as an array.
  *
  * @param object $dataSource Instance of the data source to use for conversion.
  * @param array $options Options to use when exporting the data.
  * @return array Returns an array containing a data source-specific representation of a query.
  */
 public function export(Source $dataSource, array $options = array())
 {
     $defaults = array('data' => array());
     $options += $defaults;
     $keys = array_keys($this->_config);
     $methods = $dataSource->methods();
     $results = array();
     $apply = array_intersect($keys, $methods);
     $copy = array_diff($keys, $apply);
     foreach ($apply as $item) {
         $results[$item] = $dataSource->{$item}($this->{$item}(), $this);
     }
     foreach ($copy as $item) {
         $results[$item] = $this->_config[$item];
     }
     $entity =& $this->_entity;
     $data = $entity ? $entity->export($dataSource, $options['data']) : $this->_data;
     $data = ($list = $this->_config['whitelist']) ? array_intersect_key($data, $list) : $data;
     $results += compact('data');
     $results['source'] = $dataSource->name($this->_config['source']);
     $created = array('fields', 'values');
     if (is_array($results['fields']) && array_keys($results['fields']) == $created) {
         $results = $results['fields'] + $results;
     }
     return $results;
 }
Ejemplo n.º 13
0
 /**
  * Build data source
  *
  * @param array $config Configuration
  */
 public function __construct(array $config = [])
 {
     $defaults = ['models' => LITHIUM_APP_PATH . '/models', 'proxies' => LITHIUM_APP_PATH . '/models/proxies', 'proxyNamespace' => 'proxies', 'cache' => null];
     $this->connectionSettings = array_diff_key($config, array_merge($defaults, ['type' => null, 'adapter' => null, 'login' => null, 'filters' => null]));
     parent::__construct($config + $defaults);
 }
Ejemplo n.º 14
0
 /**
  * Creates the database object and set default values for it.
  *
  * Options defined:
  *  - 'database' _string_ Name of the database to use. Defaults to 'lithium'.
  *  - 'host' _string_ Name/address of server to connect to. Defaults to 'localhost'.
  *  - 'login' _string_ Username to use when connecting to server. Defaults to 'root'.
  *  - 'password' _string_ Password to use when connecting to server. Defaults to none.
  *  - 'persistent' _boolean_ If true a persistent connection will be attempted, provided the
  *    adapter supports it. Defaults to true.
  *
  * @param $config array Array of configuration options.
  * @return Database object.
  */
 public function __construct($config = array())
 {
     $defaults = array('persistent' => true, 'host' => 'localhost', 'login' => 'root', 'password' => '', 'database' => 'lithium');
     parent::__construct((array) $config + $defaults);
 }
Ejemplo n.º 15
0
 /**
  * Determines the set of methods to be used when exporting query values.
  *
  * @return array
  */
 public function methods()
 {
     $result = parent::methods();
     unset($result[array_search('schema', $result)]);
     return $result;
 }
Ejemplo n.º 16
0
 protected function _init()
 {
     parent::_init();
     $this->_operators += array('like' => function ($key, $value) {
         return new MongoRegex($value);
     });
 }
Ejemplo n.º 17
0
 protected function _init()
 {
     $this->_connection = new $this->_classes['service']($this->_config);
     parent::_init();
 }