Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Сергей Шевченко
Added: May 16, 2019 10:01 AM
Views: 11
Tags: no tags
  1. /**
  2.  * Get value from config.
  3.  * Returned value from file $array['tree1']['tree2']['tree3']
  4.  * @example getConfigValue('tree1->tree2->tree3');
  5.  *
  6.  * @param string $path
  7.  * @param string|int|array $default
  8.  * @return string|int
  9.  * @throws Zend_Exception
  10.  */
  11. function getConfigValue($path, $default = null)
  12. {
  13.     $path = explode('->', $path);
  14.  
  15.     if (class_exists(Zend_Registry::class)) {
  16.         $config = Zend_Registry::get('config');
  17.         $config = $config->toArray();
  18.     }
  19.     else {
  20.         global $CONFIG;
  21.         $config = $CONFIG;
  22.     }
  23.  
  24.     foreach ($path as $p) {
  25.         $p = trim($p);
  26.         if (!isset($config[$p])) {
  27.             return $default;
  28.         }
  29.  
  30.         $config = $config[$p];
  31.     }
  32.  
  33.     return $config;
  34. }
  35.