File indexing completed on 2025-01-19 05:21:37
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 * @category Zend 0025 * @package Zend_Tool 0026 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0027 * @license http://framework.zend.com/license/new-bsd New BSD License 0028 */ 0029 class Zend_Tool_Project_Provider_Controller 0030 extends Zend_Tool_Project_Provider_Abstract 0031 implements Zend_Tool_Framework_Provider_Pretendable 0032 { 0033 0034 /** 0035 * createResource will create the controllerFile resource at the appropriate location in the 0036 * profile. NOTE: it is your job to execute the create() method on the resource, as well as 0037 * store the profile when done. 0038 * 0039 * @param Zend_Tool_Project_Profile $profile 0040 * @param string $controllerName 0041 * @param string $moduleName 0042 * @return Zend_Tool_Project_Profile_Resource 0043 */ 0044 public static function createResource(Zend_Tool_Project_Profile $profile, $controllerName, $moduleName = null) 0045 { 0046 if (!is_string($controllerName)) { 0047 throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Controller::createResource() expects \"controllerName\" is the name of a controller resource to create.'); 0048 } 0049 0050 if (!($controllersDirectory = self::_getControllersDirectoryResource($profile, $moduleName))) { 0051 if ($moduleName) { 0052 $exceptionMessage = 'A controller directory for module "' . $moduleName . '" was not found.'; 0053 } else { 0054 $exceptionMessage = 'A controller directory was not found.'; 0055 } 0056 throw new Zend_Tool_Project_Provider_Exception($exceptionMessage); 0057 } 0058 0059 $newController = $controllersDirectory->createResource( 0060 'controllerFile', 0061 array('controllerName' => $controllerName, 'moduleName' => $moduleName) 0062 ); 0063 0064 return $newController; 0065 } 0066 0067 /** 0068 * hasResource() 0069 * 0070 * @param Zend_Tool_Project_Profile $profile 0071 * @param string $controllerName 0072 * @param string $moduleName 0073 * @return boolean 0074 */ 0075 public static function hasResource(Zend_Tool_Project_Profile $profile, $controllerName, $moduleName = null) 0076 { 0077 if (!is_string($controllerName)) { 0078 throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Controller::createResource() expects \"controllerName\" is the name of a controller resource to create.'); 0079 } 0080 0081 $controllersDirectory = self::_getControllersDirectoryResource($profile, $moduleName); 0082 return ($controllersDirectory &&($controllersDirectory->search(array('controllerFile' => array('controllerName' => $controllerName)))) instanceof Zend_Tool_Project_Profile_Resource); 0083 } 0084 0085 /** 0086 * _getControllersDirectoryResource() 0087 * 0088 * @param Zend_Tool_Project_Profile $profile 0089 * @param string $moduleName 0090 * @return Zend_Tool_Project_Profile_Resource 0091 */ 0092 protected static function _getControllersDirectoryResource(Zend_Tool_Project_Profile $profile, $moduleName = null) 0093 { 0094 $profileSearchParams = array(); 0095 0096 if ($moduleName != null && is_string($moduleName)) { 0097 $profileSearchParams = array('modulesDirectory', 'moduleDirectory' => array('moduleName' => $moduleName)); 0098 } 0099 0100 $profileSearchParams[] = 'controllersDirectory'; 0101 0102 return $profile->search($profileSearchParams); 0103 } 0104 0105 /** 0106 * Create a new controller 0107 * 0108 * @param string $name The name of the controller to create, in camelCase. 0109 * @param bool $indexActionIncluded Whether or not to create the index action. 0110 */ 0111 public function create($name, $indexActionIncluded = true, $module = null) 0112 { 0113 $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION); 0114 0115 // get request & response 0116 $request = $this->_registry->getRequest(); 0117 $response = $this->_registry->getResponse(); 0118 0119 // determine if testing is enabled in the project 0120 // require_once 'Zend/Tool/Project/Provider/Test.php'; 0121 $testingEnabled = Zend_Tool_Project_Provider_Test::isTestingEnabled($this->_loadedProfile); 0122 0123 if ($testingEnabled && !Zend_Tool_Project_Provider_Test::isPHPUnitAvailable()) { 0124 $testingEnabled = false; 0125 $response->appendContent( 0126 'Note: PHPUnit is required in order to generate controller test stubs.', 0127 array('color' => array('yellow')) 0128 ); 0129 } 0130 0131 $originalName = $name; 0132 $name = ucfirst($name); 0133 0134 if (self::hasResource($this->_loadedProfile, $name, $module)) { 0135 throw new Zend_Tool_Project_Provider_Exception('This project already has a controller named ' . $name); 0136 } 0137 0138 // Check that there is not a dash or underscore, return if doesnt match regex 0139 if (preg_match('#[_-]#', $name)) { 0140 throw new Zend_Tool_Project_Provider_Exception('Controller names should be camel cased.'); 0141 } 0142 0143 try { 0144 $controllerResource = self::createResource($this->_loadedProfile, $name, $module); 0145 if ($indexActionIncluded) { 0146 $indexActionResource = Zend_Tool_Project_Provider_Action::createResource($this->_loadedProfile, 'index', $name, $module); 0147 $indexActionViewResource = Zend_Tool_Project_Provider_View::createResource($this->_loadedProfile, 'index', $name, $module); 0148 } 0149 if ($testingEnabled) { 0150 $testActionResource = Zend_Tool_Project_Provider_Test::createApplicationResource($this->_loadedProfile, $name, 'index', $module); 0151 } 0152 0153 } catch (Exception $e) { 0154 $response->setException($e); 0155 return; 0156 } 0157 0158 // determime if we need to note to the user about the name 0159 if (($name !== $originalName)) { 0160 $tense = (($request->isPretend()) ? 'would be' : 'is'); 0161 $response->appendContent( 0162 'Note: The canonical controller name that ' . $tense 0163 . ' used with other providers is "' . $name . '";' 0164 . ' not "' . $originalName . '" as supplied', 0165 array('color' => array('yellow')) 0166 ); 0167 unset($tense); 0168 } 0169 0170 // do the creation 0171 if ($request->isPretend()) { 0172 0173 $response->appendContent('Would create a controller at ' . $controllerResource->getContext()->getPath()); 0174 0175 if (isset($indexActionResource)) { 0176 $response->appendContent('Would create an index action method in controller ' . $name); 0177 $response->appendContent('Would create a view script for the index action method at ' . $indexActionViewResource->getContext()->getPath()); 0178 } 0179 0180 if ($testingEnabled) { 0181 $response->appendContent('Would create a controller test file at ' . $testActionResource->getParentResource()->getContext()->getPath()); 0182 } 0183 0184 } else { 0185 0186 $response->appendContent('Creating a controller at ' . $controllerResource->getContext()->getPath()); 0187 $controllerResource->create(); 0188 0189 if (isset($indexActionResource)) { 0190 $response->appendContent('Creating an index action method in controller ' . $name); 0191 $indexActionResource->create(); 0192 $response->appendContent('Creating a view script for the index action method at ' . $indexActionViewResource->getContext()->getPath()); 0193 $indexActionViewResource->create(); 0194 } 0195 0196 if ($testingEnabled) { 0197 $response->appendContent('Creating a controller test file at ' . $testActionResource->getParentResource()->getContext()->getPath()); 0198 $testActionResource->getParentResource()->create(); 0199 $testActionResource->create(); 0200 } 0201 0202 $this->_storeProfile(); 0203 } 0204 0205 } 0206 0207 0208 0209 }