PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Tool/Project/Provider/Action.php

https://gitlab.com/grayhamster/open-social-media-monitoring
PHP | 242 lines | 126 code | 44 blank | 72 comment | 23 complexity | 17c091317696bd47c9a5821224582438 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: Action.php 24594 2012-01-05 21:27:01Z matthew $
  21. */
  22. /**
  23. * @see Zend_Tool_Project_Provider_Abstract
  24. */
  25. require_once 'Zend/Tool/Project/Provider/Abstract.php';
  26. /**
  27. * @see Zend_Tool_Framework_Provider_Pretendable
  28. */
  29. require_once 'Zend/Tool/Framework/Provider/Pretendable.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. class Zend_Tool_Project_Provider_Action
  37. extends Zend_Tool_Project_Provider_Abstract
  38. implements Zend_Tool_Framework_Provider_Pretendable
  39. {
  40. /**
  41. * createResource()
  42. *
  43. * @param Zend_Tool_Project_Profile $profile
  44. * @param string $actionName
  45. * @param string $controllerName
  46. * @param string $moduleName
  47. * @return Zend_Tool_Project_Profile_Resource
  48. */
  49. public static function createResource(Zend_Tool_Project_Profile $profile, $actionName, $controllerName, $moduleName = null)
  50. {
  51. if (!is_string($actionName)) {
  52. throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Action::createResource() expects \"actionName\" is the name of a action resource to create.');
  53. }
  54. if (!is_string($controllerName)) {
  55. throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Action::createResource() expects \"controllerName\" is the name of a controller resource to create.');
  56. }
  57. $controllerFile = self::_getControllerFileResource($profile, $controllerName, $moduleName);
  58. $actionMethod = $controllerFile->createResource('ActionMethod', array('actionName' => $actionName));
  59. return $actionMethod;
  60. }
  61. /**
  62. * hasResource()
  63. *
  64. * @param Zend_Tool_Project_Profile $profile
  65. * @param string $actionName
  66. * @param string $controllerName
  67. * @param string $moduleName
  68. * @return Zend_Tool_Project_Profile_Resource
  69. */
  70. public static function hasResource(Zend_Tool_Project_Profile $profile, $actionName, $controllerName, $moduleName = null)
  71. {
  72. if (!is_string($actionName)) {
  73. throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Action::createResource() expects \"actionName\" is the name of a action resource to create.');
  74. }
  75. if (!is_string($controllerName)) {
  76. throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Action::createResource() expects \"controllerName\" is the name of a controller resource to create.');
  77. }
  78. $controllerFile = self::_getControllerFileResource($profile, $controllerName, $moduleName);
  79. if ($controllerFile == null) {
  80. throw new Zend_Tool_Project_Provider_Exception('Controller ' . $controllerName . ' was not found.');
  81. }
  82. return (($controllerFile->search(array('actionMethod' => array('actionName' => $actionName)))) instanceof Zend_Tool_Project_Profile_Resource);
  83. }
  84. /**
  85. * _getControllerFileResource()
  86. *
  87. * @param Zend_Tool_Project_Profile $profile
  88. * @param string $controllerName
  89. * @param string $moduleName
  90. * @return Zend_Tool_Project_Profile_Resource
  91. */
  92. protected static function _getControllerFileResource(Zend_Tool_Project_Profile $profile, $controllerName, $moduleName = null)
  93. {
  94. $profileSearchParams = array();
  95. if ($moduleName != null && is_string($moduleName)) {
  96. $profileSearchParams = array('modulesDirectory', 'moduleDirectory' => array('moduleName' => $moduleName));
  97. }
  98. $profileSearchParams[] = 'controllersDirectory';
  99. $profileSearchParams['controllerFile'] = array('controllerName' => $controllerName);
  100. return $profile->search($profileSearchParams);
  101. }
  102. /**
  103. * create()
  104. *
  105. * @param string $name Action name for controller, in camelCase format.
  106. * @param string $controllerName Controller name action should be applied to.
  107. * @param bool $viewIncluded Whether the view should the view be included.
  108. * @param string $module Module name action should be applied to.
  109. */
  110. public function create($name, $controllerName = 'Index', $viewIncluded = true, $module = null)
  111. {
  112. $this->_loadProfile();
  113. // get request/response object
  114. $request = $this->_registry->getRequest();
  115. $response = $this->_registry->getResponse();
  116. // determine if testing is enabled in the project
  117. require_once 'Zend/Tool/Project/Provider/Test.php';
  118. $testingEnabled = Zend_Tool_Project_Provider_Test::isTestingEnabled($this->_loadedProfile);
  119. if ($testingEnabled && !Zend_Tool_Project_Provider_Test::isPHPUnitAvailable()) {
  120. $testingEnabled = false;
  121. $response->appendContent(
  122. 'Note: PHPUnit is required in order to generate controller test stubs.',
  123. array('color' => array('yellow'))
  124. );
  125. }
  126. // Check that there is not a dash or underscore, return if doesnt match regex
  127. if (preg_match('#[_-]#', $name)) {
  128. throw new Zend_Tool_Project_Provider_Exception('Action names should be camel cased.');
  129. }
  130. $originalName = $name;
  131. $originalControllerName = $controllerName;
  132. // ensure it is camelCase (lower first letter)
  133. $name = strtolower(substr($name, 0, 1)) . substr($name, 1);
  134. // ensure controller is MixedCase
  135. $controllerName = ucfirst($controllerName);
  136. if (self::hasResource($this->_loadedProfile, $name, $controllerName, $module)) {
  137. throw new Zend_Tool_Project_Provider_Exception('This controller (' . $controllerName . ') already has an action named (' . $name . ')');
  138. }
  139. $actionMethodResource = self::createResource($this->_loadedProfile, $name, $controllerName, $module);
  140. $testActionMethodResource = null;
  141. if ($testingEnabled) {
  142. $testActionMethodResource = Zend_Tool_Project_Provider_Test::createApplicationResource($this->_loadedProfile, $controllerName, $name, $module);
  143. }
  144. // alert the user about inline converted names
  145. $tense = (($request->isPretend()) ? 'would be' : 'is');
  146. if ($name !== $originalName) {
  147. $response->appendContent(
  148. 'Note: The canonical action name that ' . $tense
  149. . ' used with other providers is "' . $name . '";'
  150. . ' not "' . $originalName . '" as supplied',
  151. array('color' => array('yellow'))
  152. );
  153. }
  154. if ($controllerName !== $originalControllerName) {
  155. $response->appendContent(
  156. 'Note: The canonical controller name that ' . $tense
  157. . ' used with other providers is "' . $controllerName . '";'
  158. . ' not "' . $originalControllerName . '" as supplied',
  159. array('color' => array('yellow'))
  160. );
  161. }
  162. unset($tense);
  163. if ($request->isPretend()) {
  164. $response->appendContent(
  165. 'Would create an action named ' . $name .
  166. ' inside controller at ' . $actionMethodResource->getParentResource()->getContext()->getPath()
  167. );
  168. if ($testActionMethodResource) {
  169. $response->appendContent('Would create an action test in ' . $testActionMethodResource->getParentResource()->getContext()->getPath());
  170. }
  171. } else {
  172. $response->appendContent(
  173. 'Creating an action named ' . $name .
  174. ' inside controller at ' . $actionMethodResource->getParentResource()->getContext()->getPath()
  175. );
  176. $actionMethodResource->create();
  177. if ($testActionMethodResource) {
  178. $response->appendContent('Creating an action test in ' . $testActionMethodResource->getParentResource()->getContext()->getPath());
  179. $testActionMethodResource->create();
  180. }
  181. $this->_storeProfile();
  182. }
  183. if ($viewIncluded) {
  184. $viewResource = Zend_Tool_Project_Provider_View::createResource($this->_loadedProfile, $name, $controllerName, $module);
  185. if ($this->_registry->getRequest()->isPretend()) {
  186. $response->appendContent(
  187. 'Would create a view script for the ' . $name . ' action method at ' . $viewResource->getContext()->getPath()
  188. );
  189. } else {
  190. $response->appendContent(
  191. 'Creating a view script for the ' . $name . ' action method at ' . $viewResource->getContext()->getPath()
  192. );
  193. $viewResource->create();
  194. $this->_storeProfile();
  195. }
  196. }
  197. }
  198. }