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