-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored out readers and ReaderFactory
- Loading branch information
Showing
7 changed files
with
237 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |