diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..072705c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea/ +vendor/ +composer.lock diff --git a/README.md b/README.md index ddbb7f9..d46bc48 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,14 @@ a base template. This is an example of retrieving and parsing a simple navigatio ``` +## 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 diff --git a/composer.json b/composer.json index 789664f..14965ae 100644 --- a/composer.json +++ b/composer.json @@ -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": { @@ -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 } } } diff --git a/ecs.php b/ecs.php new file mode 100644 index 0000000..fc58b8e --- /dev/null +++ b/ecs.php @@ -0,0 +1,23 @@ +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 \n\n@license MIT", + ]]) + ; +}; diff --git a/src/ContaoManager/Plugin.php b/src/ContaoManager/Plugin.php index 66fc402..d7103e0 100644 --- a/src/ContaoManager/Plugin.php +++ b/src/ContaoManager/Plugin.php @@ -5,12 +5,9 @@ /* * This file is part of richardhj/contao-knp-menu. * - * Copyright (c) 2020-2021 Richard Henkenjohann + * (c) Richard Henkenjohann * - * @package richardhj/contao-knp-menu - * @author Richard Henkenjohann - * @copyright 2020-2021 Richard Henkenjohann - * @license MIT + * @license MIT */ namespace Richardhj\ContaoKnpMenuBundle\ContaoManager; @@ -27,7 +24,7 @@ class Plugin implements BundlePluginInterface /** * Gets a list of autoload configurations for this bundle. * - * @return ConfigInterface[] + * @return array */ public function getBundles(ParserInterface $parser): array { diff --git a/src/DependencyInjection/RichardhjContaoKnpMenuExtension.php b/src/DependencyInjection/RichardhjContaoKnpMenuExtension.php index 4b8af66..56cec4a 100644 --- a/src/DependencyInjection/RichardhjContaoKnpMenuExtension.php +++ b/src/DependencyInjection/RichardhjContaoKnpMenuExtension.php @@ -5,12 +5,9 @@ /* * This file is part of richardhj/contao-knp-menu. * - * Copyright (c) 2020-2021 Richard Henkenjohann + * (c) Richard Henkenjohann * - * @package richardhj/contao-knp-menu - * @author Richard Henkenjohann - * @copyright 2020-2021 Richard Henkenjohann - * @license MIT + * @license MIT */ namespace Richardhj\ContaoKnpMenuBundle\DependencyInjection; @@ -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'); diff --git a/src/Menu/MenuBuilder.php b/src/Menu/MenuBuilder.php index f574e04..dfe085a 100644 --- a/src/Menu/MenuBuilder.php +++ b/src/Menu/MenuBuilder.php @@ -5,12 +5,9 @@ /* * This file is part of richardhj/contao-knp-menu. * - * Copyright (c) 2020-2021 Richard Henkenjohann + * (c) Richard Henkenjohann * - * @package richardhj/contao-knp-menu - * @author Richard Henkenjohann - * @copyright 2020-2021 Richard Henkenjohann - * @license MIT + * @license MIT */ namespace Richardhj\ContaoKnpMenuBundle\Menu; @@ -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 @@ -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)) { @@ -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; } @@ -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); } @@ -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 @@ -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); diff --git a/src/Menu/NavigationModuleProvider.php b/src/Menu/NavigationModuleProvider.php index ee019e0..c959939 100644 --- a/src/Menu/NavigationModuleProvider.php +++ b/src/Menu/NavigationModuleProvider.php @@ -5,12 +5,9 @@ /* * This file is part of richardhj/contao-knp-menu. * - * Copyright (c) 2020-2021 Richard Henkenjohann + * (c) Richard Henkenjohann * - * @package richardhj/contao-knp-menu - * @author Richard Henkenjohann - * @copyright 2020-2021 Richard Henkenjohann - * @license MIT + * @license MIT */ namespace Richardhj\ContaoKnpMenuBundle\Menu; @@ -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)) { @@ -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 @@ -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; } @@ -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; } diff --git a/src/Resources/contao/dca/tl_module.php b/src/Resources/contao/dca/tl_module.php index d0660f3..59ff90d 100644 --- a/src/Resources/contao/dca/tl_module.php +++ b/src/Resources/contao/dca/tl_module.php @@ -5,12 +5,9 @@ /* * This file is part of richardhj/contao-knp-menu. * - * Copyright (c) 2020-2021 Richard Henkenjohann + * (c) Richard Henkenjohann * - * @package richardhj/contao-knp-menu - * @author Richard Henkenjohann - * @copyright 2020-2021 Richard Henkenjohann - * @license MIT + * @license MIT */ use Contao\CoreBundle\DataContainer\PaletteManipulator; @@ -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 ''", ]; diff --git a/src/RichardhjContaoKnpMenuBundle.php b/src/RichardhjContaoKnpMenuBundle.php index fe4f22a..36dd77b 100644 --- a/src/RichardhjContaoKnpMenuBundle.php +++ b/src/RichardhjContaoKnpMenuBundle.php @@ -5,12 +5,9 @@ /* * This file is part of richardhj/contao-knp-menu. * - * Copyright (c) 2020-2021 Richard Henkenjohann + * (c) Richard Henkenjohann * - * @package richardhj/contao-knp-menu - * @author Richard Henkenjohann - * @copyright 2020-2021 Richard Henkenjohann - * @license MIT + * @license MIT */ namespace Richardhj\ContaoKnpMenuBundle;