Skip to content

Commit

Permalink
Adopt router changes (#392)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexander Makarov <sam@rmcreative.ru>
  • Loading branch information
rustamwin and samdark committed Nov 22, 2021
1 parent 8d998d1 commit 2413911
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 28 deletions.
7 changes: 4 additions & 3 deletions config/routes.php
Expand Up @@ -24,6 +24,7 @@
use Yiisoft\DataResponse\Middleware\FormatDataResponseAsJson;
use Yiisoft\DataResponse\Middleware\FormatDataResponseAsXml;
use Yiisoft\Http\Method;
use Yiisoft\Router\CurrentRouteInterface;
use Yiisoft\Router\Group;
use Yiisoft\Router\Route;
use Yiisoft\Swagger\Middleware\SwaggerJson;
Expand Down Expand Up @@ -114,9 +115,9 @@
// Post page
Route::get('/page/{slug}')
->middleware(
fn (HttpCache $httpCache, PostRepository $postRepository) =>
$httpCache->withEtagSeed(function (ServerRequestInterface $request, $params) use ($postRepository) {
$post = $postRepository->findBySlug($request->getAttribute('slug'));
fn (HttpCache $httpCache, PostRepository $postRepository, CurrentRouteInterface $currentRoute) =>
$httpCache->withEtagSeed(function (ServerRequestInterface $request, $params) use ($postRepository, $currentRoute) {
$post = $postRepository->findBySlug($currentRoute->getParameter('slug'));
return $post->getSlug() . '-' . $post->getUpdatedAt()->getTimestamp();
})
)
Expand Down
13 changes: 7 additions & 6 deletions src/Blog/Archive/ArchiveController.php
Expand Up @@ -8,6 +8,7 @@
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Yiisoft\Data\Paginator\OffsetPaginator;
use Yiisoft\Router\CurrentRouteInterface;
use Yiisoft\Yii\View\ViewRenderer;

final class ArchiveController
Expand All @@ -26,11 +27,11 @@ public function index(ArchiveRepository $archiveRepo): Response
return $this->viewRenderer->render('index', ['archive' => $archiveRepo->getFullArchive()]);
}

public function monthlyArchive(Request $request, TagRepository $tagRepository, ArchiveRepository $archiveRepo): Response
public function monthlyArchive(Request $request, TagRepository $tagRepository, ArchiveRepository $archiveRepo, CurrentRouteInterface $currentRoute): Response
{
$pageNum = (int)$request->getAttribute('page', 1);
$year = (int)$request->getAttribute('year', 0);
$month = (int)$request->getAttribute('month', 0);
$pageNum = (int)$currentRoute->getArgument('page', '1');
$year = (int)$currentRoute->getArgument('year', '0');
$month = (int)$currentRoute->getArgument('month', '0');

$dataReader = $archiveRepo->getMonthlyArchive($year, $month);
$paginator = (new OffsetPaginator($dataReader))
Expand All @@ -47,9 +48,9 @@ public function monthlyArchive(Request $request, TagRepository $tagRepository, A
return $this->viewRenderer->render('monthly-archive', $data);
}

public function yearlyArchive(Request $request, ArchiveRepository $archiveRepo): Response
public function yearlyArchive(Request $request, ArchiveRepository $archiveRepo, CurrentRouteInterface $currentRoute): Response
{
$year = (int)$request->getAttribute('year', 0);
$year = (int)$currentRoute->getArgument('year', '0');

$data = [
'year' => $year,
Expand Down
8 changes: 4 additions & 4 deletions src/Blog/BlogController.php
Expand Up @@ -8,8 +8,8 @@
use App\Blog\Post\PostRepository;
use App\Blog\Tag\TagRepository;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Yiisoft\Data\Paginator\OffsetPaginator;
use Yiisoft\Router\CurrentRouteInterface;
use Yiisoft\User\CurrentUser;
use Yiisoft\Yii\View\ViewRenderer;

Expand All @@ -27,13 +27,13 @@ public function __construct(ViewRenderer $viewRenderer)
}

public function index(
Request $request,
PostRepository $postRepository,
TagRepository $tagRepository,
ArchiveRepository $archiveRepo,
CurrentUser $currentUser
CurrentUser $currentUser,
CurrentRouteInterface $currentRoute
): Response {
$pageNum = (int)$request->getAttribute('page', 1);
$pageNum = (int)$currentRoute->getArgument('page', '1');
$dataReader = $postRepository->findAllPreloaded();
$paginator = (new OffsetPaginator($dataReader))
->withPageSize(self::POSTS_PER_PAGE)
Expand Down
7 changes: 4 additions & 3 deletions src/Blog/CommentController.php
Expand Up @@ -7,6 +7,7 @@
use App\Blog\Comment\CommentService;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Yiisoft\Router\CurrentRouteInterface;
use Yiisoft\Yii\View\ViewRenderer;

final class CommentController
Expand All @@ -18,11 +19,11 @@ public function __construct(ViewRenderer $viewRenderer)
$this->viewRenderer = $viewRenderer->withControllerName('blog/comments');
}

public function index(Request $request, CommentService $service): Response
public function index(Request $request, CommentService $service, CurrentRouteInterface $currentRoute): Response
{
$paginator = $service->getFeedPaginator();
if ($request->getAttribute('next') !== null) {
$paginator = $paginator->withNextPageToken((string)$request->getAttribute('next'));
if ($currentRoute->getArgument('next') !== null) {
$paginator = $paginator->withNextPageToken((string)$currentRoute->getArgument('next'));
}

if ($this->isAjaxRequest($request)) {
Expand Down
10 changes: 6 additions & 4 deletions src/Blog/Post/PostController.php
Expand Up @@ -10,6 +10,7 @@
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Yiisoft\Http\Method;
use Yiisoft\Router\CurrentRouteInterface;
use Yiisoft\Validator\ValidatorInterface;
use Yiisoft\Yii\View\ViewRenderer;

Expand All @@ -32,10 +33,10 @@ public function __construct(
$this->userService = $userService;
}

public function index(Request $request, PostRepository $postRepository): Response
public function index(Request $request, PostRepository $postRepository, CurrentRouteInterface $currentRoute): Response
{
$canEdit = $this->userService->hasPermission('editPost');
$slug = $request->getAttribute('slug', null);
$slug = $currentRoute->getArgument('slug');
$item = $postRepository->fullPostPage($slug);
if ($item === null) {
return $this->webService->getNotFoundResponse();
Expand Down Expand Up @@ -69,9 +70,10 @@ public function add(Request $request, ValidatorInterface $validator): Response
public function edit(
Request $request,
PostRepository $postRepository,
ValidatorInterface $validator
ValidatorInterface $validator,
CurrentRouteInterface $currentRoute
): Response {
$slug = $request->getAttribute('slug', null);
$slug = $currentRoute->getArgument('slug');
$post = $postRepository->fullPostPage($slug);
if ($post === null) {
return $this->webService->getNotFoundResponse();
Expand Down
6 changes: 3 additions & 3 deletions src/User/Controller/ApiUserController.php
Expand Up @@ -7,9 +7,9 @@
use App\User\User;
use App\User\UserRepository;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\Data\Reader\Sort;
use Yiisoft\DataResponse\DataResponseFactoryInterface;
use Yiisoft\Router\CurrentRouteInterface;

class ApiUserController
{
Expand All @@ -33,9 +33,9 @@ public function index(UserRepository $userRepository): ResponseInterface
return $this->responseFactory->createResponse($items);
}

public function profile(ServerRequestInterface $request, UserRepository $userRepository): ResponseInterface
public function profile(UserRepository $userRepository, CurrentRouteInterface $currentRoute): ResponseInterface
{
$login = $request->getAttribute('login', null);
$login = $currentRoute->getArgument('login');

/** @var User $user */
$user = $userRepository->findByLogin($login);
Expand Down
10 changes: 5 additions & 5 deletions src/User/Controller/UserController.php
Expand Up @@ -7,9 +7,9 @@
use App\User\UserRepository;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Yiisoft\Data\Paginator\OffsetPaginator;
use Yiisoft\Data\Reader\Sort;
use Yiisoft\Router\CurrentRouteInterface;
use Yiisoft\Yii\View\ViewRenderer;

class UserController
Expand All @@ -23,9 +23,9 @@ public function __construct(ViewRenderer $viewRenderer)
$this->viewRenderer = $viewRenderer->withControllerName('user');
}

public function index(Request $request, UserRepository $userRepository): Response
public function index(UserRepository $userRepository, CurrentRouteInterface $currentRoute): Response
{
$pageNum = (int)$request->getAttribute('page', 1);
$pageNum = (int)$currentRoute->getArgument('page', '1');

$dataReader = $userRepository->findAll()->withSort(Sort::only(['login'])->withOrderString('login'));
$paginator = (new OffsetPaginator($dataReader))
Expand All @@ -35,9 +35,9 @@ public function index(Request $request, UserRepository $userRepository): Respons
return $this->viewRenderer->render('index', ['paginator' => $paginator]);
}

public function profile(Request $request, UserRepository $userRepository, ResponseFactoryInterface $responseFactory): Response
public function profile(CurrentRouteInterface $currentRoute, UserRepository $userRepository, ResponseFactoryInterface $responseFactory): Response
{
$login = $request->getAttribute('login', null);
$login = $currentRoute->getArgument('login');
$item = $userRepository->findByLogin($login);
if ($item === null) {
return $responseFactory->createResponse(404);
Expand Down

0 comments on commit 2413911

Please sign in to comment.