1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Tool
* @subpackage Framework
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Controller.php 20967 2010-02-07 18:17:49Z ralph $
*/
/**
* @category Zend
* @package Zend_Tool
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Tool_Project_Provider_Controller
extends Zend_Tool_Project_Provider_Abstract
implements Zend_Tool_Framework_Provider_Pretendable
{
/**
* createResource will create the controllerFile resource at the appropriate location in the
* profile. NOTE: it is your job to execute the create() method on the resource, as well as
* store the profile when done.
*
* @param Zend_Tool_Project_Profile $profile
* @param string $controllerName
* @param string $moduleName
* @return Zend_Tool_Project_Profile_Resource
*/
public static function createResource(Zend_Tool_Project_Profile $profile, $controllerName, $moduleName = null)
{
if (!is_string($controllerName)) {
throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Controller::createResource() expects \"controllerName\" is the name of a controller resource to create.');
}
if (!($controllersDirectory = self::_getControllersDirectoryResource($profile, $moduleName))) {
if ($moduleName) {
$exceptionMessage = 'A controller directory for module "' . $moduleName . '" was not found.';
} else {
$exceptionMessage = 'A controller directory was not found.';
}
throw new Zend_Tool_Project_Provider_Exception($exceptionMessage);
}
$newController = $controllersDirectory->createResource(
'controllerFile',
array('controllerName' => $controllerName, 'moduleName' => $moduleName)
);
return $newController;
}
/**
* hasResource()
*
* @param Zend_Tool_Project_Profile $profile
* @param string $controllerName
* @param string $moduleName
* @return Zend_Tool_Project_Profile_Resource
*/
public static function hasResource(Zend_Tool_Project_Profile $profile, $controllerName, $moduleName = null)
{
if (!is_string($controllerName)) {
throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Controller::createResource() expects \"controllerName\" is the name of a controller resource to create.');
}
$controllersDirectory = self::_getControllersDirectoryResource($profile, $moduleName);
return (($controllersDirectory->search(array('controllerFile' => array('controllerName' => $controllerName)))) instanceof Zend_Tool_Project_Profile_Resource);
}
/**
* _getControllersDirectoryResource()
*
* @param Zend_Tool_Project_Profile $profile
* @param string $moduleName
* @return Zend_Tool_Project_Profile_Resource
*/
protected static function _getControllersDirectoryResource(Zend_Tool_Project_Profile $profile, $moduleName = null)
{
$profileSearchParams = array();
if ($moduleName != null && is_string($moduleName)) {
$profileSearchParams = array('modulesDirectory', 'moduleDirectory' => array('moduleName' => $moduleName));
}
$profileSearchParams[] = 'controllersDirectory';
return $profile->search($profileSearchParams);
}
/**
* Create a new controller
*
* @param string $name The name of the controller to create, in camelCase.
* @param bool $indexActionIncluded Whether or not to create the index action.
*/
public function create($name, $indexActionIncluded = true, $module = null)
{
$this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
// determine if testing is enabled in the project
require_once 'Zend/Tool/Project/Provider/Test.php';
$testingEnabled = Zend_Tool_Project_Provider_Test::isTestingEnabled($this->_loadedProfile);
if (self::hasResource($this->_loadedProfile, $name, $module)) {
throw new Zend_Tool_Project_Provider_Exception('This project already has a controller named ' . $name);
}
// Check that there is not a dash or underscore, return if doesnt match regex
if (preg_match('#[_-]#', $name)) {
throw new Zend_Tool_Project_Provider_Exception('Controller names should be camel cased.');
}
$originalName = $name;
$name = ucfirst($name);
// get request & response
$request = $this->_registry->getRequest();
$response = $this->_registry->getResponse();
try {
$controllerResource = self::createResource($this->_loadedProfile, $name, $module);
if ($indexActionIncluded) {
$indexActionResource = Zend_Tool_Project_Provider_Action::createResource($this->_loadedProfile, 'index', $name, $module);
$indexActionViewResource = Zend_Tool_Project_Provider_View::createResource($this->_loadedProfile, 'index', $name, $module);
}
if ($testingEnabled) {
$testControllerResource = Zend_Tool_Project_Provider_Test::createApplicationResource($this->_loadedProfile, $name, 'index', $module);
}
} catch (Exception $e) {
$response->setException($e);
return;
}
// determime if we need to note to the user about the name
if (($name !== $originalName)) {
$tense = (($request->isPretend()) ? 'would be' : 'is');
$response->appendContent(
'Note: The canonical controller name that ' . $tense
. ' used with other providers is "' . $name . '";'
. ' not "' . $originalName . '" as supplied',
array('color' => array('yellow'))
);
unset($tense);
}
// do the creation
if ($request->isPretend()) {
$response->appendContent('Would create a controller at ' . $controllerResource->getContext()->getPath());
if (isset($indexActionResource)) {
$response->appendContent('Would create an index action method in controller ' . $name);
$response->appendContent('Would create a view script for the index action method at ' . $indexActionViewResource->getContext()->getPath());
}
if ($testControllerResource) {
$response->appendContent('Would create a controller test file at ' . $testControllerResource->getContext()->getPath());
}
} else {
$response->appendContent('Creating a controller at ' . $controllerResource->getContext()->getPath());
$controllerResource->create();
if (isset($indexActionResource)) {
$response->appendContent('Creating an index action method in controller ' . $name);
$indexActionResource->create();
$response->appendContent('Creating a view script for the index action method at ' . $indexActionViewResource->getContext()->getPath());
$indexActionViewResource->create();
}
if ($testControllerResource) {
$response->appendContent('Creating a controller test file at ' . $testControllerResource->getContext()->getPath());
$testControllerResource->create();
}
$this->_storeProfile();
}
}
}
|