Skip to content

Commit

Permalink
Merge pull request #68 from kynx/mezzio-development-config
Browse files Browse the repository at this point in the history
Added Mezzio development config discovery and injection
  • Loading branch information
boesing authored Jul 17, 2023
2 parents ceb4277 + a8996c4 commit e1ceb9a
Show file tree
Hide file tree
Showing 17 changed files with 506 additions and 234 deletions.
12 changes: 8 additions & 4 deletions src/ConfigDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ final class ConfigDiscovery
'config/application.config.php' => ConfigDiscovery\ApplicationConfig::class,
'config/modules.config.php' => ConfigDiscovery\ModulesConfig::class,
'config/development.config.php.dist' => [
'dist' => ConfigDiscovery\DevelopmentConfig::class,
'work' => ConfigDiscovery\DevelopmentWorkConfig::class,
'dist' => ConfigDiscovery\DevelopmentConfig::class,
'work' => ConfigDiscovery\DevelopmentWorkConfig::class,
'mezzio-dist' => ConfigDiscovery\MezzioDevelopmentConfig::class,
'mezzio-work' => ConfigDiscovery\MezzioDevelopmentWorkConfig::class,
],
'config/config.php' => [
'aggregator' => ConfigDiscovery\ConfigAggregator::class,
Expand All @@ -37,8 +39,10 @@ final class ConfigDiscovery
'config/application.config.php' => Injector\ApplicationConfigInjector::class,
'config/modules.config.php' => Injector\ModulesConfigInjector::class,
'config/development.config.php.dist' => [
'dist' => Injector\DevelopmentConfigInjector::class,
'work' => Injector\DevelopmentWorkConfigInjector::class,
'dist' => Injector\DevelopmentConfigInjector::class,
'work' => Injector\DevelopmentWorkConfigInjector::class,
'mezzio-dist' => Injector\MezzioDevelopmentConfigInjector::class,
'mezzio-work' => Injector\MezzioDevelopmentWorkConfigInjector::class,
],
'config/config.php' => [
'aggregator' => Injector\ConfigAggregatorInjector::class,
Expand Down
7 changes: 4 additions & 3 deletions src/ConfigDiscovery/ConfigAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class ConfigAggregator extends AbstractDiscovery
/**
* Configuration file to look for.
*/
protected string $configFile = 'config/config.php';
protected string $configFile;

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

public function __construct(string $projectDirectory = '')
public function __construct(string $projectDirectory = '', string $configFile = 'config/config.php')
{
$this->expected = sprintf(
$this->configFile = $configFile;
$this->expected = sprintf(
'/new (?:%s?%s)?ConfigAggregator\(\s*(?:array\(|\[)/s',
preg_quote('\\'),
preg_quote('Laminas\ConfigAggregator\\')
Expand Down
25 changes: 25 additions & 0 deletions src/ConfigDiscovery/MezzioDevelopmentConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Laminas\ComponentInstaller\ConfigDiscovery;

/**
* @internal
*/
final class MezzioDevelopmentConfig implements DiscoveryInterface
{
private const CONFIG_FILE = 'config/development.config.php.dist';

private ConfigAggregator $discovery;

public function __construct(string $projectDirectory = '')
{
$this->discovery = new ConfigAggregator($projectDirectory, self::CONFIG_FILE);
}

public function locate(): bool
{
return $this->discovery->locate();
}
}
25 changes: 25 additions & 0 deletions src/ConfigDiscovery/MezzioDevelopmentWorkConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Laminas\ComponentInstaller\ConfigDiscovery;

/**
* @internal
*/
final class MezzioDevelopmentWorkConfig implements DiscoveryInterface
{
private const CONFIG_FILE = 'config/development.config.php';

private ConfigAggregator $discovery;

public function __construct(string $projectDirectory = '')
{
$this->discovery = new ConfigAggregator($projectDirectory, self::CONFIG_FILE);
}

public function locate(): bool
{
return $this->discovery->locate();
}
}
2 changes: 1 addition & 1 deletion src/Injector/ConditionalDiscoveryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function remove(string $package): bool
private function validConfigAggregatorConfig(): bool
{
$discoveryClass = $this->getDiscoveryClass();
$discovery = new $discoveryClass($this->getProjectRoot());
$discovery = new $discoveryClass($this->getProjectRoot(), $this->configFile);
return $discovery->locate();
}

Expand Down
12 changes: 7 additions & 5 deletions src/Injector/ConfigAggregatorInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ final class ConfigAggregatorInjector extends AbstractInjector
{
use ConditionalDiscoveryTrait;

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

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

/** @var non-empty-string */
protected string $configFile = self::DEFAULT_CONFIG_FILE;

/**
* Discovery class, for testing if this injector is valid for the given
* configuration.
Expand Down Expand Up @@ -68,9 +66,13 @@ final class ConfigAggregatorInjector extends AbstractInjector
*
* Sets $isRegisteredPattern and pattern for $injectionPatterns to ensure
* proper PCRE quoting.
*
* @param non-empty-string $configFile
*/
public function __construct(string $projectRoot = '')
public function __construct(string $projectRoot = '', string $configFile = self::DEFAULT_CONFIG_FILE)
{
$this->configFile = $configFile;

$ns = preg_quote('\\');
$this->isRegisteredPattern = '/new (?:'
. $ns
Expand All @@ -96,7 +98,7 @@ public function __construct(string $projectRoot = '')

protected function getDefaultConfigFile(): string
{
return self::DEFAULT_CONFIG_FILE;
return $this->configFile;
}

protected function getDiscoveryClass(): string
Expand Down
55 changes: 55 additions & 0 deletions src/Injector/MezzioDevelopmentConfigInjector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Laminas\ComponentInstaller\Injector;

/**
* @internal
*/
final class MezzioDevelopmentConfigInjector implements InjectorInterface
{
private const CONFIG_FILE = 'config/development.config.php.dist';

private ConfigAggregatorInjector $injector;

public function __construct(string $projectRoot)
{
$this->injector = new ConfigAggregatorInjector($projectRoot, self::CONFIG_FILE);
}

public function registersType(int $type): bool
{
return $this->injector->registersType($type);
}

public function getTypesAllowed(): array
{
return $this->injector->getTypesAllowed();
}

public function isRegistered(string $package): bool
{
return $this->injector->isRegistered($package);
}

public function inject(string $package, int $type): bool
{
return $this->injector->inject($package, $type);
}

public function remove(string $package): bool
{
return $this->injector->remove($package);
}

public function setApplicationModules(array $modules): InjectorInterface
{
return $this->injector->setApplicationModules($modules);
}

public function setModuleDependencies(array $modules): InjectorInterface
{
return $this->injector->setModuleDependencies($modules);
}
}
55 changes: 55 additions & 0 deletions src/Injector/MezzioDevelopmentWorkConfigInjector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Laminas\ComponentInstaller\Injector;

/**
* @internal
*/
final class MezzioDevelopmentWorkConfigInjector implements InjectorInterface
{
private const CONFIG_FILE = 'config/development.config.php';

private ConfigAggregatorInjector $injector;

public function __construct(string $projectRoot)
{
$this->injector = new ConfigAggregatorInjector($projectRoot, self::CONFIG_FILE);
}

public function registersType(int $type): bool
{
return $this->injector->registersType($type);
}

public function getTypesAllowed(): array
{
return $this->injector->getTypesAllowed();
}

public function isRegistered(string $package): bool
{
return $this->injector->isRegistered($package);
}

public function inject(string $package, int $type): bool
{
return $this->injector->inject($package, $type);
}

public function remove(string $package): bool
{
return $this->injector->remove($package);
}

public function setApplicationModules(array $modules): InjectorInterface
{
return $this->injector->setApplicationModules($modules);
}

public function setModuleDependencies(array $modules): InjectorInterface
{
return $this->injector->setModuleDependencies($modules);
}
}
74 changes: 74 additions & 0 deletions test/ConfigDiscovery/AbstractConfigAggregatorTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace LaminasTest\ComponentInstaller\ConfigDiscovery;

use Laminas\ComponentInstaller\ConfigDiscovery\DiscoveryInterface;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use PHPUnit\Framework\TestCase;

abstract class AbstractConfigAggregatorTestCase extends TestCase
{
private vfsStreamDirectory $configDir;

private DiscoveryInterface $locator;

/** @var class-string<DiscoveryInterface> */
protected string $discoveryClass;

protected string $configFile;

protected function setUp(): void
{
$this->configDir = vfsStream::setup('project');
$this->locator = new $this->discoveryClass(
vfsStream::url('project')
);
}

public function testAbsenceOfFileReturnsFalseOnLocate(): void
{
$this->assertFalse($this->locator->locate());
}

public function testLocateReturnsFalseWhenFileDoesNotHaveExpectedContents(): void
{
vfsStream::newFile($this->configFile)
->at($this->configDir)
->setContent('<' . "?php\nreturn [];");
$this->assertFalse($this->locator->locate());
}

/**
* @psalm-return array<string, array{
* 0: string
* }>
*/
public function validMezzioConfigContents(): array
{
// @codingStandardsIgnoreStart
return [
'fqcn-short-array' => ['<' . "?php\n\$aggregator = new Laminas\ConfigAggregator\ConfigAggregator([\n]);"],
'globally-qualified-short-array' => ['<' . "?php\n\$aggregator = new \Laminas\ConfigAggregator\ConfigAggregator([\n]);"],
'imported-short-array' => ['<' . "?php\n\$aggregator = new ConfigAggregator([\n]);"],
'fqcn-long-array' => ['<' . "?php\n\$aggregator = new Laminas\ConfigAggregator\ConfigAggregator(array(\n));"],
'globally-qualified-long-array' => ['<' . "?php\n\$aggregator = new \Laminas\ConfigAggregator\ConfigAggregator(array(\n));"],
'imported-long-array' => ['<' . "?php\n\$aggregator = new ConfigAggregator(array(\n));"],
];
// @codingStandardsIgnoreEnd
}

/**
* @dataProvider validMezzioConfigContents
*/
public function testLocateReturnsTrueWhenFileExistsAndHasExpectedContent(string $contents): void
{
vfsStream::newFile($this->configFile)
->at($this->configDir)
->setContent($contents);

$this->assertTrue($this->locator->locate());
}
}
Loading

0 comments on commit e1ceb9a

Please sign in to comment.