theorchard / monolog-cascade

Configure multiple loggers and handlers in the blink of an eye
MIT License
145 stars 62 forks source link

Loaders: load JSON and Yaml files #8

Closed rantonmattei closed 9 years ago

rantonmattei commented 9 years ago

Load JSON, Yaml or PhpArray For files it is using Symfony's FIleLoader. The FileLoaderAbstract extends that class.

Note: FileLoaderAbstract uses late static binding to get the extensions from the child class.

This is used to load the logger config from various resources.

Will add .ini support in the future

@mortaliorchard @OrCharles

rantonmattei commented 9 years ago

Anything else, guys?

OrCharles commented 9 years ago

looks like a nice clean base: are you intending on doing some (not in a bad way) magic to auto-determine(cascade) what type of config/file you'll be loading?

OrCharles commented 9 years ago

just a couple of comment typos and a few questions.. R~

rantonmattei commented 9 years ago

Regarding your previous comment, those loaders are eventually be used by what Symfony calls a Resolver. It basicallyu figure out what loader to use to load a passed in resource.

See in what's coming next:

namespace Cascade\Config;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\DelegatingLoader;
use Symfony\Component\Config\Loader\LoaderResolver;

use Cascade\Config\Loader\PhpArray as ArrayLoader;
use Cascade\Config\Loader\FileLoader\Json as JsonLoader;
use Cascade\Config\Loader\FileLoader\Yaml as YamlLoader;

/**
 * Loader class that loads Yaml, JSON and array from various resources (file, php array, string)
 * @see DelegatingLoader
 *
 * @author Raphael Antonmattei <rantonmattei@theorchard.com>
 */
class ConfigLoader extends DelegatingLoader
{
    /**
     * Locator
     * @var Symfony\Component\Config\FileLocator
     */
    protected $locator = null;

    /**
     * Instantiate a Loader object
     * @todo: have the locator passed to the constructor so we can load more than one file
     */
    public function __construct()
    {
        $this->locator = new FileLocator();

        $loaderResolver = new LoaderResolver(array(
            // Do not change that order, it does matter as the resolver returns the first loader
            // that meets the requirements of the "supports" method for each of those loaders
            new ArrayLoader(),
            new JsonLoader($this->locator),
            new YamlLoader($this->locator)
        ));

        parent::__construct($loaderResolver);
    }

    /**
     * Loads a configuration resource: file, array, string
     *
     * @param  mixed $resource resource to load
     * @param  mixed $type not used
     * @return array array of config options
     */
    public function load($resource, $type = null)
    {
        return parent::load($resource);
    }
}
rantonmattei commented 9 years ago

It looked to me over-engineered at first but it is now really easy to add any file format and we can add more features in the future. And it was fun to learn about those Symfony components

rantonmattei commented 9 years ago

Fixed the typos

OrCharles commented 9 years ago

R+