PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Cake/Utility/ClassRegistry.php

https://github.com/gustavor/lore
PHP | 341 lines | 171 code | 27 blank | 143 comment | 32 complexity | 771fb08fd1e7071bc7c5d86abde25055 MD5 | raw file
  1. <?php
  2. /**
  3. * Class collections.
  4. *
  5. * A repository for class objects, each registered with a key.
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package Cake.Utility
  18. * @since CakePHP(tm) v 0.9.2
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. /**
  22. * Class Collections.
  23. *
  24. * A repository for class objects, each registered with a key.
  25. * If you try to add an object with the same key twice, nothing will come of it.
  26. * If you need a second instance of an object, give it another key.
  27. *
  28. * @package Cake.Utility
  29. */
  30. class ClassRegistry {
  31. /**
  32. * Names of classes with their objects.
  33. *
  34. * @var array
  35. */
  36. protected $_objects = array();
  37. /**
  38. * Names of class names mapped to the object in the registry.
  39. *
  40. * @var array
  41. */
  42. protected $_map = array();
  43. /**
  44. * Default constructor parameter settings, indexed by type
  45. *
  46. * @var array
  47. */
  48. protected $_config = array();
  49. /**
  50. * Return a singleton instance of the ClassRegistry.
  51. *
  52. * @return ClassRegistry instance
  53. */
  54. public static function &getInstance() {
  55. static $instance = array();
  56. if (!$instance) {
  57. $instance[0] = new ClassRegistry();
  58. }
  59. return $instance[0];
  60. }
  61. /**
  62. * Loads a class, registers the object in the registry and returns instance of the object. ClassRegistry::init()
  63. * is used as a factory for models, and handle correct injecting of settings, that assist in testing.
  64. *
  65. * Examples
  66. * Simple Use: Get a Post model instance ```ClassRegistry::init('Post');```
  67. *
  68. * Expanded: ```array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry', 'type' => 'Model');```
  69. *
  70. * Model Classes can accept optional ```array('id' => $id, 'table' => $table, 'ds' => $ds, 'alias' => $alias);```
  71. *
  72. * When $class is a numeric keyed array, multiple class instances will be stored in the registry,
  73. * no instance of the object will be returned
  74. * {{{
  75. * array(
  76. * array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry'),
  77. * array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry'),
  78. * array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry')
  79. * );
  80. * }}}
  81. * @param mixed $class as a string or a single key => value array instance will be created,
  82. * stored in the registry and returned.
  83. * @param boolean $strict if set to true it will return false if the class was not found instead
  84. * of trying to create an AppModel
  85. * @return object instance of ClassName
  86. */
  87. public static function init($class, $strict = false) {
  88. $_this = ClassRegistry::getInstance();
  89. $false = false;
  90. $true = true;
  91. if (is_array($class)) {
  92. $objects = $class;
  93. if (!isset($class[0])) {
  94. $objects = array($class);
  95. }
  96. } else {
  97. $objects = array(array('class' => $class));
  98. }
  99. $defaults = isset($_this->_config['Model']) ? $_this->_config['Model'] : array();
  100. $count = count($objects);
  101. foreach ($objects as $key => $settings) {
  102. if (is_array($settings)) {
  103. $pluginPath = null;
  104. $settings = array_merge($defaults, $settings);
  105. $class = $settings['class'];
  106. list($plugin, $class) = pluginSplit($class);
  107. if ($plugin) {
  108. $pluginPath = $plugin . '.';
  109. }
  110. if (empty($settings['alias'])) {
  111. $settings['alias'] = $class;
  112. }
  113. $alias = $settings['alias'];
  114. if ($model = $_this->_duplicate($alias, $class)) {
  115. $_this->map($alias, $class);
  116. return $model;
  117. }
  118. App::uses('Model', 'Model');
  119. App::uses('AppModel', 'Model');
  120. App::uses($plugin . 'AppModel', $pluginPath . 'Model');
  121. App::uses($class, $pluginPath . 'Model');
  122. if (class_exists($class)) {
  123. ${$class} = new $class($settings);
  124. if ($strict) {
  125. ${$class} = (${$class} instanceof Model) ? ${$class} : null;
  126. }
  127. }
  128. if (!isset(${$class})) {
  129. if ($strict) {
  130. return false;
  131. } elseif ($plugin && class_exists($plugin . 'AppModel')) {
  132. $appModel = $plugin . 'AppModel';
  133. } else {
  134. $appModel = 'AppModel';
  135. }
  136. if (!empty($appModel)) {
  137. $settings['name'] = $class;
  138. ${$class} = new $appModel($settings);
  139. }
  140. if (!isset(${$class})) {
  141. trigger_error(__d('cake_dev', '(ClassRegistry::init() could not create instance of %1$s class %2$s ', $class, $type), E_USER_WARNING);
  142. return $false;
  143. }
  144. }
  145. $_this->map($alias, $class);
  146. } elseif (is_numeric($settings)) {
  147. trigger_error(__d('cake_dev', '(ClassRegistry::init() Attempted to create instance of a class with a numeric name'), E_USER_WARNING);
  148. return $false;
  149. }
  150. }
  151. if ($count > 1) {
  152. return $true;
  153. }
  154. return ${$class};
  155. }
  156. /**
  157. * Add $object to the registry, associating it with the name $key.
  158. *
  159. * @param string $key Key for the object in registry
  160. * @param mixed $object Object to store
  161. * @return boolean True if the object was written, false if $key already exists
  162. */
  163. public static function addObject($key, $object) {
  164. $_this = ClassRegistry::getInstance();
  165. $key = Inflector::underscore($key);
  166. if (!isset($_this->_objects[$key])) {
  167. $_this->_objects[$key] = $object;
  168. return true;
  169. }
  170. return false;
  171. }
  172. /**
  173. * Remove object which corresponds to given key.
  174. *
  175. * @param string $key Key of object to remove from registry
  176. * @return void
  177. */
  178. public static function removeObject($key) {
  179. $_this = ClassRegistry::getInstance();
  180. $key = Inflector::underscore($key);
  181. if (isset($_this->_objects[$key])) {
  182. unset($_this->_objects[$key]);
  183. }
  184. }
  185. /**
  186. * Returns true if given key is present in the ClassRegistry.
  187. *
  188. * @param string $key Key to look for
  189. * @return boolean true if key exists in registry, false otherwise
  190. */
  191. public static function isKeySet($key) {
  192. $_this = ClassRegistry::getInstance();
  193. $key = Inflector::underscore($key);
  194. if (isset($_this->_objects[$key])) {
  195. return true;
  196. } elseif (isset($_this->_map[$key])) {
  197. return true;
  198. }
  199. return false;
  200. }
  201. /**
  202. * Get all keys from the registry.
  203. *
  204. * @return array Set of keys stored in registry
  205. */
  206. public static function keys() {
  207. $_this = ClassRegistry::getInstance();
  208. return array_keys($_this->_objects);
  209. }
  210. /**
  211. * Return object which corresponds to given key.
  212. *
  213. * @param string $key Key of object to look for
  214. * @return mixed Object stored in registry or boolean false if the object does not exist.
  215. */
  216. public static function &getObject($key) {
  217. $_this = ClassRegistry::getInstance();
  218. $key = Inflector::underscore($key);
  219. $return = false;
  220. if (isset($_this->_objects[$key])) {
  221. $return = $_this->_objects[$key];
  222. } else {
  223. $key = $_this->_getMap($key);
  224. if (isset($_this->_objects[$key])) {
  225. $return = $_this->_objects[$key];
  226. }
  227. }
  228. return $return;
  229. }
  230. /**
  231. * Sets the default constructor parameter for an object type
  232. *
  233. * @param string $type Type of object. If this parameter is omitted, defaults to "Model"
  234. * @param array $param The parameter that will be passed to object constructors when objects
  235. * of $type are created
  236. * @return mixed Void if $param is being set. Otherwise, if only $type is passed, returns
  237. * the previously-set value of $param, or null if not set.
  238. */
  239. public static function config($type, $param = array()) {
  240. $_this = ClassRegistry::getInstance();
  241. if (empty($param) && is_array($type)) {
  242. $param = $type;
  243. $type = 'Model';
  244. } elseif (is_null($param)) {
  245. unset($_this->_config[$type]);
  246. } elseif (empty($param) && is_string($type)) {
  247. return isset($_this->_config[$type]) ? $_this->_config[$type] : null;
  248. }
  249. $_this->_config[$type] = $param;
  250. }
  251. /**
  252. * Checks to see if $alias is a duplicate $class Object
  253. *
  254. * @param string $alias
  255. * @param string $class
  256. * @return boolean
  257. */
  258. protected function &_duplicate($alias, $class) {
  259. $duplicate = false;
  260. if ($this->isKeySet($alias)) {
  261. $model = $this->getObject($alias);
  262. if (is_object($model) && (is_a($model, $class) || $model->alias === $class)) {
  263. $duplicate = $model;
  264. }
  265. unset($model);
  266. }
  267. return $duplicate;
  268. }
  269. /**
  270. * Add a key name pair to the registry to map name to class in the registry.
  271. *
  272. * @param string $key Key to include in map
  273. * @param string $name Key that is being mapped
  274. * @return void
  275. */
  276. public static function map($key, $name) {
  277. $_this = ClassRegistry::getInstance();
  278. $key = Inflector::underscore($key);
  279. $name = Inflector::underscore($name);
  280. if (!isset($_this->_map[$key])) {
  281. $_this->_map[$key] = $name;
  282. }
  283. }
  284. /**
  285. * Get all keys from the map in the registry.
  286. *
  287. * @return array Keys of registry's map
  288. */
  289. public static function mapKeys() {
  290. $_this = ClassRegistry::getInstance();
  291. return array_keys($_this->_map);
  292. }
  293. /**
  294. * Return the name of a class in the registry.
  295. *
  296. * @param string $key Key to find in map
  297. * @return string Mapped value
  298. */
  299. protected function _getMap($key) {
  300. if (isset($this->_map[$key])) {
  301. return $this->_map[$key];
  302. }
  303. }
  304. /**
  305. * Flushes all objects from the ClassRegistry.
  306. *
  307. * @return void
  308. */
  309. public static function flush() {
  310. $_this = ClassRegistry::getInstance();
  311. $_this->_objects = array();
  312. $_this->_map = array();
  313. }
  314. }