File indexing completed on 2024-12-29 05:28:10

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