PageRenderTime 61ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/libs/class_registry.php

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