3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 class ContainerConfig
{
22 public $default_container = 'default';
23 public $container_key = 'gadgets.container';
24 private $config = array();
26 public function __construct($defaultContainer) {
27 if (! empty($defaultContainer)) {
28 $this->loadContainers($defaultContainer);
32 private function loadContainers($containers) {
33 if (! File
::exists($containers)) {
34 throw new Exception("Invalid container path");
36 foreach (glob("$containers/*.js") as $file) {
37 if (! File
::readable($file)) {
38 throw new Exception("Could not read container config: $file");
41 // support recursive loading of sub directories
42 $this->loadContainers($file);
44 $this->loadFromFile($file);
49 private function loadFromFile($file) {
50 $contents = file_get_contents($file);
51 // remove all comments (both /* */ and // style) because this confuses the json parser
52 // note: the json parser also crashes on trailing ,'s in records so please don't use them
53 $contents = preg_replace('/[^http:\/\/|^https:\/\/]\/\/.*$/m', '', preg_replace('@/\\*(?:.|[\\n\\r])*?\\*/@', '', $contents));
54 $config = json_decode($contents, true);
55 if ($config == $contents) {
56 throw new Exception("Failed to json_decode the container configuration");
58 if (! isset($config[$this->container_key
][0])) {
59 throw new Exception("No gadgets.container value set for current container");
61 $container = $config[$this->container_key
][0];
62 $this->config
[$container] = array();
63 foreach ($config as $key => $val) {
64 $this->config
[$container][$key] = $val;
68 public function getConfig($container, $name) {
70 if (isset($this->config
[$container]) && isset($this->config
[$container][$name])) {
71 $config = $this->config
[$container][$name];
73 if ($container != $this->default_container
&& isset($this->config
[$this->default_container
]) && isset($this->config
[$this->default_container
][$name])) {
74 $config = $this->mergeConfig($this->config
[$this->default_container
][$name], $config);
79 // Code sniplet borrowed from: http://nl.php.net/manual/en/function.array-merge-recursive.php#81409
80 // default array merge recursive doesn't overwrite values, but creates multiple elementents for that key,
81 // which is not what we want here, we want array_merge like behavior
82 private function mergeConfig() // $array1, $array2, etc
84 $arrays = func_get_args();
85 $narrays = count($arrays);
86 for ($i = 0; $i < $narrays; $i ++
) {
87 if (! is_array($arrays[$i])) {
88 trigger_error('Argument #' . ($i +
1) . ' is not an array - trying to merge array with scalar! Returning null!', E_USER_WARNING
);
93 for ($i = 1; $i < $narrays; $i ++
) {
94 foreach ($arrays[$i] as $key => $value) {
95 if (((string)$key) === ((string)intval($key))) { // integer or string as integer key - append
98 if (is_array($value) && isset($ret[$key])) {
99 $ret[$key] = $this->mergeConfig($ret[$key], $value);