Open Journal Systems  3.3.0
AbstractConfigLoader.php
1 <?php
2 
3 namespace Guzzle\Service;
4 
7 
12 {
14  protected $aliases = array();
15 
17  protected $loadedFiles = array();
18 
20  protected static $jsonErrors = array(
21  JSON_ERROR_NONE => 'JSON_ERROR_NONE - No errors',
22  JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded',
23  JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch',
24  JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found',
25  JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON',
26  JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded'
27  );
28 
29  public function load($config, array $options = array())
30  {
31  // Reset the array of loaded files because this is a new config
32  $this->loadedFiles = array();
33 
34  if (is_string($config)) {
35  $config = $this->loadFile($config);
36  } elseif (!is_array($config)) {
37  throw new InvalidArgumentException('Unknown type passed to configuration loader: ' . gettype($config));
38  } else {
39  $this->mergeIncludes($config);
40  }
41 
42  return $this->build($config, $options);
43  }
44 
53  public function addAlias($filename, $alias)
54  {
55  $this->aliases[$filename] = $alias;
56 
57  return $this;
58  }
59 
67  public function removeAlias($alias)
68  {
69  unset($this->aliases[$alias]);
70 
71  return $this;
72  }
73 
82  protected abstract function build($config, array $options);
83 
93  protected function loadFile($filename)
94  {
95  if (isset($this->aliases[$filename])) {
96  $filename = $this->aliases[$filename];
97  }
98 
99  switch (pathinfo($filename, PATHINFO_EXTENSION)) {
100  case 'js':
101  case 'json':
102  $level = error_reporting(0);
103  $json = file_get_contents($filename);
104  error_reporting($level);
105 
106  if ($json === false) {
107  $err = error_get_last();
108  throw new InvalidArgumentException("Unable to open {$filename}: " . $err['message']);
109  }
110 
111  $config = json_decode($json, true);
112  // Throw an exception if there was an error loading the file
113  if ($error = json_last_error()) {
114  $message = isset(self::$jsonErrors[$error]) ? self::$jsonErrors[$error] : 'Unknown error';
115  throw new RuntimeException("Error loading JSON data from {$filename}: ({$error}) - {$message}");
116  }
117  break;
118  case 'php':
119  if (!is_readable($filename)) {
120  throw new InvalidArgumentException("Unable to open {$filename} for reading");
121  }
122  $config = require $filename;
123  if (!is_array($config)) {
124  throw new InvalidArgumentException('PHP files must return an array of configuration data');
125  }
126  break;
127  default:
128  throw new InvalidArgumentException('Unknown file extension: ' . $filename);
129  }
130 
131  // Keep track of this file being loaded to prevent infinite recursion
132  $this->loadedFiles[$filename] = true;
133 
134  // Merge include files into the configuration array
135  $this->mergeIncludes($config, dirname($filename));
136 
137  return $config;
138  }
139 
148  protected function mergeIncludes(&$config, $basePath = null)
149  {
150  if (!empty($config['includes'])) {
151  foreach ($config['includes'] as &$path) {
152  // Account for relative paths
153  if ($path[0] != DIRECTORY_SEPARATOR && !isset($this->aliases[$path]) && $basePath) {
154  $path = "{$basePath}/{$path}";
155  }
156  // Don't load the same files more than once
157  if (!isset($this->loadedFiles[$path])) {
158  $this->loadedFiles[$path] = true;
159  $config = $this->mergeData($this->loadFile($path), $config);
160  }
161  }
162  }
163  }
164 
173  protected function mergeData(array $a, array $b)
174  {
175  return array_merge_recursive($a, $b);
176  }
177 }
Guzzle\Service\AbstractConfigLoader\$jsonErrors
static $jsonErrors
Definition: AbstractConfigLoader.php:26
Guzzle\Service\AbstractConfigLoader
Definition: AbstractConfigLoader.php:11
Guzzle\Service\AbstractConfigLoader\$loadedFiles
$loadedFiles
Definition: AbstractConfigLoader.php:23
Guzzle\Service\AbstractConfigLoader\removeAlias
removeAlias($alias)
Definition: AbstractConfigLoader.php:73
Guzzle\Service\AbstractConfigLoader\$aliases
$aliases
Definition: AbstractConfigLoader.php:17
Guzzle\Service\AbstractConfigLoader\build
build($config, array $options)
Guzzle\Common\Exception\InvalidArgumentException
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Exception/InvalidArgumentException.php:5
Guzzle\Service\AbstractConfigLoader\loadFile
loadFile($filename)
Definition: AbstractConfigLoader.php:99
Guzzle\Service\ConfigLoaderInterface
Definition: ConfigLoaderInterface.php:11
Guzzle\Service\AbstractConfigLoader\mergeIncludes
mergeIncludes(&$config, $basePath=null)
Definition: AbstractConfigLoader.php:154
Guzzle\Service
Definition: AbstractConfigLoader.php:3
Guzzle\Service\AbstractConfigLoader\load
load($config, array $options=array())
Definition: AbstractConfigLoader.php:35
Guzzle\Service\AbstractConfigLoader\addAlias
addAlias($filename, $alias)
Definition: AbstractConfigLoader.php:59
Guzzle\Common\Exception\RuntimeException
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Exception/RuntimeException.php:5
Guzzle\Service\AbstractConfigLoader\mergeData
mergeData(array $a, array $b)
Definition: AbstractConfigLoader.php:179