Skip to content

Commit

Permalink
extract yaml loader
Browse files Browse the repository at this point in the history
  • Loading branch information
marcogiovinazzi committed Mar 22, 2018
1 parent 5da3207 commit 906ac13
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 27 deletions.
48 changes: 48 additions & 0 deletions src/Comodojo/Foundation/Base/AbstractYamlLoader.php
@@ -0,0 +1,48 @@
<?php namespace Comodojo\Foundation\Base;

use \Symfony\Component\Yaml\Yaml;
use \Symfony\Component\Yaml\Exception\ParseException;
use \Comodojo\Exception\ConfigurationException;
use \Exception;

/**
* @package Comodojo Foundation
* @author Marco Giovinazzi <marco.giovinazzi@comodojo.org>
* @license MIT
*
* LICENSE:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

abstract class AbstractYamlLoader {

abstract public static function load($file, array $attributes = []);

protected static function importData($file) {

if ( file_exists($file) && is_readable($file) ) {

$data = @file_get_contents($file);

if ( $data !== false ) {

return Yaml::parse($data);

} else {
throw new ConfigurationException("Configuration file $file not readable");
}

} else {
throw new ConfigurationException("Configuration file $file not found");
}

}

}
31 changes: 4 additions & 27 deletions src/Comodojo/Foundation/Base/ConfigurationLoader.php
@@ -1,8 +1,5 @@
<?php namespace Comodojo\Foundation\Base;

use \Symfony\Component\Yaml\Yaml;
use \Symfony\Component\Yaml\Exception\ParseException;
use \Comodojo\Exception\ConfigurationException;
use \Exception;

/**
Expand All @@ -21,15 +18,15 @@
* THE SOFTWARE.
*/

class ConfigurationLoader {
class ConfigurationLoader extends AbstractYamlLoader {

public static function load($configuration, array $attributes = []) {
public static function load($file, array $attributes = []) {

$conf = new Configuration($attributes);

try {

$conf->merge(self::importData($configuration));
$conf->merge(static::importData($file));

$base = $conf->get('base-path');
$static = $conf->get('static-config');
Expand All @@ -39,7 +36,7 @@ public static function load($configuration, array $attributes = []) {

$env_path = substr($env, 0, 1) === '/' ? $env : "$base/$static/$env";

$conf->merge(self::importData($env_path));
$conf->merge(static::importData($env_path));

}

Expand All @@ -53,24 +50,4 @@ public static function load($configuration, array $attributes = []) {

}

protected static function importData($file) {

if ( file_exists($file) && is_readable($file) ) {

$data = @file_get_contents($file);

if ( $data !== false ) {

return Yaml::parse($data);

} else {
throw new ConfigurationException("Configuration file $file not readable");
}

} else {
throw new ConfigurationException("Configuration file $file not found");
}

}

}

0 comments on commit 906ac13

Please sign in to comment.