2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Class collections.
|
|
|
|
*
|
|
|
|
* A repository for class objects, each registered with a key.
|
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2011-05-29 21:31:39 +00:00
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Utility
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 0.9.2
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Class Collections.
|
|
|
|
*
|
|
|
|
* A repository for class objects, each registered with a key.
|
|
|
|
* If you try to add an object with the same key twice, nothing will come of it.
|
|
|
|
* If you need a second instance of an object, give it another key.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Utility
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class ClassRegistry {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Names of classes with their objects.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected $_objects = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Names of class names mapped to the object in the registry.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected $_map = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-07-30 15:37:26 +00:00
|
|
|
/**
|
|
|
|
* Default constructor parameter settings, indexed by type
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected $_config = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Return a singleton instance of the ClassRegistry.
|
|
|
|
*
|
|
|
|
* @return ClassRegistry instance
|
|
|
|
*/
|
2010-07-06 02:44:20 +00:00
|
|
|
public static function &getInstance() {
|
2008-05-30 11:40:08 +00:00
|
|
|
static $instance = array();
|
|
|
|
if (!$instance) {
|
2010-11-09 03:05:55 +00:00
|
|
|
$instance[0] = new ClassRegistry();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-06-20 20:17:23 +00:00
|
|
|
return $instance[0];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-04-03 14:50:05 +00:00
|
|
|
* Loads a class, registers the object in the registry and returns instance of the object. ClassRegistry::init()
|
|
|
|
* is used as a factory for models, and handle correct injecting of settings, that assist in testing.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-02-04 05:00:59 +00:00
|
|
|
* Examples
|
|
|
|
* Simple Use: Get a Post model instance ```ClassRegistry::init('Post');```
|
2009-07-24 19:18:37 +00:00
|
|
|
*
|
2011-07-14 04:36:01 +00:00
|
|
|
* Expanded: ```array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry', 'type' => 'Model');```
|
2009-07-24 19:18:37 +00:00
|
|
|
*
|
2009-02-04 05:00:59 +00:00
|
|
|
* Model Classes can accept optional ```array('id' => $id, 'table' => $table, 'ds' => $ds, 'alias' => $alias);```
|
2009-07-24 19:18:37 +00:00
|
|
|
*
|
2008-09-30 18:59:17 +00:00
|
|
|
* When $class is a numeric keyed array, multiple class instances will be stored in the registry,
|
2009-02-04 05:00:59 +00:00
|
|
|
* no instance of the object will be returned
|
|
|
|
* {{{
|
|
|
|
* array(
|
2011-07-14 04:36:01 +00:00
|
|
|
* array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry'),
|
|
|
|
* array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry'),
|
|
|
|
* array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry')
|
2009-02-04 05:00:59 +00:00
|
|
|
* );
|
|
|
|
* }}}
|
|
|
|
* @param mixed $class as a string or a single key => value array instance will be created,
|
|
|
|
* stored in the registry and returned.
|
2011-08-16 03:55:08 +00:00
|
|
|
* @param boolean $strict if set to true it will return false if the class was not found instead
|
2011-07-14 04:36:01 +00:00
|
|
|
* of trying to create an AppModel
|
2011-12-08 01:58:01 +00:00
|
|
|
* @return object instance of ClassName.
|
|
|
|
* @throws CakeException when you try to construct an interface or abstract class.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-07-14 04:36:01 +00:00
|
|
|
public static function init($class, $strict = false) {
|
2010-11-09 03:05:55 +00:00
|
|
|
$_this = ClassRegistry::getInstance();
|
2011-04-22 20:00:08 +00:00
|
|
|
$false = false;
|
2008-05-30 11:40:08 +00:00
|
|
|
$true = true;
|
2008-10-12 02:36:39 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($class)) {
|
|
|
|
$objects = $class;
|
|
|
|
if (!isset($class[0])) {
|
|
|
|
$objects = array($class);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$objects = array(array('class' => $class));
|
|
|
|
}
|
2011-08-20 04:43:34 +00:00
|
|
|
$defaults = isset($_this->_config['Model']) ? $_this->_config['Model'] : array();
|
2008-05-30 11:40:08 +00:00
|
|
|
$count = count($objects);
|
2008-10-12 02:36:39 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($objects as $key => $settings) {
|
|
|
|
if (is_array($settings)) {
|
2009-11-16 00:55:20 +00:00
|
|
|
$pluginPath = null;
|
2008-07-30 15:37:26 +00:00
|
|
|
$settings = array_merge($defaults, $settings);
|
2008-08-25 22:33:36 +00:00
|
|
|
$class = $settings['class'];
|
2011-08-16 03:55:08 +00:00
|
|
|
|
2009-11-16 00:55:20 +00:00
|
|
|
list($plugin, $class) = pluginSplit($class);
|
|
|
|
if ($plugin) {
|
2008-09-24 13:37:06 +00:00
|
|
|
$pluginPath = $plugin . '.';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2008-08-25 22:33:36 +00:00
|
|
|
if (empty($settings['alias'])) {
|
|
|
|
$settings['alias'] = $class;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-08-25 22:33:36 +00:00
|
|
|
$alias = $settings['alias'];
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-08-20 04:43:34 +00:00
|
|
|
if ($model = $_this->_duplicate($alias, $class)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$_this->map($alias, $class);
|
2008-08-07 15:36:26 +00:00
|
|
|
return $model;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2010-12-05 15:24:42 +00:00
|
|
|
App::uses('Model', 'Model');
|
|
|
|
App::uses('AppModel', 'Model');
|
2011-07-14 04:36:01 +00:00
|
|
|
App::uses($plugin . 'AppModel', $pluginPath . 'Model');
|
|
|
|
App::uses($class, $pluginPath . 'Model');
|
|
|
|
|
2011-12-08 01:58:01 +00:00
|
|
|
if (class_exists($class) || interface_exists($class)) {
|
|
|
|
$reflection = new ReflectionClass($class);
|
|
|
|
if ($reflection->isAbstract() || $reflection->isInterface()) {
|
|
|
|
throw new CakeException(__d('cake_dev', 'Cannot create instance of %s, as it is abstract or is an interface', $class));
|
|
|
|
}
|
2012-01-09 22:38:32 +00:00
|
|
|
if ($reflection->getConstructor()) {
|
|
|
|
$instance = $reflection->newInstance($settings);
|
|
|
|
} else {
|
|
|
|
$instance = $reflection->newInstance();
|
|
|
|
}
|
2011-07-24 18:32:16 +00:00
|
|
|
if ($strict) {
|
2011-11-13 02:00:03 +00:00
|
|
|
$instance = ($instance instanceof Model) ? $instance : null;
|
2011-07-24 18:32:16 +00:00
|
|
|
}
|
2011-07-14 06:00:52 +00:00
|
|
|
}
|
2011-11-13 02:00:03 +00:00
|
|
|
if (!isset($instance)) {
|
2011-07-14 21:56:55 +00:00
|
|
|
if ($strict) {
|
|
|
|
return false;
|
|
|
|
} elseif ($plugin && class_exists($plugin . 'AppModel')) {
|
|
|
|
$appModel = $plugin . 'AppModel';
|
|
|
|
} else {
|
|
|
|
$appModel = 'AppModel';
|
|
|
|
}
|
|
|
|
if (!empty($appModel)) {
|
|
|
|
$settings['name'] = $class;
|
2011-11-13 02:00:03 +00:00
|
|
|
$instance = new $appModel($settings);
|
2011-07-14 21:56:55 +00:00
|
|
|
}
|
|
|
|
|
2011-11-13 02:00:03 +00:00
|
|
|
if (!isset($instance)) {
|
2011-07-14 21:56:55 +00:00
|
|
|
trigger_error(__d('cake_dev', '(ClassRegistry::init() could not create instance of %1$s class %2$s ', $class, $type), E_USER_WARNING);
|
|
|
|
return $false;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-07-14 04:36:01 +00:00
|
|
|
$_this->map($alias, $class);
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif (is_numeric($settings)) {
|
2011-03-20 15:35:43 +00:00
|
|
|
trigger_error(__d('cake_dev', '(ClassRegistry::init() Attempted to create instance of a class with a numeric name'), E_USER_WARNING);
|
2008-05-30 11:40:08 +00:00
|
|
|
return $false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($count > 1) {
|
|
|
|
return $true;
|
|
|
|
}
|
2011-11-13 02:00:03 +00:00
|
|
|
return $instance;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Add $object to the registry, associating it with the name $key.
|
|
|
|
*
|
|
|
|
* @param string $key Key for the object in registry
|
|
|
|
* @param mixed $object Object to store
|
|
|
|
* @return boolean True if the object was written, false if $key already exists
|
|
|
|
*/
|
2011-03-26 04:04:25 +00:00
|
|
|
public static function addObject($key, $object) {
|
2010-11-09 03:05:55 +00:00
|
|
|
$_this = ClassRegistry::getInstance();
|
2008-05-30 11:40:08 +00:00
|
|
|
$key = Inflector::underscore($key);
|
2011-08-20 04:43:34 +00:00
|
|
|
if (!isset($_this->_objects[$key])) {
|
|
|
|
$_this->_objects[$key] = $object;
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Remove object which corresponds to given key.
|
|
|
|
*
|
|
|
|
* @param string $key Key of object to remove from registry
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-07-06 02:44:20 +00:00
|
|
|
public static function removeObject($key) {
|
2010-11-09 03:05:55 +00:00
|
|
|
$_this = ClassRegistry::getInstance();
|
2008-05-30 11:40:08 +00:00
|
|
|
$key = Inflector::underscore($key);
|
2011-08-20 04:43:34 +00:00
|
|
|
if (isset($_this->_objects[$key])) {
|
|
|
|
unset($_this->_objects[$key]);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns true if given key is present in the ClassRegistry.
|
|
|
|
*
|
|
|
|
* @param string $key Key to look for
|
|
|
|
* @return boolean true if key exists in registry, false otherwise
|
|
|
|
*/
|
2010-07-06 02:44:20 +00:00
|
|
|
public static function isKeySet($key) {
|
2010-11-09 03:05:55 +00:00
|
|
|
$_this = ClassRegistry::getInstance();
|
2008-05-30 11:40:08 +00:00
|
|
|
$key = Inflector::underscore($key);
|
2011-08-20 04:43:34 +00:00
|
|
|
if (isset($_this->_objects[$key])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
2011-08-20 04:43:34 +00:00
|
|
|
} elseif (isset($_this->_map[$key])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-09-08 16:58:39 +00:00
|
|
|
* Get all keys from the registry.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @return array Set of keys stored in registry
|
|
|
|
*/
|
2010-07-06 02:44:20 +00:00
|
|
|
public static function keys() {
|
2010-11-09 03:05:55 +00:00
|
|
|
$_this = ClassRegistry::getInstance();
|
2011-08-20 04:43:34 +00:00
|
|
|
return array_keys($_this->_objects);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Return object which corresponds to given key.
|
|
|
|
*
|
|
|
|
* @param string $key Key of object to look for
|
2009-11-30 14:18:24 +00:00
|
|
|
* @return mixed Object stored in registry or boolean false if the object does not exist.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-07-06 02:44:20 +00:00
|
|
|
public static function &getObject($key) {
|
2010-11-09 03:05:55 +00:00
|
|
|
$_this = ClassRegistry::getInstance();
|
2008-05-30 11:40:08 +00:00
|
|
|
$key = Inflector::underscore($key);
|
2008-08-07 18:53:05 +00:00
|
|
|
$return = false;
|
2011-08-20 04:43:34 +00:00
|
|
|
if (isset($_this->_objects[$key])) {
|
|
|
|
$return = $_this->_objects[$key];
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2011-08-20 04:43:34 +00:00
|
|
|
$key = $_this->_getMap($key);
|
|
|
|
if (isset($_this->_objects[$key])) {
|
|
|
|
$return = $_this->_objects[$key];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-09-25 16:49:56 +00:00
|
|
|
/**
|
2008-07-30 15:37:26 +00:00
|
|
|
* Sets the default constructor parameter for an object type
|
|
|
|
*
|
|
|
|
* @param string $type Type of object. If this parameter is omitted, defaults to "Model"
|
|
|
|
* @param array $param The parameter that will be passed to object constructors when objects
|
|
|
|
* of $type are created
|
|
|
|
* @return mixed Void if $param is being set. Otherwise, if only $type is passed, returns
|
|
|
|
* the previously-set value of $param, or null if not set.
|
|
|
|
*/
|
2010-07-06 02:44:20 +00:00
|
|
|
public static function config($type, $param = array()) {
|
2010-11-09 03:05:55 +00:00
|
|
|
$_this = ClassRegistry::getInstance();
|
2008-07-30 15:37:26 +00:00
|
|
|
|
|
|
|
if (empty($param) && is_array($type)) {
|
|
|
|
$param = $type;
|
|
|
|
$type = 'Model';
|
|
|
|
} elseif (is_null($param)) {
|
2011-08-20 04:43:34 +00:00
|
|
|
unset($_this->_config[$type]);
|
2008-07-30 15:37:26 +00:00
|
|
|
} elseif (empty($param) && is_string($type)) {
|
2011-08-20 04:43:34 +00:00
|
|
|
return isset($_this->_config[$type]) ? $_this->_config[$type] : null;
|
2008-07-30 15:37:26 +00:00
|
|
|
}
|
2011-08-20 04:43:34 +00:00
|
|
|
$_this->_config[$type] = $param;
|
2008-07-30 15:37:26 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Checks to see if $alias is a duplicate $class Object
|
|
|
|
*
|
|
|
|
* @param string $alias
|
|
|
|
* @param string $class
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected function &_duplicate($alias, $class) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$duplicate = false;
|
2008-10-12 02:36:39 +00:00
|
|
|
if ($this->isKeySet($alias)) {
|
2010-11-09 03:05:55 +00:00
|
|
|
$model = $this->getObject($alias);
|
2008-10-12 02:36:39 +00:00
|
|
|
if (is_object($model) && (is_a($model, $class) || $model->alias === $class)) {
|
2010-11-09 03:05:55 +00:00
|
|
|
$duplicate = $model;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
unset($model);
|
|
|
|
}
|
|
|
|
return $duplicate;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-08-07 15:36:26 +00:00
|
|
|
* Add a key name pair to the registry to map name to class in the registry.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $key Key to include in map
|
|
|
|
* @param string $name Key that is being mapped
|
2011-07-29 03:24:31 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-07-06 02:44:20 +00:00
|
|
|
public static function map($key, $name) {
|
2010-11-09 03:05:55 +00:00
|
|
|
$_this = ClassRegistry::getInstance();
|
2008-05-30 11:40:08 +00:00
|
|
|
$key = Inflector::underscore($key);
|
|
|
|
$name = Inflector::underscore($name);
|
2011-08-20 04:43:34 +00:00
|
|
|
if (!isset($_this->_map[$key])) {
|
|
|
|
$_this->_map[$key] = $name;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-08-15 17:11:54 +00:00
|
|
|
* Get all keys from the map in the registry.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @return array Keys of registry's map
|
|
|
|
*/
|
2010-07-06 02:44:20 +00:00
|
|
|
public static function mapKeys() {
|
2010-11-09 03:05:55 +00:00
|
|
|
$_this = ClassRegistry::getInstance();
|
2011-08-20 04:43:34 +00:00
|
|
|
return array_keys($_this->_map);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Return the name of a class in the registry.
|
|
|
|
*
|
|
|
|
* @param string $key Key to find in map
|
|
|
|
* @return string Mapped value
|
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected function _getMap($key) {
|
|
|
|
if (isset($this->_map[$key])) {
|
|
|
|
return $this->_map[$key];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Flushes all objects from the ClassRegistry.
|
|
|
|
*
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-07-06 02:44:20 +00:00
|
|
|
public static function flush() {
|
2010-11-09 03:05:55 +00:00
|
|
|
$_this = ClassRegistry::getInstance();
|
2011-08-20 04:43:34 +00:00
|
|
|
$_this->_objects = array();
|
|
|
|
$_this->_map = array();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|