File indexing completed on 2025-01-12 05:22:30
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Tool 0017 * @subpackage Framework 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 * @version $Id$ 0021 */ 0022 0023 /** 0024 * @see Zend_Tool_Project_Provider_Abstract 0025 */ 0026 // require_once 'Zend/Tool/Project/Provider/Abstract.php'; 0027 0028 /** 0029 * @see Zend_Tool_Framework_Provider_Pretendable 0030 */ 0031 // require_once 'Zend/Tool/Framework/Provider/Pretendable.php'; 0032 0033 /** 0034 * @category Zend 0035 * @package Zend_Tool 0036 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0037 * @license http://framework.zend.com/license/new-bsd New BSD License 0038 */ 0039 class Zend_Tool_Project_Provider_Action 0040 extends Zend_Tool_Project_Provider_Abstract 0041 implements Zend_Tool_Framework_Provider_Pretendable 0042 { 0043 0044 /** 0045 * createResource() 0046 * 0047 * @param Zend_Tool_Project_Profile $profile 0048 * @param string $actionName 0049 * @param string $controllerName 0050 * @param string $moduleName 0051 * @return Zend_Tool_Project_Profile_Resource 0052 */ 0053 public static function createResource(Zend_Tool_Project_Profile $profile, $actionName, $controllerName, $moduleName = null) 0054 { 0055 0056 if (!is_string($actionName)) { 0057 throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Action::createResource() expects \"actionName\" is the name of a action resource to create.'); 0058 } 0059 0060 if (!is_string($controllerName)) { 0061 throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Action::createResource() expects \"controllerName\" is the name of a controller resource to create.'); 0062 } 0063 0064 $controllerFile = self::_getControllerFileResource($profile, $controllerName, $moduleName); 0065 0066 $actionMethod = $controllerFile->createResource('ActionMethod', array('actionName' => $actionName)); 0067 0068 return $actionMethod; 0069 } 0070 0071 /** 0072 * hasResource() 0073 * 0074 * @param Zend_Tool_Project_Profile $profile 0075 * @param string $actionName 0076 * @param string $controllerName 0077 * @param string $moduleName 0078 * @return Zend_Tool_Project_Profile_Resource 0079 */ 0080 public static function hasResource(Zend_Tool_Project_Profile $profile, $actionName, $controllerName, $moduleName = null) 0081 { 0082 if (!is_string($actionName)) { 0083 throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Action::createResource() expects \"actionName\" is the name of a action resource to create.'); 0084 } 0085 0086 if (!is_string($controllerName)) { 0087 throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Action::createResource() expects \"controllerName\" is the name of a controller resource to create.'); 0088 } 0089 0090 $controllerFile = self::_getControllerFileResource($profile, $controllerName, $moduleName); 0091 0092 if ($controllerFile == null) { 0093 throw new Zend_Tool_Project_Provider_Exception('Controller ' . $controllerName . ' was not found.'); 0094 } 0095 0096 return (($controllerFile->search(array('actionMethod' => array('actionName' => $actionName)))) instanceof Zend_Tool_Project_Profile_Resource); 0097 } 0098 0099 /** 0100 * _getControllerFileResource() 0101 * 0102 * @param Zend_Tool_Project_Profile $profile 0103 * @param string $controllerName 0104 * @param string $moduleName 0105 * @return Zend_Tool_Project_Profile_Resource 0106 */ 0107 protected static function _getControllerFileResource(Zend_Tool_Project_Profile $profile, $controllerName, $moduleName = null) 0108 { 0109 $profileSearchParams = array(); 0110 0111 if ($moduleName != null && is_string($moduleName)) { 0112 $profileSearchParams = array('modulesDirectory', 'moduleDirectory' => array('moduleName' => $moduleName)); 0113 } 0114 0115 $profileSearchParams[] = 'controllersDirectory'; 0116 $profileSearchParams['controllerFile'] = array('controllerName' => $controllerName); 0117 0118 return $profile->search($profileSearchParams); 0119 } 0120 0121 /** 0122 * create() 0123 * 0124 * @param string $name Action name for controller, in camelCase format. 0125 * @param string $controllerName Controller name action should be applied to. 0126 * @param bool $viewIncluded Whether the view should the view be included. 0127 * @param string $module Module name action should be applied to. 0128 */ 0129 public function create($name, $controllerName = 'Index', $viewIncluded = true, $module = null) 0130 { 0131 0132 $this->_loadProfile(); 0133 0134 // get request/response object 0135 $request = $this->_registry->getRequest(); 0136 $response = $this->_registry->getResponse(); 0137 0138 // determine if testing is enabled in the project 0139 // require_once 'Zend/Tool/Project/Provider/Test.php'; 0140 $testingEnabled = Zend_Tool_Project_Provider_Test::isTestingEnabled($this->_loadedProfile); 0141 0142 if ($testingEnabled && !Zend_Tool_Project_Provider_Test::isPHPUnitAvailable()) { 0143 $testingEnabled = false; 0144 $response->appendContent( 0145 'Note: PHPUnit is required in order to generate controller test stubs.', 0146 array('color' => array('yellow')) 0147 ); 0148 } 0149 0150 // Check that there is not a dash or underscore, return if doesnt match regex 0151 if (preg_match('#[_-]#', $name)) { 0152 throw new Zend_Tool_Project_Provider_Exception('Action names should be camel cased.'); 0153 } 0154 0155 $originalName = $name; 0156 $originalControllerName = $controllerName; 0157 0158 // ensure it is camelCase (lower first letter) 0159 $name = strtolower(substr($name, 0, 1)) . substr($name, 1); 0160 0161 // ensure controller is MixedCase 0162 $controllerName = ucfirst($controllerName); 0163 0164 if (self::hasResource($this->_loadedProfile, $name, $controllerName, $module)) { 0165 throw new Zend_Tool_Project_Provider_Exception('This controller (' . $controllerName . ') already has an action named (' . $name . ')'); 0166 } 0167 0168 $actionMethodResource = self::createResource($this->_loadedProfile, $name, $controllerName, $module); 0169 0170 $testActionMethodResource = null; 0171 if ($testingEnabled) { 0172 $testActionMethodResource = Zend_Tool_Project_Provider_Test::createApplicationResource($this->_loadedProfile, $controllerName, $name, $module); 0173 } 0174 0175 // alert the user about inline converted names 0176 $tense = (($request->isPretend()) ? 'would be' : 'is'); 0177 0178 if ($name !== $originalName) { 0179 $response->appendContent( 0180 'Note: The canonical action name that ' . $tense 0181 . ' used with other providers is "' . $name . '";' 0182 . ' not "' . $originalName . '" as supplied', 0183 array('color' => array('yellow')) 0184 ); 0185 } 0186 0187 if ($controllerName !== $originalControllerName) { 0188 $response->appendContent( 0189 'Note: The canonical controller name that ' . $tense 0190 . ' used with other providers is "' . $controllerName . '";' 0191 . ' not "' . $originalControllerName . '" as supplied', 0192 array('color' => array('yellow')) 0193 ); 0194 } 0195 0196 unset($tense); 0197 0198 if ($request->isPretend()) { 0199 $response->appendContent( 0200 'Would create an action named ' . $name . 0201 ' inside controller at ' . $actionMethodResource->getParentResource()->getContext()->getPath() 0202 ); 0203 0204 if ($testActionMethodResource) { 0205 $response->appendContent('Would create an action test in ' . $testActionMethodResource->getParentResource()->getContext()->getPath()); 0206 } 0207 0208 } else { 0209 $response->appendContent( 0210 'Creating an action named ' . $name . 0211 ' inside controller at ' . $actionMethodResource->getParentResource()->getContext()->getPath() 0212 ); 0213 $actionMethodResource->create(); 0214 0215 if ($testActionMethodResource) { 0216 $response->appendContent('Creating an action test in ' . $testActionMethodResource->getParentResource()->getContext()->getPath()); 0217 $testActionMethodResource->create(); 0218 } 0219 0220 $this->_storeProfile(); 0221 } 0222 0223 if ($viewIncluded) { 0224 $viewResource = Zend_Tool_Project_Provider_View::createResource($this->_loadedProfile, $name, $controllerName, $module); 0225 0226 if ($this->_registry->getRequest()->isPretend()) { 0227 $response->appendContent( 0228 'Would create a view script for the ' . $name . ' action method at ' . $viewResource->getContext()->getPath() 0229 ); 0230 } else { 0231 $response->appendContent( 0232 'Creating a view script for the ' . $name . ' action method at ' . $viewResource->getContext()->getPath() 0233 ); 0234 $viewResource->create(); 0235 $this->_storeProfile(); 0236 } 0237 0238 } 0239 0240 } 0241 0242 }