web/lib/Zend/Tool/Project/Provider/Form.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: Form.php 23209 2010-10-21 16:09:34Z 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_Form extends Zend_Tool_Project_Provider_Abstract
       
    30 {
       
    31 
       
    32     public static function createResource(Zend_Tool_Project_Profile $profile, $formName, $moduleName = null)
       
    33     {
       
    34         if (!is_string($formName)) {
       
    35             throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Form::createResource() expects \"formName\" is the name of a form resource to create.');
       
    36         }
       
    37 
       
    38         if (!($formsDirectory = self::_getFormsDirectoryResource($profile, $moduleName))) {
       
    39             if ($moduleName) {
       
    40                 $exceptionMessage = 'A form directory for module "' . $moduleName . '" was not found.';
       
    41             } else {
       
    42                 $exceptionMessage = 'A form directory was not found.';
       
    43             }
       
    44             throw new Zend_Tool_Project_Provider_Exception($exceptionMessage);
       
    45         }
       
    46 
       
    47         $newForm = $formsDirectory->createResource(
       
    48             'formFile', 
       
    49             array('formName' => $formName, 'moduleName' => $moduleName)
       
    50             );
       
    51 
       
    52         return $newForm;
       
    53     }
       
    54 
       
    55     /**
       
    56      * hasResource()
       
    57      *
       
    58      * @param Zend_Tool_Project_Profile $profile
       
    59      * @param string $formName
       
    60      * @param string $moduleName
       
    61      * @return Zend_Tool_Project_Profile_Resource
       
    62      */
       
    63     public static function hasResource(Zend_Tool_Project_Profile $profile, $formName, $moduleName = null)
       
    64     {
       
    65         if (!is_string($formName)) {
       
    66             throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Form::createResource() expects \"formName\" is the name of a form resource to check for existence.');
       
    67         }
       
    68 
       
    69         $formsDirectory = self::_getFormsDirectoryResource($profile, $moduleName);
       
    70         return (($formsDirectory->search(array('formFile' => array('formName' => $formName)))) instanceof Zend_Tool_Project_Profile_Resource);
       
    71     }
       
    72     
       
    73     /**
       
    74      * _getFormsDirectoryResource()
       
    75      *
       
    76      * @param Zend_Tool_Project_Profile $profile
       
    77      * @param string $moduleName
       
    78      * @return Zend_Tool_Project_Profile_Resource
       
    79      */
       
    80     protected static function _getFormsDirectoryResource(Zend_Tool_Project_Profile $profile, $moduleName = null)
       
    81     {
       
    82         $profileSearchParams = array();
       
    83 
       
    84         if ($moduleName != null && is_string($moduleName)) {
       
    85             $profileSearchParams = array('modulesDirectory', 'moduleDirectory' => array('moduleName' => $moduleName));
       
    86         }
       
    87 
       
    88         $profileSearchParams[] = 'formsDirectory';
       
    89 
       
    90         return $profile->search($profileSearchParams);
       
    91     }
       
    92     
       
    93     public function enable($module = null)
       
    94     {
       
    95         $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
       
    96 
       
    97         // determine if testing is enabled in the project
       
    98         $testingEnabled = Zend_Tool_Project_Provider_Test::isTestingEnabled($this->_loadedProfile);
       
    99 
       
   100         $formDirectoryResource = self::_getFormsDirectoryResource($this->_loadedProfile, $module);
       
   101         
       
   102         if ($formDirectoryResource->isEnabled()) {
       
   103             throw new Zend_Tool_Project_Provider_Exception('This project already has forms enabled.');
       
   104         } else {
       
   105             if ($this->_registry->getRequest()->isPretend()) {
       
   106                 $this->_registry->getResponse()->appendContent('Would enable forms directory at ' . $formDirectoryResource->getContext()->getPath());
       
   107             } else {
       
   108                 $this->_registry->getResponse()->appendContent('Enabling forms directory at ' . $formDirectoryResource->getContext()->getPath());
       
   109                 $formDirectoryResource->setEnabled(true);
       
   110                 $formDirectoryResource->create();
       
   111                 $this->_storeProfile();                
       
   112             }
       
   113 
       
   114         }
       
   115     }
       
   116     
       
   117     /**
       
   118      * Create a new form
       
   119      *
       
   120      * @param string $name
       
   121      * @param string $module
       
   122      */
       
   123     public function create($name, $module = null)
       
   124     {
       
   125         $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
       
   126 
       
   127         // determine if testing is enabled in the project
       
   128         $testingEnabled = Zend_Tool_Project_Provider_Test::isTestingEnabled($this->_loadedProfile);
       
   129 
       
   130         if (self::hasResource($this->_loadedProfile, $name, $module)) {
       
   131             throw new Zend_Tool_Project_Provider_Exception('This project already has a form named ' . $name);
       
   132         }
       
   133 
       
   134         // Check that there is not a dash or underscore, return if doesnt match regex
       
   135         if (preg_match('#[_-]#', $name)) {
       
   136             throw new Zend_Tool_Project_Provider_Exception('Form names should be camel cased.');
       
   137         }
       
   138         
       
   139         $name = ucwords($name);
       
   140         
       
   141         try {
       
   142             $formResource = self::createResource($this->_loadedProfile, $name, $module);
       
   143 
       
   144             if ($testingEnabled) {
       
   145                 $testFormResource = null;
       
   146                 // $testFormResource = Zend_Tool_Project_Provider_Test::createApplicationResource($this->_loadedProfile, $name, 'index', $module);
       
   147             }
       
   148 
       
   149         } catch (Exception $e) {
       
   150             $response = $this->_registry->getResponse();
       
   151             $response->setException($e);
       
   152             return;
       
   153         }
       
   154 
       
   155         // do the creation
       
   156         if ($this->_registry->getRequest()->isPretend()) {
       
   157 
       
   158             $this->_registry->getResponse()->appendContent('Would create a form at '  . $formResource->getContext()->getPath());
       
   159 
       
   160             if ($testFormResource) {
       
   161                 $this->_registry->getResponse()->appendContent('Would create a form test file at ' . $testFormResource->getContext()->getPath());
       
   162             }
       
   163 
       
   164         } else {
       
   165 
       
   166             $this->_registry->getResponse()->appendContent('Creating a form at ' . $formResource->getContext()->getPath());
       
   167             $formResource->create();
       
   168 
       
   169             if ($testFormResource) {
       
   170                 $this->_registry->getResponse()->appendContent('Creating a form test file at ' . $testFormResource->getContext()->getPath());
       
   171                 $testFormResource->create();
       
   172             }
       
   173 
       
   174             $this->_storeProfile();
       
   175         }
       
   176 
       
   177     }
       
   178 
       
   179 
       
   180 }