このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン 次のリビジョン 両方とも次のリビジョン | ||
psr:psr11 [2020/06/24 08:56] y2sunlight [PSR-11: コンテナインターフェース] |
psr:psr11 [2020/07/28 14:04] tanaka [PSR-11: Container interface] |
||
---|---|---|---|
行 3: | 行 3: | ||
--- // | --- // | ||
- | 本章は、若干の補足を加筆してはいるものの単に[[https:// | + | 本章は、若干の補足を加筆してはいるものの単に[[https:// |
==== 目次 ==== | ==== 目次 ==== | ||
行 14: | 行 14: | ||
* [[psr: | * [[psr: | ||
* PSR-11: Container Interface - コンテナインターフェイス | * PSR-11: Container Interface - コンテナインターフェイス | ||
- | * [[psr: | + | * [[psr: |
* [[psr: | * [[psr: | ||
* [[psr: | * [[psr: | ||
- | * [[psr: | + | * [[psr: |
----- | ----- | ||
行 38: | 行 38: | ||
> '' | > '' | ||
- | このドキュメントの「インプリメンター」という言葉は、依存性注入に関連するライブラリまたはフレームワークに ContainerInterface を実装している人のことと解釈して下さい。依存性注入コンテナー(DIC)の利用者は、ユーザーと呼びます。 | + | このドキュメントの「インプリメンター」という言葉は、依存性注入に関連するライブラリまたはフレームワークに |
\\ | \\ | ||
行 49: | 行 49: | ||
=== 1.1.1 エントリ識別子 === | === 1.1.1 エントリ識別子 === | ||
- | An entry identifier is any PHP-legal string of at least one character that uniquely identifies an item within a container. An entry identifier is an opaque string, so callers SHOULD NOT assume that the structure of the string carries any semantic meaning. | + | エントリ識別子は、少なくとも1文字のPHPの正当な文字列で、コンテナ内のアイテムを一意に識別します。 エントリ識別子は不透明(opaque)な文字列であるため、呼び出し元は文字列の構造に意味的な物があると仮定してはなりません ( '' |
- | + | ||
- | エントリ識別子は、少なくとも1文字のPHPの正当な文字列で、コンテナ内のアイテムを一意に識別します。 エントリ識別子は不透明(opaque)な文字列であるため、呼び出し元は文字列の構造に意味的な物があると仮定してはなりません ('' | + | |
=== 1.1.2 Reading from a containerコンテナーからの読み取り === | === 1.1.2 Reading from a containerコンテナーからの読み取り === | ||
- | * The Psr\Container\ContainerInterface exposes two methods: get and has.\\ \\ Psr\Container\ContainerInterfaceは、getとhasの2つのメソッドを公開しています。 | + | * Psr\Container\ContainerInterfaceは、'' |
- | * get takes one mandatory parameter: an entry identifier, which MUST be a string. get can return anything (a mixed value), or throw a NotFoundExceptionInterface if the identifier is not known to the container. Two successive calls to get with the same identifier SHOULD return the same value. However, depending on the implementor design and/or user configuration, | + | * '' |
- | * has takes one unique parameter: an entry identifier, which MUST be a string. has MUST return true if an entry identifier is known to the container and false if it is not. If has($id) returns false, get($id) MUST throw a NotFoundExceptionInterface.\\ \\ hasは、一意のパラメータを1つ取ります:それはエントリ識別子で文字列でなければなりません ('' | + | * '' |
\\ | \\ | ||
行 65: | 行 63: | ||
==== 1.2 例外 ===== | ==== 1.2 例外 ===== | ||
- | Exceptions directly thrown by the container SHOULD implement the Psr\Container\ContainerExceptionInterface. | + | コンテナによって直接スローされる例外は [[# |
- | コンテナによって直接スローされる例外は、Psr\Container\ContainerExceptionInterfaceを実装すべきです ('' | + | 存在しないIDでgetメソッドを呼び出すと、[[# |
- | + | ||
- | A call to the get method with a non-existing id MUST throw a Psr\Container\NotFoundExceptionInterface. | + | |
- | + | ||
- | 存在しないIDでgetメソッドを呼び出すと、Psr\Container\NotFoundExceptionInterfaceがスローされる必要があります ('' | + | |
\\ | \\ | ||
行 77: | 行 71: | ||
==== 1.3 推奨される使用法 ===== | ==== 1.3 推奨される使用法 ===== | ||
- | 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. | + | オブジェクトが独自の依存関係を取得できるように、ユーザーはコンテナをオブジェクトに渡すべきではありません ( '' |
- | オブジェクトが独自の依存関係を取得できるように、ユーザーはコンテナをオブジェクトに渡すべきではありません ('' | + | 詳細については、METAドキュメントの[[https:// |
- | + | ||
- | Please refer to section 4 of the META document for more details. | + | |
- | + | ||
- | 詳細については、METAドキュメントのセクション4を参照してください。 | + | |
\\ | \\ | ||
行 89: | 行 79: | ||
===== 2. パッケージ ===== | ===== 2. パッケージ ===== | ||
- | The interfaces and classes described as well as relevant exceptions are provided as part of the psr/ | + | 説明されているインターフェースとクラス、および関連する例外は、[[https:// |
- | + | ||
- | 説明されているインターフェースとクラス、および関連する例外は、psr/ | + | |
- | + | ||
- | Packages providing a PSR container implementation should declare that they provide psr/ | + | |
- | + | ||
- | PSRコンテナーの実装を提供するパッケージは、psr/ | + | |
- | Projects requiring an implementation should require | + | PSRコンテナーの実装を提供するパッケージは、'' |
- | 実装が必要なプロジェクトでは、psr/ | + | 実装が必要なプロジェクトでは、'' |
\\ | \\ | ||
行 121: | 行 105: | ||
* 識別子によってコンテナのエントリを検索して返します。 | * 識別子によってコンテナのエントリを検索して返します。 | ||
* | * | ||
- | * @param string $id Identifier of the entry to look for. | + | * @param string $id 検索するエントリの識別子 |
- | * 検索するエントリの識別子 | + | |
* | * | ||
- | * @throws NotFoundExceptionInterface | + | * @throws NotFoundExceptionInterface |
- | * この識別子のエントリが見つかりませんでした。 | + | * @throws ContainerExceptionInterface エントリの取得中にエラーが発生しました |
- | * @throws ContainerExceptionInterface | + | |
- | * エントリの取得中にエラーが発生しました。 | + | |
* | * | ||
- | * @return mixed Entry. | + | * @return mixed エントリ |
- | | + | |
*/ | */ | ||
public function get($id); | public function get($id); | ||
行 137: | 行 117: | ||
* Returns true if the container can return an entry for the given identifier. | * Returns true if the container can return an entry for the given identifier. | ||
* Returns false otherwise. | * Returns false otherwise. | ||
+ | | ||
* コンテナが指定された識別子のエントリを返すことができる場合はtrueを返します。 | * コンテナが指定された識別子のエントリを返すことができる場合はtrueを返します。 | ||
* それ以外の場合はfalseを返します。 | * それ以外の場合はfalseを返します。 | ||
行 142: | 行 123: | ||
* `has($id)` returning true does not mean that `get($id)` will not throw an exception. | * `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`. | * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. | ||
+ | | ||
* has($id)がtrueを返すことは、get($id)が例外をスローしないことを意味しません。 | * has($id)がtrueを返すことは、get($id)が例外をスローしないことを意味しません。 | ||
* ただし、get($id)はNotFoundExceptionInterfaceをスローしないことは意味します。 | * ただし、get($id)はNotFoundExceptionInterfaceをスローしないことは意味します。 | ||
* | * | ||
- | * @param string $id Identifier of the entry to look for. | + | * @param string $id 検索するエントリの識別子 |
- | | + | |
* | * | ||
* @return bool | * @return bool | ||
行 164: | 行 145: | ||
/** | /** | ||
* Base interface representing a generic exception in a container. | * Base interface representing a generic exception in a container. | ||
- | * コンテナの一般的な例外を表す基本インターフェース。 | + | * コンテナの一般的な例外を表す基本インターフェース |
*/ | */ | ||
interface ContainerExceptionInterface | interface ContainerExceptionInterface | ||
行 181: | 行 162: | ||
/** | /** | ||
* No entry was found in the container. | * No entry was found in the container. | ||
- | * コンテナにエントリが見つかりませんでした。 | + | * コンテナにエントリが見つかりませんでした |
*/ | */ | ||
interface NotFoundExceptionInterface extends ContainerExceptionInterface | interface NotFoundExceptionInterface extends ContainerExceptionInterface |