File indexing completed on 2025-01-19 05:21:36

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  */
0021 
0022 /**
0023  * @see Zend_Tool_Framework_Provider_Abstract
0024  */
0025 // require_once "Zend/Tool/Framework/Provider/Abstract.php";
0026 
0027 /**
0028  * @see Zend_Config
0029  */
0030 // require_once "Zend/Config.php";
0031 
0032 /**
0033  * @see Zend_Config_Writer_Ini
0034  */
0035 // require_once "Zend/Config/Writer/Ini.php";
0036 
0037 /**
0038  * @see Zend_Loader
0039  */
0040 // require_once "Zend/Loader.php";
0041 
0042 /**
0043  * Configuration Provider
0044  *
0045  * @category   Zend
0046  * @package    Zend_Tool
0047  * @package    Framework
0048  * @uses       Zend_Tool_Framework_Provider_Abstract
0049  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0050  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0051  * @version    $Id$
0052  */
0053 class Zend_Tool_Framework_System_Provider_Config extends Zend_Tool_Framework_Provider_Abstract
0054 {
0055     /**
0056      * @var array
0057      */
0058     protected $_levelCompleted = array();
0059 
0060     /**
0061      * array of specialties handled by this provider
0062      *
0063      * @var array
0064      */
0065     protected $_specialties = array('Manifest', 'Provider');
0066 
0067     /**
0068      * @param string $type
0069      */
0070     public function create()
0071     {
0072         /* @var $userConfig Zend_Tool_Framework_Client_Config */
0073         $userConfig = $this->_registry->getConfig();
0074 
0075         $resp = $this->_registry->getResponse();
0076         if ($userConfig->exists()) {
0077             // require_once "Zend/Tool/Framework/Exception.php";
0078             throw new Zend_Tool_Framework_Exception(
0079                 "A configuration already exists, cannot create a new one.");
0080         }
0081 
0082         $homeDirectory = $this->_detectHomeDirectory();
0083 
0084         $writer = new Zend_Config_Writer_Ini();
0085         $writer->setRenderWithoutSections();
0086         $filename = $homeDirectory."/.zf.ini";
0087 
0088         $config = array(
0089             'php' => array(
0090                 'include_path' => get_include_path(),
0091             ),
0092         );
0093         $writer->write($filename, new Zend_Config($config));
0094 
0095         $resp = $this->_registry->getResponse();
0096         $resp->appendContent("Successfully written Zend Tool config.");
0097         $resp->appendContent("It is located at: ".$filename);
0098     }
0099 
0100     /**
0101      * @return string
0102      */
0103     protected function _detectHomeDirectory()
0104     {
0105         $envVars = array("ZF_HOME", "HOME", "HOMEPATH");
0106         foreach($envVars AS $env) {
0107             $homeDirectory = getenv($env);
0108             if ($homeDirectory != false && file_exists($homeDirectory)) {
0109                 return $homeDirectory;
0110             }
0111         }
0112         // require_once "Zend/Tool/Framework/Exception.php";
0113         throw new Zend_Tool_Framework_Exception("Cannot detect user home directory, set ZF_HOME enviroment variable.");
0114     }
0115 
0116     /**
0117      * Show Zend Tool User Configuration
0118      *
0119      * @return void
0120      */
0121     public function show()
0122     {
0123         $userConfig = $this->_loadUserConfigIfExists();
0124         $configArray = $userConfig->getConfigInstance()->toArray();
0125 
0126         $resp = $this->_registry->getResponse();
0127 
0128         $i = 0;
0129         $tree = "";
0130         foreach($configArray AS $k => $v) {
0131             $i++;
0132             $tree .= $this->_printTree($k, $v, 1, count($configArray)==$i);
0133         }
0134         $resp->appendContent("User Configuration: ".$userConfig->getConfigFilepath(), array("color" => "green"));
0135         $resp->appendContent($tree, array("indention" => 2));
0136     }
0137 
0138     /**
0139      *
0140      * @param string $key
0141      * @param string $value
0142      * @param int $level
0143      * @return string
0144      */
0145     protected function _printTree($key, $value, $level=1, $isLast=false)
0146     {
0147         $this->_levelCompleted[$level] = false;
0148 
0149         $prefix = "";
0150         for ($i = 1; $i < $level; $i++) {
0151             if ($this->_levelCompleted[$i] == true) {
0152                 $prefix .= "    ";
0153             } else {
0154                 $prefix .= "|   ";
0155             }
0156         }
0157         if ($isLast) {
0158             $pointer = "`-- ";
0159         } else {
0160             $pointer = "|-- ";
0161         }
0162 
0163         $tree = "";
0164         if (is_array($value)) {
0165             $tree .= $prefix.$pointer.$key.PHP_EOL;
0166 
0167             if ($isLast == true) {
0168                 $this->_levelCompleted[$level] = true;
0169             }
0170 
0171             $i = 0;
0172             foreach ($value as $k => $v) {
0173                 $i++;
0174                 $tree .= $this->_printTree($k, $v, $level+1, (count($value)==$i));
0175             }
0176         } else {
0177             $tree .= $prefix.$pointer.$key.": ".trim($value).PHP_EOL;
0178         }
0179 
0180         return $tree;
0181     }
0182 
0183     public function enable()
0184     {
0185         $resp = $this->_registry->getResponse();
0186         $resp->appendContent('Use either "zf enable config.provider" or "zf enable config.manifest".');
0187     }
0188 
0189     public function disable()
0190     {
0191         $resp = $this->_registry->getResponse();
0192         $resp->appendContent('Use either "zf disable config.provider" or "zf disable config.manifest".');
0193     }
0194 
0195     /**
0196      * @param string $className
0197      */
0198     public function enableProvider($className)
0199     {
0200         Zend_Loader::loadClass($className);
0201         $reflClass = new ReflectionClass($className);
0202         if (!in_array("Zend_Tool_Framework_Provider_Interface", $reflClass->getInterfaceNames())) {
0203             // require_once "Zend/Tool/Framework/Exception.php";
0204             throw new Zend_Tool_Framework_Exception("Given class is not a provider");
0205         }
0206         $this->_doEnable($className);
0207     }
0208 
0209     protected function _doEnable($className)
0210     {
0211 
0212         $userConfig = $this->_loadUserConfigIfExists();
0213 
0214         if (!isset($userConfig->basicloader)) {
0215             $userConfig->basicloader = array();
0216         }
0217         if (!isset($userConfig->basicloader->classes)) {
0218             $userConfig->basicloader->classes = array();
0219         }
0220 
0221         $providerClasses = $userConfig->basicloader->classes->toArray();
0222         if (!in_array($className, $providerClasses)) {
0223             if (count($providerClasses)) {
0224                 $pos = max(array_keys($providerClasses))+1;
0225             } else {
0226                 $pos = 0;
0227             }
0228             $userConfig->basicloader->classes->$pos = $className;
0229 
0230             if ($userConfig->save()) {
0231                 $this->_registry->getResponse()->appendContent(
0232                     "Provider/Manifest '".$className."' was enabled for usage with Zend Tool.",
0233                     array("color" => "green", "aligncenter" => true)
0234                 );
0235             } else {
0236                 // require_once "Zend/Tool/Framework/Exception.php";
0237                 throw new Zend_Tool_Framework_Exception(
0238                     "Could not write user configuration to persistence."
0239                 );
0240             }
0241         } else {
0242             // require_once "Zend/Tool/Framework/Exception.php";
0243             throw new Zend_Tool_Framework_Exception(
0244                 "Provider/Manifest '".$className."' is already enabled."
0245             );
0246         }
0247     }
0248 
0249     /**
0250      * @param string $className
0251      */
0252     public function enableManifest($className)
0253     {
0254         Zend_Loader::loadClass($className);
0255         $reflClass = new ReflectionClass($className);
0256         if (!in_array("Zend_Tool_Framework_Manifest_Interface", $reflClass->getInterfaceNames())) {
0257             // require_once "Zend/Tool/Framework/Exception.php";
0258             throw new Zend_Tool_Framework_Exception("Given class is not a manifest.");
0259         }
0260         $this->_doEnable($className);
0261     }
0262 
0263     /**
0264      * @param string $className
0265      */
0266     public function disableManifest($className)
0267     {
0268         $this->disableProvider($className);
0269     }
0270 
0271     /**
0272      * @param string $className
0273      */
0274     public function disableProvider($className)
0275     {
0276         $userConfig = $this->_loadUserConfigIfExists();
0277 
0278         if (!isset($userConfig->basicloader)) {
0279             $userConfig->basicloader = array();
0280         }
0281         if (!isset($userConfig->basicloader->classes)) {
0282             $userConfig->basicloader->classes = array();
0283         }
0284 
0285         $providerClasses = $userConfig->basicloader->classes->toArray();
0286         if (($key = array_search($className, $providerClasses)) !== false) {
0287             unset($userConfig->basicloader->classes->$key);
0288 
0289             if ($userConfig->save()) {
0290                 $this->_registry->getResponse()->appendContent(
0291                     "Provider/Manifest '".$className."' was disabled.",
0292                     array("color" => "green", "aligncenter" => true)
0293                 );
0294             } else {
0295                 // require_once "Zend/Tool/Framework/Exception.php";
0296                 throw new Zend_Tool_Framework_Exception(
0297                     "Could not write user configuration to persistence."
0298                 );
0299             }
0300         } else {
0301             // require_once "Zend/Tool/Framework/Exception.php";
0302             throw new Zend_Tool_Framework_Exception(
0303                 "Provider/Manifest '".$className."' is not enabled."
0304             );
0305         }
0306     }
0307 
0308     /**
0309      * @return Zend_Tool_Framework_Client_Config
0310      */
0311     protected function _loadUserConfigIfExists()
0312     {
0313         /* @var $userConfig Zend_Tool_Framework_Client_Config */
0314         $userConfig = $this->_registry->getConfig();
0315 
0316         $resp = $this->_registry->getResponse();
0317         if (!$userConfig->exists()) {
0318             $resp->appendContent("User has no config file.", array("aligncenter" => true, "color" => array('hiWhite', 'bgRed')));
0319         }
0320 
0321         return $userConfig;
0322     }
0323 }