Windowsで作る - PHPプログラミングの開発環境
このページへのアクセス 今日: 2 / 昨日: 1 総計: 3254
文書の過去の版を表示しています。
— y2sunlight 2020-05-25
本章は、若干の補足を加筆してはいるものの単にPSRのサイトを翻訳したものに過ぎません。英語が堪能な方は原文をご参照下さい。翻訳に当たっては、基本的に機械翻訳を使い、理解できない部分は独断で意訳しております。拙い訳では御座いますが恥を忍んで投稿しておりますので、ご指摘など御座いましたらコメントを頂ければ幸いです。
— // 原文より翻訳 PSR-11: Container interface 2020-05-25 現在 //
このドキュメントでは、依存性注入コンテナの一般的なインターフェースについて説明します。
ContainerInterface が設定する目標は、フレームワークとライブラリがコンテナーを使用してオブジェクトとパラメーター(以下では「エントリ」と呼びます)を取得する方法を標準化することです。
このドキュメントのキーワード MUST , MUST NOT , REQUIRED , SHALL , SHALL NOT , SHOULD , SHOULD NOT , RECOMMENDED , MAY 及び OPTIONAL は、 RFC 2119で説明されているように解釈して下さい。
MUST
MUST NOT
REQUIRED
SHALL
SHALL NOT
SHOULD
SHOULD NOT
RECOMMENDED
MAY
OPTIONAL
RFC 2119の説明 MUST, REQUIRED, SHALL — 絶対必要 MUST NOT, SHALL NOT — 絶対禁止 SHOULD, RECOMMENDED — 推奨(但し、無視できる特定の正当な理由が存在するかもしれない) SHOULD NOT — 推奨できない(但し、許可できる特定の正当な理由が存在するかもしれない) MAY, OPTIONAL — オプション
このドキュメントの「インプリメンター」という言葉は、依存性注入に関連するライブラリまたはフレームワークに ContainerInterface を実装している人のことと解釈して下さい。依存性注入コンテナー(DIC)の利用者は、ユーザーと呼びます。
エントリ識別子は、少なくとも1文字のPHPの正当な文字列で、コンテナ内のアイテムを一意に識別します。 エントリ識別子は不透明(opaque)な文字列であるため、呼び出し元は文字列の構造に意味的な物があると仮定してはなりません ( SHOULD NOT )。
get
has
Exceptions directly thrown by the container SHOULD implement the Psr\Container\ContainerExceptionInterface.
コンテナによって直接スローされる例外は、Psr\Container\ContainerExceptionInterfaceを実装すべきです (SHOULD)。
A call to the get method with a non-existing id MUST throw a Psr\Container\NotFoundExceptionInterface.
存在しないIDでgetメソッドを呼び出すと、Psr\Container\NotFoundExceptionInterfaceがスローされる必要があります (MUST)。
Users SHOULD NOT pass a container into an object so that the object can retrieve its own dependencies. This means the container is used as a Service Locator which is a pattern that is generally discouraged.
オブジェクトが独自の依存関係を取得できるように、ユーザーはコンテナをオブジェクトに渡すべきではありません (SHOULD NOT)。 つまり、コンテナーをサービスロケーターとして使用することは、一般的には推奨されないパターンです。
Please refer to section 4 of the META document for more details.
詳細については、METAドキュメントのセクション4を参照してください。
The interfaces and classes described as well as relevant exceptions are provided as part of the psr/container package.
説明されているインターフェースとクラス、および関連する例外は、psr/containerパッケージの一部として提供されます。
Packages providing a PSR container implementation should declare that they provide psr/container-implementation 1.0.0.
PSRコンテナーの実装を提供するパッケージは、psr/container-implementation 1.0.0を提供することを宣言する必要があります。
Projects requiring an implementation should require psr/container-implementation 1.0.0.
実装が必要なプロジェクトでは、psr/container-implementation 1.0.0をrequireする必要があります。
<?php namespace Psr\Container; /** * Describes the interface of a container that exposes methods to read its entries. * エントリを読み取るメソッドを公開するコンテナのインターフェースについて説明します。 */ interface ContainerInterface { /** * Finds an entry of the container by its identifier and returns it. * 識別子によってコンテナのエントリを検索して返します。 * * @param string $id Identifier of the entry to look for. * 検索するエントリの識別子 * * @throws NotFoundExceptionInterface No entry was found for **this** identifier. * この識別子のエントリが見つかりませんでした。 * @throws ContainerExceptionInterface Error while retrieving the entry. * エントリの取得中にエラーが発生しました。 * * @return mixed Entry. * エントリ */ public function get($id); /** * Returns true if the container can return an entry for the given identifier. * Returns false otherwise. * コンテナが指定された識別子のエントリを返すことができる場合はtrueを返します。 * それ以外の場合はfalseを返します。 * * `has($id)` returning true does not mean that `get($id)` will not throw an exception. * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. * has($id)がtrueを返すことは、get($id)が例外をスローしないことを意味しません。 * ただし、get($id)はNotFoundExceptionInterfaceをスローしないことは意味します。 * * @param string $id Identifier of the entry to look for. * 検索するエントリの識別子 * * @return bool */ public function has($id); }
<?php namespace Psr\Container; /** * Base interface representing a generic exception in a container. * コンテナの一般的な例外を表す基本インターフェース。 */ interface ContainerExceptionInterface { }
<?php namespace Psr\Container; /** * No entry was found in the container. * コンテナにエントリが見つかりませんでした。 */ interface NotFoundExceptionInterface extends ContainerExceptionInterface { }