Overview

Namespaces

  • CzProject
    • Configuration
      • Adapters

Classes

  • CzProject\Configuration\Adapters\JsonAdapter
  • CzProject\Configuration\Adapters\NeonAdapter
  • CzProject\Configuration\Adapters\PhpAdapter
  • CzProject\Configuration\ConfigLoader
  • CzProject\Configuration\Configurator
  • CzProject\Configuration\Helpers

Interfaces

  • CzProject\Configuration\IAdapter
  • CzProject\Configuration\IAdapterLoader
  • CzProject\Configuration\IAdapterParser

Exceptions

  • CzProject\Configuration\ConfigLoaderException
  • CzProject\Configuration\ConfigurationException
  • CzProject\Configuration\ConfiguratorException
  • CzProject\Configuration\InvalidArgumentException
  • Overview
  • Namespace
  • Class
 1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 
<?php

    namespace CzProject\Configuration;


    class ConfigLoader
    {
        /** @var array  [extension => IConfigAdapter] */
        protected $adapters = array();


        /**
         * @param  string
         * @return self
         */
        public function addAdapter($extension, IAdapter $adapter)
        {
            $this->adapters[strtolower($extension)] = $adapter;
            return $this;
        }


        /**
         * @param  string
         * @return array
         * @throws ConfigLoaderException
         */
        public function loadConfig($file)
        {
            $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));

            if ($extension === '') {
                throw new ConfigLoaderException("Missing extension for file '$file'.");
            }

            if (!isset($this->adapters[$extension])) {
                throw new ConfigLoaderException("Missing adapter for extension '$extension', file '$file'.");
            }

            $adapter = $this->adapters[$extension];

            if ($adapter instanceof IAdapterLoader) {
                return (array) $adapter->loadConfig($file);
            }

            if ($adapter instanceof IAdapterParser) {
                $content = file_get_contents($file);

                if ($content === FALSE) {
                    throw new ConfigLoaderException("Reading of file '$file' failed.");
                }

                return (array) $adapter->parseConfig($content);
            }

            throw new ConfigLoaderException("Adapter must implement IAdapterLoader or IAdapterParser.");
        }
    }
czproject/configuration v1.0.0 API documentation API documentation generated by ApiGen