File indexing completed on 2024-06-23 05:55:50

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_File
0025  */
0026 // require_once 'Zend/Tool/Project/Context/Filesystem/File.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_Zf_ApplicationConfigFile extends Zend_Tool_Project_Context_Filesystem_File
0040 {
0041 
0042     /**
0043      * @var string
0044      */
0045     protected $_filesystemName = 'application.ini';
0046 
0047     /**
0048      * @var string
0049      */
0050     protected $_content = null;
0051 
0052     /**
0053      * getName()
0054      *
0055      * @return string
0056      */
0057     public function getName()
0058     {
0059         return 'ApplicationConfigFile';
0060     }
0061 
0062     /**
0063      * init()
0064      *
0065      * @return Zend_Tool_Project_Context_Zf_ApplicationConfigFile
0066      */
0067     public function init()
0068     {
0069         $this->_type = $this->_resource->getAttribute('type');
0070         parent::init();
0071         return $this;
0072     }
0073 
0074     /**
0075      * getPersistentAttributes()
0076      *
0077      * @return array
0078      */
0079     public function getPersistentAttributes()
0080     {
0081         return array('type' => $this->_type);
0082     }
0083 
0084     /**
0085      * getContents()
0086      *
0087      * @return string
0088      */
0089     public function getContents()
0090     {
0091         if ($this->_content === null) {
0092             if (file_exists($this->getPath())) {
0093                 $this->_content = file_get_contents($this->getPath());
0094             } else {
0095                 $this->_content = $this->_getDefaultContents();
0096             }
0097 
0098         }
0099 
0100         return $this->_content;
0101     }
0102 
0103     public function getAsZendConfig($section = 'production')
0104     {
0105         return new Zend_Config_Ini($this->getPath(), $section);
0106     }
0107 
0108     /**
0109      * addStringItem()
0110      *
0111      * @param string $key
0112      * @param string $value
0113      * @param string $section
0114      * @param bool   $quoteValue
0115      * @return Zend_Tool_Project_Context_Zf_ApplicationConfigFile
0116      */
0117     public function addStringItem($key, $value, $section = 'production', $quoteValue = true)
0118     {
0119         // null quote value means to auto-detect
0120         if ($quoteValue === null) {
0121             $quoteValue = preg_match('#[\"\']#', $value) ? false : true;
0122         }
0123 
0124         if ($quoteValue == true) {
0125             $value = '"' . $value . '"';
0126         }
0127 
0128         $contentLines = preg_split('#[\n\r]#', $this->getContents());
0129 
0130         $newLines = array();
0131         $insideSection = false;
0132 
0133         foreach ($contentLines as $contentLineIndex => $contentLine) {
0134 
0135             if ($insideSection === false && preg_match('#^\[' . $section . '#', $contentLine)) {
0136                 $insideSection = true;
0137             }
0138 
0139             $newLines[] = $contentLine;
0140             if ($insideSection) {
0141                 // if its blank, or a section heading
0142                 if (isset($contentLines[$contentLineIndex + 1]{0}) && $contentLines[$contentLineIndex + 1]{0} == '[') {
0143                     $newLines[] = $key . ' = ' . $value;
0144                     $insideSection = null;
0145                 } else if (!isset($contentLines[$contentLineIndex + 1])){
0146                     $newLines[] = $key . ' = ' . $value;
0147                     $insideSection = null;
0148                 }
0149             }
0150         }
0151 
0152         $this->_content = implode("\n", $newLines);
0153         return $this;
0154     }
0155 
0156     /**
0157      *
0158      * @param array $item
0159      * @param string $section
0160      * @param bool $quoteValue
0161      * @return Zend_Tool_Project_Context_Zf_ApplicationConfigFile
0162      */
0163     public function addItem($item, $section = 'production', $quoteValue = true)
0164     {
0165         $stringItems = array();
0166         $stringValues = array();
0167         $configKeyNames = array();
0168 
0169         $rii = new RecursiveIteratorIterator(
0170             new RecursiveArrayIterator($item),
0171             RecursiveIteratorIterator::SELF_FIRST
0172             );
0173 
0174         $lastDepth = 0;
0175 
0176         // loop through array structure recursively to create proper keys
0177         foreach ($rii as $name => $value) {
0178             $lastDepth = $rii->getDepth();
0179 
0180             if (is_array($value)) {
0181                 array_push($configKeyNames, $name);
0182             } else {
0183                 $stringItems[] = implode('.', $configKeyNames) . '.' . $name;
0184                 $stringValues[] = $value;
0185             }
0186         }
0187 
0188         foreach ($stringItems as $stringItemIndex => $stringItem) {
0189             $this->addStringItem($stringItem, $stringValues[$stringItemIndex], $section, $quoteValue);
0190         }
0191 
0192         return $this;
0193     }
0194 
0195     public function removeStringItem($key, $section = 'production')
0196     {
0197         $contentLines = file($this->getPath());
0198 
0199         $newLines = array();
0200         $insideSection = false;
0201 
0202         foreach ($contentLines as $contentLineIndex => $contentLine) {
0203 
0204             if ($insideSection === false && preg_match('#^\[' . $section . '#', $contentLine)) {
0205                 $insideSection = true;
0206             }
0207 
0208             if ($insideSection) {
0209                 // if its blank, or a section heading
0210                 if ((trim($contentLine) == null) || ($contentLines[$contentLineIndex + 1][0] == '[')) {
0211                     $insideSection = null;
0212                 }
0213             }
0214 
0215             if (!preg_match('#' . $key . '\s?=.*#', $contentLine)) {
0216                 $newLines[] = $contentLine;
0217             }
0218         }
0219 
0220         $this->_content = implode('', $newLines);
0221     }
0222 
0223     public function removeItem($item, $section = 'production')
0224     {
0225         $stringItems = array();
0226         $stringValues = array();
0227         $configKeyNames = array();
0228 
0229         $rii = new RecursiveIteratorIterator(
0230             new RecursiveArrayIterator($item),
0231             RecursiveIteratorIterator::SELF_FIRST
0232             );
0233 
0234         $lastDepth = 0;
0235 
0236         // loop through array structure recursively to create proper keys
0237         foreach ($rii as $name => $value) {
0238             $lastDepth = $rii->getDepth();
0239 
0240             if (is_array($value)) {
0241                 array_push($configKeyNames, $name);
0242             } else {
0243                 $stringItems[] = implode('.', $configKeyNames) . '.' . $name;
0244                 $stringValues[] = $value;
0245             }
0246         }
0247 
0248         foreach ($stringItems as $stringItemIndex => $stringItem) {
0249             $this->removeStringItem($stringItem, $section);
0250         }
0251 
0252         return $this;
0253     }
0254 
0255     protected function _getDefaultContents()
0256     {
0257 
0258         $contents =<<<EOS
0259 [production]
0260 phpSettings.display_startup_errors = 0
0261 phpSettings.display_errors = 0
0262 includePaths.library = APPLICATION_PATH "/../library"
0263 bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
0264 bootstrap.class = "Bootstrap"
0265 appnamespace = "Application"
0266 resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
0267 resources.frontController.params.displayExceptions = 0
0268 
0269 [staging : production]
0270 
0271 [testing : production]
0272 phpSettings.display_startup_errors = 1
0273 phpSettings.display_errors = 1
0274 
0275 [development : production]
0276 phpSettings.display_startup_errors = 1
0277 phpSettings.display_errors = 1
0278 resources.frontController.params.displayExceptions = 1
0279 
0280 EOS;
0281 
0282         return $contents;
0283     }
0284 
0285 }