Overview

Namespaces

  • Contrib
    • Bundle
      • CoverallsBundle
        • Console
        • Entity
      • CoverallsV1Bundle
        • Api
        • Collector
        • Command
        • Config
        • Entity
          • Git
    • Component
      • File
      • Log
      • System
        • Git
  • Guzzle
    • Batch
      • Exception
    • Cache
    • Common
      • Exception
    • Http
      • Curl
      • Exception
      • Message
      • QueryAggregator
    • Inflection
    • Iterator
    • Log
    • Parser
      • Cookie
      • Message
      • UriTemplate
      • Url
    • Plugin
      • Async
      • Backoff
      • Cache
      • Cookie
        • CookieJar
        • Exception
      • CurlAuth
      • ErrorResponse
        • Exception
      • History
      • Log
      • Md5
      • Mock
      • Oauth
    • Service
      • Builder
      • Command
        • Factory
        • LocationVisitor
          • Request
          • Response
      • Description
      • Exception
      • Resource
    • Stream
  • PHP
  • Psr
    • Log
  • Symfony
    • Component
      • Config
        • Definition
          • Builder
          • Exception
        • Exception
        • Loader
        • Resource
        • Util
      • Console
        • Command
        • Formatter
        • Helper
        • Input
        • Output
        • Tester
      • EventDispatcher
        • Debug
      • Finder
        • Adapter
        • Comparator
        • Exception
        • Expression
        • Iterator
        • Shell
      • Stopwatch
      • Yaml
        • Exception

Classes

  • AbstractConfigLoader
  • CachingConfigLoader
  • Client

Interfaces

  • ClientInterface
  • ConfigLoaderInterface
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
  1: <?php
  2: 
  3: namespace Guzzle\Service;
  4: 
  5: use Guzzle\Common\Exception\InvalidArgumentException;
  6: use Guzzle\Common\Exception\RuntimeException;
  7: 
  8: /**
  9:  * Abstract config loader
 10:  */
 11: abstract class AbstractConfigLoader implements ConfigLoaderInterface
 12: {
 13:     /**
 14:      * @var array Array of aliases for actual filenames
 15:      */
 16:     protected $aliases = array();
 17: 
 18:     /**
 19:      * @var array Hash of previously loaded filenames
 20:      */
 21:     protected $loadedFiles = array();
 22: 
 23:     /**
 24:      * {@inheritdoc}
 25:      */
 26:     public function load($config, array $options = array())
 27:     {
 28:         // Reset the array of loaded files because this is a new config
 29:         $this->loadedFiles = array();
 30: 
 31:         if (is_string($config)) {
 32:             $config = $this->loadFile($config);
 33:         } elseif (!is_array($config)) {
 34:             throw new InvalidArgumentException('Unknown type passed to configuration loader: ' . gettype($config));
 35:         } else {
 36:             $this->mergeIncludes($config);
 37:         }
 38: 
 39:         return $this->build($config, $options);
 40:     }
 41: 
 42:     /**
 43:      * Add an include alias to the loader
 44:      *
 45:      * @param string $filename Filename to alias (e.g. _foo)
 46:      * @param string $alias    Actual file to use (e.g. /path/to/foo.json)
 47:      *
 48:      * @return self
 49:      */
 50:     public function addAlias($filename, $alias)
 51:     {
 52:         $this->aliases[$filename] = $alias;
 53: 
 54:         return $this;
 55:     }
 56: 
 57:     /**
 58:      * Remove an alias from the loader
 59:      *
 60:      * @param string $alias Alias to remove
 61:      *
 62:      * @return self
 63:      */
 64:     public function removeAlias($alias)
 65:     {
 66:         unset($this->aliases[$alias]);
 67: 
 68:         return $this;
 69:     }
 70: 
 71:     /**
 72:      * Perform the parsing of a config file and create the end result
 73:      *
 74:      * @param array $config  Configuration data
 75:      * @param array $options Options to use when building
 76:      *
 77:      * @return mixed
 78:      */
 79:     protected abstract function build($config, array $options);
 80: 
 81:     /**
 82:      * Load a configuration file (can load JSON or PHP files that return an array when included)
 83:      *
 84:      * @param string $filename File to load
 85:      *
 86:      * @return array
 87:      * @throws InvalidArgumentException
 88:      * @throws RuntimeException when the JSON cannot be parsed
 89:      */
 90:     protected function loadFile($filename)
 91:     {
 92:         if (isset($this->aliases[$filename])) {
 93:             $filename = $this->aliases[$filename];
 94:         }
 95: 
 96:         switch (pathinfo($filename, PATHINFO_EXTENSION)) {
 97:             case 'js':
 98:             case 'json':
 99:                 $level = error_reporting(0);
100:                 $json = file_get_contents($filename);
101:                 error_reporting($level);
102: 
103:                 if ($json === false) {
104:                     $err = error_get_last();
105:                     throw new InvalidArgumentException("Unable to open {$filename}: " . $err['message']);
106:                 }
107: 
108:                 $config = json_decode($json, true);
109:                 // Throw an exception if there was an error loading the file
110:                 if ($error = json_last_error()) {
111:                     throw new RuntimeException("Error loading JSON data from {$filename}: {$error}");
112:                 }
113:                 break;
114:             case 'php':
115:                 if (!is_readable($filename)) {
116:                     throw new InvalidArgumentException("Unable to open {$filename} for reading");
117:                 }
118:                 $config = require $filename;
119:                 if (!is_array($config)) {
120:                     throw new InvalidArgumentException('PHP files must return an array of configuration data');
121:                 }
122:                 break;
123:             default:
124:                 throw new InvalidArgumentException('Unknown file extension: ' . $filename);
125:         }
126: 
127:         // Keep track of this file being loaded to prevent infinite recursion
128:         $this->loadedFiles[$filename] = true;
129: 
130:         // Merge include files into the configuration array
131:         $this->mergeIncludes($config, dirname($filename));
132: 
133:         return $config;
134:     }
135: 
136:     /**
137:      * Merges in all include files
138:      *
139:      * @param array  $config   Config data that contains includes
140:      * @param string $basePath Base path to use when a relative path is encountered
141:      *
142:      * @return array Returns the merged and included data
143:      */
144:     protected function mergeIncludes(&$config, $basePath = null)
145:     {
146:         if (!empty($config['includes'])) {
147:             foreach ($config['includes'] as &$path) {
148:                 // Account for relative paths
149:                 if ($path[0] != DIRECTORY_SEPARATOR && !isset($this->aliases[$path]) && $basePath) {
150:                     $path = "{$basePath}/{$path}";
151:                 }
152:                 // Don't load the same files more than once
153:                 if (!isset($this->loadedFiles[$path])) {
154:                     $this->loadedFiles[$path] = true;
155:                     $config = $this->mergeData($this->loadFile($path), $config);
156:                 }
157:             }
158:         }
159:     }
160: 
161:     /**
162:      * Default implementation for merging two arrays of data (uses array_merge_recursive)
163:      *
164:      * @param array $a Original data
165:      * @param array $b Data to merge into the original and overwrite existing values
166:      *
167:      * @return array
168:      */
169:     protected function mergeData(array $a, array $b)
170:     {
171:         return array_merge_recursive($a, $b);
172:     }
173: }
174: 
php-coveralls API documentation generated by ApiGen 2.8.0