File indexing completed on 2024-09-22 05:21:25

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_Filesystem_Abstract
0025  */
0026 // require_once 'Zend/Tool/Project/Context/Filesystem/Abstract.php';
0027 
0028 /**
0029  * This class is the front most class for utilizing Zend_Tool_Project
0030  *
0031  * A profile is a hierarchical set of resources that keep track of
0032  * items within a specific project.
0033  *
0034  * @category   Zend
0035  * @package    Zend_Tool
0036  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0037  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0038  */
0039 class Zend_Tool_Project_Context_Filesystem_File extends Zend_Tool_Project_Context_Filesystem_Abstract
0040 {
0041 
0042     protected $_fileOnlyContext = null;
0043 
0044     protected $_filesystemName = null;
0045 
0046     protected $_content = null;
0047 
0048     /**
0049      * getName()
0050      *
0051      * @return string
0052      */
0053     public function getName()
0054     {
0055         return 'file';
0056     }
0057 
0058     /**
0059      * init()
0060      *
0061      * @return Zend_Tool_Project_Context_Filesystem_File
0062      */
0063     public function init()
0064     {
0065         if ($this->_resource->hasAttribute('filesystemName')) {
0066             $this->_filesystemName = $this->_resource->getAttribute('filesystemName');
0067         }
0068 
0069         // check to see if this file is
0070         if ($this->getName() == 'file') {
0071             $this->_initFileOnlyContext();
0072         }
0073 
0074         // @potential-todo check to ensure that this 'file' resource has no children
0075         parent::init();
0076         return $this;
0077     }
0078 
0079     /**
0080      * getPersistentAttributes()
0081      *
0082      * @return array
0083      */
0084     public function getPersistentAttributes()
0085     {
0086         $returnAttrs = array();
0087         if ($this->_filesystemName !== null) {
0088             $returnAttrs['filesystemName'] = $this->_filesystemName;
0089         }
0090         return $returnAttrs;
0091     }
0092 
0093     /**
0094      * setResource()
0095      *
0096      * @param unknown_type $resource
0097      */
0098     public function setResource(Zend_Tool_Project_Profile_Resource $resource)
0099     {
0100         $this->_resource = $resource;
0101         $this->_resource->setAppendable(false);
0102         return $this;
0103     }
0104 
0105     /**
0106      * getResource()
0107      *
0108      * @return Zend_Tool_Project_Profile_Resource
0109      */
0110     public function getResource()
0111     {
0112         return $this->_resource;
0113     }
0114 
0115     /**
0116      * create()
0117      *
0118      * @return Zend_Tool_Project_Context_Filesystem_File
0119      */
0120     public function create()
0121     {
0122         // check to ensure the parent exists, if not, call it and create it
0123         if (($parentResource = $this->_resource->getParentResource()) instanceof Zend_Tool_Project_Profile_Resource) {
0124             if ((($parentContext = $parentResource->getContext()) instanceof Zend_Tool_Project_Context_Filesystem_Abstract)
0125                 && (!$parentContext->exists())) {
0126                 $parentResource->create();
0127             }
0128         }
0129 
0130 
0131         if (file_exists($this->getPath())) {
0132             // @todo propt user to determine if its ok to overwrite file
0133         }
0134 
0135         file_put_contents($this->getPath(), $this->getContents());
0136         return $this;
0137     }
0138 
0139     /**
0140      * delete()
0141      *
0142      * @return Zend_Tool_Project_Context_Filesystem_File
0143      */
0144     public function delete()
0145     {
0146         unlink($this->getPath());
0147         $this->_resource->setDeleted(true);
0148         return $this;
0149     }
0150 
0151     /**
0152      * getContents()
0153      *
0154      * @return null
0155      */
0156     public function getContents()
0157     {
0158         return $this->_content;
0159     }
0160 
0161     protected function _initFileOnlyContext()
0162     {
0163         if ($this->_resource->hasAttribute('defaultContentCallback')) {
0164             $contentFunc = $this->_resource->getAttribute('defaultContentCallback');
0165             if (is_callable($contentFunc)) {
0166                 $this->_content = call_user_func_array($contentFunc, array($this));
0167             }
0168         }
0169         if ($this->_filesystemName == null) {
0170             $this->_filesystemName = 'file.txt';
0171         }
0172     }
0173 
0174 }