web/lib/Zend/Tool/Project/Context/Zf/ApplicationConfigFile.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: ApplicationConfigFile.php 20851 2010-02-02 21:45:51Z ralph $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Tool_Project_Context_Filesystem_File
       
    25  */
       
    26 require_once 'Zend/Tool/Project/Context/Filesystem/File.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_Zf_ApplicationConfigFile extends Zend_Tool_Project_Context_Filesystem_File
       
    40 {
       
    41 
       
    42     /**
       
    43      * @var string
       
    44      */
       
    45     protected $_filesystemName = 'application.ini';
       
    46 
       
    47     /**
       
    48      * @var string
       
    49      */
       
    50     protected $_content = null;
       
    51     
       
    52     /**
       
    53      * getName()
       
    54      *
       
    55      * @return string
       
    56      */
       
    57     public function getName()
       
    58     {
       
    59         return 'ApplicationConfigFile';
       
    60     }
       
    61 
       
    62     /**
       
    63      * init()
       
    64      *
       
    65      * @return Zend_Tool_Project_Context_Zf_ApplicationConfigFile
       
    66      */
       
    67     public function init()
       
    68     {
       
    69         $this->_type = $this->_resource->getAttribute('type');
       
    70         parent::init();
       
    71         return $this;
       
    72     }
       
    73 
       
    74     /**
       
    75      * getPersistentAttributes()
       
    76      *
       
    77      * @return array
       
    78      */
       
    79     public function getPersistentAttributes()
       
    80     {
       
    81         return array('type' => $this->_type);
       
    82     }
       
    83 
       
    84     /**
       
    85      * getContents()
       
    86      *
       
    87      * @return string
       
    88      */
       
    89     public function getContents()
       
    90     {
       
    91         if ($this->_content === null) {
       
    92             if (file_exists($this->getPath())) {
       
    93                 $this->_content = file_get_contents($this->getPath());
       
    94             } else {
       
    95                 $this->_content = $this->_getDefaultContents();
       
    96             }
       
    97             
       
    98         }
       
    99         
       
   100         return $this->_content;
       
   101     }
       
   102 
       
   103     public function getAsZendConfig($section = 'production')
       
   104     {
       
   105         return new Zend_Config_Ini($this->getPath(), $section);
       
   106     }
       
   107     
       
   108     /**
       
   109      * addStringItem()
       
   110      * 
       
   111      * @param string $key 
       
   112      * @param string $value
       
   113      * @param string $section
       
   114      * @param bool   $quoteValue
       
   115      * @return Zend_Tool_Project_Context_Zf_ApplicationConfigFile
       
   116      */
       
   117     public function addStringItem($key, $value, $section = 'production', $quoteValue = true)
       
   118     {
       
   119         // null quote value means to auto-detect
       
   120         if ($quoteValue === null) {
       
   121             $quoteValue = preg_match('#[\"\']#', $value) ? false : true;
       
   122         }
       
   123         
       
   124         if ($quoteValue == true) {
       
   125             $value = '"' . $value . '"';
       
   126         }
       
   127         
       
   128         $contentLines = preg_split('#[\n\r]#', $this->getContents());
       
   129         
       
   130         $newLines = array();
       
   131         $insideSection = false;
       
   132         
       
   133         foreach ($contentLines as $contentLineIndex => $contentLine) {
       
   134             
       
   135             if ($insideSection === false && preg_match('#^\[' . $section . '#', $contentLine)) {
       
   136                 $insideSection = true;
       
   137             }
       
   138             
       
   139             if ($insideSection) {
       
   140                 // if its blank, or a section heading
       
   141                 if ((trim($contentLine) == null) || (isset($contentLines[$contentLineIndex + 1]{0}) && $contentLines[$contentLineIndex + 1]{0} == '[')) {
       
   142                     $newLines[] = $key . ' = ' . $value;
       
   143                     $insideSection = null;
       
   144                 }
       
   145             }
       
   146             
       
   147             $newLines[] = $contentLine;
       
   148         }
       
   149 
       
   150         $this->_content = implode("\n", $newLines);
       
   151         return $this;
       
   152     }
       
   153     
       
   154     /**
       
   155      * 
       
   156      * @param array $item
       
   157      * @param string $section
       
   158      * @param bool $quoteValue
       
   159      * @return Zend_Tool_Project_Context_Zf_ApplicationConfigFile
       
   160      */
       
   161     public function addItem($item, $section = 'production', $quoteValue = true)
       
   162     {
       
   163         $stringItems = array();
       
   164         $stringValues = array();
       
   165         $configKeyNames = array();
       
   166         
       
   167         $rii = new RecursiveIteratorIterator(
       
   168             new RecursiveArrayIterator($item),
       
   169             RecursiveIteratorIterator::SELF_FIRST
       
   170             );
       
   171         
       
   172         $lastDepth = 0;
       
   173         
       
   174         // loop through array structure recursively to create proper keys
       
   175         foreach ($rii as $name => $value) {
       
   176             $lastDepth = $rii->getDepth();
       
   177             
       
   178             if (is_array($value)) {
       
   179                 array_push($configKeyNames, $name);
       
   180             } else {
       
   181                 $stringItems[] = implode('.', $configKeyNames) . '.' . $name;
       
   182                 $stringValues[] = $value;
       
   183             }
       
   184         }
       
   185         
       
   186         foreach ($stringItems as $stringItemIndex => $stringItem) {
       
   187             $this->addStringItem($stringItem, $stringValues[$stringItemIndex], $section, $quoteValue);
       
   188         }
       
   189         
       
   190         return $this;
       
   191     }
       
   192     
       
   193     public function removeStringItem($key, $section = 'production')
       
   194     {
       
   195         $contentLines = file($this->getPath());
       
   196         
       
   197         $newLines = array();
       
   198         $insideSection = false;
       
   199         
       
   200         foreach ($contentLines as $contentLineIndex => $contentLine) {
       
   201             
       
   202             if ($insideSection === false && preg_match('#^\[' . $section . '#', $contentLine)) {
       
   203                 $insideSection = true;
       
   204             }
       
   205             
       
   206             if ($insideSection) {
       
   207                 // if its blank, or a section heading
       
   208                 if ((trim($contentLine) == null) || ($contentLines[$contentLineIndex + 1][0] == '[')) {
       
   209                     $insideSection = null;
       
   210                 }
       
   211             }
       
   212             
       
   213             if (!preg_match('#' . $key . '\s?=.*#', $contentLine)) { 
       
   214                 $newLines[] = $contentLine;
       
   215             }
       
   216         }
       
   217 
       
   218         $this->_content = implode('', $newLines);
       
   219     }
       
   220     
       
   221     public function removeItem($item, $section = 'production')
       
   222     {
       
   223         $stringItems = array();
       
   224         $stringValues = array();
       
   225         $configKeyNames = array();
       
   226         
       
   227         $rii = new RecursiveIteratorIterator(
       
   228             new RecursiveArrayIterator($item),
       
   229             RecursiveIteratorIterator::SELF_FIRST
       
   230             );
       
   231         
       
   232         $lastDepth = 0;
       
   233         
       
   234         // loop through array structure recursively to create proper keys
       
   235         foreach ($rii as $name => $value) {
       
   236             $lastDepth = $rii->getDepth();
       
   237             
       
   238             if (is_array($value)) {
       
   239                 array_push($configKeyNames, $name);
       
   240             } else {
       
   241                 $stringItems[] = implode('.', $configKeyNames) . '.' . $name;
       
   242                 $stringValues[] = $value;
       
   243             }
       
   244         }
       
   245         
       
   246         foreach ($stringItems as $stringItemIndex => $stringItem) {
       
   247             $this->removeStringItem($stringItem, $section);
       
   248         }
       
   249         
       
   250         return $this;
       
   251     }
       
   252     
       
   253     protected function _getDefaultContents()
       
   254     {
       
   255 
       
   256         $contents =<<<EOS
       
   257 [production]
       
   258 phpSettings.display_startup_errors = 0
       
   259 phpSettings.display_errors = 0
       
   260 includePaths.library = APPLICATION_PATH "/../library"
       
   261 bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
       
   262 bootstrap.class = "Bootstrap"
       
   263 appnamespace = "Application"
       
   264 resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
       
   265 resources.frontController.params.displayExceptions = 0
       
   266 
       
   267 [staging : production]
       
   268 
       
   269 [testing : production]
       
   270 phpSettings.display_startup_errors = 1
       
   271 phpSettings.display_errors = 1
       
   272 
       
   273 [development : production]
       
   274 phpSettings.display_startup_errors = 1
       
   275 phpSettings.display_errors = 1
       
   276 resources.frontController.params.displayExceptions = 1
       
   277 
       
   278 EOS;
       
   279 
       
   280         return $contents;
       
   281     }
       
   282     
       
   283 }