Skip to content

Commit

Permalink
Update blog (#566)
Browse files Browse the repository at this point in the history
* Add `yiisoft/request-model`
* Update blog-api
* Update widget to 2.0
* Other fixes
  • Loading branch information
rustamwin committed Jan 28, 2023
1 parent d23f0d8 commit 00da52c
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 51 deletions.
4 changes: 2 additions & 2 deletions blog-api/composer.json
Expand Up @@ -54,8 +54,8 @@
"yiisoft/log-target-file": "^2.0",
"yiisoft/request-body-parser": "^1.1",
"yiisoft/request-model": "dev-master",
"yiisoft/router": "^2.0",
"yiisoft/router-fastroute": "^2.0",
"yiisoft/router": "^2.1",
"yiisoft/router-fastroute": "^2.1",
"yiisoft/security": "^1.0",
"yiisoft/translator": "^2.0",
"yiisoft/translator-message-php": "^1.1",
Expand Down
8 changes: 4 additions & 4 deletions blog-api/config/web/middleware-dispatcher.php
Expand Up @@ -2,13 +2,13 @@

declare(strict_types=1);

use Yiisoft\Middleware\Dispatcher\WrapperFactoryInterface;
use Yiisoft\RequestModel\WrapperFactory;

/**
* @var array $params
*/

use Yiisoft\Middleware\Dispatcher\ParametersResolverInterface;
use Yiisoft\RequestModel\HandlerParametersResolver;

return [
WrapperFactoryInterface::class => WrapperFactory::class,
ParametersResolverInterface::class => HandlerParametersResolver::class,
];
7 changes: 4 additions & 3 deletions blog/composer.json
Expand Up @@ -59,11 +59,12 @@
"yiisoft/mailer": "^5.0",
"yiisoft/mailer-symfony": "^2.1",
"yiisoft/rate-limiter": "dev-master",
"yiisoft/request-model": "dev-master",
"yiisoft/rbac": "^1.0",
"yiisoft/rbac-php": "^1.0",
"yiisoft/rbac-rules-container": "^1.1",
"yiisoft/router": "^2.0",
"yiisoft/router-fastroute": "^2.0",
"yiisoft/router": "^2.1",
"yiisoft/router-fastroute": "^2.1",
"yiisoft/security": "^1.0",
"yiisoft/session": "^1.0",
"yiisoft/translator": "^2.0",
Expand All @@ -72,7 +73,7 @@
"yiisoft/validator": "3.0.x-dev",
"yiisoft/var-dumper": "^1.0",
"yiisoft/view": "^7.0",
"yiisoft/widget": "^1.1",
"yiisoft/widget": "^2.0",
"yiisoft/yii-bootstrap5": "^3.0@dev",
"yiisoft/yii-console": "^1.0",
"yiisoft/yii-cycle": "dev-master",
Expand Down
15 changes: 15 additions & 0 deletions blog/config/web/middleware-dispatcher.php
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);


/**
* @var array $params
*/

use Yiisoft\Middleware\Dispatcher\ParametersResolverInterface;
use Yiisoft\RequestModel\HandlerParametersResolver;

return [
ParametersResolverInterface::class => HandlerParametersResolver::class,
];
2 changes: 1 addition & 1 deletion blog/public/index.php
Expand Up @@ -31,5 +31,5 @@
require_once dirname(__DIR__) . '/autoload.php';

// Run HTTP application runner
$runner = new HttpApplicationRunner(dirname(__DIR__), $_ENV['YII_DEBUG'], $_ENV['YII_ENV']);
$runner = new HttpApplicationRunner(dirname(__DIR__), $_ENV['YII_DEBUG'], environment: $_ENV['YII_ENV']);
$runner->run();
19 changes: 9 additions & 10 deletions blog/src/Blog/Archive/ArchiveController.php
Expand Up @@ -7,7 +7,7 @@
use App\Blog\Tag\TagRepository;
use Psr\Http\Message\ResponseInterface as Response;
use Yiisoft\Data\Paginator\OffsetPaginator;
use Yiisoft\Router\CurrentRoute;
use Yiisoft\RequestModel\Attribute\Route;
use Yiisoft\Yii\View\ViewRenderer;

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

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

public function monthlyArchive(
TagRepository $tagRepository,
ArchiveRepository $archiveRepo,
#[Route('page')] int $pageNum = 1,
#[Route('year')] int $year = 0,
#[Route('month')] int $month = 0,
): Response {
$dataReader = $archiveRepo->getMonthlyArchive($year, $month);
$paginator = (new OffsetPaginator($dataReader))
->withPageSize(self::POSTS_PER_PAGE)
Expand All @@ -50,10 +51,8 @@ public function monthlyArchive(CurrentRoute $currentRoute, TagRepository $tagRep
return $this->viewRenderer->render('monthly-archive', $data);
}

public function yearlyArchive(CurrentRoute $currentRoute, ArchiveRepository $archiveRepo): Response
public function yearlyArchive(ArchiveRepository $archiveRepo, #[Route('year')] int $year = 0): Response
{
$year = (int) $currentRoute->getArgument('year', '0');

$data = [
'year' => $year,
'items' => $archiveRepo->getYearlyArchive($year),
Expand Down
5 changes: 2 additions & 3 deletions blog/src/Blog/BlogController.php
Expand Up @@ -9,7 +9,7 @@
use App\Blog\Tag\TagRepository;
use Psr\Http\Message\ResponseInterface as Response;
use Yiisoft\Data\Paginator\OffsetPaginator;
use Yiisoft\Router\CurrentRoute;
use Yiisoft\RequestModel\Attribute\Route;
use Yiisoft\User\CurrentUser;
use Yiisoft\Yii\View\ViewRenderer;

Expand All @@ -31,9 +31,8 @@ public function index(
TagRepository $tagRepository,
ArchiveRepository $archiveRepo,
CurrentUser $currentUser,
CurrentRoute $currentRoute
#[Route('page')] $pageNum = 1,
): Response {
$pageNum = (int) $currentRoute->getArgument('page', '1');
$dataReader = $postRepository->findAllPreloaded();
$paginator = (new OffsetPaginator($dataReader))
->withPageSize(self::POSTS_PER_PAGE)
Expand Down
8 changes: 4 additions & 4 deletions blog/src/Blog/CommentController.php
Expand Up @@ -7,7 +7,7 @@
use App\Blog\Comment\CommentService;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Yiisoft\Router\CurrentRoute;
use Yiisoft\RequestModel\Attribute\Route;
use Yiisoft\Yii\View\ViewRenderer;

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

public function index(Request $request, CommentService $service, CurrentRoute $currentRoute): Response
public function index(Request $request, CommentService $service, #[Route('next')] ?string $next): Response
{
$paginator = $service->getFeedPaginator();
if ($currentRoute->getArgument('next') !== null) {
$paginator = $paginator->withNextPageToken((string) $currentRoute->getArgument('next'));
if ($next !== null) {
$paginator = $paginator->withNextPageToken($next);
}

if ($this->isAjaxRequest($request)) {
Expand Down
2 changes: 1 addition & 1 deletion blog/src/Blog/Widget/PostCard.php
Expand Up @@ -19,7 +19,7 @@ public function __construct(private UrlGeneratorInterface $urlGenerator)
{
}

protected function run(): string
public function render(): string
{
if (!isset($this->options['id'])) {
$this->options['id'] = "{$this->getId()}-post-card";
Expand Down
31 changes: 13 additions & 18 deletions blog/src/User/Controller/UserController.php
Expand Up @@ -7,10 +7,11 @@
use App\User\UserRepository;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\Data\Paginator\OffsetPaginator;
use Yiisoft\Data\Reader\Sort;
use Yiisoft\Router\CurrentRoute;
use Yiisoft\RequestModel\Attribute\Body;
use Yiisoft\RequestModel\Attribute\Query;
use Yiisoft\RequestModel\Attribute\Route;
use Yiisoft\Yii\View\ViewRenderer;

final class UserController
Expand All @@ -23,25 +24,20 @@ public function __construct(private ViewRenderer $viewRenderer)
}

public function index(
CurrentRoute $currentRoute,
ServerRequestInterface $request,
UserRepository $userRepository
UserRepository $userRepository,
#[Body] array $body,
#[Query] array $sortOrder,
#[Route('page')] int $page = 1,
#[Route('pagesize')] int $pageSize = null,
): Response {
/** @var array */
$body = $request->getParsedBody();
$sortOrderString = $request->getQueryParams();

$dataReader = $userRepository
->findAll()
->withSort(Sort::only(['id', 'login'])
->withOrderString($sortOrderString['sort'] ?? 'id'));
->withOrderString($sortOrder['sort'] ?? 'id'));

$page = (int) $currentRoute->getArgument('page', '1');

$pageSize = (int) $currentRoute->getArgument(
'pagesize',
$body['pageSize'] ?? (string) OffSetPaginator::DEFAULT_PAGE_SIZE,
);
if ($pageSize === null) {
$pageSize = (int) ($body['pageSize'] ?? OffSetPaginator::DEFAULT_PAGE_SIZE);
}

$paginator = (new OffsetPaginator($dataReader));
$paginator = $paginator->withNextPageToken((string) $page)->withPageSize($pageSize);
Expand All @@ -50,11 +46,10 @@ public function index(
}

public function profile(
CurrentRoute $currentRoute,
#[Route('login')] string $login,
ResponseFactoryInterface $responseFactory,
UserRepository $userRepository
): Response {
$login = $currentRoute->getArgument('login');
$item = $userRepository->findByLogin($login);

if ($item === null) {
Expand Down
2 changes: 1 addition & 1 deletion blog/src/Widget/FlashMessage.php
Expand Up @@ -14,7 +14,7 @@ public function __construct(private FlashInterface $flash)
{
}

public function run(): string
public function render(): string
{
$flashes = $this->flash->getAll();

Expand Down
4 changes: 2 additions & 2 deletions blog/src/Widget/OffsetPagination.php
Expand Up @@ -37,7 +37,7 @@ public function urlGenerator(Closure $generator): self

public function isRequired(): bool
{
return $this->paginator === null ? false : $this->paginator->isRequired();
return $this->paginator !== null && $this->paginator->isRequired();
}

/**
Expand All @@ -52,7 +52,7 @@ public function options(array $value): self
return $this;
}

protected function run(): string
public function render(): string
{
if ($this->paginator === null) {
return '';
Expand Down
2 changes: 1 addition & 1 deletion blog/src/Widget/PerformanceMetrics.php
Expand Up @@ -13,7 +13,7 @@ public function __construct(private Timer $timer)
{
}

protected function run(): string
public function render(): string
{
$time = round($this->timer->get('overall'), 4);
$memory = round(memory_get_peak_usage() / (1024 * 1024), 4);
Expand Down
2 changes: 1 addition & 1 deletion blog/yii
Expand Up @@ -8,5 +8,5 @@ use Yiisoft\Yii\Runner\Console\ConsoleApplicationRunner;
require_once __DIR__ . '/autoload.php';

// Run console application runner
$runner = new ConsoleApplicationRunner(__DIR__, $_ENV['YII_DEBUG'], $_ENV['YII_ENV']);
$runner = new ConsoleApplicationRunner(__DIR__, $_ENV['YII_DEBUG'], environment: $_ENV['YII_ENV']);
$runner->run();

0 comments on commit 00da52c

Please sign in to comment.