2
0

Abstract.php 11 KB

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