76 lines
1.9 KiB
PHP
76 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace DigiComp\Menu\MenuService;
|
|
|
|
/*
|
|
* This file is part of the DigiComp.Menu package.
|
|
*
|
|
* (c) digital competence
|
|
*
|
|
* This package is Open Source Software. For the full copyright and license
|
|
* information, please view the LICENSE file which was distributed with this
|
|
* source code.
|
|
*/
|
|
|
|
use Neos\Eel\EelEvaluatorInterface;
|
|
use Neos\Eel\Utility;
|
|
use Neos\Flow\Annotations as Flow;
|
|
|
|
/**
|
|
* @Flow\Scope("singleton")
|
|
*/
|
|
class SettingsService implements ServiceInterface
|
|
{
|
|
/**
|
|
* @Flow\InjectConfiguration(type="Menu")
|
|
* @var array
|
|
*/
|
|
protected array $menuConfiguration;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected array $items = [];
|
|
|
|
#[Flow\InjectConfiguration(path: 'defaultEelContext', package: 'DigiComp.Menu')]
|
|
protected array $defaultContext = [];
|
|
|
|
/**
|
|
* @Flow\Inject
|
|
* @var EelEvaluatorInterface
|
|
*/
|
|
protected $eelEvaluator;
|
|
|
|
public function getItems(string $forPath = null): \Iterator
|
|
{
|
|
if ($forPath) {
|
|
$items = &$this->menuConfiguration[$forPath];
|
|
} else {
|
|
$items = &$this->menuConfiguration;
|
|
}
|
|
if ($items) {
|
|
\uasort(
|
|
$items,
|
|
static function ($a, $b) {
|
|
return $a['sorting'] > $b['sorting'];
|
|
}
|
|
);
|
|
}
|
|
foreach ($items as $itemName => &$item) {
|
|
\array_walk_recursive(
|
|
$item,
|
|
fn (&$item) => $item =
|
|
\is_string($item) && \str_starts_with($item, '${') && \str_ends_with($item, '}')
|
|
? Utility::evaluateEelExpression(
|
|
$item,
|
|
$this->eelEvaluator,
|
|
[...$items, 'this' => $item, 'item' => $itemName],
|
|
$this->defaultContext
|
|
)
|
|
: $item
|
|
);
|
|
}
|
|
|
|
return new \ArrayIterator($items);
|
|
}
|
|
}
|