Skip to content

Commit

Permalink
Do not use dynamic settings for content.tree_root.location_id to avoi…
Browse files Browse the repository at this point in the history
…d config resolver warnings
  • Loading branch information
emodric committed Jul 3, 2019
1 parent c6afb85 commit 4c94550
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 19 deletions.
26 changes: 11 additions & 15 deletions bundle/Helper/PathHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\Core\Helper\TranslationHelper;
use eZ\Publish\Core\MVC\ConfigResolverInterface;
use Symfony\Component\Routing\RouterInterface;

class PathHelper
Expand All @@ -20,42 +21,35 @@ class PathHelper
protected $translationHelper;

/**
* @var \Symfony\Component\Routing\RouterInterface
* @var \eZ\Publish\Core\MVC\ConfigResolverInterface
*/
protected $router;
protected $configResolver;

/**
* @var int|string
* @var \Symfony\Component\Routing\RouterInterface
*/
protected $rootLocationId;
protected $router;

/**
* Constructor.
*
* @param \eZ\Publish\API\Repository\LocationService $locationService
* @param \eZ\Publish\Core\Helper\TranslationHelper $translationHelper
* @param \eZ\Publish\Core\MVC\ConfigResolverInterface $configResolver
* @param \Symfony\Component\Routing\RouterInterface $router
*/
public function __construct(
LocationService $locationService,
TranslationHelper $translationHelper,
ConfigResolverInterface $configResolver,
RouterInterface $router
) {
$this->locationService = $locationService;
$this->translationHelper = $translationHelper;
$this->configResolver = $configResolver;
$this->router = $router;
}

/**
* Sets the root location ID.
*
* @param int|string $rootLocationId
*/
public function setRootLocationId($rootLocationId)
{
$this->rootLocationId = (int) $rootLocationId;
}

/**
* Returns the path array for location ID.
*
Expand All @@ -75,8 +69,10 @@ public function getPath($locationId)
array_shift($path);

$rootLocationFound = false;
$rootLocationId = (int) $this->configResolver->getParameter('content.tree_root.location_id');

foreach ($path as $index => $pathItem) {
if ((int) $pathItem === $this->rootLocationId) {
if ((int) $pathItem === $rootLocationId) {
$rootLocationFound = true;
}

Expand Down
3 changes: 1 addition & 2 deletions bundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,5 @@ services:
arguments:
- "@ezpublish.api.service.location"
- "@ezpublish.translation_helper"
- "@ezpublish.config.resolver"
- "@router"
calls:
- [setRootLocationId, ["$content.tree_root.location_id$"]]
17 changes: 15 additions & 2 deletions tests/Helper/PathHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
use eZ\Publish\Core\Repository\Values\Content\Location;
use Netgen\Bundle\AdminUIBundle\Helper\PathHelper;
use Netgen\Bundle\AdminUIBundle\Tests\Stubs\ConfigResolverStub;
use PHPUnit\Framework\TestCase;

class PathHelperTest extends TestCase
Expand Down Expand Up @@ -56,8 +57,20 @@ public function setUp()
)),
));

$this->helper = new PathHelper($this->locationService, $this->translationHelper, $this->router);
$this->helper->setRootLocationId($this->rootLocation->id);
$this->helper = new PathHelper(
$this->locationService,
$this->translationHelper,
new ConfigResolverStub(
array(
'ezsettings' => array(
'content.tree_root.location_id' => array(
'default' => $this->rootLocation->id,
),
),
)
),
$this->router
);
}

public function testGetPath()
Expand Down
43 changes: 43 additions & 0 deletions tests/Stubs/ConfigResolverStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Netgen\Bundle\AdminUIBundle\Tests\Stubs;

use eZ\Publish\Core\MVC\ConfigResolverInterface;

class ConfigResolverStub implements ConfigResolverInterface
{
/**
* @var array
*/
private $parameters;

/**
* @var string
*/
private $defaultNamespace = 'ezsettings';

public function __construct(array $parameters)
{
$this->parameters = $parameters;
}

public function getParameter($paramName, $namespace = null, $scope = null)
{
return $this->parameters[$namespace ?: $this->defaultNamespace][$paramName];
}

public function hasParameter($paramName, $namespace = null, $scope = null)
{
return isset($this->parameters[$namespace ?: $this->defaultNamespace][$paramName]);
}

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

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

0 comments on commit 4c94550

Please sign in to comment.