Skip to content

Commit

Permalink
Link creation update - look for signal handler in the whole way up to…
Browse files Browse the repository at this point in the history
… the UI\Presenter, fixes #592
  • Loading branch information
paveljanda committed Jan 11, 2018
1 parent 50e5b81 commit fa1787b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/Exception/DataGridLinkCreationException.php
@@ -0,0 +1,7 @@
<?php

namespace Ublaboo\DataGrid\Exception;

class DataGridLinkCreationException extends \RuntimeException
{
}
55 changes: 47 additions & 8 deletions src/Traits/TLink.php
Expand Up @@ -8,8 +8,11 @@

namespace Ublaboo\DataGrid\Traits;

use Nette\Application\UI\InvalidLinkException;
use Nette\Application\UI\Presenter;
use Ublaboo\DataGrid\DataGrid;
use Ublaboo\DataGrid\Exception\DataGridHasToBeAttachedToPresenterComponentException;
use Ublaboo\DataGrid\Exception\DataGridLinkCreationException;

trait TLink
{
Expand All @@ -22,20 +25,56 @@ trait TLink
* @return string
* @throws DataGridHasToBeAttachedToPresenterComponentException
* @throws \InvalidArgumentException
* @throws DataGridLinkCreationException
*/
protected function createLink(DataGrid $grid, $href, $params)
{
try {
$parent = $grid->getParent();
$targetComponent = $grid;

return $parent->link($href, $params);
} catch (DataGridHasToBeAttachedToPresenterComponentException $e) {
$parent = $grid->getPresenter();
for ($iteration = 0; $iteration < 10; $iteration++) {
$targetComponent = $targetComponent->getParent();

} catch (\InvalidArgumentException $e) {
$parent = $grid->getPresenter();
try {
@$link = $targetComponent->link($href, $params);

} catch (InvalidLinkException $e) {
$link = false;
}

if ($link) {
if (strpos($link, '#error') === 0) {
continue; // Did not find signal handler
}

return $link; // Found signal handler!
} else {
continue; // Did not find signal handler
}

if ($targetComponent instanceof Presenter) {
// Went the whole way up to the UI\Presenter and did not find any signal handler
$this->throwHierarchyLookupException($grid, $href, $params);
}
}

return $parent->link($href, $params);
// Went 10 steps up to the UI\Presenter and did not find any signal handler
$this->throwHierarchyLookupException($grid, $href, $params);
}


/**
* @throws DataGridLinkCreationException
*/
private function throwHierarchyLookupException(DataGrid $grid, $href, $params): void
{
$desiredHandler = get_class($grid->getParent()) . '::handle' . ucfirst($href) . '()';

throw new DataGridLinkCreationException(
'DataGrid could not create link "'
. $href . '" - did not find any signal handler in componenet hierarchy from '
. get_class($grid->getParent()) . ' up to the '
. get_class($grid->getPresenter()) . '. '
. 'Try adding handler ' . $desiredHandler
);
}
}

0 comments on commit fa1787b

Please sign in to comment.