Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 331 lines (302 sloc) 11.172 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
<?php
/**
* Lithium: the most rad php framework
*
* @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/

namespace lithium\core;

use lithium\core\Environment;
use SplDoublyLinkedList;
use lithium\core\ConfigException;

/**
* The `Adaptable` static class is the base class from which all adapter implementations extend.
*
* `Adaptable` provides the logic necessary for generic configuration of named adapter
* configurations (such as the ones used in `Cache`) as well as a unified method of locating and
* obtaining an instance to a specified adapter.
*
* All immediate subclasses to `Adaptable` must define the protected attributes `$_configurations`
* and `$_adapters`. The former is where all local adapter named configurations will be
* stored (as an array of named configuration settings), and the latter must contain the
* `Libraries::locate()`-compatible path string (or array of strings) specifying how adapter classes
* should be located.
*
* This static class should **never** be called explicitly.
*/
class Adaptable extends \lithium\core\StaticObject {

/**
* Must always be re-defined in sub-classes. Can provide initial
* configurations, i.e. `'development'` or `'default'` along with
* default options for each.
*
* Example:
* {{{
* array(
* 'production' => array(),
* 'development' => array(),
* 'test' => array()
* )
* }}}
*
* @var object `Collection` of configurations, indexed by name.
*/
protected static $_configurations = array();

/**
* Must only be re-defined in sub-classes if the class is using strategies.
* Holds the `Libraries::locate()` compatible path string where the strategy
* in question may be found i.e. `'strategy.storage.cache'`.
*
* @var string Path string.
*/
protected static $_strategies = null;

/**
* Must always be re-defined in sub-classes. Holds the
* `Libraries::locate()` compatible path string where the adapter in
* question may be found i.e. `'adapter.storage.cache'`.
*
* @var string Path string.
*/
protected static $_adapters = null;

/**
* Sets configurations for a particular adaptable implementation, or returns the current
* configuration settings.
*
* @param array $config Configurations, indexed by name.
* @return object `Collection` of configurations or void if setting configurations.
*/
public static function config($config = null) {
if ($config && is_array($config)) {
static::$_configurations = $config;
return;
}
if ($config) {
return static::_config($config);
}
$result = array();
static::$_configurations = array_filter(static::$_configurations);

foreach (array_keys(static::$_configurations) as $key) {
$result[$key] = static::_config($key);
}
return $result;
}

/**
* Clears all configurations.
*
* @return void
*/
public static function reset() {
static::$_configurations = array();
}

/**
* Returns adapter class name for given `$name` configuration, using
* the `$_adapter` path defined in Adaptable subclasses.
*
* @param string $name Class name of adapter to load.
* @return object Adapter object.
*/
public static function adapter($name = null) {
$config = static::_config($name);

if ($config === null) {
throw new ConfigException("Configuration `{$name}` has not been defined.");
}

if (isset($config['object'])) {
return $config['object'];
}
$class = static::_class($config, static::$_adapters);
$settings = static::$_configurations[$name];
$settings[0]['object'] = static::_initAdapter($class, $config);
static::$_configurations[$name] = $settings;
return static::$_configurations[$name][0]['object'];
}

/**
* Obtain an `SplDoublyLinkedList` of the strategies for the given `$name` configuration, using
* the `$_strategies` path defined in `Adaptable` subclasses.
*
* @param string $name Class name of adapter to load.
* @return object `SplDoublyLinkedList` of strategies, or `null` if none are defined.
*/
public static function strategies($name) {
$config = static::_config($name);

if ($config === null) {
throw new ConfigException("Configuration `{$name}` has not been defined.");
}
if (!isset($config['strategies'])) {
return null;
}
$stack = new SplDoublyLinkedList();

foreach ($config['strategies'] as $key => $strategy) {
if (!is_array($strategy)) {
$name = $strategy;
$class = static::_strategy($name, static::$_strategies);
$stack->push(new $class());
continue;
}
$class = static::_strategy($key, static::$_strategies);
$index = (isset($config['strategies'][$key])) ? $key : $class;
$stack->push(new $class($config['strategies'][$index]));
}
return $stack;
}

/**
* Applies strategies configured in `$name` for `$method` on `$data`.
*
* @param string $method The strategy method to be applied.
* @param string $name The named configuration
* @param mixed $data The data to which the strategies will be applied.
* @param array $options If `mode` is set to 'LIFO', the strategies are applied in reverse.
* order of their definition.
* @return mixed Result of application of strategies to data. If no strategies
* have been configured, this method will simply return the original data.
*/
public static function applyStrategies($method, $name, $data, array $options = array()) {
$options += array('mode' => null);

if (!$strategies = static::strategies($name)) {
return $data;
}
if (!count($strategies)) {
return $data;
}

if (isset($options['mode']) && ($options['mode'] === 'LIFO')) {
$strategies->setIteratorMode(SplDoublyLinkedList::IT_MODE_LIFO);
unset($options['mode']);
}

foreach ($strategies as $strategy) {
if (method_exists($strategy, $method)) {
$data = $strategy->{$method}($data, $options);
}
}
return $data;
}

/**
* Determines if the adapter specified in the named configuration is enabled.
*
* `Enabled` can mean various things, e.g. having a PECL memcached extension compiled
* & loaded, as well as having the memcache server up & available.
*
* @param string $name The named configuration whose adapter will be checked.
* @return boolean True if adapter is enabled, false if not. This method will
* return null if no configuration under the given $name exists.
*/
public static function enabled($name) {
if (!static::_config($name)) {
return null;
}
$adapter = static::adapter($name);
return $adapter::enabled();
}

/**
* Provides an extension point for modifying how adapters are instantiated.
*
* @see lithium\core\Object::__construct()
* @param string $class The fully-namespaced class name of the adapter to instantiate.
* @param array $config The configuration array to be passed to the adapter instance. See the
* `$config` parameter of `Object::__construct()`.
* @return object The adapter's class.
* @filter This method can be filtered.
*/
protected static function _initAdapter($class, array $config) {
return static::_filter(__FUNCTION__, compact('class', 'config'), function($self, $params) {
return new $params['class']($params['config']);
});
}

/**
* Looks up an adapter by class by name.
*
* @see lithium\core\libraries::locate()
* @param string $config Configuration array of class to be found.
* @param array $paths Optional array of search paths that will be checked.
* @return string Returns a fully-namespaced class reference to the adapter class.
*/
protected static function _class($config, $paths = array()) {
if (!$name = $config['adapter']) {
$self = get_called_class();
throw new ConfigException("No adapter set for configuration in class `{$self}`.");
}
if (!$class = static::_locate($paths, $name)) {
$self = get_called_class();
throw new ConfigException("Could not find adapter `{$name}` in class `{$self}`.");
}
return $class;
}

/**
* Looks up a strategy by class by name.
*
* @see lithium\core\libraries::locate()
* @param string $name The strategy to locate.
* @param array $paths Optional array of search paths that will be checked.
* @return string Returns a fully-namespaced class reference to the adapter class.
*/
protected static function _strategy($name, $paths = array()) {
if (!$name) {
$self = get_called_class();
throw new ConfigException("No strategy set for configuration in class `{$self}`.");
}
if (!$class = static::_locate($paths, $name)) {
$self = get_called_class();
throw new ConfigException("Could not find strategy `{$name}` in class `{$self}`.");
}
return $class;
}

/**
* Perform library location for an array of paths or a single string-based path.
*
* @param string|array $paths Paths that Libraries::locate() will utilize.
* @param string $name The name of the class to be located.
* @return string Fully-namespaced path to the class, or null if not found.
*/
protected static function _locate($paths, $name) {
foreach ((array) $paths as $path) {
if ($class = Libraries::locate($path, $name)) {
return $class;
}
}
return null;
}

/**
* Gets an array of settings for the given named configuration in the current
* environment.
*
* The default types of settings for all adapters will contain keys for:
* `adapter` - The class name of the adapter
* `filters` - An array of filters to be applied to the adapter methods
*
* @see lithium\core\Environment
* @param string $name Named configuration.
* @return array Settings for the named configuration.
*/
protected static function _config($name) {
if (!isset(static::$_configurations[$name])) {
return null;
}
$settings = static::$_configurations[$name];

if (isset($settings[0])) {
return $settings[0];
}
$env = Environment::get();
$config = isset($settings[$env]) ? $settings[$env] : $settings;

if (isset($settings[$env]) && isset($settings[true])) {
$config += $settings[true];
}
static::$_configurations[$name] += array(static::_initConfig($name, $config));
return static::$_configurations[$name][0];
}

/**
* A stub method called by `_config()` which allows `Adaptable` subclasses to automatically
* assign or auto-generate additional configuration data, once a configuration is first
* accessed. This allows configuration data to be lazy-loaded from adapters or other data
* sources.
*
* @param string $name The name of the configuration which is being accessed. This is the key
* name containing the specific set of configuration passed into `config()`.
* @param array $config Contains the configuration assigned to `$name`. If this configuration is
* segregated by environment, then this will contain the configuration for the
* current environment.
* @return array Returns the final array of settings for the given named configuration.
*/
protected static function _initConfig($name, $config) {
$defaults = array('adapter' => null, 'filters' => array());
return (array) $config + $defaults;
}
}

?>
Something went wrong with that request. Please try again.