Pārlūkot izejas kodu

ZF-7940
- DbTable scan from database is supported
- Client & Argument parsing fixes
- Help system modified to handle specialty only providers
- Tests cleanup

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@19710 44c647ce-9c0f-0410-b52a-842ac1e357ba

ralph 16 gadi atpakaļ
vecāks
revīzija
0393662a29

+ 5 - 4
library/Zend/Tool/Framework/Client/Abstract.php

@@ -280,8 +280,9 @@ abstract class Zend_Tool_Framework_Client_Abstract implements Zend_Tool_Framewor
 
         // get the action name
         $actionName = $this->_registry->getRequest()->getActionName();
+        $specialtyName = $this->_registry->getRequest()->getSpecialtyName();
 
-        if (!$actionableMethod = $providerSignature->getActionableMethodByActionName($actionName)) {
+        if (!$actionableMethod = $providerSignature->getActionableMethodByActionName($actionName, $specialtyName)) {
             require_once 'Zend/Tool/Framework/Client/Exception.php';
             throw new Zend_Tool_Framework_Client_Exception('Dispatcher error - actionable method not found');
         }
@@ -314,9 +315,9 @@ abstract class Zend_Tool_Framework_Client_Abstract implements Zend_Tool_Framewor
             }
         }
 
-        if (($specialtyName = $this->_registry->getRequest()->getSpecialtyName()) != '_Global') {
-            $methodName .= $specialtyName;
-        }
+//        if ($specialtyName != '_Global') {
+//            $methodName .= $specialtyName;
+//        }
 
         $this->_handleDispatchExecution($provider, $methodName, $callParameters);
     }

+ 18 - 11
library/Zend/Tool/Framework/Client/Console/HelpSystem.php

@@ -242,6 +242,14 @@ class Zend_Tool_Framework_Client_Console_HelpSystem
                     'clientName'    => 'console'
                     ));
 
+                $actionableGlobalMetadatas = $manifest->getMetadatas(array(
+                    'type'          => 'Tool',
+                    'name'          => 'actionableMethodLongParams',
+                    'providerName'  => $providerName,
+                    'actionName'    => $actionName,
+                    'clientName'    => 'console'
+                    ));
+
                 if ($actionableGlobalLongParamMetadata) {
 
                     if (!$providerNameDisplayed) {
@@ -256,19 +264,15 @@ class Zend_Tool_Framework_Client_Console_HelpSystem
                     $actionIsGlobal = false;
                 }
 
-                $actionableGlobalMetadatas = $manifest->getMetadatas(array(
-                    'type'          => 'Tool',
-                    'name'          => 'actionableMethodLongParams',
-                    'providerName'  => $providerName,
-                    'actionName'    => $actionName,
-                    'clientName'    => 'console'
-                    ));
-
+                // check for providers without a _Global action
+                $isSingleSpecialProviderAction = false;
                 if (!$actionIsGlobal && count($actionableGlobalMetadatas) == 1) {
-                    $this->_response->appendContent('single special action/provider');
+                    $isSingleSpecialProviderAction = true;
+                    $this->_respondWithProviderName($providerMetadata);
+                    $providerNameDisplayed = true;
                 }
-
-                if ($includeAllSpecialties) {
+                
+                if ($includeAllSpecialties || $isSingleSpecialProviderAction) {
 
                     foreach ($providerSignature->getSpecialties() as $specialtyName) {
 
@@ -299,6 +303,9 @@ class Zend_Tool_Framework_Client_Console_HelpSystem
 
                     }
                 }
+                
+                // reset the special flag for single provider action with specialty
+                $isSingleSpecialProviderAction = false;
 
                 if (!$includeAllSpecialties && count($actionableGlobalMetadatas) > 1) {
                     $this->_response->appendContent('    Note: There are specialties, use ', array('color' => 'yellow', 'separator' => false));

+ 3 - 2
library/Zend/Tool/Framework/Provider/Signature.php

@@ -202,10 +202,11 @@ class Zend_Tool_Framework_Provider_Signature implements Zend_Tool_Framework_Regi
      * @param string $actionName
      * @return array
      */
-    public function getActionableMethodByActionName($actionName)
+    public function getActionableMethodByActionName($actionName, $specialtyName = '_Global')
     {
         foreach ($this->_actionableMethods as $actionableMethod) {
-            if ($actionName == $actionableMethod['actionName']) {
+            if ($actionName == $actionableMethod['actionName']
+                && $specialtyName == $actionableMethod['specialty']) {
                 return $actionableMethod;
             }
         }

+ 42 - 3
library/Zend/Tool/Project/Context/Zf/AbstractClassFile.php

@@ -1,11 +1,49 @@
 <?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Tool
+ * @subpackage Framework
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: BootstrapFile.php 19643 2009-12-14 14:57:07Z ralph $
+ */
 
+/**
+ * This class is the front most class for utilizing Zend_Tool_Project
+ *
+ * A profile is a hierarchical set of resources that keep track of
+ * items within a specific project.
+ *
+ * @category   Zend
+ * @package    Zend_Tool
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
 abstract class Zend_Tool_Project_Context_Zf_AbstractClassFile extends Zend_Tool_Project_Context_Filesystem_File
 {
     
+    /**
+     * getFullClassName()
+     * 
+     * @param $localClassName
+     * @param $classContextName
+     */
     public function getFullClassName($localClassName, $classContextName = null)
     {
 
+        // find the ApplicationDirectory OR ModuleDirectory
         $currentResource = $this->_resource;
         do {
             $resourceName = $currentResource->getName();
@@ -17,7 +55,8 @@ abstract class Zend_Tool_Project_Context_Zf_AbstractClassFile extends Zend_Tool_
             && $currentResource = $currentResource->getParentResource());
         
         $fullClassName = '';
-            
+
+        // go find the proper prefix
         if (isset($containingResource)) {
             if ($containingResource->getName() == 'ApplicationDirectory') {
                 $prefix = $containingResource->getAttribute('classNamePrefix');
@@ -27,12 +66,12 @@ abstract class Zend_Tool_Project_Context_Zf_AbstractClassFile extends Zend_Tool_
                 $fullClassName = $prefix;    
             }
         }
-                
+
         if ($classContextName) {
             $fullClassName .= rtrim($classContextName, '_') . '_';
         }
         $fullClassName .= $localClassName;
-                    
+
         return $fullClassName;
     }
 

+ 30 - 35
library/Zend/Tool/Project/Context/Zf/BootstrapFile.php

@@ -21,13 +21,6 @@
  */
 
 /**
- * @see Zend_Tool_Project_Context_Filesystem_File
- */
-require_once 'Zend/Tool/Project/Context/Filesystem/File.php';
-
-require_once 'Zend/Application.php';
-
-/**
  * This class is the front most class for utilizing Zend_Tool_Project
  *
  * A profile is a hierarchical set of resources that keep track of
@@ -46,8 +39,21 @@ class Zend_Tool_Project_Context_Zf_BootstrapFile extends Zend_Tool_Project_Conte
      */
     protected $_filesystemName = 'Bootstrap.php';
 
+    /**
+     * @var Zend_Tool_Project_Profile_Resource
+     */
+    protected $_applicationConfigFile = null;
+    
+    /**
+     * @var Zend_Tool_Project_Profile_Resource
+     */
+    protected $_applicationDirectory = null;
+    
+    /**
+     * @var Zend_Application
+     */
     protected $_applicationInstance = null;
-    protected $_bootstrapInstance = null;
+
 
     /**
      * getName()
@@ -63,23 +69,14 @@ class Zend_Tool_Project_Context_Zf_BootstrapFile extends Zend_Tool_Project_Conte
     {
         parent::init();
 
-        $applicationConfigFile = $this->_resource->getProfile()->search('ApplicationConfigFile');
-        $applicationDirectory = $this->_resource->getProfile()->search('ApplicationDirectory');
+        $this->_applicationConfigFile = $this->_resource->getProfile()->search('ApplicationConfigFile');
+        $this->_applicationDirectory = $this->_resource->getProfile()->search('ApplicationDirectory');
 
-        if (($applicationConfigFile === false) || ($applicationDirectory === false)) {
+        if (($this->_applicationConfigFile === false) || ($this->_applicationDirectory === false)) {
             throw new Exception('To use the BootstrapFile context, your project requires the use of both the "ApplicationConfigFile" and "ApplicationDirectory" contexts.');
         }
 
-        if ($applicationConfigFile->getContext()->exists()) {
-            define('APPLICATION_PATH', $applicationDirectory->getPath());
-            $applicationOptions = array();
-            $applicationOptions['config'] = $applicationConfigFile->getPath();
 
-            $this->_applicationInstance = new Zend_Application(
-                'development',
-                $applicationOptions
-                );
-        }
     }
 
     /**
@@ -89,27 +86,12 @@ class Zend_Tool_Project_Context_Zf_BootstrapFile extends Zend_Tool_Project_Conte
      */
     public function getContents()
     {
-        $autoloadMethodBody =<<<EOS
-\$autoloader = new Zend_Application_Module_Autoloader(array(
-    'namespace' => 'Application_',
-    'basePath'  => APPLICATION_PATH,
-    ));
-return \$autoloader;
-EOS;
 
-        
-        
         $codeGenFile = new Zend_CodeGenerator_Php_File(array(
             'classes' => array(
                 new Zend_CodeGenerator_Php_Class(array(
                     'name' => 'Bootstrap',
                     'extendedClass' => 'Zend_Application_Bootstrap_Bootstrap',
-                    'methods' => array(
-                        new Zend_CodeGenerator_Php_Method(array(
-                            'name' => '_initAppAutoloader',
-                            'body' => $autoloadMethodBody,
-                            ))
-                        )
                     )),
                 )
             ));
@@ -119,6 +101,19 @@ EOS;
     
     public function getApplicationInstance()
     {
+        if ($this->_applicationInstance == null) {
+            if ($this->_applicationConfigFile->getContext()->exists()) {
+                define('APPLICATION_PATH', $this->_applicationDirectory->getPath());
+                $applicationOptions = array();
+                $applicationOptions['config'] = $this->_applicationConfigFile->getPath();
+    
+                $this->_applicationInstance = new Zend_Application(
+                    'development',
+                    $applicationOptions
+                    );
+            }
+        }
+        
         return $this->_applicationInstance;
     }
 }

+ 77 - 16
library/Zend/Tool/Project/Provider/DbTable.php

@@ -21,17 +21,14 @@
  */
 
 /**
- * @see Zend_Tool_Project_Provider_Abstract
- */
-require_once 'Zend/Tool/Project/Provider/Abstract.php';
-
-/**
  * @category   Zend
  * @package    Zend_Tool
  * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  */
-class Zend_Tool_Project_Provider_DbTable extends Zend_Tool_Project_Provider_Abstract
+class Zend_Tool_Project_Provider_DbTable 
+    extends Zend_Tool_Project_Provider_Abstract
+    implements Zend_Tool_Framework_Provider_Pretendable
 {
     
     protected $_specialties = array('FromDatabase');
@@ -53,6 +50,13 @@ class Zend_Tool_Project_Provider_DbTable extends Zend_Tool_Project_Provider_Abst
         
         $modelsDirectory = $profile->search($profileSearchParams);
         
+        if (!($modelsDirectory instanceof Zend_Tool_Project_Profile_Resource)) {
+            throw new Zend_Tool_Project_Provider_Exception(
+                'A models directory was not found' .
+                (($moduleName) ? ' for module ' . $moduleName . '.' : '.')
+                );
+        }
+        
         if (!($dbTableDirectory = $modelsDirectory->search('DbTableDirectory'))) {
             $dbTableDirectory = $modelsDirectory->createResource('DbTableDirectory');
         }
@@ -74,7 +78,8 @@ class Zend_Tool_Project_Provider_DbTable extends Zend_Tool_Project_Provider_Abst
         
         $modelsDirectory = $profile->search($profileSearchParams);
         
-        if (!($dbTableDirectory = $modelsDirectory->search('DbTableDirectory'))) {
+        if (!($modelsDirectory instanceof Zend_Tool_Project_Profile_Resource)
+            || !($dbTableDirectory = $modelsDirectory->search('DbTableDirectory'))) {
             return false;
         }
         
@@ -84,7 +89,7 @@ class Zend_Tool_Project_Provider_DbTable extends Zend_Tool_Project_Provider_Abst
     }
       
     
-    public function create($name, $actualTableName, $module = null, $forceOverwrite = true)
+    public function create($name, $actualTableName, $module = null, $forceOverwrite = false)
     {
         $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
 
@@ -104,7 +109,7 @@ class Zend_Tool_Project_Provider_DbTable extends Zend_Tool_Project_Provider_Abst
         $name = ucwords($name);
         
         try {
-            $modelResource = self::createResource($this->_loadedProfile, $name, $actualTableName, $module);
+            $tableResource = self::createResource($this->_loadedProfile, $name, $actualTableName, $module);
         } catch (Exception $e) {
             $response = $this->_registry->getResponse();
             $response->setException($e);
@@ -114,22 +119,78 @@ class Zend_Tool_Project_Provider_DbTable extends Zend_Tool_Project_Provider_Abst
         // do the creation
         if ($this->_registry->getRequest()->isPretend()) {
 
-            $this->_registry->getResponse()->appendContent('Would create a DbTable at '  . $modelResource->getContext()->getPath());
+            $this->_registry->getResponse()->appendContent('Would create a DbTable at '  . $tableResource->getContext()->getPath());
 
         } else {
 
-            $this->_registry->getResponse()->appendContent('Creating a model at ' . $modelResource->getContext()->getPath());
-            $modelResource->create();
+            $this->_registry->getResponse()->appendContent('Creating a DbTable at ' . $tableResource->getContext()->getPath());
+            $tableResource->create();
 
             $this->_storeProfile();
         }
     }
     
-    public function createFromDatabase($module = null, $forceOverwrite = true)
+    public function createFromDatabase($module = null, $forceOverwrite = false)
     {
-        $bootstrapResource = $profile->search('Bootstrap');
-        $bi = $bootstrapResource->getApplicationInstance();
-        var_dump($bi);
+        $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
+        
+        $bootstrapResource = $this->_loadedProfile->search('BootstrapFile');
+        
+        /* @var $zendApp Zend_Application */
+        $zendApp = $bootstrapResource->getApplicationInstance();
+        
+        try {
+            $zendApp->bootstrap('db');
+        } catch (Zend_Application_Exception $e) {
+            throw new Zend_Tool_Project_Provider_Exception('Db resource not available, you might need to configure a DbAdapter.');
+            return;
+        }
+        
+        /* @var $db Zend_Db_Adapter_Abstract */
+        $db = $zendApp->getBootstrap()->getResource('db');
+        
+        $tableResources = array();
+        foreach ($db->listTables() as $actualTableName) {
+            
+            $dbTableName = $this->_convertTableNameToClassName($actualTableName);
+            
+            if (!$forceOverwrite && self::hasResource($this->_loadedProfile, $dbTableName, $module)) {
+                throw new Zend_Tool_Project_Provider_Exception(
+                    'This DbTable resource already exists, if you wish to overwrite it, '
+                    . 'pass the "forceOverwrite" flag to this provider.'
+                    );
+            }
+            
+            $tableResources[] = self::createResource(
+                $this->_loadedProfile,
+                $dbTableName,
+                $actualTableName,
+                $module
+                );
+        }
+        
+        if (count($tableResources) == 0) {
+            $this->_registry->getResponse()->appendContent('There are no tables in the selected database to write.');
+        }
+        
+        // do the creation
+        if ($this->_registry->getRequest()->isPretend()) {
+
+            foreach ($tableResources as $tableResource) {
+                $this->_registry->getResponse()->appendContent('Would create a DbTable at '  . $tableResource->getContext()->getPath());
+            }
+
+        } else {
+
+            foreach ($tableResources as $tableResource) {
+                $this->_registry->getResponse()->appendContent('Creating a DbTable at ' . $tableResource->getContext()->getPath());
+                $tableResource->create();
+            }
+
+            $this->_storeProfile();
+        }
+        
+        
     }
     
     protected function _convertTableNameToClassName($tableName)

+ 1 - 1
tests/Zend/Tool/Project/ProfileTest.php

@@ -163,7 +163,7 @@ class Zend_Tool_Project_ProfileTest extends PHPUnit_Framework_TestCase
     public function testProfileCanReturnStorageData()
     {
         $this->_standardProfileFromData->loadFromData();
-        $expectedValue = '<?xml version="1.0"?><projectProfile>  <projectDirectory>    <projectProfileFile/>    <applicationDirectory>      <apisDirectory enabled="false"/>      <configsDirectory>        <applicationConfigFile type="ini"/>      </configsDirectory>      <controllersDirectory>        <controllerFile controllerName="index"/>        <controllerFile controllerName="error"/>      </controllersDirectory>      <layoutsDirectory enabled="false"/>      <modelsDirectory/>      <modulesDirectory enabled="false"/>      <viewsDirectory>        <viewScriptsDirectory>          <viewControllerScriptsDirectory forControllerName="index">            <viewScriptFile scriptName="index"/>          </viewControllerScriptsDirectory>        </viewScriptsDirectory>        <viewHelpersDirectory/>        <viewFiltersDirectory enabled="false"/>      </viewsDirectory>      <bootstrapFile/>    </applicationDirectory>    <dataDirectory enabled="false">      <cacheDirectory enabled="false"/>      <searchIndexesDirectory enabled="false"/>      <localesDirectory enabled="false"/>      <logsDirectory enabled="false"/>      <sessionsDirectory enabled="false"/>      <uploadsDirectory enabled="false"/>    </dataDirectory>    <libraryDirectory>      <zfStandardLibraryDirectory/>    </libraryDirectory>    <publicDirectory>      <publicStylesheetsDirectory enabled="false"/>      <publicScriptsDirectory enabled="false"/>      <publicImagesDirectory enabled="false"/>      <publicIndexFile/>      <htaccessFile/>    </publicDirectory>    <projectProvidersDirectory enabled="false"/>  </projectDirectory></projectProfile>';
+        $expectedValue = '<?xml version="1.0"?><projectProfile>  <projectDirectory>    <projectProfileFile/>    <applicationDirectory classNamePrefix="Application_">      <apisDirectory enabled="false"/>      <configsDirectory>        <applicationConfigFile type="ini"/>      </configsDirectory>      <controllersDirectory>        <controllerFile controllerName="index"/>        <controllerFile controllerName="error"/>      </controllersDirectory>      <layoutsDirectory enabled="false"/>      <modelsDirectory/>      <modulesDirectory enabled="false"/>      <viewsDirectory>        <viewScriptsDirectory>          <viewControllerScriptsDirectory forControllerName="index">            <viewScriptFile scriptName="index"/>          </viewControllerScriptsDirectory>        </viewScriptsDirectory>        <viewHelpersDirectory/>        <viewFiltersDirectory enabled="false"/>      </viewsDirectory>      <bootstrapFile/>    </applicationDirectory>    <dataDirectory enabled="false">      <cacheDirectory enabled="false"/>      <searchIndexesDirectory enabled="false"/>      <localesDirectory enabled="false"/>      <logsDirectory enabled="false"/>      <sessionsDirectory enabled="false"/>      <uploadsDirectory enabled="false"/>    </dataDirectory>    <libraryDirectory>      <zfStandardLibraryDirectory/>    </libraryDirectory>    <publicDirectory>      <publicStylesheetsDirectory enabled="false"/>      <publicScriptsDirectory enabled="false"/>      <publicImagesDirectory enabled="false"/>      <publicIndexFile/>      <htaccessFile/>    </publicDirectory>    <projectProvidersDirectory enabled="false"/>  </projectDirectory></projectProfile>';
         $this->assertEquals($expectedValue, str_replace(PHP_EOL, '', $this->_standardProfileFromData->storeToData()));
     }