Jelajahi Sumber

ZF-7260
- Merging Zend_Tool incubator features since r16970 to trunk

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

ralph 16 tahun lalu
induk
melakukan
f9f9eae539

+ 22 - 2
library/Zend/Tool/Framework/Client/Console.php

@@ -65,11 +65,16 @@ class Zend_Tool_Framework_Client_Console
 {
 
     /**
-     * @var string
+     * @var array
      */
     protected $_configOptions = null;
     
     /**
+     * @var array
+     */
+    protected $_storageOptions = null;
+    
+    /**
      * @var Zend_Filter_Word_CamelCaseToDash
      */
     protected $_filterToClientNaming = null;
@@ -97,6 +102,12 @@ class Zend_Tool_Framework_Client_Console
         return $this;
     }
     
+    public function setStorageOptions($storageOptions)
+    {
+        $this->_storageOptions = $storageOptions;
+        return $this;
+    }
+    
     /**
      * getName() - return the name of the client, in this case 'console'
      *
@@ -114,11 +125,20 @@ class Zend_Tool_Framework_Client_Console
     protected function _preInit()
     {
         $config = $this->_registry->getConfig();
-
+        
         if ($this->_configOptions != null) {
             $config->setOptions($this->_configOptions);
         }
         
+        $storage = $this->_registry->getStorage();
+        
+        if ($this->_storageOptions != null && isset($this->_storageOptions['directory'])) {
+            require_once 'Zend/Tool/Framework/Client/Storage/Directory.php';
+            $storage->setAdapter(
+                new Zend_Tool_Framework_Client_Storage_Directory($this->_storageOptions['directory'])
+                );
+        }
+        
         // support the changing of the current working directory, necessary for some providers
         if (isset($_ENV['ZEND_TOOL_CURRENT_WORKING_DIRECTORY'])) {
             chdir($_ENV['ZEND_TOOL_CURRENT_WORKING_DIRECTORY']);

+ 14 - 1
library/Zend/Tool/Framework/Client/Storage.php

@@ -46,8 +46,13 @@ class Zend_Tool_Framework_Client_Storage
         }
     }
     
-    public function setAdapter(Zend_Tool_Framework_Client_Storage_AdapterInterface $adapter)
+    public function setAdapter($adapter)
     {
+        if (is_string($adapter)) {
+            $storageAdapterClass = 'Zend_Tool_Framework_Client_Storage_' . ucfirst($adapter);
+            Zend_Loader::loadClass($storageAdapterClass);
+            $adapter = new $storageAdapterClass();
+        }
         $this->_adapter = $adapter;
     }
     
@@ -101,4 +106,12 @@ class Zend_Tool_Framework_Client_Storage
         return $this;
     }
     
+    public function getStreamUri($name)
+    {
+        if (!$this->_adapter) {
+            return false;
+        }
+        
+        return $this->_adapter->getStreamUri($name);
+    }
 }

+ 2 - 0
library/Zend/Tool/Framework/Client/Storage/AdapterInterface.php

@@ -37,4 +37,6 @@ interface Zend_Tool_Framework_Client_Storage_AdapterInterface
     
     public function remove($name);
     
+    public function getStreamUri($name);
+    
 }

+ 9 - 0
library/Zend/Tool/Framework/Client/Storage/Directory.php

@@ -32,12 +32,16 @@ require_once 'Zend/Tool/Framework/Client/Storage/AdapterInterface.php';
  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  */
 class Zend_Tool_Framework_Client_Storage_Directory
+    implements Zend_Tool_Framework_Client_Storage_AdapterInterface 
 {
     
     protected $_directoryPath = null;
     
     public function __construct($directoryPath)
     {
+        if (!file_exists($directoryPath)) {
+            throw new Zend_Tool_Framework_Client_Exception(__CLASS__ . ': the supplied directory does not exist');
+        }
         $this->_directoryPath = $directoryPath;
     }
     
@@ -61,4 +65,9 @@ class Zend_Tool_Framework_Client_Storage_Directory
         return unlink($this->_directoryPath . DIRECTORY_SEPARATOR . $name);
     }
     
+    public function getStreamUri($name)
+    {
+        return $this->_directoryPath . DIRECTORY_SEPARATOR . $name;
+    }
+    
 }

+ 3 - 0
library/Zend/Tool/Framework/Provider/Repository.php

@@ -256,6 +256,9 @@ class Zend_Tool_Framework_Provider_Repository
     {
         $className = get_class($provider);
         $providerName = substr($className, strrpos($className, '_')+1);
+        if (substr($providerName, -8) == 'Provider') {
+            $providerName = substr($providerName, 0, strlen($providerName)-8);
+        }
         return $providerName;
     }
 

+ 32 - 0
library/Zend/Tool/Framework/Registry.php

@@ -49,6 +49,11 @@ class Zend_Tool_Framework_Registry implements Zend_Tool_Framework_Registry_Inter
     protected $_config = null;
     
     /**
+     * @var Zend_Tool_Framework_Client_Storage
+     */
+    protected $_storage = null;
+    
+    /**
      * @var Zend_Tool_Framework_Action_Repository
      */
     protected $_actionRepository = null;
@@ -145,6 +150,33 @@ class Zend_Tool_Framework_Registry implements Zend_Tool_Framework_Registry_Inter
     }
     
     /**
+     * setStorage() 
+     *
+     * @param Zend_Tool_Framework_Client_Storage $storage
+     * @return Zend_Tool_Framework_Registry
+     */
+    public function setStorage(Zend_Tool_Framework_Client_Storage $storage)
+    {
+        $this->_storage = $storage;
+        return $this;
+    }
+    
+    /**
+     * getConfig()
+     *
+     * @return Zend_Tool_Framework_Client_Storage
+     */
+    public function getStorage()
+    {
+        if ($this->_storage === null) {
+            require_once 'Zend/Tool/Framework/Client/Storage.php';
+            $this->setStorage(new Zend_Tool_Framework_Client_Storage());
+        }
+        
+        return $this->_storage;
+    }
+    
+    /**
      * setLoader() 
      *
      * @param Zend_Tool_Framework_Loader_Abstract $loader

+ 106 - 0
library/Zend/Tool/Project/Context/Content/Engine.php

@@ -0,0 +1,106 @@
+<?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$
+ */
+
+/**
+ * @see Zend_Tool_Project_Context_Content_Engine_CodeGenerator
+ */
+require_once 'Zend/Tool/Project/Context/Content/Engine/CodeGenerator.php';
+
+/**
+ * @see Zend_Tool_Project_Context_Content_Engine_Phtml
+ */
+require_once 'Zend/Tool/Project/Context/Content/Engine/Phtml.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
+ * 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
+ */
+class Zend_Tool_Project_Context_Content_Engine
+{
+    /**
+     * @var Zend_Tool_Framework_Client_Storage
+     */
+    protected $_storage = null;
+    
+    /**
+     * @var string
+     */
+    protected $_keyInStorage = 'project/content';
+    
+    /**
+     * @var array
+     */
+    protected $_engines = array();
+    
+    /**
+     * __construct()
+     *
+     * @param Zend_Tool_Framework_Client_Storage $storage
+     */
+    public function __construct(Zend_Tool_Framework_Client_Storage $storage)
+    {
+        $this->_storage = $storage;
+        $this->_engines = array(
+            new Zend_Tool_Project_Context_Content_Engine_CodeGenerator($storage, $this->_keyInStorage),
+            new Zend_Tool_Project_Context_Content_Engine_Phtml($storage, $this->_keyInStorage),
+            );
+    }
+    
+    /**
+     * getContent()
+     *
+     * @param Zend_Tool_Project_Context_Interface $context
+     * @param string $methodName
+     * @param mixed $parameters
+     * @return string
+     */
+    public function getContent(Zend_Tool_Project_Context_Interface $context, $methodName, $parameters)
+    {
+        $content = null;
+        
+        foreach ($this->_engines as $engine) {
+            if ($engine->hasContent($context, $methodName, $parameters)) {
+                $content = $engine->getContent($context, $methodName, $parameters);
+                
+                if ($content != null) {
+                    break;
+                }
+                
+            }
+            
+        }
+        
+        if ($content == null) {
+            return false;
+        }
+        
+        return $content;
+    }
+    
+}

+ 98 - 0
library/Zend/Tool/Project/Context/Content/Engine/CodeGenerator.php

@@ -0,0 +1,98 @@
+<?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$
+ */
+
+/**
+ * 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
+ */
+class Zend_Tool_Project_Context_Content_Engine_CodeGenerator
+{
+    /**
+     * @var Zend_Tool_Framework_Client_Storage
+     */
+    protected $_storage = null;
+    
+    /**
+     * @var string
+     */
+    protected $_contentPrefix = null;
+    
+    /**
+     * __construct()
+     *
+     * @param Zend_Tool_Framework_Client_Storage $storage
+     * @param string $contentPrefix
+     */
+    public function __construct(Zend_Tool_Framework_Client_Storage $storage, $contentPrefix)
+    {
+        $this->_storage       = $storage;
+        $this->_contentPrefix = $contentPrefix;
+    }
+    
+    /**
+     * hasContent()
+     *
+     * @param Zend_Tool_Project_Context_Interface $context
+     * @param string $method
+     * @return string
+     */
+    public function hasContent(Zend_Tool_Project_Context_Interface $context, $method)
+    {
+        return $this->_storage->has($this->_contentPrefix . '/' . $context->getName() . '/' . $method . '.php');
+    }
+    
+    /**
+     * getContent()
+     *
+     * @param Zend_Tool_Project_Context_Interface $context
+     * @param string $method
+     * @param mixed $parameters
+     * @return string
+     */
+    public function getContent(Zend_Tool_Project_Context_Interface $context, $method, $parameters)
+    {
+        $streamUri = $this->_storage->getStreamUri($this->_contentPrefix . '/' . $context->getName() . '/' . $method . '.php');
+        
+        if (method_exists($context, 'getCodeGenerator')) {
+            $codeGenerator = $context->getCodeGenerator();
+        } else {
+            $codeGenerator = new Zend_CodeGenerator_Php_File();
+        }
+        
+        $codeGenerator = include $streamUri;
+        
+        if (!$codeGenerator instanceof Zend_CodeGenerator_Abstract) {
+            throw new Zend_Tool_Project_Exception('Custom file at ' . $streamUri . ' did not return the $codeGenerator object.');
+        }
+        
+        return $codeGenerator->generate();
+    }
+    
+    
+}

+ 89 - 0
library/Zend/Tool/Project/Context/Content/Engine/Phtml.php

@@ -0,0 +1,89 @@
+<?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$
+ */
+
+/**
+ * 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
+ */
+class Zend_Tool_Project_Context_Content_Engine_Phtml
+{
+    
+    /**
+     * @var Zend_Tool_Framework_Client_Storage
+     */
+    protected $_storage = null;
+    
+    /**
+     * @var string
+     */
+    protected $_contentPrefix = null;
+    
+    /**
+     * __construct()
+     *
+     * @param Zend_Tool_Framework_Client_Storage $storage
+     * @param string $contentPrefix
+     */
+    public function __construct(Zend_Tool_Framework_Client_Storage $storage, $contentPrefix)
+    {
+        $this->_storage = $storage;
+        $this->_contentPrefix = $contentPrefix;
+    }
+    
+    /**
+     * hasContext()
+     *
+     * @param Zend_Tool_Project_Context_Interface  $context
+     * @param string $method
+     * @return string
+     */
+    public function hasContent(Zend_Tool_Project_Context_Interface $context, $method)
+    {
+        return $this->_storage->has($this->_contentPrefix . '/' . $context . '/' . $method . '.phtml');
+    }
+    
+    /**
+     * getContent()
+     *
+     * @param Zend_Tool_Project_Context_Interface $context
+     * @param string $method
+     * @param mixed $parameters
+     */
+    public function getContent(Zend_Tool_Project_Context_Interface $context, $method, $parameters)
+    {
+        $streamUri = $this->_storage->getStreamUri($this->_contentPrefix . '/' . $context->getName() . '/' . $method . '.phtml');
+        
+        ob_start();
+        include $streamUri;
+        $content = ob_get_clean();
+        
+        return $content;
+    }
+    
+}

+ 1 - 1
library/Zend/Tool/Project/Context/Filesystem/Directory.php

@@ -60,7 +60,7 @@ abstract class Zend_Tool_Project_Context_Filesystem_Directory extends Zend_Tool_
 
         return $this;
     }
-    
+
     /**
      * delete()
      *

+ 13 - 4
library/Zend/Tool/Project/Context/Zf/ControllerFile.php

@@ -185,13 +185,22 @@ EOS
      */
     public function addAction($actionName)
     {
-        //require_once $this->getPath();
-        //$codeGenFile = Zend_CodeGenerator_Php_File::fromReflection(new Zend_Reflection_File($this->getPath()));
+        $class = $this->getCodeGenerator();
+        $class->setMethod(array('name' => $actionName . 'Action', 'body' => '        // action body here'));
+        file_put_contents($this->getPath(), $codeGenFile->generate());
+    }
+    
+    /**
+     * getCodeGenerator()
+     *
+     * @return Zend_CodeGenerator_Php_Class
+     */
+    public function getCodeGenerator()
+    {
         $codeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName($this->getPath());
         $codeGenFileClasses = $codeGenFile->getClasses();
         $class = array_shift($codeGenFileClasses);
-        $class->setMethod(array('name' => $actionName . 'Action', 'body' => '        // action body here'));
-        file_put_contents($this->getPath(), $codeGenFile->generate());
+        return $class;
     }
     
 }

+ 15 - 0
library/Zend/Tool/Project/Provider/Abstract.php

@@ -201,6 +201,21 @@ abstract class Zend_Tool_Project_Provider_Abstract extends Zend_Tool_Framework_P
         $projectProfileFile->getContext()->save();
     }
 
+    protected function _getContentForContext(Zend_Tool_Project_Context_Interface $context, $methodName, $parameters)
+    {
+        $storage = $this->_registry->getStorage(); 
+        if (!$storage->isEnabled()) {
+            return false;
+        }
+        
+        if (!class_exists('Zend_Tool_Project_Context_Content_Engine')) {
+            require_once 'Zend/Tool/Project/Context/Content/Engine.php';
+        }
+
+        $engine = new Zend_Tool_Project_Context_Content_Engine($storage);
+        return $engine->getContent($context, $methodName, $parameters);
+    }
+    
     /**
      * _loadContextClassesIntoRegistry() - This is called by the constructor
      * so that child providers can provide a list of contexts to load into the

+ 17 - 2
library/Zend/Tool/Project/Provider/Project.php

@@ -41,7 +41,7 @@ class Zend_Tool_Project_Provider_Project extends Zend_Tool_Project_Provider_Abst
      *
      * @param string $path
      */
-    public function create($path)
+    public function create($path, $nameOfProfile = null, $fileOfProfile = null)
     {
         if ($path == null) {
             $path = getcwd();
@@ -64,9 +64,24 @@ class Zend_Tool_Project_Provider_Project extends Zend_Tool_Project_Provider_Abst
             throw new Zend_Tool_Framework_Client_Exception('A project already exists here');
         }
 
+        $profileData = null;
+        
+        if ($fileOfProfile != null && file_exists($fileOfProfile)) {
+            $profileData = file_get_contents($fileOfProfile);
+        }
+        
+        $storage = $this->_registry->getStorage();
+        if ($profileData == '' && $nameOfProfile != null && $storage->isEnabled()) {
+            $profileData = $storage->get('project/profiles/' . $nameOfProfile . '.xml');
+        }
+        
+        if ($profileData == '') {
+            $profileData = $this->_getDefaultProfile();
+        }
+        
         $newProfile = new Zend_Tool_Project_Profile(array(
             'projectDirectory' => $path,
-            'profileData' => $this->_getDefaultProfile()
+            'profileData' => $profileData
             ));
 
         $newProfile->loadFromData();