Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add route/list command #96

Merged
merged 34 commits into from May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
f9e6e3f
use `auth` package
rustamwin Nov 15, 2019
ada6934
Merge branch 'master' of https://github.com/yiisoft/yii-demo
rustamwin Nov 27, 2019
44ab216
Merge branch 'master' of https://github.com/yiisoft/yii-demo
rustamwin Nov 29, 2019
3e81a5a
Merge branch 'master' of https://github.com/yiisoft/yii-demo
rustamwin Dec 3, 2019
3b82211
Merge branch 'master' of https://github.com/yiisoft/yii-demo
rustamwin Dec 6, 2019
a89b836
Merge branch 'master' of https://github.com/yiisoft/yii-demo
rustamwin Dec 20, 2019
0d0fea7
Merge branch 'master' of https://github.com/yiisoft/yii-demo
rustamwin Dec 21, 2019
3ff5e92
Merge remote-tracking branch 'origin/master'
rustamwin Dec 21, 2019
72013f4
Update Docker container to PHP 7.4
rezaei121 Dec 30, 2019
3d444f6
Subfolder Middleware Renamed (#38)
roxblnfk Dec 31, 2019
d2ecea2
Merge branch 'master' of https://github.com/yiisoft/yii-demo
rustamwin Jan 4, 2020
bea3c7d
Merge branch 'master' of https://github.com/yiisoft/yii-demo
rustamwin Jan 11, 2020
f76d6b2
Merge branch 'master' of https://github.com/yiisoft/yii-demo
rustamwin Jan 25, 2020
93456da
Merge branch 'master' of https://github.com/yiisoft/yii-demo
rustamwin Jan 26, 2020
10e1ca0
Merge branch 'master' of https://github.com/yiisoft/yii-demo
rustamwin Jan 27, 2020
5da8c53
Merge branch 'master' of https://github.com/yiisoft/yii-demo
rustamwin Feb 1, 2020
a11a1ba
Merge branch 'master' of https://github.com/yiisoft/yii-demo
rustamwin Feb 3, 2020
babd26a
Merge branch 'master' of github.com:rustamwin/yii-demo
rustamwin Feb 8, 2020
72da2d8
Merge branch 'master' of https://github.com/yiisoft/yii-demo
rustamwin Feb 8, 2020
5188936
Merge branch 'master' of https://github.com/yiisoft/yii-demo
rustamwin Mar 3, 2020
e92b828
Merge branch 'master' of https://github.com/yiisoft/yii-demo
rustamwin Mar 13, 2020
e8527e0
Merge branch 'master' of https://github.com/yiisoft/yii-demo
rustamwin Mar 23, 2020
47510f1
Merge branch 'master' of https://github.com/yiisoft/yii-demo
rustamwin Apr 1, 2020
14cac50
Merge branch 'master' of https://github.com/yiisoft/yii-demo
rustamwin Apr 5, 2020
bd8aa38
Merge branch 'master' of https://github.com/yiisoft/yii-demo
rustamwin Apr 25, 2020
fc4af96
Merge branch 'master' of https://github.com/yiisoft/yii-demo
rustamwin Apr 30, 2020
a0f359b
Add route/list command
rustamwin May 7, 2020
c5319f4
fix
rustamwin May 7, 2020
b4939b2
type hints
rustamwin May 7, 2020
9a3a247
fixes
rustamwin May 12, 2020
04acd49
fixes
rustamwin May 12, 2020
a57b4b3
fix naming
rustamwin May 14, 2020
474a12a
Merge branch 'master' of https://github.com/yiisoft/yii-demo into rou…
rustamwin May 16, 2020
1c410a1
Minor formatting fixes
samdark May 18, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -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"
]
Expand Down
26 changes: 19 additions & 7 deletions config/common.php
@@ -1,22 +1,28 @@
<?php

use App\Factory\AppRouterFactory;
use App\Factory\LoggerFactory;
use App\Factory\MailerFactory;
use App\Timer;
use Psr\Log\LoggerInterface;
use Yiisoft\Aliases\Aliases;
use Yiisoft\Cache\File\FileCache;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\EventDispatcher\ListenerProviderInterface;
use Psr\Log\LoggerInterface;
use Psr\SimpleCache\CacheInterface;
use Yiisoft\Aliases\Aliases;
use Yiisoft\Cache\Cache;
use Yiisoft\Cache\CacheInterface as YiiCacheInterface;
use Yiisoft\Cache\File\FileCache;
use Yiisoft\EventDispatcher\Dispatcher\Dispatcher;
use Yiisoft\EventDispatcher\Provider\Provider;
use Yiisoft\Log\Target\File\FileRotator;
use Yiisoft\Log\Target\File\FileRotatorInterface;
use Yiisoft\Mailer\MailerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\EventDispatcher\ListenerProviderInterface;
use Yiisoft\EventDispatcher\Dispatcher\Dispatcher;
use Yiisoft\EventDispatcher\Provider\Provider;
use Yiisoft\Router\FastRoute\UrlGenerator;
use Yiisoft\Router\Group;
use Yiisoft\Router\RouteCollectorInterface;
use Yiisoft\Router\UrlGeneratorInterface;
use Yiisoft\Router\UrlMatcherInterface;

/**
* @var array $params
Expand Down Expand Up @@ -57,6 +63,12 @@
'setUsername()' => [$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,
];
2 changes: 1 addition & 1 deletion config/console.php
Expand Up @@ -8,7 +8,7 @@

return [
Aliases::class => [
'__class' => Aliases::class,
'__class' => Aliases::class,
'__construct()' => [$params['aliases']],
],
];
1 change: 1 addition & 0 deletions config/params.php
Expand Up @@ -31,6 +31,7 @@
'commands' => [
'user/create' => Command\User\CreateCommand::class,
'fixture/add' => Command\Fixture\AddCommand::class,
'router/list' => Command\Router\ListCommand::class,
],
],

Expand Down
29 changes: 8 additions & 21 deletions config/web.php
@@ -1,38 +1,30 @@
<?php

use App\Blog\Comment\CommentRepository;
use App\Blog\Comment\CommentService;
use App\Blog\Entity\Comment;
use App\Contact\ContactMailer;
use Yiisoft\Mailer\MailerInterface;
use Yiisoft\Yii\Web\Data\DataResponseFormatterInterface;
use Yiisoft\Yii\Web\Data\Formatter\HtmlDataResponseFormatter;
use App\Factory\AppRouterFactory;
use App\Factory\MiddlewareDispatcherFactory;
use App\Factory\ViewFactory;
use Cycle\ORM\ORMInterface;
use Nyholm\Psr7\Factory\Psr17Factory;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UploadedFileFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
use Yiisoft\Auth\IdentityRepositoryInterface;
use Yiisoft\Router\FastRoute\UrlGenerator;
use Yiisoft\Router\Group;
use Yiisoft\Router\RouteCollectorInterface;
use Yiisoft\Router\UrlGeneratorInterface;
use Yiisoft\Router\UrlMatcherInterface;
use Yiisoft\Mailer\MailerInterface;
use Yiisoft\View\WebView;
use Yiisoft\Yii\Web\Data\DataResponseFactoryInterface;
use Yiisoft\Yii\Web\Data\DataResponseFactory;
use Yiisoft\Yii\Web\Data\DataResponseFactoryInterface;
use Yiisoft\Yii\Web\Data\DataResponseFormatterInterface;
use Yiisoft\Yii\Web\Data\Formatter\HtmlDataResponseFormatter;
use Yiisoft\Yii\Web\MiddlewareDispatcher;
use Yiisoft\Yii\Web\Session\Session;
use Yiisoft\Yii\Web\Session\SessionInterface;
use Yiisoft\Yii\Web\User\User;
use App\Blog\Comment\CommentService;
use Cycle\ORM\ORMInterface;
use App\Blog\Entity\Comment;
use App\Blog\Comment\CommentRepository;

/**
* @var array $params
Expand All @@ -49,11 +41,6 @@
DataResponseFormatterInterface::class => 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,
Expand Down
2 changes: 1 addition & 1 deletion 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();
Expand Down
10 changes: 5 additions & 5 deletions src/Blog/CommentController.php
Expand Up @@ -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();
Expand All @@ -34,4 +29,9 @@ private function isAjaxRequest(Request $request): bool
{
return $request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest';
}

protected function getId(): string
{
return 'blog/comments';
}
}
63 changes: 63 additions & 0 deletions src/Command/Router/ListCommand.php
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace App\Command\Router;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Yiisoft\Router\UrlMatcherInterface;
use Yiisoft\Yii\Console\ExitCode;

class ListCommand extends Command
{
private UrlMatcherInterface $urlMatcher;

protected static $defaultName = 'router/list';

public function __construct(UrlMatcherInterface $urlMatcher)
{
$this->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;
}
}
4 changes: 2 additions & 2 deletions src/Controller.php
Expand Up @@ -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
{
Expand All @@ -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);
}
Expand Down
10 changes: 5 additions & 5 deletions src/Factory/AppRouterFactory.php
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down
14 changes: 8 additions & 6 deletions views/blog/archive/monthly-archive.php
Expand Up @@ -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
*/
Expand All @@ -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]
)
);
?>
<h1>Archive <small class="text-muted"><?= "$monthName $year" ?></small></h1>
<div class="row">
Expand Down
2 changes: 1 addition & 1 deletion views/blog/comments/_comments.php
@@ -1,9 +1,9 @@
<?php

use Yiisoft\Assets\AssetManager;
use Yiisoft\Data\Paginator\KeysetPaginator;
use Yiisoft\Html\Html;
use Yiisoft\Router\UrlGeneratorInterface;
use Yiisoft\Assets\AssetManager;

/**
* @var KeysetPaginator $data
Expand Down
2 changes: 1 addition & 1 deletion views/blog/comments/index.php
@@ -1,8 +1,8 @@
<?php

use Yiisoft\Assets\AssetManager;
use Yiisoft\Data\Paginator\KeysetPaginator;
use Yiisoft\Router\UrlGeneratorInterface;
use Yiisoft\Assets\AssetManager;

/**
* @var KeysetPaginator $data
Expand Down
26 changes: 13 additions & 13 deletions views/blog/post/index.php
Expand Up @@ -9,16 +9,16 @@
use Yiisoft\Html\Html;

?>
<h1><?= Html::encode($item->getTitle()) ?></h1>
<div>
<span class="text-muted"><?= $item->getPublishedAt()->format('H:i:s d.m.Y') ?> by</span>
<?php
echo Html::a(
Html::encode($item->getUser()->getLogin()),
$urlGenerator->generate('user/profile', ['login' => $item->getUser()->getLogin()])
);
?>
</div>
<h1><?= Html::encode($item->getTitle()) ?></h1>
<div>
<span class="text-muted"><?= $item->getPublishedAt()->format('H:i:s d.m.Y') ?> by</span>
<?php
echo Html::a(
Html::encode($item->getUser()->getLogin()),
$urlGenerator->generate('user/profile', ['login' => $item->getUser()->getLogin()])
);
?>
</div>
<?php

echo Html::tag('article', Html::encode($item->getContent()), ['class' => 'text-justify']);
Expand All @@ -44,9 +44,9 @@
<div class="media-body">
<div>
<?= Html::a(
Html::encode($comment->getUser()->getLogin()),
$urlGenerator->generate('user/profile', ['login' => $comment->getUser()->getLogin()])
) ?>
Html::encode($comment->getUser()->getLogin()),
$urlGenerator->generate('user/profile', ['login' => $comment->getUser()->getLogin()])
) ?>
<span class="text-muted">
<i>created at</i> <?= $comment->getCreatedAt()->format('H:i d.m.Y') ?>
</span>
Expand Down
3 changes: 1 addition & 2 deletions views/site/index.php
Expand Up @@ -23,7 +23,6 @@
?>



<div class="card mt-3 col-md-6">
<div class="card-body">
<h2 class="card-title">Console</h2>
Expand All @@ -38,7 +37,7 @@
</div>
<h4 class="card-title text-muted">Migrations</h4>
<div>
<code><?= "{$binPath} migrate/create" ?></code>
<code><?= "{$binPath} migrate/create" ?></code>
<br><code><?= "{$binPath} migrate/generate" ?></code>
<br><code><?= "{$binPath} migrate/up" ?></code>
<br><code><?= "{$binPath} migrate/down" ?></code>
Expand Down