PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Utility/ClassRegistry.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 346 lines | 175 code | 27 blank | 144 comment | 35 complexity | 590c262fdca37d29683377edea01a443 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. * @throws CakeException when you try to construct an interface or abstract class.
  87. */
  88. public static function init($class, $strict = false) {
  89. $_this = ClassRegistry::getInstance();
  90. $false = false;
  91. $true = true;
  92. if (is_array($class)) {
  93. $objects = $class;
  94. if (!isset($class[0])) {
  95. $objects = array($class);
  96. }
  97. } else {
  98. $objects = array(array('class' => $class));
  99. }
  100. $defaults = isset($_this->_config['Model']) ? $_this->_config['Model'] : array();
  101. $count = count($objects);
  102. foreach ($objects as $key => $settings) {
  103. if (is_array($settings)) {
  104. $pluginPath = null;
  105. $settings = array_merge($defaults, $settings);
  106. $class = $settings['class'];
  107. list($plugin, $class) = pluginSplit($class);
  108. if ($plugin) {
  109. $pluginPath = $plugin . '.';
  110. }
  111. if (empty($settings['alias'])) {
  112. $settings['alias'] = $class;
  113. }
  114. $alias = $settings['alias'];
  115. if ($model = $_this->_duplicate($alias, $class)) {
  116. $_this->map($alias, $class);
  117. return $model;
  118. }
  119. App::uses('Model', 'Model');
  120. App::uses('AppModel', 'Model');
  121. App::uses($plugin . 'AppModel', $pluginPath . 'Model');
  122. App::uses($class, $pluginPath . 'Model');
  123. if (class_exists($class) || interface_exists($class)) {
  124. $reflection = new ReflectionClass($class);
  125. if ($reflection->isAbstract() || $reflection->isInterface()) {
  126. throw new CakeException(__d('cake_dev', 'Cannot create instance of %s, as it is abstract or is an interface', $class));
  127. }
  128. $instance = $reflection->newInstance($settings);
  129. if ($strict) {
  130. $instance = ($instance instanceof Model) ? $instance : null;
  131. }
  132. }
  133. if (!isset($instance)) {
  134. if ($strict) {
  135. return false;
  136. } elseif ($plugin && class_exists($plugin . 'AppModel')) {
  137. $appModel = $plugin . 'AppModel';
  138. } else {
  139. $appModel = 'AppModel';
  140. }
  141. if (!empty($appModel)) {
  142. $settings['name'] = $class;
  143. $instance = new $appModel($settings);
  144. }
  145. if (!isset($instance)) {
  146. trigger_error(__d('cake_dev', '(ClassRegistry::init() could not create instance of %1$s class %2$s ', $class, $type), E_USER_WARNING);
  147. return $false;
  148. }
  149. }
  150. $_this->map($alias, $class);
  151. } elseif (is_numeric($settings)) {
  152. trigger_error(__d('cake_dev', '(ClassRegistry::init() Attempted to create instance of a class with a numeric name'), E_USER_WARNING);
  153. return $false;
  154. }
  155. }
  156. if ($count > 1) {
  157. return $true;
  158. }
  159. return $instance;
  160. }
  161. /**
  162. * Add $object to the registry, associating it with the name $key.
  163. *
  164. * @param string $key Key for the object in registry
  165. * @param mixed $object Object to store
  166. * @return boolean True if the object was written, false if $key already exists
  167. */
  168. public static function addObject($key, $object) {
  169. $_this = ClassRegistry::getInstance();
  170. $key = Inflector::underscore($key);
  171. if (!isset($_this->_objects[$key])) {
  172. $_this->_objects[$key] = $object;
  173. return true;
  174. }
  175. return false;
  176. }
  177. /**
  178. * Remove object which corresponds to given key.
  179. *
  180. * @param string $key Key of object to remove from registry
  181. * @return void
  182. */
  183. public static function removeObject($key) {
  184. $_this = ClassRegistry::getInstance();
  185. $key = Inflector::underscore($key);
  186. if (isset($_this->_objects[$key])) {
  187. unset($_this->_objects[$key]);
  188. }
  189. }
  190. /**
  191. * Returns true if given key is present in the ClassRegistry.
  192. *
  193. * @param string $key Key to look for
  194. * @return boolean true if key exists in registry, false otherwise
  195. */
  196. public static function isKeySet($key) {
  197. $_this = ClassRegistry::getInstance();
  198. $key = Inflector::underscore($key);
  199. if (isset($_this->_objects[$key])) {
  200. return true;
  201. } elseif (isset($_this->_map[$key])) {
  202. return true;
  203. }
  204. return false;
  205. }
  206. /**
  207. * Get all keys from the registry.
  208. *
  209. * @return array Set of keys stored in registry
  210. */
  211. public static function keys() {
  212. $_this = ClassRegistry::getInstance();
  213. return array_keys($_this->_objects);
  214. }
  215. /**
  216. * Return object which corresponds to given key.
  217. *
  218. * @param string $key Key of object to look for
  219. * @return mixed Object stored in registry or boolean false if the object does not exist.
  220. */
  221. public static function &getObject($key) {
  222. $_this = ClassRegistry::getInstance();
  223. $key = Inflector::underscore($key);
  224. $return = false;
  225. if (isset($_this->_objects[$key])) {
  226. $return = $_this->_objects[$key];
  227. } else {
  228. $key = $_this->_getMap($key);
  229. if (isset($_this->_objects[$key])) {
  230. $return = $_this->_objects[$key];
  231. }
  232. }
  233. return $return;
  234. }
  235. /**
  236. * Sets the default constructor parameter for an object type
  237. *
  238. * @param string $type Type of object. If this parameter is omitted, defaults to "Model"
  239. * @param array $param The parameter that will be passed to object constructors when objects
  240. * of $type are created
  241. * @return mixed Void if $param is being set. Otherwise, if only $type is passed, returns
  242. * the previously-set value of $param, or null if not set.
  243. */
  244. public static function config($type, $param = array()) {
  245. $_this = ClassRegistry::getInstance();
  246. if (empty($param) && is_array($type)) {
  247. $param = $type;
  248. $type = 'Model';
  249. } elseif (is_null($param)) {
  250. unset($_this->_config[$type]);
  251. } elseif (empty($param) && is_string($type)) {
  252. return isset($_this->_config[$type]) ? $_this->_config[$type] : null;
  253. }
  254. $_this->_config[$type] = $param;
  255. }
  256. /**
  257. * Checks to see if $alias is a duplicate $class Object
  258. *
  259. * @param string $alias
  260. * @param string $class
  261. * @return boolean
  262. */
  263. protected function &_duplicate($alias, $class) {
  264. $duplicate = false;
  265. if ($this->isKeySet($alias)) {
  266. $model = $this->getObject($alias);
  267. if (is_object($model) && (is_a($model, $class) || $model->alias === $class)) {
  268. $duplicate = $model;
  269. }
  270. unset($model);
  271. }
  272. return $duplicate;
  273. }
  274. /**
  275. * Add a key name pair to the registry to map name to class in the registry.
  276. *
  277. * @param string $key Key to include in map
  278. * @param string $name Key that is being mapped
  279. * @return void
  280. */
  281. public static function map($key, $name) {
  282. $_this = ClassRegistry::getInstance();
  283. $key = Inflector::underscore($key);
  284. $name = Inflector::underscore($name);
  285. if (!isset($_this->_map[$key])) {
  286. $_this->_map[$key] = $name;
  287. }
  288. }
  289. /**
  290. * Get all keys from the map in the registry.
  291. *
  292. * @return array Keys of registry's map
  293. */
  294. public static function mapKeys() {
  295. $_this = ClassRegistry::getInstance();
  296. return array_keys($_this->_map);
  297. }
  298. /**
  299. * Return the name of a class in the registry.
  300. *
  301. * @param string $key Key to find in map
  302. * @return string Mapped value
  303. */
  304. protected function _getMap($key) {
  305. if (isset($this->_map[$key])) {
  306. return $this->_map[$key];
  307. }
  308. }
  309. /**
  310. * Flushes all objects from the ClassRegistry.
  311. *
  312. * @return void
  313. */
  314. public static function flush() {
  315. $_this = ClassRegistry::getInstance();
  316. $_this->_objects = array();
  317. $_this->_map = array();
  318. }
  319. }