Skip to content

Commit

Permalink
Add an example how to deal with route attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Mar 27, 2023
1 parent 20e27ab commit 929a569
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 5 deletions.
1 change: 1 addition & 0 deletions blog/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Dotenv\Dotenv;

require_once __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/vendor/attributes.php';

$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
Expand Down
11 changes: 9 additions & 2 deletions blog/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
"doctrine/collections": "^1.6",
"fakerphp/faker": "^1.14",
"httpsoft/http-message": "^1.0.5",
"olvlvl/composer-attribute-collector": "dev-main",
"php-http/guzzle7-adapter": "^1.0",
"psr/container": "^2.0",
"psr/http-factory": "^1.0",
"psr/http-message": "^1.0",
"psr/http-server-handler": "^1.0",
"psr/http-server-middleware": "^1.0",
"psr/log": "^3.0",
"spatie/file-system-watcher": "^1.1",
"symfony/console": "^6.0",
"vlucas/phpdotenv": "^5.3",
"yiisoft/access": "^1.0",
Expand Down Expand Up @@ -63,7 +65,7 @@
"yiisoft/rbac": "^1.0",
"yiisoft/rbac-php": "^1.0",
"yiisoft/rbac-rules-container": "^2.0",
"yiisoft/router": "^3.0",
"yiisoft/router": "dev-attributes as 3.0",
"yiisoft/router-fastroute": "^3.0",
"yiisoft/security": "^1.0",
"yiisoft/session": "^2.0",
Expand Down Expand Up @@ -152,10 +154,15 @@
"composer/installers": true,
"composer/package-versions-deprecated": true,
"infection/extension-installer": true,
"yiisoft/config": true
"yiisoft/config": true,
"olvlvl/composer-attribute-collector": true
}
},
"repositories": [
{
"type": "git",
"url": "https://github.com/yiisoft/router"
},
{
"type": "composer",
"url": "https://asset-packagist.org"
Expand Down
18 changes: 15 additions & 3 deletions blog/config/common/routes/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use App\Middleware\ApiDataWrapper;
use App\User\Controller\ApiUserController;
use App\User\Controller\UserController;
use olvlvl\ComposerAttributeCollector\Attributes;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\Auth\Middleware\Authentication;
Expand All @@ -36,11 +37,22 @@
use Yiisoft\Yii\RateLimiter\LimitRequestsMiddleware;
use Yiisoft\Yii\RateLimiter\Storage\StorageInterface;

$routes = [];
foreach (Attributes::findTargetMethods(Route::class) as $method) {
/**
* @var $attribute Route
*/
$attribute = $method->attribute;
$routes[] = $attribute
->action([$method->class, $method->name]);
}

return [
...$routes,
// Lonely pages of site
Route::get('/')
->action([SiteController::class, 'index'])
->name('site/index'),
//Route::get('/')
// ->action([SiteController::class, 'index'])
// ->name('site/index'),
Route::methods([Method::GET, Method::POST], '/contact')
->action([ContactController::class, 'contact'])
->name('site/contact'),
Expand Down
37 changes: 37 additions & 0 deletions blog/src/Controller/SiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

namespace App\Controller;

use App\Middleware\ApiDataWrapper;
use Psr\Http\Message\ResponseInterface;
use Yiisoft\DataResponse\Middleware\FormatDataResponseAsJson;
use Yiisoft\DataResponse\Middleware\FormatDataResponseAsXml;
use Yiisoft\Yii\View\ViewRenderer;
use Yiisoft\Router\Route;

final class SiteController
{
Expand All @@ -14,8 +18,41 @@ public function __construct(private ViewRenderer $viewRenderer)
$this->viewRenderer = $viewRenderer->withController($this);
}

#[Route(
methods: ['GET'],
pattern: '/',
name: 'site/index',
)]
public function index(): ResponseInterface
{
return $this->viewRenderer->render('index');
}

#[Route(
methods: ['GET'],
pattern: '/json',
name: 'site/index.json',
middlewares: [
FormatDataResponseAsJson::class,
ApiDataWrapper::class,
],
)]
public function json(): ResponseInterface
{
return $this->viewRenderer->render('index');
}

#[Route(
methods: ['GET'],
pattern: '/xml',
name: 'site/index.xml',
middlewares: [
FormatDataResponseAsXml::class,
ApiDataWrapper::class,
],
)]
public function xml(): ResponseInterface
{
return $this->viewRenderer->render('index');
}
}
18 changes: 18 additions & 0 deletions blog/watcher-build.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

require_once __DIR__ . '/vendor/autoload.php';

$io = new \Composer\IO\NullIO();
$composer = \Composer\Factory::create($io);

echo 'dumping...' . PHP_EOL;

\olvlvl\ComposerAttributeCollector\Plugin::dump(
$composer,
$io,
__DIR__ . '/vendor/attributes.php',
);

echo 'done...' . PHP_EOL;
22 changes: 22 additions & 0 deletions blog/watcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

use Spatie\Watcher\Watch;

require_once __DIR__ . '/vendor/autoload.php';

echo 'Run watcher-build...' . PHP_EOL;

`php watcher-build.php`;

echo 'Ready for listening changes...' . PHP_EOL;

Watch::paths(__DIR__ . '/src')
->onAnyChange(function (string $type, string $path) {
echo sprintf('File changed: "%s".', $path) . PHP_EOL;
echo 'Run watcher-build...' . PHP_EOL;
`php watcher-build.php`;
echo 'Dumped...' . PHP_EOL;
})
->start();

0 comments on commit 929a569

Please sign in to comment.