File indexing completed on 2024-06-23 05:55:52

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_Form extends Zend_Tool_Project_Provider_Abstract
0030 {
0031 
0032     public static function createResource(Zend_Tool_Project_Profile $profile, $formName, $moduleName = null)
0033     {
0034         if (!is_string($formName)) {
0035             throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Form::createResource() expects \"formName\" is the name of a form resource to create.');
0036         }
0037 
0038         if (!($formsDirectory = self::_getFormsDirectoryResource($profile, $moduleName))) {
0039             if ($moduleName) {
0040                 $exceptionMessage = 'A form directory for module "' . $moduleName . '" was not found.';
0041             } else {
0042                 $exceptionMessage = 'A form directory was not found.';
0043             }
0044             throw new Zend_Tool_Project_Provider_Exception($exceptionMessage);
0045         }
0046 
0047         $newForm = $formsDirectory->createResource(
0048             'formFile',
0049             array('formName' => $formName, 'moduleName' => $moduleName)
0050             );
0051 
0052         return $newForm;
0053     }
0054 
0055     /**
0056      * hasResource()
0057      *
0058      * @param Zend_Tool_Project_Profile $profile
0059      * @param string $formName
0060      * @param string $moduleName
0061      * @return Zend_Tool_Project_Profile_Resource
0062      */
0063     public static function hasResource(Zend_Tool_Project_Profile $profile, $formName, $moduleName = null)
0064     {
0065         if (!is_string($formName)) {
0066             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.');
0067         }
0068 
0069         $formsDirectory = self::_getFormsDirectoryResource($profile, $moduleName);
0070         return (($formsDirectory->search(array('formFile' => array('formName' => $formName)))) instanceof Zend_Tool_Project_Profile_Resource);
0071     }
0072 
0073     /**
0074      * _getFormsDirectoryResource()
0075      *
0076      * @param Zend_Tool_Project_Profile $profile
0077      * @param string $moduleName
0078      * @return Zend_Tool_Project_Profile_Resource
0079      */
0080     protected static function _getFormsDirectoryResource(Zend_Tool_Project_Profile $profile, $moduleName = null)
0081     {
0082         $profileSearchParams = array();
0083 
0084         if ($moduleName != null && is_string($moduleName)) {
0085             $profileSearchParams = array('modulesDirectory', 'moduleDirectory' => array('moduleName' => $moduleName));
0086         }
0087 
0088         $profileSearchParams[] = 'formsDirectory';
0089 
0090         return $profile->search($profileSearchParams);
0091     }
0092 
0093     public function enable($module = null)
0094     {
0095         $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
0096 
0097         // determine if testing is enabled in the project
0098         $testingEnabled = Zend_Tool_Project_Provider_Test::isTestingEnabled($this->_loadedProfile);
0099 
0100         $formDirectoryResource = self::_getFormsDirectoryResource($this->_loadedProfile, $module);
0101 
0102         if ($formDirectoryResource->isEnabled()) {
0103             throw new Zend_Tool_Project_Provider_Exception('This project already has forms enabled.');
0104         } else {
0105             if ($this->_registry->getRequest()->isPretend()) {
0106                 $this->_registry->getResponse()->appendContent('Would enable forms directory at ' . $formDirectoryResource->getContext()->getPath());
0107             } else {
0108                 $this->_registry->getResponse()->appendContent('Enabling forms directory at ' . $formDirectoryResource->getContext()->getPath());
0109                 $formDirectoryResource->setEnabled(true);
0110                 $formDirectoryResource->create();
0111                 $this->_storeProfile();
0112             }
0113 
0114         }
0115     }
0116 
0117     /**
0118      * Create a new form
0119      *
0120      * @param string $name
0121      * @param string $module
0122      */
0123     public function create($name, $module = null)
0124     {
0125         $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
0126 
0127         // determine if testing is enabled in the project
0128         $testingEnabled = Zend_Tool_Project_Provider_Test::isTestingEnabled($this->_loadedProfile);
0129 
0130         if (self::hasResource($this->_loadedProfile, $name, $module)) {
0131             throw new Zend_Tool_Project_Provider_Exception('This project already has a form named ' . $name);
0132         }
0133 
0134         // Check that there is not a dash or underscore, return if doesnt match regex
0135         if (preg_match('#[_-]#', $name)) {
0136             throw new Zend_Tool_Project_Provider_Exception('Form names should be camel cased.');
0137         }
0138 
0139         $name = ucwords($name);
0140 
0141         try {
0142             $formResource = self::createResource($this->_loadedProfile, $name, $module);
0143 
0144             if ($testingEnabled) {
0145                 $testFormResource = null;
0146                 // $testFormResource = Zend_Tool_Project_Provider_Test::createApplicationResource($this->_loadedProfile, $name, 'index', $module);
0147             }
0148 
0149         } catch (Exception $e) {
0150             $response = $this->_registry->getResponse();
0151             $response->setException($e);
0152             return;
0153         }
0154 
0155         // do the creation
0156         if ($this->_registry->getRequest()->isPretend()) {
0157 
0158             $this->_registry->getResponse()->appendContent('Would create a form at '  . $formResource->getContext()->getPath());
0159 
0160             if ($testFormResource) {
0161                 $this->_registry->getResponse()->appendContent('Would create a form test file at ' . $testFormResource->getContext()->getPath());
0162             }
0163 
0164         } else {
0165 
0166             $this->_registry->getResponse()->appendContent('Creating a form at ' . $formResource->getContext()->getPath());
0167             $formResource->create();
0168 
0169             if ($testFormResource) {
0170                 $this->_registry->getResponse()->appendContent('Creating a form test file at ' . $testFormResource->getContext()->getPath());
0171                 $testFormResource->create();
0172             }
0173 
0174             $this->_storeProfile();
0175         }
0176 
0177     }
0178 
0179 
0180 }