Skip to content

Commit

Permalink
basically finished splitting out Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqsol committed Dec 25, 2016
1 parent c53bb32 commit b85299b
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 130 deletions.
132 changes: 124 additions & 8 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace hiqdev\composer\config;

use Composer\IO\IOInterface;

/**
* Builder assembles config files.
*
Expand All @@ -19,32 +21,146 @@
class Builder
{
/**
* @var array list of configs to build: name => array of pathes
* @var string path to output assembled configs
*/
protected $outputDir;

/**
* @var array files to process: config name => list of files
*/
public $configs = [];
protected $files = [];

/**
* @var array collected variables
*/
protected $vars = [];

const BASE_DIR_SAMPLE = '<base-dir>';
const FILES_FILENAME = '__files';

protected function assembleFile($name, array $configs)
public function __construct($outputDir, array $files = [])
{
$this->data[$name] = call_user_func_array(['\\hiqdev\\composer\\config\\Helper', 'mergeConfig'], $configs);
$this->writeFile($name, (array) $this->data[$name]);
$this->files = $files;
$this->outputDir = $outputDir;
}

public function loadFiles()
{
$this->files = $this->readConfig(static::FILES_FILENAME);
}

public function saveFiles()
{
$this->writeConfig(static::FILES_FILENAME, $this->files);
}

public function buildConfigs($files = null)
{
if (is_null($files)) {
$files = $this->files;
}
foreach ($files as $name => $pathes) {
$configs = [];
foreach ($pathes as $path) {
$configs[] = $this->readFile($path);
}
$this->buildConfig($name, $configs);
}
}

/**
* Merges given configs and writes at given name.
* @param mixed $name
* @param array $configs
*/
public function buildConfig($name, array $configs)
{
if (!$this->isSpecialConfig($name)) {
array_push($configs, [
'params' => $this->vars['params'],
]);
}
$this->vars[$name] = call_user_func_array([Helper::className(), 'mergeConfig'], $configs);
$this->writeConfig($name, (array) $this->vars[$name]);
}

protected function isSpecialConfig($name)
{
return in_array($name, ['defines', 'params'], true);
}

/**
* Writes config file.
* Writes config file by name.
* @param string $name
* @param array $data
*/
public function writeConfig($name, array $data)
{
static::writeFile($this->getOutputPath($name), $data);
}

public function getOutputPath($name)
{
return $this->outputDir . DIRECTORY_SEPARATOR . $name . '.php';
}

/**
* Writes config file by full path.
* @param string $path
* @param array $data
*/
protected function writeFile($path, array $data)
public static function writeFile($path, array $data)
{
$path = $this->buildOutputPath($path);
if (!file_exists(dirname($path))) {
mkdir(dirname($path), 0777, true);
}
$array = str_replace("'" . self::BASE_DIR_SAMPLE, '$baseDir . \'', Helper::exportVar($data));
file_put_contents($path, "<?php\n\n\$baseDir = dirname(dirname(dirname(__DIR__)));\n\nreturn $array;\n");
}

/**
* Reads config file.
* @param string $__path
* @return array configuration read from file
*/
public function readFile($__path)
{
if (strncmp($__path, '?', 1) === 0) {
$__skippable = true;
$__path = substr($__path, 1);
}

if (file_exists($__path)) {
/// Expose variables to be used in configs
extract($this->vars);

return (array) require $__path;
}

if (empty($__skippable)) {
$this->writeError('<error>Non existent config file</error> ' . $__path);
}

return [];
}

/**
* @var IOInterface
*/
protected $io;

public function setIo(IOInterface $io)
{
$this->io = $io;
}

protected function writeError($text)
{
if (isset($this->io)) {
$this->io->writeError($text);
} else {
echo $text . "\n";
}
}

}
Loading

0 comments on commit b85299b

Please sign in to comment.