Skip to content

Commit

Permalink
added work with addition
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqsol committed Dec 25, 2016
1 parent b85299b commit 436f10d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 23 deletions.
40 changes: 28 additions & 12 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,55 @@ class Builder
protected $outputDir;

/**
* @var array files to process: config name => list of files
* @var array files to build configs
* @see buildConfigs()
*/
protected $files = [];

/**
* @var array additional data to be merged into every config (e.g. aliases)
*/
protected $addition = [];

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

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

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

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

public function setAddition(array $addition)
{
$this->addition = $addition;
}

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

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

/**
* Builds configs by given files list
* @param null|array $files files to process: config name => list of files
*/
public function buildConfigs($files = null)
{
if (is_null($files)) {
Expand All @@ -76,7 +97,7 @@ public function buildConfigs($files = null)
public function buildConfig($name, array $configs)
{
if (!$this->isSpecialConfig($name)) {
array_push($configs, [
array_push($configs, $this->addition, [
'params' => $this->vars['params'],
]);
}
Expand Down Expand Up @@ -114,7 +135,7 @@ public static function writeFile($path, array $data)
if (!file_exists(dirname($path))) {
mkdir(dirname($path), 0777, true);
}
$array = str_replace("'" . self::BASE_DIR_SAMPLE, '$baseDir . \'', Helper::exportVar($data));
$array = str_replace("'" . self::BASE_DIR_TAG, '$baseDir . \'', Helper::exportVar($data));
file_put_contents($path, "<?php\n\n\$baseDir = dirname(dirname(dirname(__DIR__)));\n\nreturn $array;\n");
}

Expand Down Expand Up @@ -144,11 +165,6 @@ public function readFile($__path)
return [];
}

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

public function setIo(IOInterface $io)
{
$this->io = $io;
Expand Down
31 changes: 20 additions & 11 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public function onPostAutoloadDump(Event $event)
$this->scanPackages();

$builder = new Builder(static::getOutputDir(), $this->files);
$builder->setAddition(['aliases' => $this->aliases]);
$builder->setIo($this->io);
$builder->saveFiles();
$builder->writeConfig('aliases', $this->aliases);
Expand Down Expand Up @@ -141,7 +142,7 @@ protected function scanPackages()
* Scans the given package and collects extensions data.
* @param PackageInterface $package
*/
protected function processPackage(PackageInterface $package)
protected function processPackage(CompletePackageInterface $package)
{
$extra = $package->getExtra();
$files = isset($extra[self::EXTRA_OPTION_NAME]) ? $extra[self::EXTRA_OPTION_NAME] : null;
Expand All @@ -150,28 +151,36 @@ protected function processPackage(PackageInterface $package)
return;
}

foreach ($files as $name => $pathes) {
foreach ((array) $pathes as $path) {
if (!isset($this->files[$name])) {
$this->files[$name] = [];
}
array_push($this->files[$name], $this->preparePath($package, $path));
}
if (is_array($files)) {
$this->processFiles($package, $files);
}

$this->aliases = array_merge(
$this->aliases,
$aliases = array_merge(
$this->prepareAliases($package, 'psr-0'),
$this->prepareAliases($package, 'psr-4')
);
$this->aliases = array_merge($this->aliases, $aliases);

$this->extensions[$package->getPrettyName()] = array_filter([
'name' => $package->getPrettyName(),
'version' => $package->getVersion(),
'reference' => $package->getSourceReference() ?: $package->getDistReference(),
'aliases' => $aliases,
]);
}

protected function processFiles(CompletePackageInterface $package, array $files)
{
foreach ($files as $name => $pathes) {
foreach ((array) $pathes as $path) {
if (!isset($this->files[$name])) {
$this->files[$name] = [];
}
array_push($this->files[$name], $this->preparePath($package, $path));
}
}
}

/**
* Prepare aliases.
* @param PackageInterface $package
Expand All @@ -194,7 +203,7 @@ 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_SAMPLE);
$path = $this->substitutePath($path, $this->getBaseDir(), Builder::BASE_DIR_TAG);
if ('psr-0' === $psr) {
$path .= '/' . $name;
}
Expand Down

0 comments on commit 436f10d

Please sign in to comment.