Skip to content

Commit e1ceb9a

Browse files
authored
Merge pull request #68 from kynx/mezzio-development-config
Added Mezzio development config discovery and injection
2 parents ceb4277 + a8996c4 commit e1ceb9a

17 files changed

+506
-234
lines changed

src/ConfigDiscovery.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ final class ConfigDiscovery
2121
'config/application.config.php' => ConfigDiscovery\ApplicationConfig::class,
2222
'config/modules.config.php' => ConfigDiscovery\ModulesConfig::class,
2323
'config/development.config.php.dist' => [
24-
'dist' => ConfigDiscovery\DevelopmentConfig::class,
25-
'work' => ConfigDiscovery\DevelopmentWorkConfig::class,
24+
'dist' => ConfigDiscovery\DevelopmentConfig::class,
25+
'work' => ConfigDiscovery\DevelopmentWorkConfig::class,
26+
'mezzio-dist' => ConfigDiscovery\MezzioDevelopmentConfig::class,
27+
'mezzio-work' => ConfigDiscovery\MezzioDevelopmentWorkConfig::class,
2628
],
2729
'config/config.php' => [
2830
'aggregator' => ConfigDiscovery\ConfigAggregator::class,
@@ -37,8 +39,10 @@ final class ConfigDiscovery
3739
'config/application.config.php' => Injector\ApplicationConfigInjector::class,
3840
'config/modules.config.php' => Injector\ModulesConfigInjector::class,
3941
'config/development.config.php.dist' => [
40-
'dist' => Injector\DevelopmentConfigInjector::class,
41-
'work' => Injector\DevelopmentWorkConfigInjector::class,
42+
'dist' => Injector\DevelopmentConfigInjector::class,
43+
'work' => Injector\DevelopmentWorkConfigInjector::class,
44+
'mezzio-dist' => Injector\MezzioDevelopmentConfigInjector::class,
45+
'mezzio-work' => Injector\MezzioDevelopmentWorkConfigInjector::class,
4246
],
4347
'config/config.php' => [
4448
'aggregator' => Injector\ConfigAggregatorInjector::class,

src/ConfigDiscovery/ConfigAggregator.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final class ConfigAggregator extends AbstractDiscovery
1515
/**
1616
* Configuration file to look for.
1717
*/
18-
protected string $configFile = 'config/config.php';
18+
protected string $configFile;
1919

2020
/**
2121
* Expected pattern to match if the configuration file exists.
@@ -24,9 +24,10 @@ final class ConfigAggregator extends AbstractDiscovery
2424
*/
2525
protected string $expected = '';
2626

27-
public function __construct(string $projectDirectory = '')
27+
public function __construct(string $projectDirectory = '', string $configFile = 'config/config.php')
2828
{
29-
$this->expected = sprintf(
29+
$this->configFile = $configFile;
30+
$this->expected = sprintf(
3031
'/new (?:%s?%s)?ConfigAggregator\(\s*(?:array\(|\[)/s',
3132
preg_quote('\\'),
3233
preg_quote('Laminas\ConfigAggregator\\')
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laminas\ComponentInstaller\ConfigDiscovery;
6+
7+
/**
8+
* @internal
9+
*/
10+
final class MezzioDevelopmentConfig implements DiscoveryInterface
11+
{
12+
private const CONFIG_FILE = 'config/development.config.php.dist';
13+
14+
private ConfigAggregator $discovery;
15+
16+
public function __construct(string $projectDirectory = '')
17+
{
18+
$this->discovery = new ConfigAggregator($projectDirectory, self::CONFIG_FILE);
19+
}
20+
21+
public function locate(): bool
22+
{
23+
return $this->discovery->locate();
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laminas\ComponentInstaller\ConfigDiscovery;
6+
7+
/**
8+
* @internal
9+
*/
10+
final class MezzioDevelopmentWorkConfig implements DiscoveryInterface
11+
{
12+
private const CONFIG_FILE = 'config/development.config.php';
13+
14+
private ConfigAggregator $discovery;
15+
16+
public function __construct(string $projectDirectory = '')
17+
{
18+
$this->discovery = new ConfigAggregator($projectDirectory, self::CONFIG_FILE);
19+
}
20+
21+
public function locate(): bool
22+
{
23+
return $this->discovery->locate();
24+
}
25+
}

src/Injector/ConditionalDiscoveryTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function remove(string $package): bool
4040
private function validConfigAggregatorConfig(): bool
4141
{
4242
$discoveryClass = $this->getDiscoveryClass();
43-
$discovery = new $discoveryClass($this->getProjectRoot());
43+
$discovery = new $discoveryClass($this->getProjectRoot(), $this->configFile);
4444
return $discovery->locate();
4545
}
4646

src/Injector/ConfigAggregatorInjector.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@ final class ConfigAggregatorInjector extends AbstractInjector
1818
{
1919
use ConditionalDiscoveryTrait;
2020

21+
/** @var non-empty-string */
2122
public const DEFAULT_CONFIG_FILE = 'config/config.php';
2223

2324
/** @var list<InjectorInterface::TYPE_*> */
2425
protected array $allowedTypes = [
2526
self::TYPE_CONFIG_PROVIDER,
2627
];
2728

28-
/** @var non-empty-string */
29-
protected string $configFile = self::DEFAULT_CONFIG_FILE;
30-
3129
/**
3230
* Discovery class, for testing if this injector is valid for the given
3331
* configuration.
@@ -68,9 +66,13 @@ final class ConfigAggregatorInjector extends AbstractInjector
6866
*
6967
* Sets $isRegisteredPattern and pattern for $injectionPatterns to ensure
7068
* proper PCRE quoting.
69+
*
70+
* @param non-empty-string $configFile
7171
*/
72-
public function __construct(string $projectRoot = '')
72+
public function __construct(string $projectRoot = '', string $configFile = self::DEFAULT_CONFIG_FILE)
7373
{
74+
$this->configFile = $configFile;
75+
7476
$ns = preg_quote('\\');
7577
$this->isRegisteredPattern = '/new (?:'
7678
. $ns
@@ -96,7 +98,7 @@ public function __construct(string $projectRoot = '')
9698

9799
protected function getDefaultConfigFile(): string
98100
{
99-
return self::DEFAULT_CONFIG_FILE;
101+
return $this->configFile;
100102
}
101103

102104
protected function getDiscoveryClass(): string
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laminas\ComponentInstaller\Injector;
6+
7+
/**
8+
* @internal
9+
*/
10+
final class MezzioDevelopmentConfigInjector implements InjectorInterface
11+
{
12+
private const CONFIG_FILE = 'config/development.config.php.dist';
13+
14+
private ConfigAggregatorInjector $injector;
15+
16+
public function __construct(string $projectRoot)
17+
{
18+
$this->injector = new ConfigAggregatorInjector($projectRoot, self::CONFIG_FILE);
19+
}
20+
21+
public function registersType(int $type): bool
22+
{
23+
return $this->injector->registersType($type);
24+
}
25+
26+
public function getTypesAllowed(): array
27+
{
28+
return $this->injector->getTypesAllowed();
29+
}
30+
31+
public function isRegistered(string $package): bool
32+
{
33+
return $this->injector->isRegistered($package);
34+
}
35+
36+
public function inject(string $package, int $type): bool
37+
{
38+
return $this->injector->inject($package, $type);
39+
}
40+
41+
public function remove(string $package): bool
42+
{
43+
return $this->injector->remove($package);
44+
}
45+
46+
public function setApplicationModules(array $modules): InjectorInterface
47+
{
48+
return $this->injector->setApplicationModules($modules);
49+
}
50+
51+
public function setModuleDependencies(array $modules): InjectorInterface
52+
{
53+
return $this->injector->setModuleDependencies($modules);
54+
}
55+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laminas\ComponentInstaller\Injector;
6+
7+
/**
8+
* @internal
9+
*/
10+
final class MezzioDevelopmentWorkConfigInjector implements InjectorInterface
11+
{
12+
private const CONFIG_FILE = 'config/development.config.php';
13+
14+
private ConfigAggregatorInjector $injector;
15+
16+
public function __construct(string $projectRoot)
17+
{
18+
$this->injector = new ConfigAggregatorInjector($projectRoot, self::CONFIG_FILE);
19+
}
20+
21+
public function registersType(int $type): bool
22+
{
23+
return $this->injector->registersType($type);
24+
}
25+
26+
public function getTypesAllowed(): array
27+
{
28+
return $this->injector->getTypesAllowed();
29+
}
30+
31+
public function isRegistered(string $package): bool
32+
{
33+
return $this->injector->isRegistered($package);
34+
}
35+
36+
public function inject(string $package, int $type): bool
37+
{
38+
return $this->injector->inject($package, $type);
39+
}
40+
41+
public function remove(string $package): bool
42+
{
43+
return $this->injector->remove($package);
44+
}
45+
46+
public function setApplicationModules(array $modules): InjectorInterface
47+
{
48+
return $this->injector->setApplicationModules($modules);
49+
}
50+
51+
public function setModuleDependencies(array $modules): InjectorInterface
52+
{
53+
return $this->injector->setModuleDependencies($modules);
54+
}
55+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LaminasTest\ComponentInstaller\ConfigDiscovery;
6+
7+
use Laminas\ComponentInstaller\ConfigDiscovery\DiscoveryInterface;
8+
use org\bovigo\vfs\vfsStream;
9+
use org\bovigo\vfs\vfsStreamDirectory;
10+
use PHPUnit\Framework\TestCase;
11+
12+
abstract class AbstractConfigAggregatorTestCase extends TestCase
13+
{
14+
private vfsStreamDirectory $configDir;
15+
16+
private DiscoveryInterface $locator;
17+
18+
/** @var class-string<DiscoveryInterface> */
19+
protected string $discoveryClass;
20+
21+
protected string $configFile;
22+
23+
protected function setUp(): void
24+
{
25+
$this->configDir = vfsStream::setup('project');
26+
$this->locator = new $this->discoveryClass(
27+
vfsStream::url('project')
28+
);
29+
}
30+
31+
public function testAbsenceOfFileReturnsFalseOnLocate(): void
32+
{
33+
$this->assertFalse($this->locator->locate());
34+
}
35+
36+
public function testLocateReturnsFalseWhenFileDoesNotHaveExpectedContents(): void
37+
{
38+
vfsStream::newFile($this->configFile)
39+
->at($this->configDir)
40+
->setContent('<' . "?php\nreturn [];");
41+
$this->assertFalse($this->locator->locate());
42+
}
43+
44+
/**
45+
* @psalm-return array<string, array{
46+
* 0: string
47+
* }>
48+
*/
49+
public function validMezzioConfigContents(): array
50+
{
51+
// @codingStandardsIgnoreStart
52+
return [
53+
'fqcn-short-array' => ['<' . "?php\n\$aggregator = new Laminas\ConfigAggregator\ConfigAggregator([\n]);"],
54+
'globally-qualified-short-array' => ['<' . "?php\n\$aggregator = new \Laminas\ConfigAggregator\ConfigAggregator([\n]);"],
55+
'imported-short-array' => ['<' . "?php\n\$aggregator = new ConfigAggregator([\n]);"],
56+
'fqcn-long-array' => ['<' . "?php\n\$aggregator = new Laminas\ConfigAggregator\ConfigAggregator(array(\n));"],
57+
'globally-qualified-long-array' => ['<' . "?php\n\$aggregator = new \Laminas\ConfigAggregator\ConfigAggregator(array(\n));"],
58+
'imported-long-array' => ['<' . "?php\n\$aggregator = new ConfigAggregator(array(\n));"],
59+
];
60+
// @codingStandardsIgnoreEnd
61+
}
62+
63+
/**
64+
* @dataProvider validMezzioConfigContents
65+
*/
66+
public function testLocateReturnsTrueWhenFileExistsAndHasExpectedContent(string $contents): void
67+
{
68+
vfsStream::newFile($this->configFile)
69+
->at($this->configDir)
70+
->setContent($contents);
71+
72+
$this->assertTrue($this->locator->locate());
73+
}
74+
}

0 commit comments

Comments
 (0)