From fc86f808800536e953fbfd198c03c81a00b4940b Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Fri, 6 Oct 2023 11:42:26 +0300 Subject: [PATCH] Blog: Fix locale, routes and npm dependencies (#597) --- .github/workflows/blog-api_build.yml | 1 + .github/workflows/blog-api_dependency.yml | 1 + .github/workflows/blog-api_static.yml | 1 + .github/workflows/blog_build.yml | 5 +++++ .github/workflows/blog_dependency.yml | 1 + .github/workflows/blog_static.yml | 1 + blog/.editorconfig | 4 ++++ blog/.gitignore | 1 + blog/Makefile | 20 +++++++++++++++++++ blog/config/common/di/router.php | 22 +++++++++++++++++++-- blog/config/web/events.php | 7 ++++++- blog/configuration.php | 2 +- blog/package.json | 10 ++++++++++ blog/src/User/Controller/UserController.php | 2 +- 14 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 blog/Makefile create mode 100644 blog/package.json diff --git a/.github/workflows/blog-api_build.yml b/.github/workflows/blog-api_build.yml index 9ad9ca26c..2e9e3a62a 100644 --- a/.github/workflows/blog-api_build.yml +++ b/.github/workflows/blog-api_build.yml @@ -14,6 +14,7 @@ on: - 'blog-api/psalm.xml' push: + branches: ['master'] paths-ignore: - 'blog-api/docs/**' - 'blog-api/README.md' diff --git a/.github/workflows/blog-api_dependency.yml b/.github/workflows/blog-api_dependency.yml index ed73b9d87..47f523773 100644 --- a/.github/workflows/blog-api_dependency.yml +++ b/.github/workflows/blog-api_dependency.yml @@ -14,6 +14,7 @@ on: - 'blog-api/psalm.xml' push: + branches: ['master'] paths-ignore: - 'blog-api/docs/**' - 'blog-api/README.md' diff --git a/.github/workflows/blog-api_static.yml b/.github/workflows/blog-api_static.yml index 84059dd27..047577c97 100644 --- a/.github/workflows/blog-api_static.yml +++ b/.github/workflows/blog-api_static.yml @@ -14,6 +14,7 @@ on: - 'blog-api/phpunit.xml.dist' push: + branches: ['master'] paths-ignore: - 'blog-api/docs/**' - 'blog-api/README.md' diff --git a/.github/workflows/blog_build.yml b/.github/workflows/blog_build.yml index 4a010ae65..5bbfbdc06 100644 --- a/.github/workflows/blog_build.yml +++ b/.github/workflows/blog_build.yml @@ -14,6 +14,7 @@ on: - 'blog/psalm.xml' push: + branches: ['master'] paths-ignore: - 'blog/docs/**' - 'blog/README.md' @@ -94,6 +95,10 @@ jobs: run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi working-directory: ${{ env.working_directory }} + - name: Install dependencies with npm + run: npm update + working-directory: ${{ env.working_directory }} + - name: Run tests codeception if: matrix.os != 'ubuntu-latest' || matrix.php != '8.0' run: vendor/bin/codecept run diff --git a/.github/workflows/blog_dependency.yml b/.github/workflows/blog_dependency.yml index 3a2c9afd9..08c7d5742 100644 --- a/.github/workflows/blog_dependency.yml +++ b/.github/workflows/blog_dependency.yml @@ -14,6 +14,7 @@ on: - 'blog/psalm.xml' push: + branches: ['master'] paths-ignore: - 'blog/docs/**' - 'blog/README.md' diff --git a/.github/workflows/blog_static.yml b/.github/workflows/blog_static.yml index d0ab289a6..6e5c3a323 100644 --- a/.github/workflows/blog_static.yml +++ b/.github/workflows/blog_static.yml @@ -14,6 +14,7 @@ on: - 'blog/phpunit.xml.dist' push: + branches: ['master'] paths-ignore: - 'blog/docs/**' - 'blog/README.md' diff --git a/blog/.editorconfig b/blog/.editorconfig index 5e9a93ea5..8802775ba 100644 --- a/blog/.editorconfig +++ b/blog/.editorconfig @@ -10,6 +10,10 @@ indent_style = space indent_size = 4 trim_trailing_whitespace = true +[*.php] +ij_php_space_before_short_closure_left_parenthesis = false +ij_php_space_after_type_cast = true + [*.md] trim_trailing_whitespace = false diff --git a/blog/.gitignore b/blog/.gitignore index 94819d1f3..e1bda997b 100644 --- a/blog/.gitignore +++ b/blog/.gitignore @@ -25,6 +25,7 @@ phpunit.phar /vendor /composer.lock /node_modules +/package-lock.json # Codeception c3.php diff --git a/blog/Makefile b/blog/Makefile new file mode 100644 index 000000000..5cc895d70 --- /dev/null +++ b/blog/Makefile @@ -0,0 +1,20 @@ +init: composer-update npm-update up + +up: + docker-compose up -d +down: + docker-compose down + +composer: + docker-compose run php composer $(filter-out $@, $(MAKECMDGOALS)) +composer-update: + docker-compose run php composer update + npm i bootstrap@5.3.1 + +npm: + docker-compose run php npm $(filter-out $@, $(MAKECMDGOALS)) +npm-update: + docker-compose run php npm update + +yii3: + docker-compose run php ./yii $(filter-out $@, $(MAKECMDGOALS)) diff --git a/blog/config/common/di/router.php b/blog/config/common/di/router.php index d192a6d17..3cbc11477 100644 --- a/blog/config/common/di/router.php +++ b/blog/config/common/di/router.php @@ -5,22 +5,40 @@ use Yiisoft\Config\Config; use Yiisoft\Csrf\CsrfMiddleware; use Yiisoft\DataResponse\Middleware\FormatDataResponse; +use Yiisoft\Router\FastRoute\UrlGenerator; use Yiisoft\Router\Group; use Yiisoft\Router\RouteCollection; use Yiisoft\Router\RouteCollectionInterface; use Yiisoft\Router\RouteCollectorInterface; +use Yiisoft\Router\UrlGeneratorInterface; use Yiisoft\Yii\Debug\Viewer\Middleware\ToolbarMiddleware; -/** @var Config $config */ +/** + * @var Config $config + * @var array $params + */ return [ + UrlGeneratorInterface::class => [ + 'class' => UrlGenerator::class, + 'setEncodeRaw()' => [$params['yiisoft/router-fastroute']['encodeRaw']], + 'setDefaultArgument()' => ['_language', 'en'], + 'reset' => function () { + $this->defaultArguments = ['_language', 'en']; + }, + ], + RouteCollectionInterface::class => static function (RouteCollectorInterface $collector) use ($config) { $collector ->middleware(CsrfMiddleware::class) ->middleware(FormatDataResponse::class) ->addGroup( - Group::create('/{_language}') + Group::create() ->routes(...$config->get('routes')) + ) + ->addGroup( + Group::create('/{_language}') + ->routes(...$config->get('app-routes')) ); if (!str_starts_with(getenv('YII_ENV') ?: '', 'prod')) { diff --git a/blog/config/web/events.php b/blog/config/web/events.php index fffb00e90..c8373efd0 100644 --- a/blog/config/web/events.php +++ b/blog/config/web/events.php @@ -3,10 +3,15 @@ declare(strict_types=1); use App\Timer; +use Yiisoft\Translator\TranslatorInterface; use Yiisoft\Yii\Http\Event\ApplicationStartup; +use Yiisoft\Yii\Middleware\Event\SetLocaleEvent; return [ ApplicationStartup::class => [ - static fn (Timer $timer) => $timer->start('overall'), + static fn(Timer $timer) => $timer->start('overall'), + ], + SetLocaleEvent::class => [ + static fn(TranslatorInterface $translator, SetLocaleEvent $event) => $translator->setLocale($event->getLocale()), ], ]; diff --git a/blog/configuration.php b/blog/configuration.php index 20520fe03..918ce8a47 100644 --- a/blog/configuration.php +++ b/blog/configuration.php @@ -44,7 +44,7 @@ '$events', 'console/events.php', ], - 'routes' => [ + 'app-routes' => [ 'common/routes/*.php', ], 'bootstrap' => [ diff --git a/blog/package.json b/blog/package.json new file mode 100644 index 000000000..ec1ad179b --- /dev/null +++ b/blog/package.json @@ -0,0 +1,10 @@ +{ + "name": "yii3-demo-blog", + "version": "1.0.0", + "description": "Yii3 Demo Blog", + "dependencies": { + "bootstrap": "^5.3.1" + }, + "devDependencies": {}, + "license": "BSD-3-Clause" +} diff --git a/blog/src/User/Controller/UserController.php b/blog/src/User/Controller/UserController.php index 3174bb79c..60d9a2927 100644 --- a/blog/src/User/Controller/UserController.php +++ b/blog/src/User/Controller/UserController.php @@ -25,7 +25,7 @@ public function __construct(private ViewRenderer $viewRenderer) public function index( UserRepository $userRepository, - #[Body] array $body, + #[Body] ?array $body, #[Query] array $sortOrder, #[Route('page')] int $page = 1, #[Route('pagesize')] int $pageSize = null,