PageRenderTime 28ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/application/libraries/Zend/Tool/Framework/Client/Abstract.php

https://github.com/grandison/budo16
PHP | 317 lines | 142 code | 57 blank | 118 comment | 27 complexity | bb91cb83d1493ab0e5d76cf8412cfa31 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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Abstract.php 16971 2009-07-22 18:05:45Z mikaelkael $
  21. */
  22. /**
  23. * @see Zend_Tool_Framework_Registry_EnabledInterface
  24. */
  25. // require_once 'Zend/Tool/Framework/Registry/EnabledInterface.php';
  26. /**
  27. * @see Zend_Tool_Framework_Registry
  28. */
  29. // require_once 'Zend/Tool/Framework/Registry.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Tool
  33. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. abstract class Zend_Tool_Framework_Client_Abstract implements Zend_Tool_Framework_Registry_EnabledInterface
  37. {
  38. /**
  39. * @var Zend_Tool_Framework_Registry
  40. */
  41. protected $_registry = null;
  42. /**
  43. * @var callback|null
  44. */
  45. protected $_interactiveCallback = null;
  46. /**
  47. * @var bool
  48. */
  49. protected $_isInitialized = false;
  50. /**
  51. * @var Zend_Log
  52. */
  53. protected $_debugLogger = null;
  54. public function __construct($options = array())
  55. {
  56. if ($options) {
  57. $this->setOptions($options);
  58. }
  59. }
  60. public function setOptions(Array $options)
  61. {
  62. foreach ($options as $optionName => $optionValue) {
  63. $setMethodName = 'set' . $optionName;
  64. if (method_exists($this, $setMethodName)) {
  65. $this->{$setMethodName}($optionValue);
  66. }
  67. }
  68. }
  69. /**
  70. * getName() - Return the client name which can be used to
  71. * query the manifest if need be.
  72. *
  73. * @return string The client name
  74. */
  75. abstract public function getName();
  76. /**
  77. * initialized() - This will initialize the client for use
  78. *
  79. */
  80. public function initialize()
  81. {
  82. // if its already initialized, no need to initialize again
  83. if ($this->_isInitialized) {
  84. return;
  85. }
  86. // this might look goofy, but this is setting up the
  87. // registry for dependency injection into the client
  88. $registry = new Zend_Tool_Framework_Registry();
  89. $registry->setClient($this);
  90. // NOTE: at this moment, $this->_registry should contain
  91. // the registry object
  92. // run any preInit
  93. $this->_preInit();
  94. // setup the debug log
  95. if (!$this->_debugLogger instanceof Zend_Log) {
  96. // require_once 'Zend/Log.php';
  97. // require_once 'Zend/Log/Writer/Null.php';
  98. $this->_debugLogger = new Zend_Log(new Zend_Log_Writer_Null());
  99. }
  100. // let the loader load, then the repositories process whats been loaded
  101. $this->_registry->getLoader()->load();
  102. // process the action repository
  103. $this->_registry->getActionRepository()->process();
  104. // process the provider repository
  105. $this->_registry->getProviderRepository()->process();
  106. // process the manifest repository
  107. $this->_registry->getManifestRepository()->process();
  108. if ($this instanceof Zend_Tool_Framework_Client_Interactive_InputInterface) {
  109. // require_once 'Zend/Tool/Framework/Client/Interactive/InputHandler.php';
  110. }
  111. if ($this instanceof Zend_Tool_Framework_Client_Interactive_OutputInterface) {
  112. $this->_registry->getResponse()->setContentCallback(array($this, 'handleInteractiveOutput'));
  113. }
  114. }
  115. /**
  116. * This method should be implemented by the client implementation to
  117. * construct and set custom inflectors, request and response objects.
  118. */
  119. protected function _preInit()
  120. {
  121. }
  122. /**
  123. * This method *must* be implemented by the client implementation to
  124. * parse out and setup the request objects action, provider and parameter
  125. * information.
  126. */
  127. abstract protected function _preDispatch();
  128. /**
  129. * This method should be implemented by the client implementation to
  130. * take the output of the response object and return it (in an client
  131. * specific way) back to the Tooling Client.
  132. */
  133. protected function _postDispatch()
  134. {
  135. }
  136. /**
  137. * setRegistry() - Required by the Zend_Tool_Framework_Registry_EnabledInterface
  138. * interface which ensures proper registry dependency resolution
  139. *
  140. * @param Zend_Tool_Framework_Registry_Interface $registry
  141. * @return Zend_Tool_Framework_Client_Abstract
  142. */
  143. public function setRegistry(Zend_Tool_Framework_Registry_Interface $registry)
  144. {
  145. $this->_registry = $registry;
  146. return $this;
  147. }
  148. /**
  149. * hasInteractiveInput() - Convienence method for determining if this
  150. * client can handle interactive input, and thus be able to run the
  151. * promptInteractiveInput
  152. *
  153. * @return bool
  154. */
  155. public function hasInteractiveInput()
  156. {
  157. return ($this instanceof Zend_Tool_Framework_Client_Interactive_InputInterface);
  158. }
  159. public function promptInteractiveInput($inputRequest)
  160. {
  161. if (!$this->hasInteractiveInput()) {
  162. // require_once 'Zend/Tool/Framework/Client/Exception.php';
  163. throw new Zend_Tool_Framework_Client_Exception('promptInteractive() cannot be called on a non-interactive client.');
  164. }
  165. $inputHandler = new Zend_Tool_Framework_Client_Interactive_InputHandler();
  166. $inputHandler->setClient($this);
  167. $inputHandler->setInputRequest($inputRequest);
  168. return $inputHandler->handle();
  169. }
  170. /**
  171. * This method should be called in order to "handle" a Tooling Client
  172. * request that has come to the client that has been implemented.
  173. */
  174. public function dispatch()
  175. {
  176. $this->initialize();
  177. try {
  178. $this->_preDispatch();
  179. if ($this->_registry->getRequest()->isDispatchable()) {
  180. if ($this->_registry->getRequest()->getActionName() == null) {
  181. // require_once 'Zend/Tool/Framework/Client/Exception.php';
  182. throw new Zend_Tool_Framework_Client_Exception('Client failed to setup the action name.');
  183. }
  184. if ($this->_registry->getRequest()->getProviderName() == null) {
  185. // require_once 'Zend/Tool/Framework/Client/Exception.php';
  186. throw new Zend_Tool_Framework_Client_Exception('Client failed to setup the provider name.');
  187. }
  188. $this->_handleDispatch();
  189. }
  190. } catch (Exception $exception) {
  191. $this->_registry->getResponse()->setException($exception);
  192. }
  193. $this->_postDispatch();
  194. }
  195. public function convertToClientNaming($string)
  196. {
  197. return $string;
  198. }
  199. public function convertFromClientNaming($string)
  200. {
  201. return $string;
  202. }
  203. protected function _handleDispatch()
  204. {
  205. // get the provider repository
  206. $providerRepository = $this->_registry->getProviderRepository();
  207. $request = $this->_registry->getRequest();
  208. // get the dispatchable provider signature
  209. $providerSignature = $providerRepository->getProviderSignature($request->getProviderName());
  210. // get the actual provider
  211. $provider = $providerSignature->getProvider();
  212. // ensure that we can pretend if this is a pretend request
  213. if ($request->isPretend() && (!$provider instanceof Zend_Tool_Framework_Provider_Pretendable)) {
  214. // require_once 'Zend/Tool/Framework/Client/Exception.php';
  215. throw new Zend_Tool_Framework_Client_Exception('Dispatcher error - provider does not support pretend');
  216. }
  217. // get the action name
  218. $actionName = $this->_registry->getRequest()->getActionName();
  219. if (!$actionableMethod = $providerSignature->getActionableMethodByActionName($actionName)) {
  220. // require_once 'Zend/Tool/Framework/Client/Exception.php';
  221. throw new Zend_Tool_Framework_Client_Exception('Dispatcher error - actionable method not found');
  222. }
  223. // get the actual method and param information
  224. $methodName = $actionableMethod['methodName'];
  225. $methodParameters = $actionableMethod['parameterInfo'];
  226. // get the provider params
  227. $requestParameters = $this->_registry->getRequest()->getProviderParameters();
  228. // @todo This seems hackish, determine if there is a better way
  229. $callParameters = array();
  230. foreach ($methodParameters as $methodParameterName => $methodParameterValue) {
  231. if (!array_key_exists($methodParameterName, $requestParameters) && $methodParameterValue['optional'] == false) {
  232. if ($this instanceof Zend_Tool_Framework_Client_Interactive_InputInterface) {
  233. $promptSting = $this->getMissingParameterPromptString($provider, $actionableMethod['action'], $methodParameterValue['name']);
  234. $parameterPromptValue = $this->promptInteractiveInput($promptSting)->getContent();
  235. if ($parameterPromptValue == null) {
  236. // require_once 'Zend/Tool/Framework/Client/Exception.php';
  237. throw new Zend_Tool_Framework_Client_Exception('Value supplied for required parameter "' . $methodParameterValue['name'] . '" is empty');
  238. }
  239. $callParameters[] = $parameterPromptValue;
  240. } else {
  241. // require_once 'Zend/Tool/Framework/Client/Exception.php';
  242. throw new Zend_Tool_Framework_Client_Exception('A required parameter "' . $methodParameterValue['name'] . '" was not supplied.');
  243. }
  244. } else {
  245. $callParameters[] = (array_key_exists($methodParameterName, $requestParameters)) ? $requestParameters[$methodParameterName] : $methodParameterValue['default'];
  246. }
  247. }
  248. if (($specialtyName = $this->_registry->getRequest()->getSpecialtyName()) != '_Global') {
  249. $methodName .= $specialtyName;
  250. }
  251. if (method_exists($provider, $methodName)) {
  252. call_user_func_array(array($provider, $methodName), $callParameters);
  253. } elseif (method_exists($provider, $methodName . 'Action')) {
  254. call_user_func_array(array($provider, $methodName . 'Action'), $callParameters);
  255. } else {
  256. // require_once 'Zend/Tool/Framework/Client/Exception.php';
  257. throw new Zend_Tool_Framework_Client_Exception('Not a supported method.');
  258. }
  259. }
  260. }