Base for a static organization website

ClassRegistry.php 10KB

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