diff --git a/composer.json b/composer.json index e6e1ac4c..a1c95d73 100644 --- a/composer.json +++ b/composer.json @@ -99,11 +99,11 @@ "config/providers-console.php" ], "events": "config/events.php", - "events-web": [ + "events-web": [ "$events", "config/events-web.php" ], - "events-console": [ + "events-console": [ "$events", "config/events-console.php" ] diff --git a/config/common.php b/config/common.php index 606a1655..d1cdd748 100644 --- a/config/common.php +++ b/config/common.php @@ -1,22 +1,28 @@ [$params['mailer']['username']], 'setPassword()' => [$params['mailer']['password']], ], + + // Router: + RouteCollectorInterface::class => Group::create(), + UrlMatcherInterface::class => new AppRouterFactory(), + UrlGeneratorInterface::class => UrlGenerator::class, + MailerInterface::class => new MailerFactory($params['mailer']['writeToFiles']), Timer::class => $timer, ]; diff --git a/config/console.php b/config/console.php index cf6b592b..6b9a607e 100644 --- a/config/console.php +++ b/config/console.php @@ -8,7 +8,7 @@ return [ Aliases::class => [ - '__class' => Aliases::class, + '__class' => Aliases::class, '__construct()' => [$params['aliases']], ], ]; diff --git a/config/params.php b/config/params.php index 1d819d47..2aca9154 100644 --- a/config/params.php +++ b/config/params.php @@ -31,6 +31,7 @@ 'commands' => [ 'user/create' => Command\User\CreateCommand::class, 'fixture/add' => Command\Fixture\AddCommand::class, + 'router/list' => Command\Router\ListCommand::class, ], ], diff --git a/config/web.php b/config/web.php index b8798f49..ed222de4 100644 --- a/config/web.php +++ b/config/web.php @@ -1,15 +1,14 @@ HtmlDataResponseFormatter::class, DataResponseFactoryInterface::class => DataResponseFactory::class, - // Router: - RouteCollectorInterface::class => Group::create(), - UrlMatcherInterface::class => new AppRouterFactory(), - UrlGeneratorInterface::class => UrlGenerator::class, - MiddlewareDispatcher::class => new MiddlewareDispatcherFactory(), SessionInterface::class => [ '__class' => Session::class, diff --git a/public/js/app.js b/public/js/app.js index fbccbb63..34becd9f 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -1,5 +1,5 @@ // app.js -$(document).on('click', '.load-more-comment', function(event) { +$(document).on('click', '.load-more-comment', function (event) { event.preventDefault(); $.get($(this).attr('href'), function (data) { $('.load-more-comment-container').hide(); diff --git a/src/Blog/CommentController.php b/src/Blog/CommentController.php index bbdf2710..5dd294ff 100644 --- a/src/Blog/CommentController.php +++ b/src/Blog/CommentController.php @@ -11,11 +11,6 @@ final class CommentController extends Controller { - protected function getId(): string - { - return 'blog/comments'; - } - public function index(Request $request, CommentService $service): Response { $paginator = $service->getFeedPaginator(); @@ -34,4 +29,9 @@ private function isAjaxRequest(Request $request): bool { return $request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest'; } + + protected function getId(): string + { + return 'blog/comments'; + } } diff --git a/src/Command/Router/ListCommand.php b/src/Command/Router/ListCommand.php new file mode 100644 index 00000000..68913411 --- /dev/null +++ b/src/Command/Router/ListCommand.php @@ -0,0 +1,63 @@ +urlMatcher = $urlMatcher; + parent::__construct(); + } + + protected function configure(): void + { + $this + ->setDescription('List all registered routes') + ->setHelp('This command displays a list of registered routes.'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $table = new Table($output); + $routes = $this->urlMatcher->getRouteCollection()->getRoutes(); + uasort( + $routes, + static function ($a, $b) { + return ($a->getHost() <=> $b->getHost()) ?: ($a->getName() <=> $b->getName()); + } + ); + $table->setHeaders(['Host', 'Methods', 'Name', 'Pattern', 'Defaults']); + foreach ($routes as $route) { + $table->addRow( + [ + $route->getHost(), + implode(',', $route->getMethods()), + $route->getName(), + $route->getPattern(), + implode(',', $route->getDefaults()) + ] + ); + if (next($routes) !== false) { + $table->addRow(new TableSeparator()); + } + } + + $table->render(); + return ExitCode::OK; + } +} diff --git a/src/Controller.php b/src/Controller.php index 89bd0417..6c43fc87 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -6,8 +6,8 @@ use Yiisoft\Aliases\Aliases; use Yiisoft\View\ViewContextInterface; use Yiisoft\View\WebView; -use Yiisoft\Yii\Web\User\User; use Yiisoft\Yii\Web\Data\DataResponseFactoryInterface; +use Yiisoft\Yii\Web\User\User; abstract class Controller implements ViewContextInterface { @@ -33,7 +33,7 @@ public function __construct( protected function render(string $view, array $parameters = []): ResponseInterface { - $contentRenderer = fn () => $this->renderProxy($view, $parameters); + $contentRenderer = fn() => $this->renderProxy($view, $parameters); return $this->responseFactory->createResponse($contentRenderer); } diff --git a/src/Factory/AppRouterFactory.php b/src/Factory/AppRouterFactory.php index e5b1c4dd..aba37355 100644 --- a/src/Factory/AppRouterFactory.php +++ b/src/Factory/AppRouterFactory.php @@ -4,20 +4,17 @@ use App\Blog\Archive\ArchiveController; use App\Blog\BlogController; +use App\Blog\CommentController; use App\Blog\Post\PostController; use App\Blog\Tag\TagController; +use App\Contact\ContactController; use App\Controller\ApiInfo; use App\Controller\ApiUserController; use App\Controller\AuthController; -use App\Contact\ContactController; -use App\Blog\CommentController; use App\Controller\SignupController; use App\Controller\SiteController; use App\Controller\UserController; use App\Middleware\ApiDataWrapper; -use Yiisoft\Yii\Web\Data\Middleware\FormatDataResponse; -use Yiisoft\Yii\Web\Data\Middleware\FormatDataResponseAsJson; -use Yiisoft\Yii\Web\Data\Middleware\FormatDataResponseAsXml; use Psr\Container\ContainerInterface; use Yiisoft\Http\Method; use Yiisoft\Router\FastRoute\UrlMatcher; @@ -26,6 +23,9 @@ use Yiisoft\Router\RouteCollection; use Yiisoft\Router\RouteCollectorInterface; use Yiisoft\Yii\Web\Data\DataResponseFactoryInterface; +use Yiisoft\Yii\Web\Data\Middleware\FormatDataResponse; +use Yiisoft\Yii\Web\Data\Middleware\FormatDataResponseAsJson; +use Yiisoft\Yii\Web\Data\Middleware\FormatDataResponseAsXml; class AppRouterFactory { diff --git a/views/blog/archive/monthly-archive.php b/views/blog/archive/monthly-archive.php index 3918a064..8e1132bc 100644 --- a/views/blog/archive/monthly-archive.php +++ b/views/blog/archive/monthly-archive.php @@ -3,7 +3,7 @@ /** * @var int $year * @var int $month - * @var \Yiisoft\Data\Paginator\OffsetPaginator $paginator; + * @var \Yiisoft\Data\Paginator\OffsetPaginator $paginator * @var \Yiisoft\Router\UrlGeneratorInterface $urlGenerator * @var \Yiisoft\View\WebView $this */ @@ -15,11 +15,13 @@ $monthName = DateTime::createFromFormat('!m', $month)->format('F'); $pagination = OffsetPagination::widget() - ->paginator($paginator) - ->urlGenerator(fn ($page) => $urlGenerator->generate( - 'blog/archive/month', - ['year' => $year, 'month' => $month, 'page' => $page] - )); + ->paginator($paginator) + ->urlGenerator( + fn($page) => $urlGenerator->generate( + 'blog/archive/month', + ['year' => $year, 'month' => $month, 'page' => $page] + ) + ); ?>

Archive

diff --git a/views/blog/comments/_comments.php b/views/blog/comments/_comments.php index 4da128c0..a1d79b92 100644 --- a/views/blog/comments/_comments.php +++ b/views/blog/comments/_comments.php @@ -1,9 +1,9 @@ -

getTitle()) ?>

-
- getPublishedAt()->format('H:i:s d.m.Y') ?> by - getUser()->getLogin()), - $urlGenerator->generate('user/profile', ['login' => $item->getUser()->getLogin()]) -); - ?> -
+

getTitle()) ?>

+
+ getPublishedAt()->format('H:i:s d.m.Y') ?> by + getUser()->getLogin()), + $urlGenerator->generate('user/profile', ['login' => $item->getUser()->getLogin()]) + ); + ?> +
getContent()), ['class' => 'text-justify']); @@ -44,9 +44,9 @@
getUser()->getLogin()), - $urlGenerator->generate('user/profile', ['login' => $comment->getUser()->getLogin()]) - ) ?> + Html::encode($comment->getUser()->getLogin()), + $urlGenerator->generate('user/profile', ['login' => $comment->getUser()->getLogin()]) + ) ?> created at getCreatedAt()->format('H:i d.m.Y') ?> diff --git a/views/site/index.php b/views/site/index.php index 3f28aff2..1434042d 100644 --- a/views/site/index.php +++ b/views/site/index.php @@ -23,7 +23,6 @@ ?> -

Console

@@ -38,7 +37,7 @@

Migrations

- +