Skip to content

Commit

Permalink
Removed dependency on Nette Application
Browse files Browse the repository at this point in the history
  • Loading branch information
foxycode authored and davidkudera committed Aug 30, 2018
1 parent 33d453c commit 13d3cd4
Show file tree
Hide file tree
Showing 15 changed files with 156 additions and 104 deletions.
13 changes: 6 additions & 7 deletions src/Carrooi/Menu/AbstractMenuItemsContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Carrooi\Menu\LinkGenerator\ILinkGenerator;
use Carrooi\Menu\Security\IAuthorizator;
use Nette\Application\Application;
use Nette\Http\Request;
use Nette\Localization\ITranslator;

Expand All @@ -17,6 +16,9 @@ abstract class AbstractMenuItemsContainer implements IMenuItemsContainer
{


/** @var \Carrooi\Menu\IMenu */
protected $menu;

/** @var \Carrooi\Menu\LinkGenerator\ILinkGenerator */
protected $linkGenerator;

Expand All @@ -26,9 +28,6 @@ abstract class AbstractMenuItemsContainer implements IMenuItemsContainer
/** @var \Carrooi\Menu\Security\IAuthorizator */
protected $authorizator;

/** @var \Nette\Application\Application */
protected $application;

/** @var \Nette\Http\Request */
protected $httpRequest;

Expand All @@ -39,12 +38,12 @@ abstract class AbstractMenuItemsContainer implements IMenuItemsContainer
private $items = [];


public function __construct(ILinkGenerator $linkGenerator, ITranslator $translator, IAuthorizator $authorizator, Application $application, Request $httpRequest, IMenuItemFactory $menuItemFactory)
public function __construct(IMenu $menu, ILinkGenerator $linkGenerator, ITranslator $translator, IAuthorizator $authorizator, Request $httpRequest, IMenuItemFactory $menuItemFactory)
{
$this->menu = $menu;
$this->linkGenerator = $linkGenerator;
$this->translator = $translator;
$this->authorizator = $authorizator;
$this->application = $application;
$this->httpRequest = $httpRequest;
$this->menuItemFactory = $menuItemFactory;
}
Expand Down Expand Up @@ -85,7 +84,7 @@ public function getItem(string $name): IMenuItem

public function addItem(string $name, string $title, callable $fn = null): void
{
$this->items[$name] = $item = $this->menuItemFactory->create($this->linkGenerator, $this->translator, $this->authorizator, $this->application, $this->httpRequest, $this->menuItemFactory, $title);
$this->items[$name] = $item = $this->menuItemFactory->create($this->menu, $this->linkGenerator, $this->translator, $this->authorizator, $this->httpRequest, $this->menuItemFactory, $title);

if ($fn) {
$fn($item);
Expand Down
2 changes: 0 additions & 2 deletions src/Carrooi/Menu/DI/MenuExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Carrooi\Menu\Security\OptimisticAuthorizator;
use Carrooi\Menu\UI\IMenuComponentFactory;
use Carrooi\Menu\UI\MenuComponent;
use Nette\Application\Application;
use Nette\DI\CompilerExtension;
use Nette\DI\ContainerBuilder;
use Nette\DI\ServiceDefinition;
Expand Down Expand Up @@ -127,7 +126,6 @@ private function loadMenuConfiguration(ContainerBuilder $builder, string $menuNa
$linkGenerator,
$translator,
$authorizator,
'@'. Application::class,
'@'. Http\Request::class,
$itemFactory,
$loader,
Expand Down
8 changes: 8 additions & 0 deletions src/Carrooi/Menu/IMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Carrooi\Menu;

use Nette\Application\UI\Presenter;

/**
* @author David Kudera <[email protected]>
*/
Expand All @@ -25,4 +27,10 @@ public function getSitemapTemplate(): string;

public function getPath(): array;


public function getActivePresenter(): ?Presenter;


public function setActivePresenter(?Presenter $link): void;

}
3 changes: 1 addition & 2 deletions src/Carrooi/Menu/IMenuItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Carrooi\Menu\LinkGenerator\ILinkGenerator;
use Carrooi\Menu\Security\IAuthorizator;
use Nette\Application\Application;
use Nette\Http\Request;
use Nette\Localization\ITranslator;

Expand All @@ -17,6 +16,6 @@ interface IMenuItemFactory
{


public function create(ILinkGenerator $linkGenerator, ITranslator $translator, IAuthorizator $authorizator, Application $application, Request $httpRequest, IMenuItemFactory $menuItemFactory, string $title): IMenuItem;
public function create(IMenu $menu, ILinkGenerator $linkGenerator, ITranslator $translator, IAuthorizator $authorizator, Request $httpRequest, IMenuItemFactory $menuItemFactory, string $title): IMenuItem;

}
12 changes: 6 additions & 6 deletions src/Carrooi/Menu/LinkGenerator/NetteLinkGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Carrooi\Menu\LinkGenerator;

use Carrooi\Menu\IMenuItem;
use Nette\Application\Application;
use Nette\Application\LinkGenerator;

/**
* @author David Kudera <[email protected]>
Expand All @@ -14,20 +14,20 @@ final class NetteLinkGenerator implements ILinkGenerator
{


/** @var \Nette\Application\Application */
private $application;
/** @var \Nette\Application\LinkGenerator */
private $linkGenerator;


public function __construct(Application $application)
public function __construct(LinkGenerator $linkGenerator)
{
$this->application = $application;
$this->linkGenerator = $linkGenerator;
}


public function link(IMenuItem $item): string
{
if (($action = $item->getAction()) !== null) {
return $this->application->getPresenter()->link($action, $item->getActionParameters());
return $this->linkGenerator->link($action, $item->getActionParameters());

} elseif (($link = $item->getLink()) !== null) {
return $link;
Expand Down
21 changes: 18 additions & 3 deletions src/Carrooi/Menu/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Carrooi\Menu\LinkGenerator\ILinkGenerator;
use Carrooi\Menu\Loaders\IMenuLoader;
use Carrooi\Menu\Security\IAuthorizator;
use Nette\Application\Application;
use Nette\Application\UI\Presenter;
use Nette\Http\Request;
use Nette\Localization\ITranslator;

Expand All @@ -31,10 +31,13 @@ final class Menu extends AbstractMenuItemsContainer implements IMenu
'sitemap' => null,
];

/** @var Presenter */
private $activePresenter;

public function __construct(ILinkGenerator $linkGenerator, ITranslator $translator, IAuthorizator $authorizator, Application $application, Request $httpRequest, IMenuItemFactory $menuItemFactory, IMenuLoader $loader, string $name, string $menuTemplate, string $breadcrumbsTemplate, string $sitemapTemplate)

public function __construct(ILinkGenerator $linkGenerator, ITranslator $translator, IAuthorizator $authorizator, Request $httpRequest, IMenuItemFactory $menuItemFactory, IMenuLoader $loader, string $name, string $menuTemplate, string $breadcrumbsTemplate, string $sitemapTemplate)
{
parent::__construct($linkGenerator, $translator, $authorizator, $application, $httpRequest, $menuItemFactory);
parent::__construct($this, $linkGenerator, $translator, $authorizator, $httpRequest, $menuItemFactory);

$this->loader = $loader;
$this->name = $name;
Expand Down Expand Up @@ -92,4 +95,16 @@ public function getPath(): array
return $path;
}


public function getActivePresenter(): ?Presenter
{
return $this->activePresenter;
}


public function setActivePresenter(?Presenter $presenter): void
{
$this->activePresenter = $presenter;
}

}
11 changes: 4 additions & 7 deletions src/Carrooi/Menu/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Carrooi\Menu\LinkGenerator\ILinkGenerator;
use Carrooi\Menu\Security\IAuthorizator;
use Nette\Application\Application;
use Nette\Application\UI\InvalidLinkException;
use Nette\Http\Request;
use Nette\Localization\ITranslator;
Expand Down Expand Up @@ -46,9 +45,10 @@ final class MenuItem extends AbstractMenuItemsContainer implements IMenuItem
/** @var string[] */
private $include = [];

public function __construct(ILinkGenerator $linkGenerator, ITranslator $translator, IAuthorizator $authorizator, Application $application, Request $httpRequest, IMenuItemFactory $menuItemFactory, string $title)

public function __construct(IMenu $menu, ILinkGenerator $linkGenerator, ITranslator $translator, IAuthorizator $authorizator, Request $httpRequest, IMenuItemFactory $menuItemFactory, string $title)
{
parent::__construct($linkGenerator, $translator, $authorizator, $application, $httpRequest, $menuItemFactory);
parent::__construct($menu, $linkGenerator, $translator, $authorizator, $httpRequest, $menuItemFactory);

$this->title = $title;
}
Expand All @@ -64,10 +64,7 @@ public function isActive(): bool
return $this->active = false;
}

if ($this->getAction()) {

/** @var \Nette\Application\UI\Presenter $presenter */
$presenter = $this->application->getPresenter();
if ($this->getAction() && ($presenter = $this->menu->getActivePresenter())) {

try {
$presenter->link($this->getAction(), $this->getActionParameters());
Expand Down
5 changes: 2 additions & 3 deletions src/Carrooi/Menu/MenuItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Carrooi\Menu\LinkGenerator\ILinkGenerator;
use Carrooi\Menu\Security\IAuthorizator;
use Nette\Application\Application;
use Nette\Http\Request;
use Nette\Localization\ITranslator;

Expand All @@ -17,9 +16,9 @@ final class MenuItemFactory implements IMenuItemFactory
{


public function create(ILinkGenerator $linkGenerator, ITranslator $translator, IAuthorizator $authorizator, Application $application, Request $httpRequest, IMenuItemFactory $menuItemFactory, string $title): IMenuItem
public function create(IMenu $menu, ILinkGenerator $linkGenerator, ITranslator $translator, IAuthorizator $authorizator, Request $httpRequest, IMenuItemFactory $menuItemFactory, string $title): IMenuItem
{
return new MenuItem($linkGenerator, $translator, $authorizator, $application, $httpRequest, $menuItemFactory, $title);
return new MenuItem($menu, $linkGenerator, $translator, $authorizator, $httpRequest, $menuItemFactory, $title);
}

}
18 changes: 18 additions & 0 deletions src/Carrooi/Menu/UI/MenuComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Carrooi\Menu\IMenu;
use Carrooi\Menu\MenuContainer;
use Nette\Application\UI\Control;
use Nette\Application\UI\Presenter;

/**
* @author David Kudera <[email protected]>
Expand Down Expand Up @@ -60,4 +61,21 @@ public function renderType(IMenu $menu, string $menuTemplate): void
$this->template->render();
}


/**
* This method will be called when the component (or component's parent)
* becomes attached to a monitored object. Do not call this method yourself.
* @param \Nette\ComponentModel\IComponent
* @return void
*/
protected function attached($presenter)
{
if ($presenter instanceof Presenter) {
$menu = $this->container->getMenu($this->menuName);
$menu->setActivePresenter($presenter);
}

parent::attached($presenter);
}

}
20 changes: 10 additions & 10 deletions tests/CarrooiTests/Menu/AbstractMenuItemsContainerTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ final class AbstractMenuItemsContainerTest extends TestCase

public function testItems(): void
{
$menu = $this->createMockMenu();
$linkGenerator = $this->createMockLinkGenerator();
$translator = $this->createMockTranslator();
$authorizator = $this->createMockAuthorizator();
$application = $this->createMockApplication();
$request = $this->createMockHttpRequest();
$item = $this->createMockMenuItem();

$itemFactory = $this->createMockMenuItemFactory(function(MockInterface $itemFactory) use ($item) {
$itemFactory->shouldReceive('create')->andReturn($item);
});

$container = $this->createPartialMockAbstractMenuItemsContainer(null, [$linkGenerator, $translator, $authorizator, $application, $request, $itemFactory]);
$container = $this->createPartialMockAbstractMenuItemsContainer(null, [$menu, $linkGenerator, $translator, $authorizator, $request, $itemFactory]);

Assert::equal([], $container->getItems());

Expand All @@ -44,29 +44,29 @@ final class AbstractMenuItemsContainerTest extends TestCase

public function testGetItem_direct(): void
{
$menu = $this->createMockMenu();
$linkGenerator = $this->createMockLinkGenerator();
$translator = $this->createMockTranslator();
$authorizator = $this->createMockAuthorizator();
$application = $this->createMockApplication();
$request = $this->createMockHttpRequest();
$itemFactory = $this->createMockMenuItemFactory();

$item = $this->createMockMenuItem();

$container = $this->createPartialMockAbstractMenuItemsContainer(function(MockInterface $container) use ($item) {
$container->shouldReceive('getItems')->andReturn(['item' => $item]);
}, [$linkGenerator, $translator, $authorizator, $application, $request, $itemFactory]);
}, [$menu, $linkGenerator, $translator, $authorizator, $request, $itemFactory]);

Assert::same($item, $container->getItem('item'));
}


public function testGetItem(): void
{
$menu = $this->createMockMenu();
$linkGenerator = $this->createMockLinkGenerator();
$translator = $this->createMockTranslator();
$authorizator = $this->createMockAuthorizator();
$application = $this->createMockApplication();
$request = $this->createMockHttpRequest();
$itemFactory = $this->createMockMenuItemFactory();

Expand All @@ -82,18 +82,18 @@ final class AbstractMenuItemsContainerTest extends TestCase

$container = $this->createPartialMockAbstractMenuItemsContainer(function(MockInterface $container) use ($itemA) {
$container->shouldReceive('getItems')->andReturn(['a' => $itemA]);
}, [$linkGenerator, $translator, $authorizator, $application, $request, $itemFactory]);
}, [$menu, $linkGenerator, $translator, $authorizator, $request, $itemFactory]);

Assert::same($itemC, $container->getItem('a-b-c'));
}


public function testFindActiveItem(): void
{
$menu = $this->createMockMenu();
$linkGenerator = $this->createMockLinkGenerator();
$translator = $this->createMockTranslator();
$authorizator = $this->createMockAuthorizator();
$application = $this->createMockApplication();
$request = $this->createMockHttpRequest();
$itemFactory = $this->createMockMenuItemFactory();

Expand All @@ -116,18 +116,18 @@ final class AbstractMenuItemsContainerTest extends TestCase
'b' => $itemB,
'c' => $itemC,
]);
}, [$linkGenerator, $translator, $authorizator, $application, $request, $itemFactory]);
}, [$menu, $linkGenerator, $translator, $authorizator, $request, $itemFactory]);

Assert::same($itemC, $container->findActiveItem());
}


public function testVisibleItemsOn(): void
{
$menu = $this->createMockMenu();
$linkGenerator = $this->createMockLinkGenerator();
$translator = $this->createMockTranslator();
$authorizator = $this->createMockAuthorizator();
$application = $this->createMockApplication();
$request = $this->createMockHttpRequest();
$itemFactory = $this->createMockMenuItemFactory();

Expand All @@ -148,7 +148,7 @@ final class AbstractMenuItemsContainerTest extends TestCase
'a' => $itemA,
'b' => $itemB,
]);
}, [$linkGenerator, $translator, $authorizator, $application, $request, $itemFactory]);
}, [$menu, $linkGenerator, $translator, $authorizator, $request, $itemFactory]);

Assert::true($container->hasVisibleItemsOnMenu());
Assert::equal(['b' => $itemB], $container->getVisibleItemsOnMenu());
Expand Down
Loading

0 comments on commit 13d3cd4

Please sign in to comment.