PageRenderTime 642ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/www/libs/Zend/Tool/Framework/Client/Abstract.php

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