Skip to content

Commit

Permalink
refactored out readers and ReaderFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqsol committed Apr 12, 2017
1 parent 8ec1461 commit 43c0d3c
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 80 deletions.
87 changes: 7 additions & 80 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,87 +271,9 @@ public function loadConfig($name)
*/
public function loadFile($path)
{
$skippable = strncmp($path, '?', 1) === 0 ? '?' : '';
if ($skippable) {
$path = substr($path, 1);
}

if (file_exists($path)) {
$res = $this->readFile($path);

return is_array($res) ? $res : [];
}

if (empty($skippable)) {
throw new FailedReadException("failed read file: $path");
}

return [];
}

public function readFile($path)
{
$ext = pathinfo($path)['extension'];
if ($ext === 'env') {
return $this->readEnvFile($path);
} elseif ($ext === 'php') {
return $this->readPhpFile($path);
} elseif ($ext === 'json') {
return $this->readJsonFile($path);
} elseif ($ext === 'yml' || $ext === 'yaml') {
return $this->readYamlFile($path);
}

throw new UnsupportedFileTypeException("unsupported extension: $ext");
}

public function readEnvFile($path)
{
if (!class_exists('Dotenv\Dotenv')) {
throw new UnsupportedFileTypeException("for .env support require `vlucas/phpdotenv` in your composer.json");
}
$info = pathinfo($path);
$dotenv = new \Dotenv\Dotenv($info['dirname'], $info['basename']);
$oldenvs = $_ENV;
$dotenv->load();
$newenvs = $_ENV;

return array_diff_assoc($newenvs, $oldenvs);
}

public function readPhpFile($__path)
{
if (!is_readable($__path)) {
throw new FailedReadException("failed read file: $__path");
}
/// Expose variables to be used in configs
extract($this->vars);

return require $__path;
}

public function readJsonFile($path)
{
return json_decode($this->getFileContents($path), true);
}

public function readYamlFile($path)
{
if (!class_exists('Symfony\Component\Yaml\Yaml')) {
throw new UnsupportedFileTypeException("for YAML support require `symfony/yaml` in your composer.json");
}

return \Symfony\Component\Yaml\Yaml::parse($this->getFileContents($path));
}

public function getFileContents($path)
{
$res = file_get_contents($path);
if ($res === FALSE) {
throw new FailedReadException("failed read file: $path");
}
$reader = ReaderFactory::get($path);

return $res;
return $reader->read($path, $this);
}

public function setIo(IOInterface $io)
Expand All @@ -367,4 +289,9 @@ protected function writeError($text)
echo $text . "\n";
}
}

public function getVars()
{
return $this->vars;
}
}
55 changes: 55 additions & 0 deletions src/ReaderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Composer plugin for config assembling.
*
* @link https://github.com/hiqdev/composer-config-plugin
* @package composer-config-plugin
* @license BSD-3-Clause
* @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
*/

namespace hiqdev\composer\config;

use hiqdev\composer\config\exceptions\UnsupportedFileTypeException;
use hiqdev\composer\config\readers\EnvReader;
use hiqdev\composer\config\readers\PhpReader;
use hiqdev\composer\config\readers\JsonReader;
use hiqdev\composer\config\readers\YamlReader;

/**
* Reader - helper to load data from files of different types.
*
* @author Andrii Vasyliev <sol@hiqdev.com>
*/
class ReaderFactory
{
private static $loaders;

private static $knownReaders = [
'env' => EnvReader::class,
'php' => PhpReader::class,
'json' => JsonReader::class,
'yaml' => YamlReader::class,
'yml' => YamlReader::class,
];

public static function get($path)
{
$ext = pathinfo($path, PATHINFO_EXTENSION);
if (empty(static::$loaders[$ext])) {
static::$loaders[$ext] = static::create($ext);
}

return static::$loaders[$ext];
}

public static function create($ext)
{
if (empty(static::$knownReaders[$ext])) {
throw new UnsupportedFileTypeException("unsupported extension: $ext");
}
$class = static::$knownReaders[$ext];

return new $class();
}
}
54 changes: 54 additions & 0 deletions src/readers/AbstractReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Composer plugin for config assembling.
*
* @link https://github.com/hiqdev/composer-config-plugin
* @package composer-config-plugin
* @license BSD-3-Clause
* @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
*/

namespace hiqdev\composer\config\readers;

use hiqdev\composer\config\Builder;
use hiqdev\composer\config\exceptions\FailedReadException;

/**
* Reader - helper to read data from files of different types.
*
* @author Andrii Vasyliev <sol@hiqdev.com>
*/
abstract class AbstractReader
{
public function read($path, Builder $builder)
{
$skippable = strncmp($path, '?', 1) === 0 ? '?' : '';
if ($skippable) {
$path = substr($path, 1);
}

if (is_readable($path)) {
$res = $this->readRaw($path, $builder);

return is_array($res) ? $res : [];
}

if (empty($skippable)) {
throw new FailedReadException("failed read file: $path");
}

return [];
}

public function getFileContents($path)
{
$res = file_get_contents($path);
if ($res === FALSE) {
throw new FailedReadException("failed read file: $path");
}

return $res;
}

abstract public function readRaw($path, Builder $builder);
}
35 changes: 35 additions & 0 deletions src/readers/EnvReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Composer plugin for config assembling.
*
* @link https://github.com/hiqdev/composer-config-plugin
* @package composer-config-plugin
* @license BSD-3-Clause
* @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
*/

namespace hiqdev\composer\config\readers;

use hiqdev\composer\config\Builder;

/**
* EnvReader - reads `.env` files.
*
* @author Andrii Vasyliev <sol@hiqdev.com>
*/
class EnvReader extends AbstractReader
{
public function readRaw($path, Builder $builder)
{
if (!class_exists('Dotenv\Dotenv')) {
throw new UnsupportedFileTypeException("for .env support require `vlucas/phpdotenv` in your composer.json");
}
$info = pathinfo($path);
$dotenv = new \Dotenv\Dotenv($info['dirname'], $info['basename']);
$oldenvs = $_ENV;
$dotenv->load();
$newenvs = $_ENV;

return array_diff_assoc($newenvs, $oldenvs);
}
}
27 changes: 27 additions & 0 deletions src/readers/JsonReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Composer plugin for config assembling.
*
* @link https://github.com/hiqdev/composer-config-plugin
* @package composer-config-plugin
* @license BSD-3-Clause
* @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
*/

namespace hiqdev\composer\config\readers;

use hiqdev\composer\config\Builder;
use hiqdev\composer\config\exceptions\FailedReadException;

/**
* JsonReader - reads PHP files.
*
* @author Andrii Vasyliev <sol@hiqdev.com>
*/
class JsonReader extends AbstractReader
{
public function readRaw($path, $builder)
{
return json_decode($this->getFileContents($path), true);
}
}
29 changes: 29 additions & 0 deletions src/readers/PhpReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Composer plugin for config assembling.
*
* @link https://github.com/hiqdev/composer-config-plugin
* @package composer-config-plugin
* @license BSD-3-Clause
* @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
*/

namespace hiqdev\composer\config\readers;

use hiqdev\composer\config\Builder;

/**
* PhpReader - reads PHP files.
*
* @author Andrii Vasyliev <sol@hiqdev.com>
*/
class PhpReader extends AbstractReader
{
public function readRaw($__path, Builder $builder)
{
/// Expose variables to be used in configs
extract($builder->getVars());

return require $__path;
}
}
30 changes: 30 additions & 0 deletions src/readers/YamlReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Composer plugin for config assembling.
*
* @link https://github.com/hiqdev/composer-config-plugin
* @package composer-config-plugin
* @license BSD-3-Clause
* @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
*/

namespace hiqdev\composer\config\readers;

use hiqdev\composer\config\Builder;

/**
* YamlReader - reads YAML files.
*
* @author Andrii Vasyliev <sol@hiqdev.com>
*/
class YamlReader extends AbstractReader
{
public function readRaw($path, Builder $builder)
{
if (!class_exists('Symfony\Component\Yaml\Yaml')) {
throw new UnsupportedFileTypeException("for YAML support require `symfony/yaml` in your composer.json");
}

return \Symfony\Component\Yaml\Yaml::parse($this->getFileContents($path));
}
}

0 comments on commit 43c0d3c

Please sign in to comment.