web/lib/Zend/Config/Writer/FileAbstract.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_Config
       
    17  * @package    Writer
       
    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  */
       
    21 
       
    22 require_once "Zend/Config/Writer.php";
       
    23 
       
    24 /**
       
    25  * Abstract File Writer
       
    26  *
       
    27  * @category   Zend
       
    28  * @package    Zend_package
       
    29  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    30  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    31  * @version    $Id: FileAbstract.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    32  */
       
    33 class Zend_Config_Writer_FileAbstract extends Zend_Config_Writer
       
    34 {
       
    35     /**
       
    36      * Filename to write to
       
    37      *
       
    38      * @var string
       
    39      */
       
    40     protected $_filename = null;
       
    41 
       
    42     /**
       
    43      * Wether to exclusively lock the file or not
       
    44      *
       
    45      * @var boolean
       
    46      */
       
    47     protected $_exclusiveLock = false;
       
    48 
       
    49     /**
       
    50      * Set the target filename
       
    51      *
       
    52      * @param  string $filename
       
    53      * @return Zend_Config_Writer_Array
       
    54      */
       
    55     public function setFilename($filename)
       
    56     {
       
    57         $this->_filename = $filename;
       
    58 
       
    59         return $this;
       
    60     }
       
    61 
       
    62     /**
       
    63      * Set wether to exclusively lock the file or not
       
    64      *
       
    65      * @param  boolean     $exclusiveLock
       
    66      * @return Zend_Config_Writer_Array
       
    67      */
       
    68     public function setExclusiveLock($exclusiveLock)
       
    69     {
       
    70         $this->_exclusiveLock = $exclusiveLock;
       
    71 
       
    72         return $this;
       
    73     }
       
    74 
       
    75     /**
       
    76      * Write configuration to file.
       
    77      *
       
    78      * @param string $filename
       
    79      * @param Zend_Config $config
       
    80      * @param bool $exclusiveLock
       
    81      * @return void
       
    82      */
       
    83     public function write($filename = null, Zend_Config $config = null, $exclusiveLock = null)
       
    84     {
       
    85         if ($filename !== null) {
       
    86             $this->setFilename($filename);
       
    87         }
       
    88 
       
    89         if ($config !== null) {
       
    90             $this->setConfig($config);
       
    91         }
       
    92 
       
    93         if ($exclusiveLock !== null) {
       
    94             $this->setExclusiveLock($exclusiveLock);
       
    95         }
       
    96 
       
    97         if ($this->_filename === null) {
       
    98             require_once 'Zend/Config/Exception.php';
       
    99             throw new Zend_Config_Exception('No filename was set');
       
   100         }
       
   101 
       
   102         if ($this->_config === null) {
       
   103             require_once 'Zend/Config/Exception.php';
       
   104             throw new Zend_Config_Exception('No config was set');
       
   105         }
       
   106 
       
   107         $configString = $this->render();
       
   108 
       
   109         $flags = 0;
       
   110 
       
   111         if ($this->_exclusiveLock) {
       
   112             $flags |= LOCK_EX;
       
   113         }
       
   114 
       
   115         $result = @file_put_contents($this->_filename, $configString, $flags);
       
   116 
       
   117         if ($result === false) {
       
   118             require_once 'Zend/Config/Exception.php';
       
   119             throw new Zend_Config_Exception('Could not write to file "' . $this->_filename . '"');
       
   120         }
       
   121     }
       
   122 
       
   123     /**
       
   124      * Render a Zend_Config into a config file string.
       
   125      *
       
   126      * @since 1.10
       
   127      * @todo For 2.0 this should be redone into an abstract method.
       
   128      * @return string
       
   129      */
       
   130     public function render()
       
   131     {
       
   132         return "";
       
   133     }
       
   134 }