Skip to content

Commit

Permalink
Merge 8272a4a into c5971c7
Browse files Browse the repository at this point in the history
  • Loading branch information
boesing committed Mar 25, 2020
2 parents c5971c7 + 8272a4a commit d3c92ab
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 11 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#52](https://github.com/laminas/laminas-zendframework-bridge/pull/52) fixes a scenario whereby factory _values_ were not being rewritten during configuration post processing.

- [#52](https://github.com/laminas/laminas-zendframework-bridge/pull/52) fixes an issue that occurs with the configuration post processor. Previously, when a service name used as a factory or invokable was encountered that referenced a legacy class, it would get rewritten. This would cause issues if the service was not exposed in the original legacy package, however, as there would now be no alias of the legacy service to the new one. This patch modifies the configuration post processor such that it now tests to see if a service name it will rename exists as an alias; if not, it also creates the alias.

## 1.0.1 - 2020-01-07

Expand Down
62 changes: 53 additions & 9 deletions src/ConfigPostProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

namespace Laminas\ZendFrameworkBridge;

use function array_flip;
use function array_intersect_key;

class ConfigPostProcessor
{
/** @var array String keys => string values */
Expand Down Expand Up @@ -62,15 +65,12 @@ function ($value, array $keys) {
: null;
},

// Aliases and invokables
function ($value, array $keys) {
static $keysOfInterest;

$keysOfInterest = $keysOfInterest ?: ['aliases', 'invokables'];
$key = array_pop($keys);
// service- and pluginmanager handling
function ($value) {
$keysOfInterest = ['aliases', 'invokables', 'factories'];

return in_array($key, $keysOfInterest, true) && is_array($value)
? [$this, 'replaceDependencyAliases']
return is_array($value) && array_intersect_key(array_flip($keysOfInterest), $value) !== []
? [$this, 'replaceDependencyConfiguration']
: null;
},

Expand Down Expand Up @@ -182,7 +182,7 @@ public static function merge(array $a, array $b)
$a[] = $value;
continue;
}

if (is_array($value) && is_array($a[$key])) {
$a[$key] = static::merge($a[$key], $value);
continue;
Expand Down Expand Up @@ -234,6 +234,24 @@ private function replaceExactValue($value)
return $this->exactReplacements[$value];
}

private function replaceDependencyConfiguration(array $config)
{
$aliases = isset($config['aliases']) ? $this->replaceDependencyAliases($config['aliases']) : [];
$invokables = isset($config['invokables']) ? $this->replaceDependencyAliases($config['invokables']) : [];

if ($aliases) {
$config['aliases'] = $aliases;
}

if ($invokables) {
$config['invokables'] = $invokables;
}

$config = $this->replaceDependencyFactories($config);

return $config;
}

/**
* Rewrite dependency aliases array
*
Expand All @@ -260,4 +278,30 @@ private function noopReplacement($value)
{
return $value;
}

private function replaceDependencyFactories(array $config)
{
if (empty($config['factories'])) {
return $config;
}

foreach ($config['factories'] as $service => $factory) {
$replacedService = $this->replacements->replace($service);
$factory = is_string($factory) ? $this->replacements->replace($factory) : $factory;
$config['factories'][$replacedService] = $factory;

if ($replacedService === $service) {
continue;
}

unset($config['factories'][$service]);
if (isset($config['aliases'][$service])) {
continue;
}

$config['aliases'][$service] = $replacedService;
}

return $config;
}
}
1 change: 1 addition & 0 deletions test/ConfigPostProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function configurations()
yield 'equivalent key merging' => ['MergeEquivalentKeys.php'];
yield 'ignore router config' => ['RouterConfig.php'];
yield 'process invokable config' => ['InvokableConfig.php'];
yield 'non aliased service config' => ['NonAliasedServiceConfiguration.php'];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ return [
'factories' => [
'Mezzio\Router\RouterInterface' => Factory\SlimRouterFactory::class,
],
'aliases' => [
'Zend\Expressive\Router\RouterInterface' => 'Mezzio\Router\RouterInterface',
],
],
];
5 changes: 4 additions & 1 deletion test/TestAsset/ConfigPostProcessor/MwopNetAppConfig.php.out
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ return [
Csp::class => Middleware\ContentSecurityPolicyMiddlewareFactory::class,
CacheItemPoolInterface::class => Factory\CachePoolFactory::class,
EventDispatcherInterface::class => Factory\EventDispatcherFactory::class,
'Laminas\Feed\Reader\Http\ClientInterface' => Feed\HttpPlugClientFactory::class,
Handler\ComicsPageHandler::class => Handler\ComicsPageHandlerFactory::class,
Handler\HomePageHandler::class => Handler\HomePageHandlerFactory::class,
Handler\ResumePageHandler::class => Handler\PageHandlerFactory::class,
Handler\ResumePageHandler::class => Handler\PageHandlerFactory::class,
'mail.transport' => Factory\MailTransport::class,
Middleware\RedirectAmpPagesMiddleware::class => Middleware\RedirectAmpPagesMiddlewareFactory::class,
SessionCachePool::class => SessionCachePoolFactory::class,
'Laminas\Feed\Reader\Http\ClientInterface' => Feed\HttpPlugClientFactory::class,
],
'delegators' => [
DisplayPostHandler::class => [
Expand All @@ -46,6 +46,9 @@ return [
Factory\PlatesFunctionsDelegator::class,
],
],
'aliases' => [
'Zend\Feed\Reader\Http\ClientInterface' => 'Laminas\Feed\Reader\Http\ClientInterface',
],
],
'homepage' => [
'feed-count' => 10,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
return [
'dependencies' => [
'factories' => [
'Zend\Form\Factory' => 'Some\Vendor\Zend\Form\Factory',
'Zend\Cache\Storage\StorageInterface' => 'Zend\ServiceManager\Factory\InvokableFactory',
],
'aliases' => [
'foo' => 'Zend\Form\Factory',
'Zend\Cache\Storage\StorageInterface' => 'Laminas\Cache\Storage\StorageInterface',
],
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
return [
'dependencies' => [
'factories' => [
'Laminas\Form\Factory' => 'Some\Vendor\Zend\Form\Factory',
'Laminas\Cache\Storage\StorageInterface' => 'Laminas\ServiceManager\Factory\InvokableFactory',
],
'aliases' => [
'foo' => 'Laminas\Form\Factory',
'Zend\Cache\Storage\StorageInterface' => 'Laminas\Cache\Storage\StorageInterface',
'Zend\Form\Factory' => 'Laminas\Form\Factory',
],
],
];

0 comments on commit d3c92ab

Please sign in to comment.