File indexing completed on 2025-01-19 05:21:37
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_DbTable 0030 extends Zend_Tool_Project_Provider_Abstract 0031 implements Zend_Tool_Framework_Provider_Pretendable 0032 { 0033 0034 protected $_specialties = array('FromDatabase'); 0035 0036 /** 0037 * @var Zend_Filter 0038 */ 0039 protected $_nameFilter = null; 0040 0041 public static function createResource(Zend_Tool_Project_Profile $profile, $dbTableName, $actualTableName, $moduleName = null) 0042 { 0043 $profileSearchParams = array(); 0044 0045 if ($moduleName != null && is_string($moduleName)) { 0046 $profileSearchParams = array('modulesDirectory', 'moduleDirectory' => array('moduleName' => $moduleName)); 0047 } 0048 0049 $profileSearchParams[] = 'modelsDirectory'; 0050 0051 $modelsDirectory = $profile->search($profileSearchParams); 0052 0053 if (!($modelsDirectory instanceof Zend_Tool_Project_Profile_Resource)) { 0054 throw new Zend_Tool_Project_Provider_Exception( 0055 'A models directory was not found' . 0056 (($moduleName) ? ' for module ' . $moduleName . '.' : '.') 0057 ); 0058 } 0059 0060 if (!($dbTableDirectory = $modelsDirectory->search('DbTableDirectory'))) { 0061 $dbTableDirectory = $modelsDirectory->createResource('DbTableDirectory'); 0062 } 0063 0064 $dbTableFile = $dbTableDirectory->createResource('DbTableFile', array('dbTableName' => $dbTableName, 'actualTableName' => $actualTableName)); 0065 0066 return $dbTableFile; 0067 } 0068 0069 public static function hasResource(Zend_Tool_Project_Profile $profile, $dbTableName, $moduleName = null) 0070 { 0071 $profileSearchParams = array(); 0072 0073 if ($moduleName != null && is_string($moduleName)) { 0074 $profileSearchParams = array('modulesDirectory', 'moduleDirectory' => array('moduleName' => $moduleName)); 0075 } 0076 0077 $profileSearchParams[] = 'modelsDirectory'; 0078 0079 $modelsDirectory = $profile->search($profileSearchParams); 0080 0081 if (!($modelsDirectory instanceof Zend_Tool_Project_Profile_Resource) 0082 || !($dbTableDirectory = $modelsDirectory->search('DbTableDirectory'))) { 0083 return false; 0084 } 0085 0086 $dbTableFile = $dbTableDirectory->search(array('DbTableFile' => array('dbTableName' => $dbTableName))); 0087 0088 return ($dbTableFile instanceof Zend_Tool_Project_Profile_Resource) ? true : false; 0089 } 0090 0091 0092 public function create($name, $actualTableName, $module = null, $forceOverwrite = false) 0093 { 0094 $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION); 0095 0096 // Check that there is not a dash or underscore, return if doesnt match regex 0097 if (preg_match('#[_-]#', $name)) { 0098 throw new Zend_Tool_Project_Provider_Exception('DbTable names should be camel cased.'); 0099 } 0100 0101 $originalName = $name; 0102 $name = ucfirst($name); 0103 0104 if ($actualTableName == '') { 0105 throw new Zend_Tool_Project_Provider_Exception('You must provide both the DbTable name as well as the actual db table\'s name.'); 0106 } 0107 0108 if (self::hasResource($this->_loadedProfile, $name, $module)) { 0109 throw new Zend_Tool_Project_Provider_Exception('This project already has a DbTable named ' . $name); 0110 } 0111 0112 // get request/response object 0113 $request = $this->_registry->getRequest(); 0114 $response = $this->_registry->getResponse(); 0115 0116 // alert the user about inline converted names 0117 $tense = (($request->isPretend()) ? 'would be' : 'is'); 0118 0119 if ($name !== $originalName) { 0120 $response->appendContent( 0121 'Note: The canonical model name that ' . $tense 0122 . ' used with other providers is "' . $name . '";' 0123 . ' not "' . $originalName . '" as supplied', 0124 array('color' => array('yellow')) 0125 ); 0126 } 0127 0128 try { 0129 $tableResource = self::createResource($this->_loadedProfile, $name, $actualTableName, $module); 0130 } catch (Exception $e) { 0131 $response = $this->_registry->getResponse(); 0132 $response->setException($e); 0133 return; 0134 } 0135 0136 // do the creation 0137 if ($request->isPretend()) { 0138 $response->appendContent('Would create a DbTable at ' . $tableResource->getContext()->getPath()); 0139 } else { 0140 $response->appendContent('Creating a DbTable at ' . $tableResource->getContext()->getPath()); 0141 $tableResource->create(); 0142 $this->_storeProfile(); 0143 } 0144 } 0145 0146 /** 0147 * @param string $module Module name action should be applied to. 0148 * @param bool $forceOverwrite Whether should force overwriting previous classes generated 0149 * @return void 0150 */ 0151 public function createFromDatabase($module = null, $forceOverwrite = false) 0152 { 0153 $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION); 0154 0155 $bootstrapResource = $this->_loadedProfile->search('BootstrapFile'); 0156 0157 /* @var $zendApp Zend_Application */ 0158 $zendApp = $bootstrapResource->getApplicationInstance(); 0159 0160 try { 0161 $zendApp->bootstrap('db'); 0162 } catch (Zend_Application_Exception $e) { 0163 throw new Zend_Tool_Project_Provider_Exception('Db resource not available, you might need to configure a DbAdapter.'); 0164 return; 0165 } 0166 0167 /* @var $db Zend_Db_Adapter_Abstract */ 0168 $db = $zendApp->getBootstrap()->getResource('db'); 0169 0170 $tableResources = array(); 0171 foreach ($db->listTables() as $actualTableName) { 0172 0173 $dbTableName = $this->_convertTableNameToClassName($actualTableName); 0174 0175 if (!$forceOverwrite && self::hasResource($this->_loadedProfile, $dbTableName, $module)) { 0176 throw new Zend_Tool_Project_Provider_Exception( 0177 'This DbTable resource already exists, if you wish to overwrite it, ' 0178 . 'pass the "forceOverwrite" flag to this provider.' 0179 ); 0180 } 0181 0182 $tableResources[] = self::createResource( 0183 $this->_loadedProfile, 0184 $dbTableName, 0185 $actualTableName, 0186 $module 0187 ); 0188 } 0189 0190 if (count($tableResources) == 0) { 0191 $this->_registry->getResponse()->appendContent('There are no tables in the selected database to write.'); 0192 } 0193 0194 // do the creation 0195 if ($this->_registry->getRequest()->isPretend()) { 0196 0197 foreach ($tableResources as $tableResource) { 0198 $this->_registry->getResponse()->appendContent('Would create a DbTable at ' . $tableResource->getContext()->getPath()); 0199 } 0200 0201 } else { 0202 0203 foreach ($tableResources as $tableResource) { 0204 $this->_registry->getResponse()->appendContent('Creating a DbTable at ' . $tableResource->getContext()->getPath()); 0205 $tableResource->create(); 0206 } 0207 0208 $this->_storeProfile(); 0209 } 0210 0211 0212 } 0213 0214 protected function _convertTableNameToClassName($tableName) 0215 { 0216 if ($this->_nameFilter == null) { 0217 $this->_nameFilter = new Zend_Filter(); 0218 $this->_nameFilter 0219 ->addFilter(new Zend_Filter_Word_UnderscoreToCamelCase()); 0220 } 0221 0222 return $this->_nameFilter->filter($tableName); 0223 } 0224 0225 }