web/lib/Zend/Tool/Project/Context/Zf/ApplicationConfigFile.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Tool
    16  * @package    Zend_Tool
    17  * @subpackage Framework
    17  * @subpackage Framework
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    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 $
    20  * @version    $Id: ApplicationConfigFile.php 24593 2012-01-05 20:35:02Z matthew $
    21  */
    21  */
    22 
    22 
    23 /**
    23 /**
    24  * @see Zend_Tool_Project_Context_Filesystem_File
    24  * @see Zend_Tool_Project_Context_Filesystem_File
    25  */
    25  */
    31  * A profile is a hierarchical set of resources that keep track of
    31  * A profile is a hierarchical set of resources that keep track of
    32  * items within a specific project.
    32  * items within a specific project.
    33  *
    33  *
    34  * @category   Zend
    34  * @category   Zend
    35  * @package    Zend_Tool
    35  * @package    Zend_Tool
    36  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    36  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    37  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    37  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    38  */
    38  */
    39 class Zend_Tool_Project_Context_Zf_ApplicationConfigFile extends Zend_Tool_Project_Context_Filesystem_File
    39 class Zend_Tool_Project_Context_Zf_ApplicationConfigFile extends Zend_Tool_Project_Context_Filesystem_File
    40 {
    40 {
    41 
    41 
    46 
    46 
    47     /**
    47     /**
    48      * @var string
    48      * @var string
    49      */
    49      */
    50     protected $_content = null;
    50     protected $_content = null;
    51     
    51 
    52     /**
    52     /**
    53      * getName()
    53      * getName()
    54      *
    54      *
    55      * @return string
    55      * @return string
    56      */
    56      */
    92             if (file_exists($this->getPath())) {
    92             if (file_exists($this->getPath())) {
    93                 $this->_content = file_get_contents($this->getPath());
    93                 $this->_content = file_get_contents($this->getPath());
    94             } else {
    94             } else {
    95                 $this->_content = $this->_getDefaultContents();
    95                 $this->_content = $this->_getDefaultContents();
    96             }
    96             }
    97             
    97 
    98         }
    98         }
    99         
    99 
   100         return $this->_content;
   100         return $this->_content;
   101     }
   101     }
   102 
   102 
   103     public function getAsZendConfig($section = 'production')
   103     public function getAsZendConfig($section = 'production')
   104     {
   104     {
   105         return new Zend_Config_Ini($this->getPath(), $section);
   105         return new Zend_Config_Ini($this->getPath(), $section);
   106     }
   106     }
   107     
   107 
   108     /**
   108     /**
   109      * addStringItem()
   109      * addStringItem()
   110      * 
   110      *
   111      * @param string $key 
   111      * @param string $key
   112      * @param string $value
   112      * @param string $value
   113      * @param string $section
   113      * @param string $section
   114      * @param bool   $quoteValue
   114      * @param bool   $quoteValue
   115      * @return Zend_Tool_Project_Context_Zf_ApplicationConfigFile
   115      * @return Zend_Tool_Project_Context_Zf_ApplicationConfigFile
   116      */
   116      */
   118     {
   118     {
   119         // null quote value means to auto-detect
   119         // null quote value means to auto-detect
   120         if ($quoteValue === null) {
   120         if ($quoteValue === null) {
   121             $quoteValue = preg_match('#[\"\']#', $value) ? false : true;
   121             $quoteValue = preg_match('#[\"\']#', $value) ? false : true;
   122         }
   122         }
   123         
   123 
   124         if ($quoteValue == true) {
   124         if ($quoteValue == true) {
   125             $value = '"' . $value . '"';
   125             $value = '"' . $value . '"';
   126         }
   126         }
   127         
   127 
   128         $contentLines = preg_split('#[\n\r]#', $this->getContents());
   128         $contentLines = preg_split('#[\n\r]#', $this->getContents());
   129         
   129 
   130         $newLines = array();
   130         $newLines = array();
   131         $insideSection = false;
   131         $insideSection = false;
   132         
   132 
   133         foreach ($contentLines as $contentLineIndex => $contentLine) {
   133         foreach ($contentLines as $contentLineIndex => $contentLine) {
   134             
   134 
   135             if ($insideSection === false && preg_match('#^\[' . $section . '#', $contentLine)) {
   135             if ($insideSection === false && preg_match('#^\[' . $section . '#', $contentLine)) {
   136                 $insideSection = true;
   136                 $insideSection = true;
   137             }
   137             }
   138             
   138 
       
   139             $newLines[] = $contentLine;
   139             if ($insideSection) {
   140             if ($insideSection) {
   140                 // if its blank, or a section heading
   141                 // if its blank, or a section heading
   141                 if ((trim($contentLine) == null) || (isset($contentLines[$contentLineIndex + 1]{0}) && $contentLines[$contentLineIndex + 1]{0} == '[')) {
   142                 if (isset($contentLines[$contentLineIndex + 1]{0}) && $contentLines[$contentLineIndex + 1]{0} == '[') {
       
   143                     $newLines[] = $key . ' = ' . $value;
       
   144                     $insideSection = null;
       
   145                 } else if (!isset($contentLines[$contentLineIndex + 1])){
   142                     $newLines[] = $key . ' = ' . $value;
   146                     $newLines[] = $key . ' = ' . $value;
   143                     $insideSection = null;
   147                     $insideSection = null;
   144                 }
   148                 }
   145             }
   149             }
   146             
       
   147             $newLines[] = $contentLine;
       
   148         }
   150         }
   149 
   151 
   150         $this->_content = implode("\n", $newLines);
   152         $this->_content = implode("\n", $newLines);
   151         return $this;
   153         return $this;
   152     }
   154     }
   153     
   155 
   154     /**
   156     /**
   155      * 
   157      *
   156      * @param array $item
   158      * @param array $item
   157      * @param string $section
   159      * @param string $section
   158      * @param bool $quoteValue
   160      * @param bool $quoteValue
   159      * @return Zend_Tool_Project_Context_Zf_ApplicationConfigFile
   161      * @return Zend_Tool_Project_Context_Zf_ApplicationConfigFile
   160      */
   162      */
   161     public function addItem($item, $section = 'production', $quoteValue = true)
   163     public function addItem($item, $section = 'production', $quoteValue = true)
   162     {
   164     {
   163         $stringItems = array();
   165         $stringItems = array();
   164         $stringValues = array();
   166         $stringValues = array();
   165         $configKeyNames = array();
   167         $configKeyNames = array();
   166         
   168 
   167         $rii = new RecursiveIteratorIterator(
   169         $rii = new RecursiveIteratorIterator(
   168             new RecursiveArrayIterator($item),
   170             new RecursiveArrayIterator($item),
   169             RecursiveIteratorIterator::SELF_FIRST
   171             RecursiveIteratorIterator::SELF_FIRST
   170             );
   172             );
   171         
   173 
   172         $lastDepth = 0;
   174         $lastDepth = 0;
   173         
   175 
   174         // loop through array structure recursively to create proper keys
   176         // loop through array structure recursively to create proper keys
   175         foreach ($rii as $name => $value) {
   177         foreach ($rii as $name => $value) {
   176             $lastDepth = $rii->getDepth();
   178             $lastDepth = $rii->getDepth();
   177             
   179 
   178             if (is_array($value)) {
   180             if (is_array($value)) {
   179                 array_push($configKeyNames, $name);
   181                 array_push($configKeyNames, $name);
   180             } else {
   182             } else {
   181                 $stringItems[] = implode('.', $configKeyNames) . '.' . $name;
   183                 $stringItems[] = implode('.', $configKeyNames) . '.' . $name;
   182                 $stringValues[] = $value;
   184                 $stringValues[] = $value;
   183             }
   185             }
   184         }
   186         }
   185         
   187 
   186         foreach ($stringItems as $stringItemIndex => $stringItem) {
   188         foreach ($stringItems as $stringItemIndex => $stringItem) {
   187             $this->addStringItem($stringItem, $stringValues[$stringItemIndex], $section, $quoteValue);
   189             $this->addStringItem($stringItem, $stringValues[$stringItemIndex], $section, $quoteValue);
   188         }
   190         }
   189         
   191 
   190         return $this;
   192         return $this;
   191     }
   193     }
   192     
   194 
   193     public function removeStringItem($key, $section = 'production')
   195     public function removeStringItem($key, $section = 'production')
   194     {
   196     {
   195         $contentLines = file($this->getPath());
   197         $contentLines = file($this->getPath());
   196         
   198 
   197         $newLines = array();
   199         $newLines = array();
   198         $insideSection = false;
   200         $insideSection = false;
   199         
   201 
   200         foreach ($contentLines as $contentLineIndex => $contentLine) {
   202         foreach ($contentLines as $contentLineIndex => $contentLine) {
   201             
   203 
   202             if ($insideSection === false && preg_match('#^\[' . $section . '#', $contentLine)) {
   204             if ($insideSection === false && preg_match('#^\[' . $section . '#', $contentLine)) {
   203                 $insideSection = true;
   205                 $insideSection = true;
   204             }
   206             }
   205             
   207 
   206             if ($insideSection) {
   208             if ($insideSection) {
   207                 // if its blank, or a section heading
   209                 // if its blank, or a section heading
   208                 if ((trim($contentLine) == null) || ($contentLines[$contentLineIndex + 1][0] == '[')) {
   210                 if ((trim($contentLine) == null) || ($contentLines[$contentLineIndex + 1][0] == '[')) {
   209                     $insideSection = null;
   211                     $insideSection = null;
   210                 }
   212                 }
   211             }
   213             }
   212             
   214 
   213             if (!preg_match('#' . $key . '\s?=.*#', $contentLine)) { 
   215             if (!preg_match('#' . $key . '\s?=.*#', $contentLine)) {
   214                 $newLines[] = $contentLine;
   216                 $newLines[] = $contentLine;
   215             }
   217             }
   216         }
   218         }
   217 
   219 
   218         $this->_content = implode('', $newLines);
   220         $this->_content = implode('', $newLines);
   219     }
   221     }
   220     
   222 
   221     public function removeItem($item, $section = 'production')
   223     public function removeItem($item, $section = 'production')
   222     {
   224     {
   223         $stringItems = array();
   225         $stringItems = array();
   224         $stringValues = array();
   226         $stringValues = array();
   225         $configKeyNames = array();
   227         $configKeyNames = array();
   226         
   228 
   227         $rii = new RecursiveIteratorIterator(
   229         $rii = new RecursiveIteratorIterator(
   228             new RecursiveArrayIterator($item),
   230             new RecursiveArrayIterator($item),
   229             RecursiveIteratorIterator::SELF_FIRST
   231             RecursiveIteratorIterator::SELF_FIRST
   230             );
   232             );
   231         
   233 
   232         $lastDepth = 0;
   234         $lastDepth = 0;
   233         
   235 
   234         // loop through array structure recursively to create proper keys
   236         // loop through array structure recursively to create proper keys
   235         foreach ($rii as $name => $value) {
   237         foreach ($rii as $name => $value) {
   236             $lastDepth = $rii->getDepth();
   238             $lastDepth = $rii->getDepth();
   237             
   239 
   238             if (is_array($value)) {
   240             if (is_array($value)) {
   239                 array_push($configKeyNames, $name);
   241                 array_push($configKeyNames, $name);
   240             } else {
   242             } else {
   241                 $stringItems[] = implode('.', $configKeyNames) . '.' . $name;
   243                 $stringItems[] = implode('.', $configKeyNames) . '.' . $name;
   242                 $stringValues[] = $value;
   244                 $stringValues[] = $value;
   243             }
   245             }
   244         }
   246         }
   245         
   247 
   246         foreach ($stringItems as $stringItemIndex => $stringItem) {
   248         foreach ($stringItems as $stringItemIndex => $stringItem) {
   247             $this->removeStringItem($stringItem, $section);
   249             $this->removeStringItem($stringItem, $section);
   248         }
   250         }
   249         
   251 
   250         return $this;
   252         return $this;
   251     }
   253     }
   252     
   254 
   253     protected function _getDefaultContents()
   255     protected function _getDefaultContents()
   254     {
   256     {
   255 
   257 
   256         $contents =<<<EOS
   258         $contents =<<<EOS
   257 [production]
   259 [production]
   277 
   279 
   278 EOS;
   280 EOS;
   279 
   281 
   280         return $contents;
   282         return $contents;
   281     }
   283     }
   282     
   284 
   283 }
   285 }