web/lib/Zend/Tool/Project/Profile/Resource.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: Resource.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Tool_Project_Profile_Resource_Container
       
    25  */
       
    26 require_once 'Zend/Tool/Project/Profile/Resource/Container.php';
       
    27 
       
    28 /**
       
    29  * @see Zend_Tool_Project_Context_Repository
       
    30  */
       
    31 require_once 'Zend/Tool/Project/Context/Repository.php';
       
    32 
       
    33 /**
       
    34  * This class is an iterator that will iterate only over enabled resources
       
    35  *
       
    36  * @category   Zend
       
    37  * @package    Zend_Tool
       
    38  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    39  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    40  */
       
    41 class Zend_Tool_Project_Profile_Resource extends Zend_Tool_Project_Profile_Resource_Container
       
    42 {
       
    43 
       
    44     /**
       
    45      * @var Zend_Tool_Project_Profile
       
    46      */
       
    47     protected $_profile = null;
       
    48 
       
    49     /**
       
    50      * @var Zend_Tool_Project_Profile_Resource
       
    51      */
       
    52     protected $_parentResource = null;
       
    53 
       
    54     /**#@+
       
    55      * @var bool
       
    56      */
       
    57     protected $_deleted = false;
       
    58     protected $_enabled = true;
       
    59     /**#@-*/
       
    60 
       
    61     /**
       
    62      * @var Zend_Tool_Project_Context|string
       
    63      */
       
    64     protected $_context = null;
       
    65 
       
    66     /**
       
    67      * @var array
       
    68      */
       
    69     protected $_attributes = array();
       
    70 
       
    71     /**
       
    72      * @var bool
       
    73      */
       
    74     protected $_isContextInitialized = false;
       
    75 
       
    76     /**
       
    77      * __construct()
       
    78      *
       
    79      * @param string|Zend_Tool_Project_Context_Interface $context
       
    80      */
       
    81     public function __construct($context)
       
    82     {
       
    83         $this->setContext($context);
       
    84     }
       
    85 
       
    86     /**
       
    87      * setContext()
       
    88      *
       
    89      * @param string|Zend_Tool_Project_Context_Interface $context
       
    90      * @return Zend_Tool_Project_Profile_Resource
       
    91      */
       
    92     public function setContext($context)
       
    93     {
       
    94         $this->_context = $context;
       
    95         return $this;
       
    96     }
       
    97 
       
    98     /**
       
    99      * getContext()
       
   100      *
       
   101      * @return Zend_Tool_Project_Context_Interface
       
   102      */
       
   103     public function getContext()
       
   104     {
       
   105         return $this->_context;
       
   106     }
       
   107 
       
   108     /**
       
   109      * getName() - Get the resource name
       
   110      *
       
   111      * Name is derived from the context name
       
   112      *
       
   113      * @return string
       
   114      */
       
   115     public function getName()
       
   116     {
       
   117         if (is_string($this->_context)) {
       
   118             return $this->_context;
       
   119         } elseif ($this->_context instanceof Zend_Tool_Project_Context_Interface) {
       
   120             return $this->_context->getName();
       
   121         } else {
       
   122             throw new Zend_Tool_Project_Exception('Invalid context in resource');
       
   123         }
       
   124     }
       
   125 
       
   126     /**
       
   127      * setProfile()
       
   128      *
       
   129      * @param Zend_Tool_Project_Profile $profile
       
   130      * @return Zend_Tool_Project_Profile_Resource
       
   131      */
       
   132     public function setProfile(Zend_Tool_Project_Profile $profile)
       
   133     {
       
   134         $this->_profile = $profile;
       
   135         return $this;
       
   136     }
       
   137 
       
   138     /**
       
   139      * getProfile
       
   140      *
       
   141      * @return Zend_Tool_Project_Profile
       
   142      */
       
   143     public function getProfile()
       
   144     {
       
   145         return $this->_profile;
       
   146     }
       
   147 
       
   148     /**
       
   149      * getPersistentAttributes()
       
   150      *
       
   151      * @return array
       
   152      */
       
   153     public function getPersistentAttributes()
       
   154     {
       
   155         if (method_exists($this->_context, 'getPersistentAttributes')) {
       
   156             return $this->_context->getPersistentAttributes();
       
   157         }
       
   158 
       
   159         return array();
       
   160     }
       
   161 
       
   162     /**
       
   163      * setEnabled()
       
   164      *
       
   165      * @param bool $enabled
       
   166      * @return Zend_Tool_Project_Profile_Resource
       
   167      */
       
   168     public function setEnabled($enabled = true)
       
   169     {
       
   170         // convert fuzzy types to bool
       
   171         $this->_enabled = (!in_array($enabled, array('false', 'disabled', 0, -1, false), true)) ? true : false;
       
   172         return $this;
       
   173     }
       
   174 
       
   175     /**
       
   176      * isEnabled()
       
   177      *
       
   178      * @return bool
       
   179      */
       
   180     public function isEnabled()
       
   181     {
       
   182         return $this->_enabled;
       
   183     }
       
   184 
       
   185     /**
       
   186      * setDeleted()
       
   187      *
       
   188      * @param bool $deleted
       
   189      * @return Zend_Tool_Project_Profile_Resource
       
   190      */
       
   191     public function setDeleted($deleted = true)
       
   192     {
       
   193         $this->_deleted = (bool) $deleted;
       
   194         return $this;
       
   195     }
       
   196 
       
   197     /**
       
   198      * isDeleted()
       
   199      *
       
   200      * @return Zend_Tool_Project_Profile_Resource
       
   201      */
       
   202     public function isDeleted()
       
   203     {
       
   204         return $this->_deleted;
       
   205     }
       
   206 
       
   207     /**
       
   208      * initializeContext()
       
   209      *
       
   210      * @return Zend_Tool_Project_Profile_Resource
       
   211      */
       
   212     public function initializeContext()
       
   213     {
       
   214         if ($this->_isContextInitialized) {
       
   215             return;
       
   216         }
       
   217         if (is_string($this->_context)) {
       
   218             $this->_context = Zend_Tool_Project_Context_Repository::getInstance()->getContext($this->_context);
       
   219         }
       
   220 
       
   221         if (method_exists($this->_context, 'setResource')) {
       
   222             $this->_context->setResource($this);
       
   223         }
       
   224 
       
   225         if (method_exists($this->_context, 'init')) {
       
   226             $this->_context->init();
       
   227         }
       
   228 
       
   229         $this->_isContextInitialized = true;
       
   230         return $this;
       
   231     }
       
   232 
       
   233     /**
       
   234      * __toString()
       
   235      *
       
   236      * @return string
       
   237      */
       
   238     public function __toString()
       
   239     {
       
   240         return $this->_context->getName();
       
   241     }
       
   242 
       
   243     /**
       
   244      * __call()
       
   245      *
       
   246      * @param string $method
       
   247      * @param array $arguments
       
   248      * @return Zend_Tool_Project_Profile_Resource
       
   249      */
       
   250     public function __call($method, $arguments)
       
   251     {
       
   252         if (method_exists($this->_context, $method)) {
       
   253             if (!$this->isEnabled()) {
       
   254                 $this->setEnabled(true);
       
   255             }
       
   256             return call_user_func_array(array($this->_context, $method), $arguments);
       
   257         } else {
       
   258             throw new Zend_Tool_Project_Profile_Exception('cannot call ' . $method);
       
   259         }
       
   260     }
       
   261 
       
   262 }