Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EZP-31046: Improved ChainConfigResolver by extracting code to separate config resolvers #2901

Merged
merged 9 commits into from
Jan 28, 2020
5 changes: 4 additions & 1 deletion doc/bc/changes-8.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ Changes affecting version compatibility with former or future versions.
* The following configuration nodes are not available anymore:
* `ezpublish.<scope>.ezpage.*`
* `ezpublish.<scope>.block_view.*`

* `ezpublish.siteaccess.relation_map` has been replaced by `getSiteAccessesRelation` method from `eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessService`

* REST Client has been dropped.

* REST Server implementation and Common namespace have been removed in favor of
Expand Down Expand Up @@ -269,6 +270,8 @@ Changes affecting version compatibility with former or future versions.

* `ezpublish.persistence.external_storage_registry.factory`

* `ezpublish.config.resolver.core` has been removed. `eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ChainConfigResolver` should be used instead

## Changed behavior

* Service based View Matchers now require to be tagged with `ezplatform.view.matcher`. Moreover now to use it you have to prefix service name with `@` sign:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
namespace eZ\Bundle\EzPublishCoreBundle\Cache\Warmer;

use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ChainConfigResolver;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;

Expand All @@ -26,7 +27,6 @@ public function isOptional()

public function warmUp($cacheDir)
{
$this->container->set('ezpublish.config.resolver.core', null);
$this->container->set('ezpublish.config.resolver.chain', null);
$this->container->set(ChainConfigResolver::class, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
namespace eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler;

use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ChainConfigResolver;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
Expand All @@ -22,11 +23,11 @@ class ChainConfigResolverPass implements CompilerPassInterface
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('ezpublish.config.resolver.chain')) {
if (!$container->hasDefinition(ChainConfigResolver::class)) {
return;
}

$chainResolver = $container->getDefinition('ezpublish.config.resolver.chain');
$chainResolver = $container->getDefinition(ChainConfigResolver::class);

foreach ($container->findTaggedServiceIds('ezpublish.config.resolver') as $id => $attributes) {
$priority = isset($attributes[0]['priority']) ? (int)$attributes[0]['priority'] : 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,11 @@ protected function sortResolvers()
}

/**
* Returns value for $paramName, in $namespace.
*
* @param string $paramName The parameter name, without $prefix and the current scope (i.e. siteaccess name).
* @param string $namespace Namespace for the parameter name. If null, the default namespace should be used.
* @param string $scope The scope you need $paramName value for.
* @return mixed
*
* @throws \eZ\Publish\Core\MVC\Exception\ParameterNotFoundException
*
* @return mixed
*/
public function getParameter($paramName, $namespace = null, $scope = null)
public function getParameter(string $paramName, ?string $namespace = null, ?string $scope = null)
{
foreach ($this->getAllResolvers() as $resolver) {
try {
Expand All @@ -89,19 +83,10 @@ public function getParameter($paramName, $namespace = null, $scope = null)
}

// Finally throw a ParameterNotFoundException since the chain resolver couldn't find any valid resolver for demanded parameter
throw new ParameterNotFoundException($paramName, $namespace);
throw new ParameterNotFoundException($paramName, $namespace, [$scope]);
}

/**
* Checks if $paramName exists in $namespace.
*
* @param string $paramName
* @param string $namespace If null, the default namespace should be used.
* @param string $scope The scope you need $paramName value for.
*
* @return bool
*/
public function hasParameter($paramName, $namespace = null, $scope = null)
public function hasParameter(string $paramName, ?string $namespace = null, ?string $scope = null): bool
{
foreach ($this->getAllResolvers() as $resolver) {
$hasParameter = $resolver->hasParameter($paramName, $namespace, $scope);
Expand All @@ -113,12 +98,7 @@ public function hasParameter($paramName, $namespace = null, $scope = null)
return false;
}

/**
* Changes the default namespace to look parameter into.
*
* @param string $defaultNamespace
*/
public function setDefaultNamespace($defaultNamespace)
public function setDefaultNamespace(string $defaultNamespace): void
{
foreach ($this->getAllResolvers() as $resolver) {
$resolver->setDefaultNamespace($defaultNamespace);
Expand All @@ -130,7 +110,7 @@ public function setDefaultNamespace($defaultNamespace)
*
* @throws \LogicException
*/
public function getDefaultNamespace()
public function getDefaultNamespace(): string
{
throw new \LogicException('getDefaultNamespace() is not supported by the ChainConfigResolver');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,7 @@ public function getUndefinedStrategy()
return $this->undefinedStrategy;
}

/**
* Checks if $paramName exists in $namespace.
*
* @param string $paramName
* @param string $namespace If null, the default namespace should be used.
* @param string $scope The scope you need $paramName value for. It's typically the siteaccess name.
* If null, the current siteaccess name will be used.
*
* @return bool
*/
public function hasParameter($paramName, $namespace = null, $scope = null)
public function hasParameter(string $paramName, ?string $namespace = null, ?string $scope = null): bool
{
$namespace = $namespace ?: $this->defaultNamespace;
$scope = $scope ?: $this->getDefaultScope();
Expand Down Expand Up @@ -154,18 +144,11 @@ public function hasParameter($paramName, $namespace = null, $scope = null)
}

/**
* Returns value for $paramName, in $namespace.
*
* @param string $paramName The parameter name, without $prefix and the current scope (i.e. siteaccess name).
* @param string $namespace Namespace for the parameter name. If null, the default namespace will be used.
* @param string $scope The scope you need $paramName value for. It's typically the siteaccess name.
* If null, the current siteaccess name will be used.
* @return mixed
*
* @throws \eZ\Publish\Core\MVC\Exception\ParameterNotFoundException
*
* @return mixed
*/
public function getParameter($paramName, $namespace = null, $scope = null)
public function getParameter(string $paramName, ?string $namespace = null, ?string $scope = null)
{
$this->logTooEarlyLoadedListIfNeeded($paramName);

Expand Down Expand Up @@ -218,33 +201,25 @@ public function getParameter($paramName, $namespace = null, $scope = null)
}
}

/**
* Changes the default namespace to look parameter into.
*
* @param string $defaultNamespace
*/
public function setDefaultNamespace($defaultNamespace)
public function setDefaultNamespace(string $defaultNamespace): void
{
$this->defaultNamespace = $defaultNamespace;
}

/**
* @return string
*/
public function getDefaultNamespace()
public function getDefaultNamespace(): string
{
return $this->defaultNamespace;
}

public function getDefaultScope()
public function getDefaultScope(): string
{
return $this->defaultScope ?: $this->siteAccess->name;
}

/**
* @param string $scope The default "scope" aka siteaccess name, as opposed to the self::SCOPE_DEFAULT.
*/
public function setDefaultScope($scope)
public function setDefaultScope(string $scope): void
{
$this->defaultScope = $scope;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ConfigResolver;

use eZ\Publish\Core\MVC\ConfigResolverInterface;
use eZ\Publish\Core\MVC\Exception\ParameterNotFoundException;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;

abstract class ContainerConfigResolver implements ConfigResolverInterface, ContainerAwareInterface
{
use ContainerAwareTrait;

/** @var string */
private $scope;

/** @var string */
private $defaultNamespace;

public function __construct(string $scope, string $defaultNamespace)
{
$this->scope = $scope;
$this->defaultNamespace = $defaultNamespace;
}

public function getParameter(string $paramName, ?string $namespace = null, ?string $scope = null)
{
[$namespace, $scope] = $this->resolveNamespaceAndScope($namespace, $scope);
$scopeRelativeParamName = $this->getScopeRelativeParamName($paramName, $namespace, $scope);
if ($this->container->hasParameter($scopeRelativeParamName)) {
return $this->container->getParameter($scopeRelativeParamName);
}

throw new ParameterNotFoundException($paramName, $namespace, [$scope]);
}

public function hasParameter(string $paramName, ?string $namespace = null, ?string $scope = null): bool
{
return $this->container->hasParameter($this->resolveScopeRelativeParamName($paramName, $namespace, $scope));
}

public function getDefaultNamespace(): string
{
return $this->defaultNamespace;
}

public function setDefaultNamespace(string $defaultNamespace): void
{
$this->defaultNamespace = $defaultNamespace;
}

private function resolveScopeRelativeParamName(string $paramName, string $namespace = null, string $scope = null): string
{
return $this->getScopeRelativeParamName($paramName, ...$this->resolveNamespaceAndScope($namespace, $scope));
}

private function resolveNamespaceAndScope(string $namespace = null, string $scope = null): array
{
return [$namespace ?: $this->getDefaultNamespace(), $scope ?? $this->scope];
}

private function getScopeRelativeParamName(string $paramName, string $namespace, string $scope): string
{
return "$namespace.$scope.$paramName";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ConfigResolver;

/**
* @internal
*/
class DefaultScopeConfigResolver extends ContainerConfigResolver
mikadamczyk marked this conversation as resolved.
Show resolved Hide resolved
{
private const SCOPE_NAME = 'default';

public function __construct(string $defaultNamespace)
{
parent::__construct(self::SCOPE_NAME, $defaultNamespace);
}

public function hasParameter(string $paramName, ?string $namespace = null, ?string $scope = null): bool
{
return parent::hasParameter($paramName, $namespace, self::SCOPE_NAME);
}

public function getParameter(string $paramName, ?string $namespace = null, ?string $scope = null)
{
return parent::getParameter($paramName, $namespace, self::SCOPE_NAME);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ConfigResolver;

/**
* @internal
*/
class GlobalScopeConfigResolver extends ContainerConfigResolver
mikadamczyk marked this conversation as resolved.
Show resolved Hide resolved
{
private const SCOPE_NAME = 'global';

public function __construct(string $defaultNamespace)
{
parent::__construct(self::SCOPE_NAME, $defaultNamespace);
}

public function hasParameter(string $paramName, ?string $namespace = null, ?string $scope = null): bool
{
return parent::hasParameter($paramName, $namespace, self::SCOPE_NAME);
}

public function getParameter(string $paramName, ?string $namespace = null, ?string $scope = null)
{
return parent::getParameter($paramName, $namespace, self::SCOPE_NAME);
}
}
Loading