File indexing completed on 2024-12-29 05:28:09

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_Profile_Resource_Container
0025  */
0026 // require_once 'Zend/Tool/Project/Profile/Resource/Container.php';
0027 
0028 /**
0029  * @see Zend_Tool_Project_Context_Repository
0030  */
0031 // require_once 'Zend/Tool/Project/Context/Repository.php';
0032 
0033 /**
0034  * This class is an iterator that will iterate only over enabled resources
0035  *
0036  * @category   Zend
0037  * @package    Zend_Tool
0038  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0039  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0040  */
0041 class Zend_Tool_Project_Profile_Resource extends Zend_Tool_Project_Profile_Resource_Container
0042 {
0043 
0044     /**
0045      * @var Zend_Tool_Project_Profile
0046      */
0047     protected $_profile = null;
0048 
0049     /**
0050      * @var Zend_Tool_Project_Profile_Resource
0051      */
0052     protected $_parentResource = null;
0053 
0054     /**#@+
0055      * @var bool
0056      */
0057     protected $_deleted = false;
0058     protected $_enabled = true;
0059     /**#@-*/
0060 
0061     /**
0062      * @var Zend_Tool_Project_Context|string
0063      */
0064     protected $_context = null;
0065 
0066     /**
0067      * @var array
0068      */
0069     protected $_attributes = array();
0070 
0071     /**
0072      * @var bool
0073      */
0074     protected $_isContextInitialized = false;
0075 
0076     /**
0077      * __construct()
0078      *
0079      * @param string|Zend_Tool_Project_Context_Interface $context
0080      */
0081     public function __construct($context)
0082     {
0083         $this->setContext($context);
0084     }
0085 
0086     /**
0087      * setContext()
0088      *
0089      * @param string|Zend_Tool_Project_Context_Interface $context
0090      * @return Zend_Tool_Project_Profile_Resource
0091      */
0092     public function setContext($context)
0093     {
0094         $this->_context = $context;
0095         return $this;
0096     }
0097 
0098     /**
0099      * getContext()
0100      *
0101      * @return Zend_Tool_Project_Context_Interface
0102      */
0103     public function getContext()
0104     {
0105         return $this->_context;
0106     }
0107 
0108     /**
0109      * getName() - Get the resource name
0110      *
0111      * Name is derived from the context name
0112      *
0113      * @return string
0114      */
0115     public function getName()
0116     {
0117         if (is_string($this->_context)) {
0118             return $this->_context;
0119         } elseif ($this->_context instanceof Zend_Tool_Project_Context_Interface) {
0120             return $this->_context->getName();
0121         } else {
0122             throw new Zend_Tool_Project_Exception('Invalid context in resource');
0123         }
0124     }
0125 
0126     /**
0127      * setProfile()
0128      *
0129      * @param Zend_Tool_Project_Profile $profile
0130      * @return Zend_Tool_Project_Profile_Resource
0131      */
0132     public function setProfile(Zend_Tool_Project_Profile $profile)
0133     {
0134         $this->_profile = $profile;
0135         return $this;
0136     }
0137 
0138     /**
0139      * getProfile
0140      *
0141      * @return Zend_Tool_Project_Profile
0142      */
0143     public function getProfile()
0144     {
0145         return $this->_profile;
0146     }
0147 
0148     /**
0149      * getPersistentAttributes()
0150      *
0151      * @return array
0152      */
0153     public function getPersistentAttributes()
0154     {
0155         if (method_exists($this->_context, 'getPersistentAttributes')) {
0156             return $this->_context->getPersistentAttributes();
0157         }
0158 
0159         return array();
0160     }
0161 
0162     /**
0163      * setEnabled()
0164      *
0165      * @param bool $enabled
0166      * @return Zend_Tool_Project_Profile_Resource
0167      */
0168     public function setEnabled($enabled = true)
0169     {
0170         // convert fuzzy types to bool
0171         $this->_enabled = (!in_array($enabled, array('false', 'disabled', 0, -1, false), true)) ? true : false;
0172         return $this;
0173     }
0174 
0175     /**
0176      * isEnabled()
0177      *
0178      * @return bool
0179      */
0180     public function isEnabled()
0181     {
0182         return $this->_enabled;
0183     }
0184 
0185     /**
0186      * setDeleted()
0187      *
0188      * @param bool $deleted
0189      * @return Zend_Tool_Project_Profile_Resource
0190      */
0191     public function setDeleted($deleted = true)
0192     {
0193         $this->_deleted = (bool) $deleted;
0194         return $this;
0195     }
0196 
0197     /**
0198      * isDeleted()
0199      *
0200      * @return Zend_Tool_Project_Profile_Resource
0201      */
0202     public function isDeleted()
0203     {
0204         return $this->_deleted;
0205     }
0206 
0207     /**
0208      * initializeContext()
0209      *
0210      * @return Zend_Tool_Project_Profile_Resource
0211      */
0212     public function initializeContext()
0213     {
0214         if ($this->_isContextInitialized) {
0215             return;
0216         }
0217         if (is_string($this->_context)) {
0218             $this->_context = Zend_Tool_Project_Context_Repository::getInstance()->getContext($this->_context);
0219         }
0220 
0221         if (method_exists($this->_context, 'setResource')) {
0222             $this->_context->setResource($this);
0223         }
0224 
0225         if (method_exists($this->_context, 'init')) {
0226             $this->_context->init();
0227         }
0228 
0229         $this->_isContextInitialized = true;
0230         return $this;
0231     }
0232 
0233     /**
0234      * __toString()
0235      *
0236      * @return string
0237      */
0238     public function __toString()
0239     {
0240         return $this->_context->getName();
0241     }
0242 
0243     /**
0244      * __call()
0245      *
0246      * @param string $method
0247      * @param array $arguments
0248      * @return Zend_Tool_Project_Profile_Resource
0249      */
0250     public function __call($method, $arguments)
0251     {
0252         if (method_exists($this->_context, $method)) {
0253             if (!$this->isEnabled()) {
0254                 $this->setEnabled(true);
0255             }
0256             return call_user_func_array(array($this->_context, $method), $arguments);
0257         } else {
0258             throw new Zend_Tool_Project_Profile_Exception('cannot call ' . $method);
0259         }
0260     }
0261 
0262 }