Skip to content

Commit

Permalink
Merge branch 'master' into 2.1-merge
Browse files Browse the repository at this point in the history
# Conflicts:
#	README.md
#	src/amqp/src/Message/ConsumerMessageInterface.php
#	src/utils/src/Coroutine/Concurrent.php
#	src/websocket-server/src/Server.php
  • Loading branch information
limingxinleo committed Nov 9, 2020
2 parents 7ec11e1 + 26bbd9c commit 530cf89
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Listener/PageResolverListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,24 @@ public function process(object $event)

return 1;
});

Paginator::currentPathResolver(function () {
$default = '/';
if (! ApplicationContext::hasContainer() ||
! interface_exists(RequestInterface::class) ||
! Context::has(ServerRequestInterface::class)
) {
return $default;
}

$container = ApplicationContext::getContainer();
$url = $container->get(RequestInterface::class)->url();

if (filter_var($url, FILTER_VALIDATE_URL) !== false) {
return $url;
}

return $url;
});
}
}
7 changes: 7 additions & 0 deletions tests/PageResolverListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Hyperf\HttpServer\Request;
use Hyperf\Paginator\LengthAwarePaginator;
use Hyperf\Paginator\Listener\PageResolverListener;
use Hyperf\Paginator\Paginator;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Context;
use Mockery;
Expand All @@ -33,6 +34,9 @@ protected function setUp(): void
{
Context::set(PsrServerRequestInterface::class, null);
Context::set('http.request.parsedData', null);
Paginator::currentPathResolver(function () {
return '/';
});
}

protected function tearDown(): void
Expand All @@ -47,19 +51,22 @@ public function testPageResolve()
$this->getContainer();
$paginator = new LengthAwarePaginator([1, 2], 10, 2, null);
$this->assertSame('/?page=2', $paginator->nextPageUrl());
$this->assertSame('/', Paginator::resolveCurrentPath());

$listener = new PageResolverListener();
$listener->process(new BootApplication());

$paginator = new LengthAwarePaginator([1, 2], 10, 2, null);
$this->assertSame('/?page=2', $paginator->nextPageUrl());
$this->assertSame('/', Paginator::resolveCurrentPath());

Context::set(PsrServerRequestInterface::class, value(function () {
$request = new \Hyperf\HttpMessage\Server\Request('GET', '/index');
return $request->withQueryParams(['page' => 2]);
}));
$paginator = new LengthAwarePaginator([1, 2], 10, 2, null);
$this->assertSame('/?page=3', $paginator->nextPageUrl());
$this->assertSame('/index', Paginator::resolveCurrentPath());
}

protected function getContainer()
Expand Down

0 comments on commit 530cf89

Please sign in to comment.