Skip to content

Commit

Permalink
CS
Browse files Browse the repository at this point in the history
  • Loading branch information
richardhj committed Feb 10, 2023
1 parent 8f6348c commit d07e73c
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 73 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/
vendor/
composer.lock
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ a base template. This is an example of retrieving and parsing a simple navigatio
</div>
```

## Development notes:

Code style:

```shell
vendor/bin/ecs check --fix
```


[ico-version]: https://img.shields.io/packagist/v/richardhj/contao-knp-menu.svg?style=flat-square
[link-packagist]: https://packagist.org/packages/richardhj/contao-knp-menu
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"symfony/http-kernel": "^4.4 || ^5.0 || ^6.0"
},
"require-dev": {
"contao/easy-coding-standard": "^4.0",
"contao/manager-plugin": "^2.7"
},
"autoload": {
Expand All @@ -37,7 +38,9 @@
"config": {
"allow-plugins": {
"contao-components/installer": true,
"contao/manager-plugin": true
"contao/manager-plugin": true,
"php-http/discovery": true,
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
23 changes: 23 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use PhpCsFixer\Fixer\Comment\HeaderCommentFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return static function (ECSConfig $ecsConfig): void {
$ecsConfig->paths([
__DIR__ . '/src',
]);

$ecsConfig->import(__DIR__.'/vendor/contao/easy-coding-standard/config/contao.php');

$services = $ecsConfig->services();

$services
->set(HeaderCommentFixer::class)
->call('configure', [[
'header' => "This file is part of richardhj/contao-knp-menu.\n\n(c) Richard Henkenjohann <[email protected]>\n\n@license MIT",
]])
;
};
9 changes: 3 additions & 6 deletions src/ContaoManager/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
/*
* This file is part of richardhj/contao-knp-menu.
*
* Copyright (c) 2020-2021 Richard Henkenjohann
* (c) Richard Henkenjohann <[email protected]>
*
* @package richardhj/contao-knp-menu
* @author Richard Henkenjohann <[email protected]>
* @copyright 2020-2021 Richard Henkenjohann
* @license MIT
* @license MIT
*/

namespace Richardhj\ContaoKnpMenuBundle\ContaoManager;
Expand All @@ -27,7 +24,7 @@ class Plugin implements BundlePluginInterface
/**
* Gets a list of autoload configurations for this bundle.
*
* @return ConfigInterface[]
* @return array<ConfigInterface>
*/
public function getBundles(ParserInterface $parser): array
{
Expand Down
9 changes: 3 additions & 6 deletions src/DependencyInjection/RichardhjContaoKnpMenuExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
/*
* This file is part of richardhj/contao-knp-menu.
*
* Copyright (c) 2020-2021 Richard Henkenjohann
* (c) Richard Henkenjohann <[email protected]>
*
* @package richardhj/contao-knp-menu
* @author Richard Henkenjohann <[email protected]>
* @copyright 2020-2021 Richard Henkenjohann
* @license MIT
* @license MIT
*/

namespace Richardhj\ContaoKnpMenuBundle\DependencyInjection;
Expand All @@ -25,7 +22,7 @@ class RichardhjContaoKnpMenuExtension extends Extension
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
Expand Down
58 changes: 29 additions & 29 deletions src/Menu/MenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
/*
* This file is part of richardhj/contao-knp-menu.
*
* Copyright (c) 2020-2021 Richard Henkenjohann
* (c) Richard Henkenjohann <[email protected]>
*
* @package richardhj/contao-knp-menu
* @author Richard Henkenjohann <[email protected]>
* @copyright 2020-2021 Richard Henkenjohann
* @license MIT
* @license MIT
*/

namespace Richardhj\ContaoKnpMenuBundle\Menu;
Expand All @@ -31,21 +28,17 @@

class MenuBuilder
{
private FactoryInterface $factory;
private RequestStack $requestStack;
private ContaoFramework $framework;
private FactoryInterface $factory;
private RequestStack $requestStack;
private ContaoFramework $framework;
private EventDispatcherInterface $dispatcher;

public function __construct(
FactoryInterface $factory,
RequestStack $requestStack,
ContaoFramework $framework,
EventDispatcherInterface $dispatcher
) {
$this->factory = $factory;
public function __construct(FactoryInterface $factory, RequestStack $requestStack, ContaoFramework $framework, EventDispatcherInterface $dispatcher)
{
$this->factory = $factory;
$this->requestStack = $requestStack;
$this->framework = $framework;
$this->dispatcher = $dispatcher;
$this->framework = $framework;
$this->dispatcher = $dispatcher;
}

public function getMenu(ItemInterface $root, int $pid, $level = 1, $host = null, array $options = []): ItemInterface
Expand All @@ -54,10 +47,10 @@ public function getMenu(ItemInterface $root, int $pid, $level = 1, $host = null,
return $root;
}

$pageAdapter = $this->framework->getAdapter(PageModel::class);
$pageAdapter = $this->framework->getAdapter(PageModel::class);

$groups = [];
$request = $this->requestStack->getCurrentRequest();
$groups = [];
$request = $this->requestStack->getCurrentRequest();
$requestPage = $request->attributes->get('pageModel');

if (is_numeric($requestPage)) {
Expand All @@ -66,6 +59,7 @@ public function getMenu(ItemInterface $root, int $pid, $level = 1, $host = null,

/** @var FrontendUser $user */
$user = $this->framework->createInstance(FrontendUser::class);

if ($user->id) {
$groups = $user->groups;
}
Expand All @@ -86,13 +80,17 @@ public function getMenu(ItemInterface $root, int $pid, $level = 1, $host = null,

// Check whether there will be subpages
if ($page->subpages > 0) {

$level++;
++$level;
$childRecords = Database::getInstance()->getChildRecords($page->id, 'tl_page');

$item->setDisplayChildren(false);
if (!$options['showLevel'] || $options['showLevel'] >= $level || (
!$options['hardLimit'] && ($requestPage->id == $page->id || \in_array($requestPage->id, $childRecords)))) {

if (
!$options['showLevel'] || $options['showLevel'] >= $level ||
(
!$options['hardLimit'] && ($requestPage->id === $page->id || \in_array($requestPage->id, $childRecords, true))
)
) {
$item->setDisplayChildren(true);
}

Expand Down Expand Up @@ -146,7 +144,7 @@ private function getPages(int $pid, array $options): ?Collection

$ids = StringUtil::deserialize($options['pages'], true);

return PageModel::findPublishedRegularWithoutGuestsByIds($ids, ['includeRoot'=>true]);
return PageModel::findPublishedRegularWithoutGuestsByIds($ids, ['includeRoot' => true]);
}

private function populateMenuItem(MenuItem $item, ?PageModel $requestPage, PageModel $page, $href): MenuItem
Expand All @@ -160,15 +158,17 @@ private function populateMenuItem(MenuItem $item, ?PageModel $requestPage, PageM
$path = current(explode('?', Environment::get('request'), 2));

// Active page
if ($href === $path
&& ((null !== $requestPage && $requestPage->id === $page->id) || ('forward' === $page->type && $requestPage->id === $page->jumpTo))) {
if (
$href === $path
&& ((null !== $requestPage && $requestPage->id === $page->id) || ('forward' === $page->type && $requestPage->id === $page->jumpTo))
) {
$extra['isActive'] = true;
$extra['isTrail'] = false;
$extra['isTrail'] = false;

$item->setCurrent(true);
} else {
$extra['isActive'] = false;
$extra['isTrail'] = $trail;
$extra['isTrail'] = $trail;
}

$extra['class'] = trim($page->cssClass);
Expand Down
35 changes: 17 additions & 18 deletions src/Menu/NavigationModuleProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
/*
* This file is part of richardhj/contao-knp-menu.
*
* Copyright (c) 2020-2021 Richard Henkenjohann
* (c) Richard Henkenjohann <[email protected]>
*
* @package richardhj/contao-knp-menu
* @author Richard Henkenjohann <[email protected]>
* @copyright 2020-2021 Richard Henkenjohann
* @license MIT
* @license MIT
*/

namespace Richardhj\ContaoKnpMenuBundle\Menu;
Expand All @@ -26,23 +23,23 @@
class NavigationModuleProvider implements MenuProviderInterface
{
private FactoryInterface $factory;
private MenuBuilder $builder;
private ContaoFramework $framework;
private RequestStack $requestStack;
private MenuBuilder $builder;
private ContaoFramework $framework;
private RequestStack $requestStack;

public function __construct(FactoryInterface $factory, MenuBuilder $builder, ContaoFramework $framework, RequestStack $requestStack)
{
$this->factory = $factory;
$this->builder = $builder;
$this->framework = $framework;
$this->factory = $factory;
$this->builder = $builder;
$this->framework = $framework;
$this->requestStack = $requestStack;
}

public function get($name, array $options = []): ItemInterface
{
$request = $this->requestStack->getCurrentRequest();
$request = $this->requestStack->getCurrentRequest();
$moduleAdapter = $this->framework->getAdapter(ModuleModel::class);
$pageAdapter = $this->framework->getAdapter(PageModel::class);
$pageAdapter = $this->framework->getAdapter(PageModel::class);

/** @var ModuleModel $module */
if (null === $module = $moduleAdapter->findBy('menuAlias', $name)) {
Expand All @@ -55,7 +52,7 @@ public function get($name, array $options = []): ItemInterface
$currentPage = $pageAdapter->findByPk($currentPage);
}

$menu = $this->factory->createItem('root');
$menu = $this->factory->createItem('root');
$options = array_merge($module->row(), $options);

// Set the trail and level
Expand All @@ -66,16 +63,18 @@ public function get($name, array $options = []): ItemInterface
throw new \RuntimeException('Current request does not have a page model. Please define the root page in the navigation module.');
} else {
$trail = $currentPage->trail;
$level = ($options['levelOffset'] > 0) ? $options['levelOffset'] : 0;
$level = $options['levelOffset'] > 0 ? $options['levelOffset'] : 0;
}

// Overwrite the domain and language if the reference page belongs to a different root page (see #3765)
if ($options['defineRoot']
if (
$options['defineRoot']
&& $options['rootPage'] > 0
&& (null !== $rootPage = PageModel::findWithDetails($options['rootPage']))
&& $rootPage->rootId !== $currentPage->rootId
&& $rootPage->domain
&& $rootPage->domain !== $currentPage->domain) {
&& $rootPage->domain !== $currentPage->domain
) {
$host = $rootPage->domain;
}

Expand All @@ -85,7 +84,7 @@ public function get($name, array $options = []): ItemInterface
public function has($name, array $options = []): bool
{
$adapter = $this->framework->getAdapter(ModuleModel::class);
$module = $adapter->findBy('menuAlias', $name);
$module = $adapter->findBy('menuAlias', $name);

return null !== $module;
}
Expand Down
13 changes: 5 additions & 8 deletions src/Resources/contao/dca/tl_module.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
/*
* This file is part of richardhj/contao-knp-menu.
*
* Copyright (c) 2020-2021 Richard Henkenjohann
* (c) Richard Henkenjohann <[email protected]>
*
* @package richardhj/contao-knp-menu
* @author Richard Henkenjohann <[email protected]>
* @copyright 2020-2021 Richard Henkenjohann
* @license MIT
* @license MIT
*/

use Contao\CoreBundle\DataContainer\PaletteManipulator;
Expand All @@ -22,8 +19,8 @@
;

$GLOBALS['TL_DCA']['tl_module']['fields']['menuAlias'] = [
'exclude' => true,
'exclude' => true,
'inputType' => 'text',
'eval' => ['maxlength' => 255, 'tl_class' => 'w50 clr', 'unique' => true],
'sql' => "varchar(255) NOT NULL default ''",
'eval' => ['maxlength' => 255, 'tl_class' => 'w50 clr', 'unique' => true],
'sql' => "varchar(255) NOT NULL default ''",
];
7 changes: 2 additions & 5 deletions src/RichardhjContaoKnpMenuBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
/*
* This file is part of richardhj/contao-knp-menu.
*
* Copyright (c) 2020-2021 Richard Henkenjohann
* (c) Richard Henkenjohann <[email protected]>
*
* @package richardhj/contao-knp-menu
* @author Richard Henkenjohann <[email protected]>
* @copyright 2020-2021 Richard Henkenjohann
* @license MIT
* @license MIT
*/

namespace Richardhj\ContaoKnpMenuBundle;
Expand Down

0 comments on commit d07e73c

Please sign in to comment.