PHPではアノテーションを最大限に使用します

バックエンド開発者である私は、私の魂のすべての繊維を備えたマイクロサービスアーキテクチャが大好きですが、さらに、マイクロサービスを開発するのが好きです。 開発するとき、それが何であれ、私は1つの単純な原則-ミニマリズムを固守します。 ミニマリズムとは、単純な真実を意味します。コードは可能な限り「透過的」である必要があり、最小限(理想的なコード-どのコードではない)である必要があるため、注釈を優先します。


この記事では、将来のアプリケーションのためにスケルトンを紹介します。このスケルトンでは、注釈を使用して以下の問題を解決します。



このようなスケルトンは、次のパッケージに基づいています。



また、このようなスケルトンは、次のPSR推奨事項に準拠したパッケージに基づいています。



この記事では、作曲家がボロボロのタマネギについて話しているわけではありません。特にPSRについては、ある程度この知識があることを前提としています。


読むのが面倒、何がありますか?
composer create-project sunrise/awesome-skeleton app 

ホイールバイク


コントローラー


原則として、使用するツールに関係なく、常に個別のルーティング、コントローラーを個別に使用します。SymfonyRoutingは今は使用していません。少し異なります。


コントローラーでのルーティング


以下の例を検討してください。


 declare(strict_types=1); namespace App\Http\Controller; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; /** * @Route( * id="resource.update", * path="/resource/{id<\d+>}", * methods={"PATCH"}, * before={ * "App\Http\Middleware\FooMiddleware", * "App\Http\Middleware\BarMiddleware" * }, * after={ * "App\Http\Middleware\BazMiddleware", * "App\Http\Middleware\QuxMiddleware" * } * ) */ class ResourceUpdateController implements MiddlewareInterface { /** * {@inheritDoc} */ public function process( ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface { $response = $handler->handle($request); // some code return $response; } } 

Zend Expressiveのように、コントローラーがミドルウェアであることにさらに気づいたでしょう。さらに、このコントローラーを起動する前に起動するミドルウェアと起動後に指定するミドルウェアを指定できるアノテーションを使用できます。


@Routeアノテーションには、次のプロパティが含まれる場合があります。



ルートパスには{id}構造が含まれますが、これは誰もが使い慣れていますが、属性を検証するには、正規表現{id <\ d +>}を指定する必要があります。 })


注釈のプロパティが厳密に検証されることに注意してください。正規表現が正しくない場合、またはミドルウェアが存在しない場合などはすぐに通知されます。


コントローラーでの依存性注入


以下の例を検討してください。


 declare(strict_types=1); namespace App\Http\Controller; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use Psr\Log\LoggerInterface; /** * @Route( * id="resource.update", * path="/resource/{id<\d+>}", * methods={"PATCH"} * ) */ class ResourceUpdateController implements MiddlewareInterface { /** * @Inject * @var LoggerInterface */ protected $logger; /** * {@inheritDoc} */ public function process( ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface { $this->logger->debug('foo bar'); $response = $handler->handle($request); // some code return $response; } } 

コンテナから依存関係を取得するよりも簡単なことはありません...


コントローラーをアプリケーションに登録する


コントローラーを作成するだけで、アプリケーションの残りの部分があなたのためにそれを行い、そのようなコントローラーを検出し、必要に応じてそれを開始するルーターに渡します... 箱からあなたに必要な主なことは、 src / Http / Controllerディレクトリにコントローラーを作成することです。


ホイールバイク


モデル


Doctrine ORMとSymfony Validatorを使って作業した場合、何も面白く設定されていないことを除いて、何もおもしろくありません。残りについては、いくつか例を示します。 src / Entityディレクトリにモデルを作成し、 App \ Entity \ AbstractEntityを継承する必要があることをすぐに示す必要があります。


モデルの簡単な例


以下の例を検討してください。


 declare(strict_types=1); namespace App\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; /** * @ORM\Entity * @ORM\HasLifecycleCallbacks * @ORM\Table(name="resource") */ class Resource extends AbstractEntity { /** * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") * @ORM\Column(type="integer") * * @var null|int */ protected $id; /** * @ORM\Column( * type="string", * length=128, * nullable=false * ) * * @Assert\NotBlank * @Assert\Type("string") * @Assert\Length(max=128) * * @var null|string */ protected $title; /** * @ORM\Column( * type="text", * nullable=false * ) * * @Assert\NotBlank * @Assert\Type("string") * * @var null|string */ protected $content; // setters and getters } 

繰り返しになりますが、モデルをどこにでも登録する必要はありません。検証やテーブルレイアウトをすべて1か所で簡単かつ明確に記述する必要はありません。 リソーステーブルを作成するには、アプリケーションルートから次のスクリプトを実行する必要があります。


 composer db:update 

データベースに接続するための設定はファイルにあります:src / config / environment.php


コントローラーでモデルを使用する簡単な例


 declare(strict_types=1); namespace App\Http\Controller; use App\Entity\Resource; use Doctrine\ORM\EntityManager; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; /** * @Route( * id="resource.create", * path="/resource", * methods={"POST"} * ) */ class ResourceCreateController implements MiddlewareInterface { /** * @Inject * * @var EntityManager */ protected $entityManager; /** * {@inheritDoc} */ public function process( ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface { $data = (array) $request->getParsedBody(); $response = $handler->handle($request); $resource = new Resource(); $resource->setTitle($data['title'] ?? null); $resource->setContent($data['content'] ?? null); $violations = $resource->validate(); if ($violations->count() > 0) { return $response->withStatus(400); } $this->entityManager->persist($resource); $this->entityManager->flush(); return $response->withStatus(201); } } 

@Assertアノテーションは検証に責任があり、検証ロジック自体はモデルによって継承されたAbstractEntityクラスに記述されています。


実装はデモ用であり、著者の目標はコードの行数を減らすことです...


ホイールバイク


アプリケーション設定


ファイル説明
config / cli-config.phpDoctrine CLI
config / container.phpPhp di
config / definition.phpアプリケーションの依存関係
config / environment.phpアプリケーション環境の構成

あなたの中毒


アプリケーションに新しい依存関係を追加するには、 config / definition.phpファイルを開き、既存の依存関係と同様に新しい依存関係を追加します。その後、この記事の例のように、注入によって利用可能になります。


推奨事項


スケルトンをインストールした後、 config / environment.phpファイルを.gitignoreに追加し、新しいファイル名でファイルを複製することをお勧めします。


 cp config/environment.php config/environment.php.example 

symfony / dotenvパッケージを使用して.envから環境設定を入力することにより、他の方法を使用できます


ホイールバイク


「これがあるときにそれを書く理由」というゲームは延々とプレイできますが、オープンソースはそれを立てることができるかもしれませんが...


スケルトンをインストールするには、次のコマンドを使用します。


 composer create-project sunrise/awesome-skeleton app 

スケルトンのソースコードを調べるには、次のリンクを使用してください: sunrise / awesome-skeleton




Oscar Oteroのオープンソースへの貢献、特にAwesome PSR-15ミドルウェアの優れた選択に感謝します。そのうちのいくつかはsunrise / awesome-skeletonに統合されています。



Source: https://habr.com/ru/post/J435434/


All Articles