web/lib/Zend/Tool/Project/Provider/DbTable.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: DbTable.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_DbTable 
       
    30     extends Zend_Tool_Project_Provider_Abstract
       
    31     implements Zend_Tool_Framework_Provider_Pretendable
       
    32 {
       
    33     
       
    34     protected $_specialties = array('FromDatabase');
       
    35     
       
    36     /**
       
    37      * @var Zend_Filter
       
    38      */
       
    39     protected $_nameFilter = null;
       
    40     
       
    41     public static function createResource(Zend_Tool_Project_Profile $profile, $dbTableName, $actualTableName, $moduleName = null)
       
    42     {
       
    43         $profileSearchParams = array();
       
    44 
       
    45         if ($moduleName != null && is_string($moduleName)) {
       
    46             $profileSearchParams = array('modulesDirectory', 'moduleDirectory' => array('moduleName' => $moduleName));
       
    47         }
       
    48 
       
    49         $profileSearchParams[] = 'modelsDirectory';
       
    50         
       
    51         $modelsDirectory = $profile->search($profileSearchParams);
       
    52         
       
    53         if (!($modelsDirectory instanceof Zend_Tool_Project_Profile_Resource)) {
       
    54             throw new Zend_Tool_Project_Provider_Exception(
       
    55                 'A models directory was not found' .
       
    56                 (($moduleName) ? ' for module ' . $moduleName . '.' : '.')
       
    57                 );
       
    58         }
       
    59         
       
    60         if (!($dbTableDirectory = $modelsDirectory->search('DbTableDirectory'))) {
       
    61             $dbTableDirectory = $modelsDirectory->createResource('DbTableDirectory');
       
    62         }
       
    63         
       
    64         $dbTableFile = $dbTableDirectory->createResource('DbTableFile', array('dbTableName' => $dbTableName, 'actualTableName' => $actualTableName));
       
    65         
       
    66         return $dbTableFile;
       
    67     }
       
    68     
       
    69     public static function hasResource(Zend_Tool_Project_Profile $profile, $dbTableName, $moduleName = null)
       
    70     {
       
    71         $profileSearchParams = array();
       
    72 
       
    73         if ($moduleName != null && is_string($moduleName)) {
       
    74             $profileSearchParams = array('modulesDirectory', 'moduleDirectory' => array('moduleName' => $moduleName));
       
    75         }
       
    76 
       
    77         $profileSearchParams[] = 'modelsDirectory';
       
    78         
       
    79         $modelsDirectory = $profile->search($profileSearchParams);
       
    80         
       
    81         if (!($modelsDirectory instanceof Zend_Tool_Project_Profile_Resource)
       
    82             || !($dbTableDirectory = $modelsDirectory->search('DbTableDirectory'))) {
       
    83             return false;
       
    84         }
       
    85         
       
    86         $dbTableFile = $dbTableDirectory->search(array('DbTableFile' => array('dbTableName' => $dbTableName)));
       
    87         
       
    88         return ($dbTableFile instanceof Zend_Tool_Project_Profile_Resource) ? true : false;
       
    89     }
       
    90       
       
    91     
       
    92     public function create($name, $actualTableName, $module = null, $forceOverwrite = false)
       
    93     {
       
    94         $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
       
    95 
       
    96         // Check that there is not a dash or underscore, return if doesnt match regex
       
    97         if (preg_match('#[_-]#', $name)) {
       
    98             throw new Zend_Tool_Project_Provider_Exception('DbTable names should be camel cased.');
       
    99         }
       
   100         
       
   101         $originalName = $name;
       
   102         $name = ucfirst($name);
       
   103         
       
   104         if ($actualTableName == '') {
       
   105             throw new Zend_Tool_Project_Provider_Exception('You must provide both the DbTable name as well as the actual db table\'s name.');
       
   106         }
       
   107         
       
   108         if (self::hasResource($this->_loadedProfile, $name, $module)) {
       
   109             throw new Zend_Tool_Project_Provider_Exception('This project already has a DbTable named ' . $name);
       
   110         }
       
   111 
       
   112         // get request/response object
       
   113         $request = $this->_registry->getRequest();
       
   114         $response = $this->_registry->getResponse();
       
   115         
       
   116         // alert the user about inline converted names
       
   117         $tense = (($request->isPretend()) ? 'would be' : 'is');
       
   118         
       
   119         if ($name !== $originalName) {
       
   120             $response->appendContent(
       
   121                 'Note: The canonical model name that ' . $tense
       
   122                     . ' used with other providers is "' . $name . '";'
       
   123                     . ' not "' . $originalName . '" as supplied',
       
   124                 array('color' => array('yellow'))
       
   125                 );
       
   126         }
       
   127         
       
   128         try {
       
   129             $tableResource = self::createResource($this->_loadedProfile, $name, $actualTableName, $module);
       
   130         } catch (Exception $e) {
       
   131             $response = $this->_registry->getResponse();
       
   132             $response->setException($e);
       
   133             return;
       
   134         }
       
   135 
       
   136         // do the creation
       
   137         if ($request->isPretend()) {
       
   138             $response->appendContent('Would create a DbTable at '  . $tableResource->getContext()->getPath());
       
   139         } else {
       
   140             $response->appendContent('Creating a DbTable at ' . $tableResource->getContext()->getPath());
       
   141             $tableResource->create();
       
   142             $this->_storeProfile();
       
   143         }
       
   144     }
       
   145     
       
   146     public function createFromDatabase($module = null, $forceOverwrite = false)
       
   147     {
       
   148         $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
       
   149         
       
   150         $bootstrapResource = $this->_loadedProfile->search('BootstrapFile');
       
   151         
       
   152         /* @var $zendApp Zend_Application */
       
   153         $zendApp = $bootstrapResource->getApplicationInstance();
       
   154         
       
   155         try {
       
   156             $zendApp->bootstrap('db');
       
   157         } catch (Zend_Application_Exception $e) {
       
   158             throw new Zend_Tool_Project_Provider_Exception('Db resource not available, you might need to configure a DbAdapter.');
       
   159             return;
       
   160         }
       
   161         
       
   162         /* @var $db Zend_Db_Adapter_Abstract */
       
   163         $db = $zendApp->getBootstrap()->getResource('db');
       
   164         
       
   165         $tableResources = array();
       
   166         foreach ($db->listTables() as $actualTableName) {
       
   167             
       
   168             $dbTableName = $this->_convertTableNameToClassName($actualTableName);
       
   169             
       
   170             if (!$forceOverwrite && self::hasResource($this->_loadedProfile, $dbTableName, $module)) {
       
   171                 throw new Zend_Tool_Project_Provider_Exception(
       
   172                     'This DbTable resource already exists, if you wish to overwrite it, '
       
   173                     . 'pass the "forceOverwrite" flag to this provider.'
       
   174                     );
       
   175             }
       
   176             
       
   177             $tableResources[] = self::createResource(
       
   178                 $this->_loadedProfile,
       
   179                 $dbTableName,
       
   180                 $actualTableName,
       
   181                 $module
       
   182                 );
       
   183         }
       
   184         
       
   185         if (count($tableResources) == 0) {
       
   186             $this->_registry->getResponse()->appendContent('There are no tables in the selected database to write.');
       
   187         }
       
   188         
       
   189         // do the creation
       
   190         if ($this->_registry->getRequest()->isPretend()) {
       
   191 
       
   192             foreach ($tableResources as $tableResource) {
       
   193                 $this->_registry->getResponse()->appendContent('Would create a DbTable at '  . $tableResource->getContext()->getPath());
       
   194             }
       
   195 
       
   196         } else {
       
   197 
       
   198             foreach ($tableResources as $tableResource) {
       
   199                 $this->_registry->getResponse()->appendContent('Creating a DbTable at ' . $tableResource->getContext()->getPath());
       
   200                 $tableResource->create();
       
   201             }
       
   202 
       
   203             $this->_storeProfile();
       
   204         }
       
   205         
       
   206         
       
   207     }
       
   208     
       
   209     protected function _convertTableNameToClassName($tableName)
       
   210     {
       
   211         if ($this->_nameFilter == null) {
       
   212             $this->_nameFilter = new Zend_Filter();
       
   213             $this->_nameFilter
       
   214                 ->addFilter(new Zend_Filter_Word_UnderscoreToCamelCase());
       
   215         }
       
   216         
       
   217         return $this->_nameFilter->filter($tableName);
       
   218     }
       
   219     
       
   220 }