Skip to content
This repository was archived by the owner on Mar 4, 2026. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
9 changes: 8 additions & 1 deletion blog/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"cycle/orm": "^2.0",
"doctrine/collections": "^1.6",
"fakerphp/faker": "^1.14",
"olvlvl/composer-attribute-collector": "dev-main",
"httpsoft/http-message": "^1.1",
"php-http/guzzle7-adapter": "^1.0",
"psr/container": "^2.0",
Expand All @@ -34,6 +35,7 @@
"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 @@ -68,7 +70,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 @@ -159,10 +161,15 @@
"composer/package-versions-deprecated": true,
"infection/extension-installer": true,
"yiisoft/config": true,
"olvlvl/composer-attribute-collector": true,
"php-http/discovery": false
}
},
"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 @@ -37,11 +38,22 @@
use Yiisoft\Yii\RateLimiter\LimitRequestsMiddleware;
use Yiisoft\Yii\RateLimiter\Storage\StorageInterface;

$routes = [];
foreach (Attributes::findTargetMethods(Route::class) as $method) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about classes and groups?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just an example. If we want to merge it I'll migrate all routes and add groups and classes

/**
* @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,7 +4,11 @@

namespace App\Controller;

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

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`;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it disabled by default?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disabled what?

echo 'Dumped...' . PHP_EOL;
})
->start();