From ec65010c0fc17ee448a868688d2a47ca5b4102c4 Mon Sep 17 00:00:00 2001 From: Andrii Vasyliev Date: Mon, 26 Dec 2016 09:41:27 +0000 Subject: [PATCH] changed: `path()` and substituting pathes moved into Builder --- src/Builder.php | 82 ++++++++++++++++++++++++++++++++++++++++++++++--- src/Plugin.php | 42 +------------------------ 2 files changed, 79 insertions(+), 45 deletions(-) diff --git a/src/Builder.php b/src/Builder.php index a1e8082..d3b8ffa 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -46,12 +46,23 @@ class Builder */ protected $vars = []; + const OUTPUT_DIR = 'config'; const BASE_DIR_TAG = ''; - 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) @@ -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 @@ -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); } @@ -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, "readFile($this->getOutputPath($name)); + } + /** * Reads config file. * @param string $__path @@ -178,5 +253,4 @@ protected function writeError($text) echo $text . "\n"; } } - } diff --git a/src/Plugin.php b/src/Plugin.php index a9a69a8..16cf647 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -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'; @@ -113,7 +112,7 @@ public function onPostAutoloadDump(Event $event) $this->io->writeError('Assembling config files'); $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(); @@ -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) { @@ -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; } @@ -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 @@ -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