1 <?php
2 /**
3 * @package FrameworkOnFramework
4 * @subpackage utils
5 * @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
6 * @license GNU General Public License version 2 or later; see LICENSE.txt
7 */
8
9 defined('FOF_INCLUDED') or die;
10
11 /**
12 * A utility class to help you fetch component parameters without going through JComponentHelper
13 */
14 class FOFUtilsConfigHelper
15 {
16 /**
17 * Caches the component parameters without going through JComponentHelper. This is necessary since JComponentHelper
18 * cannot be reset or updated once you update parameters in the database.
19 *
20 * @var array
21 */
22 private static $componentParams = array();
23
24 /**
25 * Loads the component's configuration parameters so they can be accessed by getComponentConfigurationValue
26 *
27 * @param string $component The component for loading the parameters
28 * @param bool $force Should I force-reload the configuration information?
29 */
30 public final static function loadComponentConfig($component, $force = false)
31 {
32 if (isset(self::$componentParams[$component]) && !is_null(self::$componentParams[$component]) && !$force)
33 {
34 return;
35 }
36
37 $db = FOFPlatform::getInstance()->getDbo();
38
39 $sql = $db->getQuery(true)
40 ->select($db->qn('params'))
41 ->from($db->qn('#__extensions'))
42 ->where($db->qn('type') . ' = ' . $db->q('component'))
43 ->where($db->qn('element') . " = " . $db->q($component));
44 $db->setQuery($sql);
45 $config_ini = $db->loadResult();
46
47 // OK, Joomla! 1.6 stores values JSON-encoded so, what do I do? Right!
48 $config_ini = trim($config_ini);
49
50 if ((substr($config_ini, 0, 1) == '{') && substr($config_ini, -1) == '}')
51 {
52 $config_ini = json_decode($config_ini, true);
53 }
54 else
55 {
56 $config_ini = FOFUtilsIniParser::parse_ini_file($config_ini, false, true);
57 }
58
59 if (is_null($config_ini) || empty($config_ini))
60 {
61 $config_ini = array();
62 }
63
64 self::$componentParams[$component] = $config_ini;
65 }
66
67 /**
68 * Retrieves the value of a component configuration parameter without going through JComponentHelper
69 *
70 * @param string $component The component for loading the parameter value
71 * @param string $key The key to retrieve
72 * @param mixed $default The default value to use in case the key is missing
73 *
74 * @return mixed
75 */
76 public final static function getComponentConfigurationValue($component, $key, $default = null)
77 {
78 self::loadComponentConfig($component, false);
79
80 if (array_key_exists($key, self::$componentParams[$component]))
81 {
82 return self::$componentParams[$component][$key];
83 }
84 else
85 {
86 return $default;
87 }
88 }
89 }