Skip to content

Commit

Permalink
Remove override url name in ActionColumn::class. (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw authored Aug 20, 2023
1 parent 82674ce commit 9d44f77
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 19 deletions.
3 changes: 3 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
findUnusedBaselineEntry="true"
findUnusedCode="false"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
Expand All @@ -15,6 +17,7 @@

<issueHandlers>
<!-- Do not initialize properties to null. -->
<MixedAssignment errorLevel="suppress" />
<PropertyNotSetInConstructor errorLevel="suppress" />
<RedundantPropertyInitializationCheck errorLevel="suppress" />
</issueHandlers>
Expand Down
6 changes: 3 additions & 3 deletions src/BasePagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,9 @@ protected function createUrl(int $page): string

$urlName = $this->currentRoute->getName();

return match ($urlName !== null) {
true => $this->urlGenerator->generate($urlName, $urlArguments, $urlQueryParameters),
false => $urlQueryParameters ? '?' . http_build_query($urlQueryParameters) : '',
return match ($urlName) {
null => $urlQueryParameters ? '?' . http_build_query($urlQueryParameters) : '',
default => $this->urlGenerator->generate($urlName, $urlArguments, $urlQueryParameters),
};
}

Expand Down
18 changes: 17 additions & 1 deletion src/Column/ActionColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use InvalidArgumentException;
use Yiisoft\Html\Tag\A;
use Yiisoft\Html\Tag\Span;
use Yiisoft\Router\CurrentRoute;
use Yiisoft\Router\UrlGeneratorInterface;

use function is_array;
Expand All @@ -22,6 +23,7 @@ final class ActionColumn extends AbstractColumn
private string $template = '{view}{update}{delete}';
private array|null $urlArguments = null;
private Closure|null $urlCreator = null;
private CurrentRoute $currentRoute;
private UrlGeneratorInterface|null $urlGenerator = null;
private array $urlParamsConfig = [];
private string $urlName = '';
Expand Down Expand Up @@ -123,6 +125,14 @@ public function createDefaultButtons(): self
return $new;
}

public function currentRoute(CurrentRoute $value): self
{
$new = clone $this;
$new->currentRoute = $value;

return $new;
}

public function getLabel(): string
{
$label = parent::getLabel();
Expand Down Expand Up @@ -370,7 +380,13 @@ private function createUrl(string $action, array|object $data, mixed $key, int $
$key = $data[$this->primaryKey] ?? $key;
}

$route = $this->urlName !== '' ? $this->urlName . '/' . $action : $action;
$currentRouteName = $this->currentRoute->getName() ?? '';

$route = match ($this->urlName) {
'' => $currentRouteName . '/' . $action,
default => $this->urlName . '/' . $action,
};

$urlQueryParameters = [];
$urlParamsConfig = is_array($key) ? $key : [$this->primaryKey => $key];

Expand Down
20 changes: 10 additions & 10 deletions src/GridView.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ protected function renderItems(): string
* This function tries to guess the columns to show from the given data if {@see columns} are not explicitly
* specified.
*
* @psalm-return list<ActionColumn|DataColumn>
* @psalm-return list<ActionColumn|DataColumn>|list<array>
*/
private function guessColumns(): array
{
Expand Down Expand Up @@ -335,7 +335,7 @@ private function guessColumns(): array
* @throws NotInstantiableException
* @throws CircularReferenceException
*
* @psalm-return array<array-key,AbstractColumn|null>
* @psalm-return array<array-key,AbstractColumn>|array
*/
private function renderColumns(): array
{
Expand All @@ -352,8 +352,8 @@ private function renderColumns(): array
if ($column instanceof ActionColumn) {
$column = $column
->createDefaultButtons()
->urlGenerator($this->getUrlGenerator())
->urlName($this->currentRoute->getName() ?? '');
->currentRoute($this->currentRoute)
->urlGenerator($this->getUrlGenerator());
}

if ($column instanceof DataColumn) {
Expand All @@ -379,7 +379,7 @@ private function renderColumns(): array
*
* @param array $columns The columns of gridview.
*
* @psalm-param array<array-key,AbstractColumn|null> $columns
* @psalm-param array<array-key,AbstractColumn>|array $columns
*/
private function renderColumnGroup(array $columns): string
{
Expand All @@ -401,7 +401,7 @@ private function renderColumnGroup(array $columns): string
*
* @return string The rendering result.
*
* @psalm-param array<array-key,AbstractColumn|null> $columns
* @psalm-param array<array-key,AbstractColumn>|array $columns
*/
private function renderFilters(array $columns): string
{
Expand Down Expand Up @@ -433,7 +433,7 @@ private function renderFilters(array $columns): string
*
* @param array $columns The columns of gridview.
*
* @psalm-param array<array-key,AbstractColumn|null> $columns
* @psalm-param array<array-key,AbstractColumn>|array $columns
*/
private function renderTableBody(array $columns): string
{
Expand Down Expand Up @@ -483,7 +483,7 @@ private function renderTableBody(array $columns): string
*
* @param array $columns The columns of gridview.
*
* @psalm-param array<array-key,AbstractColumn|null> $columns
* @psalm-param array<array-key,AbstractColumn>|array $columns
*/
private function renderTableFooter(array $columns): string
{
Expand Down Expand Up @@ -512,7 +512,7 @@ private function renderTableFooter(array $columns): string
*
* @param array $columns The columns of gridview.
*
* @psalm-param array<array-key,AbstractColumn|null> $columns
* @psalm-param array<array-key,AbstractColumn>|array $columns
*/
private function renderTableHeader(array $columns): string
{
Expand Down Expand Up @@ -549,7 +549,7 @@ private function renderTableHeader(array $columns): string
* @param mixed $key The key associated with the data.
* @param int $index The zero-based index of the data in the data provider.
*
* @psalm-param array<array-key,AbstractColumn|null> $columns
* @psalm-param array<array-key,AbstractColumn>|array $columns
*/
private function renderTableRow(array $columns, array|object $data, mixed $key, int $index): string
{
Expand Down
6 changes: 3 additions & 3 deletions src/LinkSorter.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,9 @@ private function createUrl(string $attribute): string

$urlName = $this->currentRoute->getName();

return match ($urlName !== null) {
true => $this->urlGenerator->generate($urlName, $urlArguments, $urlQueryParameters),
false => $urlQueryParameters ? '?' . http_build_query($urlQueryParameters) : '',
return match ($urlName) {
null => $urlQueryParameters ? '?' . http_build_query($urlQueryParameters) : '',
default => $this->urlGenerator->generate($urlName, $urlArguments, $urlQueryParameters),
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/ListView.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ protected function renderItems(): string
* @param mixed $key The key value associated with the data.
* @param int $index The zero-based index of the data.
*
* @return string {@see afterItem} call result when {@see afterItem} is not a closure.
* @return string call result when {@see afterItem} is not a closure.
*
* {@see afterItem}
*/
Expand All @@ -273,7 +273,7 @@ private function renderAfterItem(array|object $data, mixed $key, int $index): st
* @param mixed $key The key value associated with the data.
* @param int $index The zero-based index of the data.
*
* @return string {@see beforeItem} call result or `null` when {@see beforeItem} is not a closure.
* @return string call result or `null` when {@see beforeItem} is not a closure.
*
* {@see beforeItem}
*/
Expand Down

0 comments on commit 9d44f77

Please sign in to comment.