File indexing completed on 2024-06-23 05:55:50

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  * @see Zend_Tool_Project_Context_Interface
0025  */
0026 // require_once 'Zend/Tool/Project/Context/Interface.php';
0027 
0028 /**
0029  * @see Zend_Reflection_File
0030  */
0031 // require_once 'Zend/Reflection/File.php';
0032 
0033 /**
0034  * This class is the front most class for utilizing Zend_Tool_Project
0035  *
0036  * A profile is a hierarchical set of resources that keep track of
0037  * items within a specific project.
0038  *
0039  * @category   Zend
0040  * @package    Zend_Tool
0041  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0042  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0043  */
0044 class Zend_Tool_Project_Context_Zf_ActionMethod implements Zend_Tool_Project_Context_Interface
0045 {
0046 
0047     /**
0048      * @var Zend_Tool_Project_Profile_Resource
0049      */
0050     protected $_resource = null;
0051 
0052     /**
0053      * @var Zend_Tool_Project_Profile_Resource
0054      */
0055     protected $_controllerResource = null;
0056 
0057     /**
0058      * @var string
0059      */
0060     protected $_controllerPath = '';
0061 
0062     /**
0063      * @var string
0064      */
0065     protected $_actionName = null;
0066 
0067     /**
0068      * init()
0069      *
0070      * @return Zend_Tool_Project_Context_Zf_ActionMethod
0071      */
0072     public function init()
0073     {
0074         $this->_actionName = $this->_resource->getAttribute('actionName');
0075 
0076         $this->_resource->setAppendable(false);
0077         $this->_controllerResource = $this->_resource->getParentResource();
0078         if (!$this->_controllerResource->getContext() instanceof Zend_Tool_Project_Context_Zf_ControllerFile) {
0079             // require_once 'Zend/Tool/Project/Context/Exception.php';
0080             throw new Zend_Tool_Project_Context_Exception('ActionMethod must be a sub resource of a ControllerFile');
0081         }
0082         // make the ControllerFile node appendable so we can tack on the actionMethod.
0083         $this->_resource->getParentResource()->setAppendable(true);
0084 
0085         $this->_controllerPath = $this->_controllerResource->getContext()->getPath();
0086 
0087         /*
0088          * This code block is now commented, its doing to much for init()
0089          *
0090         if ($this->_controllerPath != '' && self::hasActionMethod($this->_controllerPath, $this->_actionName)) {
0091             // require_once 'Zend/Tool/Project/Context/Exception.php';
0092             throw new Zend_Tool_Project_Context_Exception('An action named ' . $this->_actionName . 'Action already exists in this controller');
0093         }
0094         */
0095 
0096         return $this;
0097     }
0098 
0099     /**
0100      * getPersistentAttributes
0101      *
0102      * @return array
0103      */
0104     public function getPersistentAttributes()
0105     {
0106         return array(
0107             'actionName' => $this->getActionName()
0108             );
0109     }
0110 
0111     /**
0112      * getName()
0113      *
0114      * @return string
0115      */
0116     public function getName()
0117     {
0118         return 'ActionMethod';
0119     }
0120 
0121     /**
0122      * setResource()
0123      *
0124      * @param Zend_Tool_Project_Profile_Resource $resource
0125      * @return Zend_Tool_Project_Context_Zf_ActionMethod
0126      */
0127     public function setResource(Zend_Tool_Project_Profile_Resource $resource)
0128     {
0129         $this->_resource = $resource;
0130         return $this;
0131     }
0132 
0133     /**
0134      * setActionName()
0135      *
0136      * @param string $actionName
0137      * @return Zend_Tool_Project_Context_Zf_ActionMethod
0138      */
0139     public function setActionName($actionName)
0140     {
0141         $this->_actionName = $actionName;
0142         return $this;
0143     }
0144 
0145     /**
0146      * getActionName()
0147      *
0148      * @return string
0149      */
0150     public function getActionName()
0151     {
0152         return $this->_actionName;
0153     }
0154 
0155     /**
0156      * create()
0157      *
0158      * @return Zend_Tool_Project_Context_Zf_ActionMethod
0159      */
0160     public function create()
0161     {
0162         if (self::createActionMethod($this->_controllerPath, $this->_actionName) === false) {
0163             // require_once 'Zend/Tool/Project/Context/Exception.php';
0164             throw new Zend_Tool_Project_Context_Exception(
0165                 'Could not create action within controller ' . $this->_controllerPath
0166                 . ' with action name ' . $this->_actionName
0167                 );
0168         }
0169         return $this;
0170     }
0171 
0172     /**
0173      * delete()
0174      *
0175      * @return Zend_Tool_Project_Context_Zf_ActionMethod
0176      */
0177     public function delete()
0178     {
0179         // @todo do this
0180         return $this;
0181     }
0182 
0183     /**
0184      * createAcionMethod()
0185      *
0186      * @param string $controllerPath
0187      * @param string $actionName
0188      * @param string $body
0189      * @return true
0190      */
0191     public static function createActionMethod($controllerPath, $actionName, $body = '        // action body')
0192     {
0193         if (!file_exists($controllerPath)) {
0194             return false;
0195         }
0196 
0197         $controllerCodeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName($controllerPath, true, true);
0198         $controllerCodeGenFile->getClass()->setMethod(array(
0199             'name' => $actionName . 'Action',
0200             'body' => $body
0201             ));
0202 
0203         file_put_contents($controllerPath, $controllerCodeGenFile->generate());
0204         return true;
0205     }
0206 
0207     /**
0208      * hasActionMethod()
0209      *
0210      * @param string $controllerPath
0211      * @param string $actionName
0212      * @return bool
0213      */
0214     public static function hasActionMethod($controllerPath, $actionName)
0215     {
0216         if (!file_exists($controllerPath)) {
0217             return false;
0218         }
0219 
0220         $controllerCodeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName($controllerPath, true, true);
0221         return $controllerCodeGenFile->getClass()->hasMethod($actionName . 'Action');
0222     }
0223 
0224 }