web/lib/Zend/Tool/Project/Context/Filesystem/File.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: File.php 20901 2010-02-04 16:06:12Z ralph $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Tool_Project_Context_Filesystem_Abstract
       
    25  */
       
    26 require_once 'Zend/Tool/Project/Context/Filesystem/Abstract.php';
       
    27 
       
    28 /**
       
    29  * This class is the front most class for utilizing Zend_Tool_Project
       
    30  *
       
    31  * A profile is a hierarchical set of resources that keep track of
       
    32  * items within a specific project.
       
    33  *
       
    34  * @category   Zend
       
    35  * @package    Zend_Tool
       
    36  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    37  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    38  */
       
    39 class Zend_Tool_Project_Context_Filesystem_File extends Zend_Tool_Project_Context_Filesystem_Abstract
       
    40 {
       
    41 
       
    42     protected $_fileOnlyContext = null;
       
    43     
       
    44     protected $_filesystemName = null;
       
    45     
       
    46     protected $_content = null;
       
    47     
       
    48     /**
       
    49      * getName()
       
    50      * 
       
    51      * @return string
       
    52      */
       
    53     public function getName()
       
    54     {
       
    55         return 'file';
       
    56     }
       
    57     
       
    58     /**
       
    59      * init()
       
    60      *
       
    61      * @return Zend_Tool_Project_Context_Filesystem_File
       
    62      */
       
    63     public function init()
       
    64     {
       
    65         if ($this->_resource->hasAttribute('filesystemName')) {
       
    66             $this->_filesystemName = $this->_resource->getAttribute('filesystemName');
       
    67         }
       
    68         
       
    69         // check to see if this file is 
       
    70         if ($this->getName() == 'file') {
       
    71             $this->_initFileOnlyContext();
       
    72         }
       
    73         
       
    74         // @potential-todo check to ensure that this 'file' resource has no children
       
    75         parent::init();
       
    76         return $this;
       
    77     }
       
    78 
       
    79     /**
       
    80      * getPersistentAttributes()
       
    81      *
       
    82      * @return array
       
    83      */
       
    84     public function getPersistentAttributes()
       
    85     {
       
    86         $returnAttrs = array();
       
    87         if ($this->_filesystemName !== null) {
       
    88             $returnAttrs['filesystemName'] = $this->_filesystemName;
       
    89         }
       
    90         return $returnAttrs;
       
    91     }
       
    92     
       
    93     /**
       
    94      * setResource()
       
    95      *
       
    96      * @param unknown_type $resource
       
    97      */
       
    98     public function setResource(Zend_Tool_Project_Profile_Resource $resource)
       
    99     {
       
   100         $this->_resource = $resource;
       
   101         $this->_resource->setAppendable(false);
       
   102         return $this;
       
   103     }
       
   104     
       
   105     /**
       
   106      * getResource()
       
   107      * 
       
   108      * @return Zend_Tool_Project_Profile_Resource
       
   109      */
       
   110     public function getResource()
       
   111     {
       
   112         return $this->_resource;
       
   113     }
       
   114 
       
   115     /**
       
   116      * create()
       
   117      *
       
   118      * @return Zend_Tool_Project_Context_Filesystem_File
       
   119      */
       
   120     public function create()
       
   121     {
       
   122         // check to ensure the parent exists, if not, call it and create it
       
   123         if (($parentResource = $this->_resource->getParentResource()) instanceof Zend_Tool_Project_Profile_Resource) {
       
   124             if ((($parentContext = $parentResource->getContext()) instanceof Zend_Tool_Project_Context_Filesystem_Abstract)
       
   125                 && (!$parentContext->exists())) {
       
   126                 $parentResource->create();
       
   127             }
       
   128         }
       
   129 
       
   130 
       
   131         if (file_exists($this->getPath())) {
       
   132             // @todo propt user to determine if its ok to overwrite file
       
   133         }
       
   134 
       
   135         file_put_contents($this->getPath(), $this->getContents());
       
   136         return $this;
       
   137     }
       
   138 
       
   139     /**
       
   140      * delete()
       
   141      *
       
   142      * @return Zend_Tool_Project_Context_Filesystem_File
       
   143      */
       
   144     public function delete()
       
   145     {
       
   146         unlink($this->getPath());
       
   147         $this->_resource->setDeleted(true);
       
   148         return $this;
       
   149     }
       
   150 
       
   151     /**
       
   152      * getContents()
       
   153      *
       
   154      * @return null
       
   155      */
       
   156     public function getContents()
       
   157     {
       
   158         return $this->_content;
       
   159     }
       
   160 
       
   161     protected function _initFileOnlyContext()
       
   162     {
       
   163         if ($this->_resource->hasAttribute('defaultContentCallback')) {
       
   164             $contentFunc = $this->_resource->getAttribute('defaultContentCallback');
       
   165             if (is_callable($contentFunc)) {
       
   166                 $this->_content = call_user_func_array($contentFunc, array($this));
       
   167             }
       
   168         }
       
   169         if ($this->_filesystemName == null) {
       
   170             $this->_filesystemName = 'file.txt';
       
   171         }
       
   172     }
       
   173     
       
   174 }