Skip to content

Commit

Permalink
changed: path() and substituting pathes moved into Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqsol committed Dec 26, 2016
1 parent 436f10d commit ec65010
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 45 deletions.
82 changes: 78 additions & 4 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,23 @@ class Builder
*/
protected $vars = [];

const OUTPUT_DIR = 'config';
const BASE_DIR_TAG = '<base-dir>';

public function __construct($outputDir, array $files = [])
public function __construct(array $files = [], $outputDir = null)
{
$this->setFiles($files);
$this->setOutputDir($outputDir);
}

public function setFiles(array $files)
{
$this->files = $files;
$this->outputDir = $outputDir;
}

public function setOutputDir($outputDir)
{
$this->outputDir = isset($outputDir) ? $outputDir : static::defaultOutputDir();
}

public function setAddition(array $addition)
Expand All @@ -71,6 +82,32 @@ public function saveFiles()
$this->writeConfig('__addition', $this->addition);
}

public static function rebuild($outputDir)
{
$builder = new Builder([], $outputDir);
$builder->loadFiles();
$builder->buildConfigs();
}

/**
* Returns default output dir.
* @return string
*/
public static function defaultOutputDir()
{
return dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . static::OUTPUT_DIR;
}

/**
* Returns full path to assembled config file.
* @param string $filename name of config
* @return string absolute path
*/
public static function path($filename)
{
return static::defaultOutputDir() . DIRECTORY_SEPARATOR . $filename . '.php';
}

/**
* Builds configs by given files list
* @param null|array $files files to process: config name => list of files
Expand Down Expand Up @@ -117,6 +154,7 @@ protected function isSpecialConfig($name)
*/
public function writeConfig($name, array $data)
{
$data = $this->substitutePathes($data, dirname(dirname(dirname($this->outputDir))), static::BASE_DIR_TAG);
static::writeFile($this->getOutputPath($name), $data);
}

Expand All @@ -135,10 +173,47 @@ public static function writeFile($path, array $data)
if (!file_exists(dirname($path))) {
mkdir(dirname($path), 0777, true);
}
$array = str_replace("'" . self::BASE_DIR_TAG, '$baseDir . \'', Helper::exportVar($data));
$array = str_replace("'" . static::BASE_DIR_TAG, '$baseDir . \'', Helper::exportVar($data));
file_put_contents($path, "<?php\n\n\$baseDir = dirname(dirname(dirname(__DIR__)));\n\nreturn $array;\n");
}

/**
* Substitute all pathes in given array recursively with alias if applicable.
* @param array $data
* @param string $dir
* @param string $alias
* @return string
*/
public static function substitutePathes($data, $dir, $alias)
{
foreach ($data as &$value) {
if (is_string($value)) {
$value = static::substitutePath($value, $dir, $alias);
} elseif (is_array($value)) {
$value = static::substitutePathes($value, $dir, $alias);
}
}

return $data;
}

/**
* Substitute path with alias if applicable.
* @param string $path
* @param string $dir
* @param string $alias
* @return string
*/
protected static function substitutePath($path, $dir, $alias)
{
return (substr($path, 0, strlen($dir) + 1) === $dir . '/') ? $alias . substr($path, strlen($dir)) : $path;
}

public function readConfig($name)
{
return $this->readFile($this->getOutputPath($name));
}

/**
* Reads config file.
* @param string $__path
Expand Down Expand Up @@ -178,5 +253,4 @@ protected function writeError($text)
echo $text . "\n";
}
}

}
42 changes: 1 addition & 41 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
*/
class Plugin implements PluginInterface, EventSubscriberInterface
{
const OUTPUT_DIR = 'config';
const YII2_PACKAGE_TYPE = 'yii2-extension';
const EXTRA_OPTION_NAME = 'config-plugin';

Expand Down Expand Up @@ -113,7 +112,7 @@ public function onPostAutoloadDump(Event $event)
$this->io->writeError('<info>Assembling config files</info>');
$this->scanPackages();

$builder = new Builder(static::getOutputDir(), $this->files);
$builder = new Builder($this->files);
$builder->setAddition(['aliases' => $this->aliases]);
$builder->setIo($this->io);
$builder->saveFiles();
Expand All @@ -122,13 +121,6 @@ public function onPostAutoloadDump(Event $event)
$builder->buildConfigs();
}

public static function rebuild()
{
$builder = new Builder(static::getOutputDir());
$builder->loadFiles();
$builder->buildConfigs();
}

protected function scanPackages()
{
foreach ($this->getPackages() as $package) {
Expand Down Expand Up @@ -203,7 +195,6 @@ protected function prepareAliases(PackageInterface $package, $psr)
}
$name = str_replace('\\', '/', trim($name, '\\'));
$path = $this->preparePath($package, $path);
$path = $this->substitutePath($path, $this->getBaseDir(), Builder::BASE_DIR_TAG);
if ('psr-0' === $psr) {
$path .= '/' . $name;
}
Expand All @@ -213,18 +204,6 @@ protected function prepareAliases(PackageInterface $package, $psr)
return $aliases;
}

/**
* Substitute path with alias if applicable.
* @param string $path
* @param string $dir
* @param string $alias
* @return string
*/
protected function substitutePath($path, $dir, $alias)
{
return (substr($path, 0, strlen($dir) + 1) === $dir . '/') ? $alias . substr($path, strlen($dir)) : $path;
}

/**
* Builds path inside of a package.
* @param PackageInterface $package
Expand All @@ -249,25 +228,6 @@ public function preparePath(PackageInterface $package, $path)
return ($skippable ? '?' : '') . $this->getFilesystem()->normalizePath($path);
}

/**
* Get output dir.
* @return string
*/
public static function getOutputDir()
{
return dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . static::OUTPUT_DIR;
}

/**
* Returns full path to assembled config file.
* @param string $filename name of config
* @return string absolute path
*/
public static function path($filename)
{
return static::getOutputDir() . DIRECTORY_SEPARATOR . $filename . '.php';
}

/**
* Sets [[packages]].
* @param PackageInterface[] $packages
Expand Down

0 comments on commit ec65010

Please sign in to comment.