Skip to content

Commit a0f372d

Browse files
committed
added proper resolving of config dependencies with Resolver class
1 parent 8303cb7 commit a0f372d

File tree

4 files changed

+125
-4
lines changed

4 files changed

+125
-4
lines changed

src/Builder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ public function buildConfigs($files = null)
125125
if (is_null($files)) {
126126
$files = $this->files;
127127
}
128+
$resolver = new Resolver($files);
129+
$files = $resolver->get();
128130
foreach ($files as $name => $paths) {
129131
$olddefs = get_defined_constants();
130132
$configs = $this->loadConfigs($paths);

src/Plugin.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,15 +263,15 @@ protected function prepareAliases(PackageInterface $package, $psr, $dev = false)
263263
*/
264264
public function preparePath(PackageInterface $package, $path)
265265
{
266+
if (strncmp($path, '$', 1) === 0) {
267+
return $path;
268+
}
269+
266270
$skippable = strncmp($path, '?', 1) === 0 ? '?' : '';
267271
if ($skippable) {
268272
$path = substr($path, 1);
269273
}
270274

271-
if (strncmp($path, '$', 1) === 0) {
272-
$path = Builder::path(substr($path, 1));
273-
}
274-
275275
if (!$this->getFilesystem()->isAbsolutePath($path)) {
276276
$prefix = $package instanceof RootPackageInterface
277277
? $this->getBaseDir()

src/Resolver.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
/**
3+
* Composer plugin for config assembling
4+
*
5+
* @link https://github.com/hiqdev/composer-config-plugin
6+
* @package composer-config-plugin
7+
* @license BSD-3-Clause
8+
* @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
9+
*/
10+
11+
namespace hiqdev\composer\config;
12+
13+
use hiqdev\composer\config\exceptions\CircularDependencyException;
14+
15+
/**
16+
* Resolver class.
17+
* Reorders files according to their cross dependencies
18+
* and resolves `$name` pathes.
19+
* @author Andrii Vasyliev <sol@hiqdev.com>
20+
*/
21+
class Resolver
22+
{
23+
protected $order = [];
24+
25+
protected $deps = [];
26+
27+
protected $following = [];
28+
29+
public function __construct(array $files)
30+
{
31+
$this->files = $files;
32+
33+
$this->collectDeps();
34+
foreach (array_keys($this->files) as $name) {
35+
$this->followDeps($name);
36+
}
37+
38+
}
39+
40+
public function get()
41+
{
42+
$result = [];
43+
foreach ($this->order as $name) {
44+
$result[$name] = $this->resolveDeps($this->files[$name]);
45+
}
46+
47+
return $result;
48+
}
49+
50+
protected function resolveDeps(array $paths)
51+
{
52+
foreach ($paths as &$path) {
53+
$dep = $this->isDep($path);
54+
if ($dep) {
55+
$path = Builder::path($dep);
56+
}
57+
}
58+
59+
return $paths;
60+
}
61+
62+
protected function followDeps($name)
63+
{
64+
if (isset($this->order[$name])) {
65+
return;
66+
}
67+
if (isset($this->following[$name])) {
68+
throw new CircularDependencyException($name . ' ' . implode(',', $this->following));
69+
}
70+
$this->following[$name] = $name;
71+
if (isset($this->deps[$name])) {
72+
foreach ($this->deps[$name] as $dep) {
73+
$this->followDeps($dep);
74+
}
75+
}
76+
$this->order[$name] = $name;
77+
unset($this->following[$name]);
78+
}
79+
80+
protected function collectDeps()
81+
{
82+
foreach ($this->files as $name => $paths) {
83+
foreach ($paths as $path) {
84+
$dep = $this->isDep($path);
85+
if ($dep) {
86+
if (!isset($this->deps[$name])) {
87+
$this->deps[$name] = [];
88+
}
89+
$this->deps[$name][$dep] = $dep;
90+
}
91+
}
92+
}
93+
}
94+
95+
protected function isDep($path)
96+
{
97+
return strncmp($path, '$', 1) === 0 ? substr($path, 1) : false;
98+
}
99+
100+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* Composer plugin for config assembling
4+
*
5+
* @link https://github.com/hiqdev/composer-config-plugin
6+
* @package composer-config-plugin
7+
* @license BSD-3-Clause
8+
* @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
9+
*/
10+
11+
namespace hiqdev\composer\config\exceptions;
12+
13+
/**
14+
* Circular dependency exception.
15+
* @author Andrii Vasyliev <sol@hiqdev.com>
16+
*/
17+
class CircularDependencyException extends Exception
18+
{
19+
}

0 commit comments

Comments
 (0)