Skip to content

Commit

Permalink
Merge pull request #420 from bearsunday/reverse_link
Browse files Browse the repository at this point in the history
Optimized reverse link
  • Loading branch information
koriym authored Jul 25, 2023
2 parents f377e76 + ac59318 commit fa11420
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 6 deletions.
3 changes: 2 additions & 1 deletion composer-require-checker.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"null", "true", "false",
"static", "self", "parent",
"array", "string", "int", "float", "bool", "iterable", "callable", "void", "object", "mixed",
"Composer\\Autoload\\ClassLoader", "Doctrine\\Common\\Cache\\FilesystemCache", "Doctrine\\Common\\Cache\\PhpFileCache"
"Composer\\Autoload\\ClassLoader", "Doctrine\\Common\\Cache\\FilesystemCache", "Doctrine\\Common\\Cache\\PhpFileCache",
"BEAR\\Resource\\ReverseLinkInterface"
]
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"aura/cli": "^2.2",
"bear/app-meta": "^1.8",
"bear/query-repository": "^1.9.1",
"bear/resource": "^1.19",
"bear/resource": "^1.20",
"bear/streamer": "^1.2.2",
"bear/sunday": "^1.6.1",
"monolog/monolog": "^1.25 || ^2.0",
Expand Down
1 change: 1 addition & 0 deletions src/Compiler/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ public function __invoke(string $appName, string $context, array $globals, array

// @codeCoverageIgnoreStart
return 0;
// @codeCoverageIgnoreEnd
}
}
3 changes: 3 additions & 0 deletions src/Context/HalModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

use BEAR\Package\Provide\Representation\CreatedResourceRenderer;
use BEAR\Package\Provide\Representation\RouterReverseLink;
use BEAR\Package\Provide\Representation\RouterReverseLinker;
use BEAR\Resource\HalRenderer;
use BEAR\Resource\RenderInterface;
use BEAR\Resource\ReverseLinkerInterface;
use BEAR\Resource\ReverseLinkInterface;
use Ray\Di\AbstractModule;

Expand All @@ -20,6 +22,7 @@ protected function configure(): void
{
$this->bind(CreatedResourceRenderer::class);
$this->bind(RenderInterface::class)->to(HalRenderer::class);
$this->bind(ReverseLinkerInterface::class)->to(RouterReverseLinker::class);
$this->bind(ReverseLinkInterface::class)->to(RouterReverseLink::class);
}
}
36 changes: 36 additions & 0 deletions src/Provide/Representation/RouterReverseLinker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace BEAR\Package\Provide\Representation;

use BEAR\Resource\ReverseLinkerInterface;
use BEAR\Sunday\Extension\Router\RouterInterface;

use function is_string;
use function parse_url;

use const PHP_URL_PATH;

final class RouterReverseLinker implements ReverseLinkerInterface
{
public function __construct(
private RouterInterface $router,
) {
}

/**
* {@inheritDoc}
*/
public function __invoke(string $uri, array $query): string
{
$routeName = (string) parse_url($uri, PHP_URL_PATH);

$reverseUri = $this->router->generate($routeName, $query);
if (is_string($reverseUri)) {
return $reverseUri;
}

return $uri;
}
}
6 changes: 5 additions & 1 deletion tests/Fake/Provide/Representation/FakeRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public function generate($name, $data)
{
unset($name);

return '/task/' . $data['id'];
if (isset($data['id'])) {
return '/task/' . $data['id'];
}

return '/task';
}
}
6 changes: 3 additions & 3 deletions tests/Provide/Representation/HalRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use BEAR\Package\AppInjector;
use BEAR\Package\Exception\LocationHeaderRequestException;
use BEAR\Resource\HalLink;
use BEAR\Resource\HalLinker;
use BEAR\Resource\HalRenderer;
use BEAR\Resource\ResourceInterface;
use BEAR\Resource\ResourceObject;
Expand Down Expand Up @@ -158,7 +158,7 @@ public function testOptions(): void

public function testHalRendererNoParam(): void
{
$halRenderer = new HalRenderer(new AnnotationReader(), new HalLink(new RouterReverseLink(new FakeRouter())));
$halRenderer = new HalRenderer(new AnnotationReader(), new HalLinker(new RouterReverseLinker(new FakeRouter())));
$ro = new Task();
$ro->onPost();
$ro->uri = new Uri('app://self/task');
Expand All @@ -178,7 +178,7 @@ public function testHalRendererNoParam(): void

public function testHalRendererWithParam(): void
{
$halRenderer = new HalRenderer(new AnnotationReader(), new HalLink(new RouterReverseLink(new FakeRouter())));
$halRenderer = new HalRenderer(new AnnotationReader(), new HalLinker(new RouterReverseLinker(new FakeRouter())));
$ro = new Task();
$ro->uri = new Uri('app://self/task?id=1');
$ro->uri->method = 'post';
Expand Down

0 comments on commit fa11420

Please sign in to comment.