コア機能

ここではZend_Applicationのコアコンポーネントの すべてについて、APIのようにドキュメントをご覧いただけます。

Zend_Application

Zend_Application provides the base functionality of the component, and the entry point to your Zend Framework application. It's purpose is two-fold: to setup the PHP environment (including autoloading), and to execute your application bootstrap.

Typically, you will pass all configuration to the Zend_Application constructor, but you can also configure the object entirely using its own methods. This reference is intended to illustrate both use cases.

表2 Zend_Application options

Option Description
phpSettings

Array of php.ini settings to use. Keys should be the php.ini keys.

includePaths

Additional paths to prepend to the include_path. Should be an array of paths.

autoloaderNamespaces

Array of additional namespaces to register with the Zend_Loader_Autoloader instance.

bootstrap

Either the string path to the bootstrap class, or an array with elements for the 'path' and 'class' for the application bootstrap.


Option names

Please note that option names are case insensitive.

表3 Zend_Application Methods

Method Return Value Parameters Description
__construct($environment, $options = null) Void
  • $environment: required,. String representing the current application environment. Typical strings might include "development", "testing", "qa", or "production", but will be defined by your organizational requirements.

  • $options: optional. Argument may be one of the following values:

    • String: path to a Zend_Config file to load as configuration for your application. $environment will be used to determine what section of the configuration to pull.

      As of 1.10, you may also pass multiple paths containing config files to be merged into a single configuration. This assists in reducing config duplication across many contexts which share common settings (e.g. configs for HTTP, or CLI, each sharing some characteristics but with their own conflicting values for others) or merely splitting a long configuration across many smaller categorised files. The parameter in this case is an array with a single key "config" whose value is an array of the files to merge. Note: this means you either pass a literal path, or array("config"=>array("/path1","/path2"[,...]));.

    • Array: associative array of configuration data for your application.

    • Zend_Config: configuration object instance.

Constructor. Arguments are as described, and will be used to set initial object state. An instance of Zend_Loader_Autoloader is registered during instantiation. Options passed to the constructor are passed to setOptions().

getEnvironment() String N/A

Retrieve the environment string passed to the constructor.

getAutoloader() Zend_Loader_Autoloader N/A

Retrieve the Zend_Loader_Autoloader instance registered during instantiation.

setOptions(array $options) Zend_Application
  • $options: required. An array of application options.

All options are stored internally, and calling the method multiple times will merge options. Options matching the various setter methods will be passed to those methods. As an example, the option "phpSettings" will then be passed to setPhpSettings(). (Option names are case insensitive.)

getOptions() Array N/A

Retrieve all options used to initialize the object; could be used to cache Zend_Config options to a serialized format between requests.

hasOption($key) Boolean
  • $key: String option key to lookup

Determine whether or not an option with the specified key has been registered. Keys are case insensitive.

getOption($key) Mixed
  • $key: String option key to lookup

Retrieve the option value of a given key. Returns NULL if the key does not exist.

setPhpSettings(array $settings, $prefix = '') Zend_Application
  • $settings: required. Associative array of PHP INI settings.

  • $prefix: optional. String prefix with which to prepend option keys. Used internally to allow mapping nested arrays to dot-separated php.ini keys. In normal usage, this argument should never be passed by a user.

Set run-time php.ini settings. Dot-separated settings may be nested hierarchically (which may occur with INI Zend_Config files) via an array-of-arrays, and will still resolve correctly.

setAutoloaderNamespaces(array $namespaces) Zend_Application
  • $namespaces: required. Array of strings representing the namespaces to register with the Zend_Loader_Autoloader instance.

Register namespaces with the Zend_Loader_Autoloader instance.

setBootstrap($path, $class = null) Zend_Application
  • $path: required. May be either a Zend_Application_Bootstrap_Bootstrapper instance, a string path to the bootstrap class, an associative array of classname => filename, or an associative array with the keys 'class' and 'path'.

  • $class: optional. If $path is a string, $class may be specified, and should be a string class name of the class contained in the file represented by path.

 
getBootstrap() NULL | Zend_Application_Bootstrap_Bootstrapper N/A

Retrieve the registered bootstrap instance.

bootstrap() Void N/A

Call the bootstrap's bootstrap() method to bootstrap the application.

run() Void N/A

Call the bootstrap's run() method to dispatch the application.


Zend_Application_Bootstrap_Bootstrapper(日本語)

Zend_Application_Bootstrap_Bootstrapperは、 ブートストラップ・クラス全てで実装しなければならない基底インターフェースです。 基本的な機能として、構成、リソースの確認、起動(個々のリソースかまたはアプリケーション全て)、 及びアプリケーションのディスパッチを意図しています。

以下のメソッドで、インターフェースの定義が用意されています。

表4 Zend_Application_Bootstrap_Bootstrapperインタフェース

メソッド 返り値 パラメータ 説明
__construct($application) Void
  • $application: 必須。 唯一の引数としてZend_Application、 またはZend_Application_Bootstrap_Bootstrapperオブジェクトを受け取るべきです。

コンストラクタ。Zend_Applicationオブジェクト、 または別のブートストラップ・オブジェクトのはずの単一の引数を扱います。

setOptions(array $options) Zend_Application_Bootstrap_Bootstrapper
  • $options: 必須。 設定するオプションの配列

一般的に、一致するセッターを持つどんな選択肢でも、そのセッターを起動します; これ以外の場合には、選択肢は単純に後の検索に備えてたくわえられます。

getApplication() Zend_Application | Zend_Application_Bootstrap_Bootstrapper N/A

コンストラクタによって渡されるアプリケーションまたはブートストラップ・オブジェクトを取得します。

getEnvironment() String N/A

親アプリケーションまたはブートストラップ・オブジェクトで登録された環境文字列を取得します。

getClassResources() Array N/A

クラスで定義された、利用可能なりソース・イニシャライザ名のリストを取得します。 これは、固有の実装かもしれません。

bootstrap($resource = null) Mixed
  • $resource: 任意

もし$resourceが空なら、 すべてのブートストラップ・リソースを実行します。 もし文字列なら、単一のリソースを実行します; もし配列なら、配列の各々のリソースを実行します。

run() Void N/A

起動した後にどんなアプリケーション・ロジックを実行すべきか定義します。


Zend_Application_Bootstrap_ResourceBootstrapper(日本語)

Zend_Application_Bootstrap_ResourceBootstrapperは、 ブートストラップ・クラスが外部リソースをロードしようとするとき、 使用するインタフェースです。 すなわち、一つ以上のリソースがクラスで直接定められず、むしろプラグインによって定められます。 それはZend_Application_Bootstrap_Bootstrapper とともに使われなければなりません、 そして、Zend_Application_Bootstrap_BootstrapAbstract がこの機能を実装します。

以下のメソッドで、インターフェースの定義が用意されています。

表5 Zend_Application_Bootstrap_ResourceBootstrapperインタフェース

メソッド 返り値 パラメータ 説明
registerPluginResource($resource, $options = null) Zend_Application_Bootstrap_ResourceBootstrapper
  • $resource: 必須. リソース名、または Zend_Application_Resource_Resourceオブジェクト

  • $options: 任意. インスタンス化でリソースに渡す 配列、またはZend_Configオブジェクト

クラスでリソースを登録します。 そして、リソースに渡すオプションの構成を提供します。

unregisterPluginResource($resource) Zend_Application_Bootstrap_ResourceBootstrapper
  • $resource: 必須. クラスから登録を抹消するリソース名

クラスからプラグイン・リソースを除去します

hasPluginResource($resource) ブール値
  • $resource: 必須. リソース名

指定したりソースがクラスに登録されているか判断します

getPluginResource($resource) Zend_Application_Resource_Resource
  • $resource: 必須. 取得するリソース名 (文字列)

プラグイン・リソースのインスタンスを名前により取得

getPluginResourceNames() 配列 N/A

登録された全てのプラグインリソースの名前の一覧を取得

setPluginLoader(Zend_Loader_PluginLoader_Interface $loader) Zend_Application_Bootstrap_ResourceBootstrapper
  • $loader: 必須. プラグイン名からクラスに解決するときに使うプラグインローダーのインスタンス

プラグインのクラス名を解決するときに使うプラグインローダーのインスタンスを登録

getPluginLoader() Zend_Loader_PluginLoader_Interface N/A

登録されたプラグインローダーを取得


Zend_Application_Bootstrap_BootstrapAbstract

Zend_Application_Bootstrap_BootstrapAbstract is an abstract class which provides the base functionality of a common bootstrap. It implements both Zend_Application_Bootstrap_Bootstrapper and Zend_Application_Bootstrap_ResourceBootstrapper.

表6 Zend_Application_Bootstrap_BootstrapAbstract Methods

Method Return Value Parameters Description
__construct($application) Void
  • $application: required. Accepts either a Zend_Application or a Zend_Application_Bootstrap_Bootstrapper object as the sole argument.

Constructor. Accepts a single argument, which should be a Zend_Application object, or another bootstrap object.

setOptions(array $options) Zend_Application_Bootstrap_Bootstrapper
  • $options: required. Array of options to set.

Any option that has a matching setter will invoke that setter; otherwise, the option will simply be stored for later retrieval. As an example, if your extending class defined a setFoo() method, the option 'foo' would pass the value to that method.

Two additional, special options keys may also be used. pluginPaths may be used to specify prefix paths to plugin resources; it should be an array of class prefix to filesystem path pairs. resources may be used to specify plugin resources to use, and should consist of plugin resource to instantiation options pairs.

getOptions() Array N/A

Returns all options registered via setOptions().

hasOption($key) Boolean
  • $key: required. Option key to test.

Determine if an option key is present.

getOption($key) Mixed
  • $key: required. Option key to retrieve.

Retrieve the value associated with an option key; returns NULL if no option is registered with that key.

setApplication(Zend_Application | Zend_Application_Bootstrap_Bootstrapper $application) Zend_Application_Bootstrap_BootstrapAbstract
  • $application: required.

Register the parent application or bootstrap object.

getApplication() Zend_Application | Zend_Application_Bootstrap_Bootstrapper N/A

Retrieve the application or bootstrap object passed via the constructor.

getEnvironment() String N/A

Retrieve the environment string registered with the parent application or bootstrap object.

getClassResources() Array N/A

Retrieve a list of available resource initializer names as defined in the class. This may be implementation specific.

getContainer() Object N/A

Retrieves the container that stores resources. If no container is currently registered, it registers a Zend_Registry instance before returning it.

setContainer($container) Zend_Application_Bootstrap_BootstrapAbstract
  • $container, required. An object in which to store resources.

Provide a container in which to store resources. When a resource method or plugin returns a value, it will be stored in this container for later retrieval.

hasResource($name) Boolean
  • $name, required. Name of a resource to check.

When a resource method or plugin returns a value, it will be stored in the resource container (see getContainer() and setContainer(). This method will indicate whether or not a value for that resource has been set.

getResource($name) Mixed
  • $name, required. Name of a resource to fetch.

When a resource method or plugin returns a value, it will be stored in the resource container (see getContainer() and setContainer(). This method will retrieve a resources from the container.

bootstrap($resource = null) Mixed
  • $resource: optional.

If $resource is empty, execute all bootstrap resources. If a string, execute that single resource; if an array, execute each resource in the array.

This method can be used to run individual bootstraps either defined in the class itself or via resource plugin classes. A resource defined in the class will be run in preference over a resource plugin in the case of naming conflicts.

run() Void N/A

Defines what application logic to run after bootstrapping.

__call($method, $args) Mixed
  • $method: required. The method name to call.

  • $args: required. Array of arguments to use in the method call.

Provides convenience to bootstrapping individual resources by allowing you to call 'bootstrap<ResourceName>()' instead of using the bootstrap() method.


Zend_Application_Bootstrap_Bootstrap(日本語)

Zend_Application_Bootstrap_BootstrapZend_Application_Bootstrap_BootstrapAbstract を具体的に実装したものです。 これは フロントコントローラリソース を登録し、デフォルトのモジュールが定義されていることと、 それからフロントコントローラをディスパッチすることを最初にチェックする run()メソッドという主要なフィーチャーです。

多くの場合、ブートストラップする必要性に応じて拡張するか、 このクラスを単純に使って、利用するリソースプラグインのリストを与えたいことでしょう。

アプリケーションのオートローディングを有効にする

さらに、このブートストラップ実装により、 そのツリーに位置するリソースに対する "namespace" またはクラス接頭辞を指定できるようになります。 それにより、様々なアプリケーション・リソースをオートローディングできるようになります。 基本的に、それは Zend_Application_Module_Autoloader オブジェクトのインスタンスを生成します。 そして引数として、要求された名前空間およびブートストラップのディレクトリを提示します。 "appnamespace" 構成オプションに名前空間を提示することにより、この機能を使用可能にできるでしょう。 INI 例としては、

appnamespace = "Application"

または XMLで、

<appnamespace>Application</appnamespace>

デフォルトでは、Zend_Tool は、 "Application" 変数でこのオプションを有効にします。

あるいは、ブートストラップ・クラスの $_appNamespace プロパティを適切な値で簡単に定義できます。

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected $_appNamespace = 'Application';
}

Zend_Application_Resource_Resource(日本語)

Zend_Application_Resource_Resourceは、 Zend_Application_Bootstrap_ResourceBootstrapperを実装する ブートストラップ・クラスで使われるプラグイン・リソースのためのインターフェースです。 プラグイン・リソースでは、構成を可能にし、ブートストラップを認識し、 そして、リソースを初期化するためのストラテジー・パターンを実装するよう、 期待されます。

表7 Zend_Application_Resource_Resourceインターフェース

メソッド 返り値 パラメータ 説明
__construct($options = null) Void
  • $options: 任意. リソース状態を設定するオプション

状態を初期化するためのオプションを、コンストラクタに渡すことができなければなりません。

setBootstrap(Zend_Application_Bootstrap_Bootstrapper $bootstrap) Zend_Application_Resource_Resource
  • $bootstrap: 必須. このリソースを初期化する親ブートストラップ

親ブートストラップ・オブジェクトを登録できるようにしなければいけません。

getBootstrap() Zend_Application_Bootstrap_Bootstrapper N/A

登録されたブートストラップ・インスタンスを取得します。

setOptions(array $options) Zend_Application_Resource_Resource
  • $options: 必須. 状態を設定するオプション

リソースの状態を設定。

getOptions() Array N/A

登録されたオプションを取得

init() Mixed N/A

ストラテジー・パターン: リソースの初期化を実行。


Zend_Application_Resource_ResourceAbstract(日本語)

Zend_Application_Resource_ResourceAbstractZend_Application_Resource_Resourceを 実装する抽象クラスで、 独自のカスタム・プラグイン・リソースを作成し始めるために適しています。

注意: この抽象クラスは、init()メソッドを実装しません; クラスを具体的に拡張する定義のために残されます。

表8 Zend_Application_Resource_ResourceAbstractメソッド

メソッド 返り値 パラメータ 説明
__construct($options = null) Void
  • $options: 任意. リソース状態を設定するオプション

状態を初期化するためのオプションを、コンストラクタに渡すことができなければなりません。

setBootstrap(Zend_Application_Bootstrap_Bootstrapper $bootstrap) Zend_Application_Resource_ResourceAbstract
  • $bootstrap: 必須. このリソースを初期化する親ブートストラップ

親ブートストラップ・オブジェクトを登録できるようにしなければいけません。

getBootstrap() Zend_Application_Bootstrap_Bootstrapper N/A

登録されたブートストラップ・インスタンスを取得します。

setOptions(array $options) Zend_Application_Resource_ResourceAbstract
  • $options: 必須. 状態を設定するオプション

リソースの状態を設定。

getOptions() Array N/A

登録されたオプションを取得


リソース名

プラグイン・リソースを登録するときに起きる問題の一つは、 どのように親ブートストラップ・クラスからそれらを参照すべきかということです。 利用されるかもしれない3通りの異なる手法があります。 そして、ブートストラップとそのプラグイン・リソースを構成した方法によって決まります。

最初に、プラグインが定義済みのプレフィックス・パスの中で定義されるなら、 それらの「短い名前」 ― すなわち、クラス・プレフィックスの後のクラス名の部分 ― によって単純にそれらを参照するかもしれません。 例えば、プレフィックス・パス "Zend_Application_Resource" がすでに登録されていたら、 クラス "Zend_Application_Resource_View" は単純に "View" として参照されるかもしれません。 完全なクラス名または短い名前を用いてそれらを登録するかもしれません:

$app = new Zend_Application(APPLICATION_ENV, array(
    'pluginPaths' => array(
        'My_Resource' => 'My/Resource/',
    ),
    'resources' => array(
        // もし下記のクラスが存在すれば:
        'My_Resource_View' => array(),

        // その場合、これは等価です:
        'View' => array(),
    ),
));

それならどの場合もリソースを起動できて、 後で短い名前を使って、それを取得できます:

$bootstrap->bootstrap('view');
$view = $bootstrap->getResource('view');

第2には、マッチするプラグイン・パスが定義されないならば、 完全なクラス名によってリソースを今まで通り渡すかもしれません:

$app = new Zend_Application(APPLICATION_ENV, array(
    'resources' => array(
        // 標準の 'View' リソースをロードします:
        'View' => array(),

        // 一方で固有のクラス名でリソースをロードします:
        'My_Resource_View' => array(),
    ),
));

明らかに、リソースの参照がもっと冗長になります:

$bootstrap->bootstrap('My_Resource_View');
$view = $bootstrap->getResource('My_Resource_View');

これは、第3の選択肢です。 与えられたリソースクラスが登録する、明示的な名前を指定できます。 これは public $_explicitTypeプロパティを リソース・プラグイン・クラスに文字列値で加えることによって実現されます; その場合、ブートストラップを通してプラグイン・リソースを参照したいときはいつでも、その値が使われます。 例えば、独自のビュー・クラスを定義しましょう:

class My_Resource_View extends Zend_Application_Resource_ResourceAbstract
{
    public $_explicitType = 'My_View';

    public function init()
    {
        // 何らかの初期化実行...
    }
}

その場合、そのリソースを起動するか、または名前 "My_View" によって 取得できます:

$bootstrap->bootstrap('My_View');
$view = $bootstrap->getResource('My_View');

これらのいろいろな命名方法を使用して、既存のリソースをオーバーライドできます。 そして、複雑な初期化を成し遂げるために、 独自のものを追加したり、複数のリソースを混ぜたりすることなどができます。