/Zend/Tool/Framework/Registry/Registry.php

https://github.com/ftaiolivista/Zend-Framework-Namespaced- · PHP · 431 lines · 195 code · 46 blank · 190 comment · 24 complexity · ecd78e9b9313eb00047bb8505ce69173 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Tool
  17. * @subpackage Framework
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Registry.php 20096 2010-01-06 02:05:09Z bkarwin $
  21. */
  22. /**
  23. * @namespace
  24. */
  25. namespace Zend\Tool\Framework\Registry;
  26. use Zend\Tool\Framework\Client;
  27. use Zend\Tool\Framework\Client\Storage;
  28. use Zend\Tool\Framework\Action;
  29. use Zend\Tool\Framework\Provider;
  30. use Zend\Tool\Framework\Manifest;
  31. use Zend\Tool\Framework\Client;
  32. use Zend\Tool\Framework\Client\Response;
  33. /**
  34. * @see Zend_Tool_Framework_Registry_Interface
  35. */
  36. require_once 'Zend/Tool/Framework/Registry/Interface.php';
  37. /**
  38. * @category Zend
  39. * @package Zend_Tool
  40. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. */
  43. class Registry implements RegistryInterface
  44. {
  45. /**
  46. * @var Zend_Tool_Framework_Loader_Abstract
  47. */
  48. protected $_loader = null;
  49. /**
  50. * @var Zend_Tool_Framework_Client_Abstract
  51. */
  52. protected $_client = null;
  53. /**
  54. * @var Zend_Tool_Framework_Client_Config
  55. */
  56. protected $_config = null;
  57. /**
  58. * @var Zend_Tool_Framework_Client_Storage
  59. */
  60. protected $_storage = null;
  61. /**
  62. * @var Zend_Tool_Framework_Action_Repository
  63. */
  64. protected $_actionRepository = null;
  65. /**
  66. * @var Zend_Tool_Framework_Provider_Repository
  67. */
  68. protected $_providerRepository = null;
  69. /**
  70. * @var Zend_Tool_Framework_Manifest_Repository
  71. */
  72. protected $_manifestRepository = null;
  73. /**
  74. * @var Zend_Tool_Framework_Client_Request
  75. */
  76. protected $_request = null;
  77. /**
  78. * @var Zend_Tool_Framework_Client_Response
  79. */
  80. protected $_response = null;
  81. /**
  82. * reset() - Reset all internal properties
  83. *
  84. */
  85. public function reset()
  86. {
  87. unset($this->_client);
  88. unset($this->_loader);
  89. unset($this->_actionRepository);
  90. unset($this->_providerRepository);
  91. unset($this->_request);
  92. unset($this->_response);
  93. }
  94. // public function __construct()
  95. // {
  96. // // no instantiation from outside
  97. // }
  98. /**
  99. * Enter description here...
  100. *
  101. * @param Zend_Tool_Framework_Client_Abstract $client
  102. * @return Zend_Tool_Framework_Registry
  103. */
  104. public function setClient(Client\AbstractClient $client)
  105. {
  106. $this->_client = $client;
  107. if ($this->isObjectRegistryEnablable($this->_client)) {
  108. $this->enableRegistryOnObject($this->_client);
  109. }
  110. return $this;
  111. }
  112. /**
  113. * getClient() return the client in the registry
  114. *
  115. * @return Zend_Tool_Framework_Client_Abstract
  116. */
  117. public function getClient()
  118. {
  119. return $this->_client;
  120. }
  121. /**
  122. * setConfig()
  123. *
  124. * @param Zend_Tool_Framework_Client_Config $config
  125. * @return Zend_Tool_Framework_Registry
  126. */
  127. public function setConfig(Client\Config $config)
  128. {
  129. $this->_config = $config;
  130. return $this;
  131. }
  132. /**
  133. * getConfig()
  134. *
  135. * @return Zend_Tool_Framework_Client_Config
  136. */
  137. public function getConfig()
  138. {
  139. if ($this->_config === null) {
  140. require_once 'Zend/Tool/Framework/Client/Config.php';
  141. $this->setConfig(new Client\Config());
  142. }
  143. return $this->_config;
  144. }
  145. /**
  146. * setStorage()
  147. *
  148. * @param Zend_Tool_Framework_Client_Storage $storage
  149. * @return Zend_Tool_Framework_Registry
  150. */
  151. public function setStorage(Storage\Storage $storage)
  152. {
  153. $this->_storage = $storage;
  154. return $this;
  155. }
  156. /**
  157. * getConfig()
  158. *
  159. * @return Zend_Tool_Framework_Client_Storage
  160. */
  161. public function getStorage()
  162. {
  163. if ($this->_storage === null) {
  164. require_once 'Zend/Tool/Framework/Client/Storage.php';
  165. $this->setStorage(new Storage\Storage());
  166. }
  167. return $this->_storage;
  168. }
  169. /**
  170. * setLoader()
  171. *
  172. * @param Zend_Tool_Framework_Loader_Interface $loader
  173. * @return Zend_Tool_Framework_Registry
  174. */
  175. public function setLoader(\Zend\Tool\Framework\Loader\LoaderInterface $loader)
  176. {
  177. $this->_loader = $loader;
  178. if ($this->isObjectRegistryEnablable($this->_loader)) {
  179. $this->enableRegistryOnObject($this->_loader);
  180. }
  181. return $this;
  182. }
  183. /**
  184. * getLoader()
  185. *
  186. * @return Zend_Tool_Framework_Loader_Abstract
  187. */
  188. public function getLoader()
  189. {
  190. if ($this->_loader === null) {
  191. require_once 'Zend/Tool/Framework/Loader/IncludePathLoader.php';
  192. $this->setLoader(new \Zend\Tool\Framework\Loader\IncludePathLoader\IncludePathLoader());
  193. }
  194. return $this->_loader;
  195. }
  196. /**
  197. * setActionRepository()
  198. *
  199. * @param Zend_Tool_Framework_Action_Repository $actionRepository
  200. * @return Zend_Tool_Framework_Registry
  201. */
  202. public function setActionRepository(Action\Repository $actionRepository)
  203. {
  204. $this->_actionRepository = $actionRepository;
  205. if ($this->isObjectRegistryEnablable($this->_actionRepository)) {
  206. $this->enableRegistryOnObject($this->_actionRepository);
  207. }
  208. return $this;
  209. }
  210. /**
  211. * getActionRepository()
  212. *
  213. * @return Zend_Tool_Framework_Action_Repository
  214. */
  215. public function getActionRepository()
  216. {
  217. if ($this->_actionRepository == null) {
  218. require_once 'Zend/Tool/Framework/Action/Repository.php';
  219. $this->setActionRepository(new Action\Repository());
  220. }
  221. return $this->_actionRepository;
  222. }
  223. /**
  224. * setProviderRepository()
  225. *
  226. * @param Zend_Tool_Framework_Provider_Repository $providerRepository
  227. * @return Zend_Tool_Framework_Registry
  228. */
  229. public function setProviderRepository(Provider\Repository $providerRepository)
  230. {
  231. $this->_providerRepository = $providerRepository;
  232. if ($this->isObjectRegistryEnablable($this->_providerRepository)) {
  233. $this->enableRegistryOnObject($this->_providerRepository);
  234. }
  235. return $this;
  236. }
  237. /**
  238. * getProviderRepository()
  239. *
  240. * @return Zend_Tool_Framework_Provider_Repository
  241. */
  242. public function getProviderRepository()
  243. {
  244. if ($this->_providerRepository == null) {
  245. require_once 'Zend/Tool/Framework/Provider/Repository.php';
  246. $this->setProviderRepository(new Provider\Repository());
  247. }
  248. return $this->_providerRepository;
  249. }
  250. /**
  251. * setManifestRepository()
  252. *
  253. * @param Zend_Tool_Framework_Manifest_Repository $manifestRepository
  254. * @return Zend_Tool_Framework_Registry
  255. */
  256. public function setManifestRepository(Manifest\Repository $manifestRepository)
  257. {
  258. $this->_manifestRepository = $manifestRepository;
  259. if ($this->isObjectRegistryEnablable($this->_manifestRepository)) {
  260. $this->enableRegistryOnObject($this->_manifestRepository);
  261. }
  262. return $this;
  263. }
  264. /**
  265. * getManifestRepository()
  266. *
  267. * @return Zend_Tool_Framework_Manifest_Repository
  268. */
  269. public function getManifestRepository()
  270. {
  271. if ($this->_manifestRepository == null) {
  272. require_once 'Zend/Tool/Framework/Manifest/Repository.php';
  273. $this->setManifestRepository(new Manifest\Repository());
  274. }
  275. return $this->_manifestRepository;
  276. }
  277. /**
  278. * setRequest()
  279. *
  280. * @param Zend_Tool_Framework_Client_Request $request
  281. * @return Zend_Tool_Framework_Registry
  282. */
  283. public function setRequest(Client\Request $request)
  284. {
  285. $this->_request = $request;
  286. return $this;
  287. }
  288. /**
  289. * getRequest()
  290. *
  291. * @return Zend_Tool_Framework_Client_Request
  292. */
  293. public function getRequest()
  294. {
  295. if ($this->_request == null) {
  296. require_once 'Zend/Tool/Framework/Client/Request.php';
  297. $this->setRequest(new Client\Request());
  298. }
  299. return $this->_request;
  300. }
  301. /**
  302. * setResponse()
  303. *
  304. * @param Zend_Tool_Framework_Client_Response $response
  305. * @return Zend_Tool_Framework_Registry
  306. */
  307. public function setResponse(Response\Response $response)
  308. {
  309. $this->_response = $response;
  310. return $this;
  311. }
  312. /**
  313. * getResponse()
  314. *
  315. * @return Zend_Tool_Framework_Client_Response
  316. */
  317. public function getResponse()
  318. {
  319. if ($this->_response == null) {
  320. require_once 'Zend/Tool/Framework/Client/Response.php';
  321. $this->setResponse(new Response\Response());
  322. }
  323. return $this->_response;
  324. }
  325. /**
  326. * __get() - Get a property via property call $registry->foo
  327. *
  328. * @param string $name
  329. * @return mixed
  330. */
  331. public function __get($name)
  332. {
  333. if (method_exists($this, 'get' . $name)) {
  334. return $this->{'get' . $name}();
  335. } else {
  336. require_once 'Zend/Tool/Framework/Registry/Exception.php';
  337. throw new Exception('Property ' . $name . ' was not located in this registry.');
  338. }
  339. }
  340. /**
  341. * __set() - Set a property via the magic set $registry->foo = 'foo'
  342. *
  343. * @param string $name
  344. * @param mixed $value
  345. */
  346. public function __set($name, $value)
  347. {
  348. if (method_exists($this, 'set' . $name)) {
  349. $this->{'set' . $name}($value);
  350. return;
  351. } else {
  352. require_once 'Zend/Tool/Framework/Registry/Exception.php';
  353. throw new Exception('Property ' . $name . ' was not located in this registry.');
  354. }
  355. }
  356. /**
  357. * isObjectRegistryEnablable() - Check whether an object is registry enablable
  358. *
  359. * @param object $object
  360. * @return bool
  361. */
  362. public function isObjectRegistryEnablable($object)
  363. {
  364. if (!is_object($object)) {
  365. require_once 'Zend/Tool/Framework/Registry/Exception.php';
  366. throw new Exception('isObjectRegistryEnablable() expects an object.');
  367. }
  368. return ($object instanceof EnabledInterface);
  369. }
  370. /**
  371. * enableRegistryOnObject() - make an object registry enabled
  372. *
  373. * @param object $object
  374. * @return Zend_Tool_Framework_Registry
  375. */
  376. public function enableRegistryOnObject($object)
  377. {
  378. if (!$this->isObjectRegistryEnablable($object)) {
  379. require_once 'Zend/Tool/Framework/Registry/Exception.php';
  380. throw new Exception('Object provided is not registry enablable, check first with Zend_Tool_Framework_Registry::isObjectRegistryEnablable()');
  381. }
  382. $object->setRegistry($this);
  383. return $this;
  384. }
  385. }