Skip to content

Instantly share code, notes, and snippets.

@pemedina
Created November 10, 2017 14:18
Show Gist options
  • Save pemedina/a2d620441586f12f639cb54d761e8f93 to your computer and use it in GitHub Desktop.
Save pemedina/a2d620441586f12f639cb54d761e8f93 to your computer and use it in GitHub Desktop.
Cilex 2.* Config Service Provider
<?php
/*
* This file is part of the Cilex framework.
*
* (c) Mike van Riel <mike.vanriel@naenius.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Providers;
use Pimple\Container;
use Symfony\Component\Yaml;
use Pimple\ServiceProviderInterface;
/**
* Cilex Console Service Provider
*/
class ConfigServiceProvider implements ServiceProviderInterface {
/**
* {@inheritdoc}
*/
public function register( Container $app ) {
$app['config'] = function ( $app ) {
if ( ! file_exists( $app['config.path'] ) ) {
throw new \InvalidArgumentException(
$app['config.path'] . ' is not a valid path to the configuration'
);
}
$fullpath = explode( '.', $app['config.path'] );
switch ( strtolower( end( $fullpath ) ) ) {
case 'php':
$result = include( $app['config.path'] );
break;
case 'yml':
$parser = new Yaml\Parser();
$result = new \ArrayObject(
$parser->parse( file_get_contents( $app['config.path'] ) )
);
break;
case 'xml':
$result = simplexml_load_file( $app['config.path'] );
break;
case 'json':
$result = json_decode( file_get_contents( $app['config.path'] ) );
if ( null == $result ) {
throw new \InvalidArgumentException(
'Unable to decode the configuration file: ' . $app['config.path']
);
}
break;
default:
throw new \InvalidArgumentException(
'Unable to load configuration; the provided file extension was not recognized. '
. 'Only yml, xml or json allowed'
);
break;
}
return $result;
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment