Yet Another Framework (Yaf)

目次

Yaf_Application クラス

はじめに

Yaf_Application はアプリケーションのブートストラップ機能を提供します。 再利用可能なリソース、共有できるモジュールベースのブートストラップクラス、 そして依存関係のチェックなどです。

注意:

Yaf_Application はシングルトンパターンを実装しており、 シリアライズやアンシリアライズはできません。これは、 PHPUnit を使って Yaf 用のテストケースを書こうとしたときに問題の原因となります。

PHPUnit の @backupGlobals アノテーションを使えば グローバル変数のバックアップやりストア操作を制御できるので、 この問題に対応できます。

クラス概要

Yaf_Application
final class Yaf_Application {
/* プロパティ */
protected $config;
protected $dispatcher;
protected static $_app;
protected $_modules;
protected $_running;
protected $_environ;
/* メソッド */
public __construct(mixed $config, string $envrion = ?)
public staticmixed app()
public void bootstrap(Yaf_Bootstrap_Abstract $bootstrap = ?)
public Yaf_Application clearLastError()
public void environ()
public void execute(callable $entry, string ...$args)
public Yaf_Application getAppDirectory()
public Yaf_Config_Abstract getConfig()
public Yaf_Dispatcher getDispatcher()
public string getLastErrorMsg()
public int getLastErrorNo()
public array getModules()
public void run()
public Yaf_Application setAppDirectory(string $directory)
public __destruct()
}

プロパティ

config

dispatcher

_app

_modules

_running

_environ

Yaf_Bootstrap_Abstract クラス

はじめに

ブートストラップとは、アプリケーションを実行する前の初期設定に使う仕組みのことです。

ユーザーが自前のブートストラップクラスを定義するには、 Yaf_Bootstrap_Abstract を継承します。

ブートストラップクラスで定義したメソッドの中で "_init" で始まるものがすべて、定義した順に Yaf_Application::bootstrap から呼ばれます。

例1 ブートストラップの例

<?php
   
/* ブートストラップクラスは ./application/Bootstrap.php で定義しないといけません */
   
class Bootstrap extends Yaf_Bootstrap_Abstract {
        public function 
_initConfig(Yaf_Dispatcher $dispatcher) {
            
var_dump(__METHOD__);
        }
        public function 
_initPlugin(Yaf_Dispatcher $dispatcher) {
            
var_dump(__METHOD__);
        }
   }

   
$config = array(
       
"application" => array(
           
"directory" => dirname(__FILE__) . "/application/",
       ),
   );
 
   
$app = new Yaf_Application($config);
   
$app->bootstrap();
?>

上の例の出力は、 たとえば以下のようになります。

string(22) "Bootstrap::_initConfig"
string(22) "Bootstrap::_initPlugin"

クラス概要

Yaf_Bootstrap_Abstract
abstract class Yaf_Bootstrap_Abstract {
/* プロパティ */
/* メソッド */
}

Yaf_Dispatcher クラス

はじめに

Yaf_Dispatcher の役割は、 リクエスト環境の初期化、やってきたリクエストのルーティング、 そして見つかったアクションへのディスパッチです。 あらゆるレスポンスを取りまとめ、処理が完了した後でそれを返します。

Yaf_Dispatcher はシングルトンパターンを実装しています。 つまり、同時に複数のインスタンスは作れないということです。 そのため、このクラスをレジストリとして使い、 ディスパッチ中に他のオブジェクトから参照させることもできます。

クラス概要

Yaf_Dispatcher
final class Yaf_Dispatcher {
/* プロパティ */
protected $_router;
protected $_view;
protected $_request;
protected $_plugins;
protected static $_instance;
protected $_auto_render;
protected $_return_response;
protected $_instantly_flush;
protected $_default_module;
protected $_default_controller;
protected $_default_action;
/* メソッド */
public __construct()
public Yaf_Dispatcher autoRender(bool $flag = ?)
public Yaf_Dispatcher catchException(bool $flag = ?)
public bool disableView()
public Yaf_Response_Abstract dispatch(Yaf_Request_Abstract $request)
public Yaf_Dispatcher enableView()
public Yaf_Dispatcher flushInstantly(bool $flag = ?)
public Yaf_Application getApplication()
public string getDefaultAction()
public string getDefaultController()
public string getDefaultModule()
public static Yaf_Dispatcher getInstance()
public Yaf_Request_Abstract getRequest()
public Yaf_Router getRouter()
public Yaf_View_Interface initView(string $templates_dir, array $options = ?)
public Yaf_Dispatcher registerPlugin(Yaf_Plugin_Abstract $plugin)
public Yaf_Dispatcher returnResponse(bool $flag)
public Yaf_Dispatcher setDefaultAction(string $action)
public Yaf_Dispatcher setDefaultController(string $controller)
public Yaf_Dispatcher setDefaultModule(string $module)
public Yaf_Dispatcher setErrorHandler(call $callback, int $error_types)
public Yaf_Dispatcher setRequest(Yaf_Request_Abstract $request)
public Yaf_Dispatcher setView(Yaf_View_Interface $view)
public Yaf_Dispatcher throwException(bool $flag = ?)
}

プロパティ

_router

_view

_request

_plugins

_instance

_auto_render

_return_response

_instantly_flush

_default_module

_default_controller

_default_action

Yaf_Config_Abstract クラス

はじめに

クラス概要

Yaf_Config_Abstract
abstract class Yaf_Config_Abstract {
/* プロパティ */
protected $_config;
protected $_readonly;
/* メソッド */
abstract public mixed get(string $name, mixed $value)
abstract public bool readonly()
abstract public Yaf_Config_Abstract set()
abstract public array toArray()
}

プロパティ

_config

_readonly

Yaf_Config_Ini クラス

はじめに

Yaf_Config_Ini を使うと、設定データをおなじみの INI 形式で保存でき、アプリケーションからもオブジェクトのプロパティとして読めるようになります。 INI フォーマットを拡張して、設定データを階層構造で扱えるようにしたり 設定セクション間での継承を設定できるようにしたりしています。 設定データの階層は、キーをピリオド(".")で区切って表します。 セクションの継承を表すには、セクション名の後にコロン(":") を続け、その後に継承元のセクション名を指定します。

注意:

Yaf_Config_Ini は、PHP の関数 parse_ini_file() を利用します。 この関数のドキュメントも読んで、その振る舞いを知っておきましょう。 特殊な値、たとえば "true"、"false"、"yes"、"no"、そして "null" の扱いも、parse_ini_file() に従います。

クラス概要

Yaf_Config_Ini
class Yaf_Config_Ini extends Yaf_Config_Abstract implements Iterator, ArrayAccess, Countable {
/* プロパティ */
/* メソッド */
public __construct(string $config_file, string $section = ?)
public void count()
public void current()
public void __get(string $name = ?)
public void __isset(string $name)
public void key()
public void next()
public void offsetExists(string $name)
public void offsetGet(string $name)
public void offsetSet(string $name, string $value)
public void offsetUnset(string $name)
public void readonly()
public void rewind()
public void __set(string $name, mixed $value)
public array toArray()
public void valid()
/* 継承したメソッド */
abstract public mixed Yaf_Config_Abstract::get(string $name, mixed $value)
abstract public bool Yaf_Config_Abstract::readonly()
abstract public Yaf_Config_Abstract Yaf_Config_Abstract::set()
abstract public array Yaf_Config_Abstract::toArray()
}

プロパティ

_config

_readonly

例1 Yaf_Config_Ini の例

この例は、Yaf_Config_Ini で INI ファイルから設定データを読む方法を示すものです。 この例では、運用環境とステージング環境の設定データを用意しています。 ステージング環境の設定は運用環境とほぼ同じなので、 staging セクションは production セクションを継承しています。 今回の場合はどちらがどちらを継承してもあまり変わらないので 逆に production セクションが staging セクションを継承するようにもできます。 しかし、もっと複雑な設定になるとそうはいかないでしょう。 次のような設定データが /path/to/config.ini に格納されているものとします。

; Production site configuration data
[production]
webhost                  = www.example.com
database.adapter         = pdo_mysql
database.params.host     = db.example.com
database.params.username = dbuser
database.params.password = secret
database.params.dbname   = dbname
 
; Staging site configuration data inherits from production and
; overrides values as necessary
[staging : production]
database.params.host     = dev.example.com
database.params.username = devuser
database.params.password = devsecret
<?php
$config 
= new Yaf_Config_Ini('/path/to/config.ini''staging');
 
var_dump($config->database->params->host); 
var_dump($config->database->params->dbname);
var_dump($config->get("database.params.username"));
?>

上の例の出力は、 たとえば以下のようになります。

string(15) "dev.example.com"
string(6) "dbname"
string(7) "devuser

Yaf_Config_Simple クラス

はじめに

クラス概要

Yaf_Config_Simple
class Yaf_Config_Simple extends Yaf_Config_Abstract implements Iterator, ArrayAccess, Countable {
/* プロパティ */
protected $_readonly;
/* メソッド */
public __construct(array $configs, bool $readonly = false)
public void count()
public void current()
public void __get(string $name = ?)
public void __isset(string $name)
public void key()
public void next()
public void offsetExists(string $name)
public void offsetGet(string $name)
public void offsetSet(string $name, string $value)
public void offsetUnset(string $name)
public void readonly()
public void rewind()
public void __set(string $name, string $value)
public array toArray()
public void valid()
/* 継承したメソッド */
abstract public mixed Yaf_Config_Abstract::get(string $name, mixed $value)
abstract public bool Yaf_Config_Abstract::readonly()
abstract public Yaf_Config_Abstract Yaf_Config_Abstract::set()
abstract public array Yaf_Config_Abstract::toArray()
}

プロパティ

_config

_readonly

Yaf_Controller_Abstract クラス

はじめに

Yaf_Controller_Abstract は Yaf システムの中心となるクラスです。 MVC は Model-View-Controller の略で、 アプリケーションのロジックと表示のロジックを切り離すためのデザインパターンです。

すべてのカスタムコントローラは Yaf_Controller_Abstract を継承する必要があります。

カスタムコントローラでは __construct を定義できません。そのため、 Yaf_Controller_Abstract ではマジックメソッド Yaf_Controller_Abstract::init を用意しています。

カスタムコントローラで init() メソッドを定義すると、 コントローラのインスタンスを作成するときにそれが呼ばれます。

アクションには引数を持たせることができます。 リクエストが来たときに、もしリクエストのパラメータ ( Yaf_Request_Abstract::getParam を参照ください) に同名の変数があれば、Yaf はアクションのメソッド ( Yaf_Action_Abstract::execute を参照ください) にそれを渡します。

注意:

これらの引数は何もフィルタリングせずそのまま取り込まれるので、 実際に使う前には注意しないといけません。

クラス概要

Yaf_Controller_Abstract
abstract class Yaf_Controller_Abstract {
/* プロパティ */
public $actions;
protected $_module;
protected $_name;
protected $_request;
protected $_response;
protected $_invoke_args;
protected $_view;
/* メソッド */
final private __construct()
protected bool display(string $tpl, array $parameters = ?)
public bool forward(string $action, array $paramters = ?)
public void getInvokeArg(string $name)
public void getInvokeArgs()
public string getModuleName()
public string getName()
public Yaf_Request_Abstract getRequest()
public Yaf_Response_Abstract getResponse()
public Yaf_View_Interface getView()
public string getViewpath()
public void init()
public void initView(array $options = ?)
public bool redirect(string $url)
protected string render(string $tpl, array $parameters = ?)
public void setViewpath(string $view_directory)
}

プロパティ

actions

アクションメソッドを個別の PHP スクリプトとして定義することもできます。 そのときには、このプロパティと Yaf_Action_Abstract を利用します。

例1 別ファイルでのアクションの定義

<?php
class IndexController extends Yaf_Controller_Abstract {
    protected 
$actions = array(
        
/** now dummyAction is defined in a separate file */
        
"dummy" => "actions/Dummy_action.php",
    );

    
/* action method may have arguments */
    
public function indexAction($name$id) {
       
/* $name and $id are unsafe raw data */
       
assert($name == $this->getRequest()->getParam("name"));
       
assert($id   == $this->_request->getParam("id"));
    }
}
?>

例2 Dummy_action.php

<?php
class DummyAction extends Yaf_Action_Abstract {
    
/* an action class shall define this method as the entry point */
    
public function execute() {
    }
}
?>

_module

モジュール名

_name

コントローラ名

_request

現在のリクエストオブジェクト

_response

現在のレスポンスオブジェクト

_invoke_args

_view

ビューエンジンオブジェクト

Yaf_Action_Abstract クラス

はじめに

アクションは、Yaf の個別のファイルで定義します (Yaf_Controller_Abstract を参照ください)。 アクションメソッドには Yaf_Action_Abstract クラスも使えます。

Yaf から呼び出すためのエントリポイントが必要なので、 抽象メソッド Yaf_Action_Abstract::execute をアクションクラスで実装する必要があります。

クラス概要

Yaf_Action_Abstract
class Yaf_Action_Abstract extends Yaf_Controller_Abstract {
/* プロパティ */
protected $_controller;
/* メソッド */
abstract publicmixed execute(mixed ...$args)
publicYaf_Controller_Abstract getController()
public string getControllerName()
/* 継承したメソッド */
protected bool Yaf_Controller_Abstract::display(string $tpl, array $parameters = ?)
public bool Yaf_Controller_Abstract::forward(string $action, array $paramters = ?)
public void Yaf_Controller_Abstract::getInvokeArg(string $name)
public void Yaf_Controller_Abstract::getInvokeArgs()
public string Yaf_Controller_Abstract::getModuleName()
public string Yaf_Controller_Abstract::getName()
public Yaf_Request_Abstract Yaf_Controller_Abstract::getRequest()
public Yaf_Response_Abstract Yaf_Controller_Abstract::getResponse()
public Yaf_View_Interface Yaf_Controller_Abstract::getView()
public string Yaf_Controller_Abstract::getViewpath()
public void Yaf_Controller_Abstract::init()
public void Yaf_Controller_Abstract::initView(array $options = ?)
public bool Yaf_Controller_Abstract::redirect(string $url)
protected string Yaf_Controller_Abstract::render(string $tpl, array $parameters = ?)
public void Yaf_Controller_Abstract::setViewpath(string $view_directory)
}

プロパティ

_module

_name

_request

_response

_invoke_args

_view

_controller

Yaf_View_Interface クラス

はじめに

Yaf では、組み込みのビューエンジン Yaf_View_Simple のかわりにカスタムビューを自作できるようになっています。 その実例は、 Yaf_Dispatcher::setView を参照ください。

クラス概要

Yaf_View_Interface
class Yaf_View_Interface {
/* メソッド */
abstract public bool assign(string $name, string $value = ?)
abstract public bool display(string $tpl, array $tpl_vars = ?)
abstract public void getScriptPath()
abstract public string render(string $tpl, array $tpl_vars = ?)
abstract public void setScriptPath(string $template_dir)
}

Yaf_View_Simple クラス

はじめに

Yaf_View_Simple は Yaf の組み込みテンプレートエンジンです。 シンプルながらも高速なテンプレートエンジンで、PHP スクリプトテンプレートだけに対応しています。

クラス概要

Yaf_View_Simple
class Yaf_View_Simple implements Yaf_View_Interface {
/* プロパティ */
protected $_tpl_vars;
protected $_tpl_dir;
/* メソッド */
final public __construct(string $template_dir, array $options = ?)
public bool assign(string $name, mixed $value = ?)
public bool assignRef(string $name, mixed &$value)
public bool clear(string $name = ?)
public bool display(string $tpl, array $tpl_vars = ?)
public string eval(string $tpl_content, array $tpl_vars = ?)
public void __get(string $name = ?)
public string getScriptPath()
public void __isset(string $name)
public string render(string $tpl, array $tpl_vars = ?)
public void __set(string $name, mixed $value)
public bool setScriptPath(string $template_dir)
}

プロパティ

_tpl_vars

_tpl_dir

Yaf_Loader クラス

はじめに

Yaf_Loader は、 Yaf 用に包括的なオートローディング機能を導入します。

最初に Yaf_Application のインスタンスを取得するときに、 Yaf_Loader はシングルトンのインスタンスを作って それを spl_autoload に登録します。このインスタンスを取得するには Yaf_Loader::getInstance を使います。

Yaf_Loader は、クラスのロードを一度だけ試みます。 失敗した場合の挙動は yaf.use_spl_auload によって変わります。もしこの設定が On なら Yaf_Loader::autoloadfalse を返し、 他のオートロード関数でやり直せるようになります。この設定が Off (デフォルト) の場合は Yaf_Loader::autoloadtrue を返します。 そして、より重要なのは、このときに警告メッセージが出ることです (このメッセージを見れば、なぜロードできなかったのかがわかります)。

注意:

yaf.use_spl_autoload はできるだけ Off のままにしておきましょう。 ただし、何かのライブラリが自前のオートロード機構を持っていて、 それを置き換えるのが不可能だという場合は別です。

デフォルトでは、Yaf_Loader はすべてのライブラリ (クラスを定義したスクリプト) が グローバル ライブラリディレクトリ に格納されているものとみなします。このディレクトリは、 php.ini (yaf.library) で指定します。

Yaf_Loader がクラス (ライブラリ) を探すときに ローカルクラスディレクトリ (application.ini で定義するもので、デフォルトは application.directory . "/library") も検索対象とさせたい場合は、クラスのプレフィックスを Yaf_Loader::registerLocalNameSpace で登録する必要があります。

いくつか例を示します (APPLICATION_PATH は application.directory であるものとします)。

例1 設定の例

// php.ini で、これを設定しているものとします
yaf.library = "/global_dir"

// application.ini で、これを設定しているものとします
application.library = APPLICATION_PATH "/library"
また、次のローカル名前空間を登録しているものとします。

例2 localnamespace の登録

<?php
class Bootstrap extends Yaf_Bootstrap_Abstract{
     public function 
_initLoader($dispatcher) {
          
Yaf_Loader::getInstance()->registerLocalNameSpace(array("Foo""Bar"));
     }
?>
オートロードの例です。

例3 クラスのロード

class Foo_Bar_Test =>
  // APPLICATION_PATH/library/Foo/Bar/Test.php
  
class GLO_Name  =>
  // /global_dir/Glo/Name.php
 
class BarNon_Test
  // /global_dir/Barnon/Test.php

例4 名前空間つきのクラスのロード

class \Foo\Bar\Dummy =>
   // APPLICATION_PATH/library/Foo/Bar/Dummy.php

class \FooBar\Bar\Dummy =>
   // /global_dir/FooBar/Bar/Dummy.php

すべてのフォルダの先頭が大文字になっているのが気になる場合は、php.ini で yaf.lowcase_path = On とすれば小文字にすることもできます。

Yaf_Loader は MVC クラスを読み込むようにも作られています。 次のような規約に従います。

例5 MVC クラスのロード

Controller Classes =>
// APPLICATION_PATH/controllers/

Model Classes =>
// APPLICATION_PATH/models/

Plugin Classes =>
// APPLICATION_PATH/plugins/
クラスのサフィックス (デフォルト設定の場合。プレフィックスを使うには yaf.name_suffix の値を変更します) で、MVC クラスであるかそうでないかを判断します。

例6 MVC クラスかどうかの区別

Controller Classes =>
    // ***Controller

Model Classes =>
    // ***Model

Plugin Classes =>
    // ***Plugin
例を示します。

例7 MVC のロード

class IndexController
    // APPLICATION_PATH/controllers/Index.php

class DataModel =>
   // APPLICATION_PATH/models/Data.php

class DummyPlugin =>
  // APPLICATION_PATH/plugins/Dummy.php

class A_B_TestModel =>
  // APPLICATION_PATH/models/A/B/Test.php

注意:

2.1.18 からは、Yaf はユーザースクリプト側からのコントローラのオートロードに対応しました (つまり、ユーザーが書いた PHP スクリプトからオートロードを実行できるということです。 ブートストラップやプラグインから、コントローラのstaticプロパティにアクセスできます)。 しかし、オートローダーがコントローラクラスを探す場所は、デフォルトのモジュールフォルダつまり "APPLICATION_PATH/controllers/" だけになります。

また、ディレクトリは yaf.lowcase_path の影響を受けます。

クラス概要

Yaf_Loader
class Yaf_Loader {
/* プロパティ */
protected $_local_ns;
protected $_library;
protected $_global_library;
static $_instance;
/* メソッド */
private __construct()
public void autoload()
public void clearLocalNamespace()
public static void getInstance()
public Yaf_Loader getLibraryPath(bool $is_global = false)
public void getLocalNamespace()
public string getNamespacePath(string $namespaces)
public array getNamespaces()
public static void import()
public void isLocalName()
public void registerLocalNamespace(mixed $prefix)
public bool registerNamespace(stringarray $namespaces, string $path = ?)
public Yaf_Loader setLibraryPath(string $directory, bool $is_global = false)
}

プロパティ

_local_ns

_library

デフォルトでは、この値は application.directory . "/library" となります。この値を変更するには、application.ini (application.library) で設定するか、あるいは Yaf_Loader::setLibraryPath をコールします。

_global_library

_instance

Yaf_Plugin_Abstract クラス

はじめに

プラグインを使えば、フレームワークの拡張やカスタマイズを簡単に行えます。

プラグインは、クラスとして定義します。 実際のクラスの定義はコンポーネントによって異なります。 このインターフェイスを実装する必要があるかもしれませんが、 プラグイン自体がクラスであるという事実は変わりません。

プラグインを Yaf にロードするには Yaf_Dispatcher::registerPlugin を使います。登録を済ませれば、このインターフェイスに従って実装した すべてのメソッドが適切な場面で呼ばれます。

例1 プラグインの例

<?php
   
/* bootstrap class should be defined under ./application/Bootstrap.php */
   
class Bootstrap extends Yaf_Bootstrap_Abstract {
        public function 
_initPlugin(Yaf_Dispatcher $dispatcher) {
            
/* register a plugin */
            
$dispatcher->registerPlugin(new TestPlugin());
        }
   }

   
/* plugin class should be placed under ./application/plugins/ */
   
class TestPlugin extends Yaf_Plugin_Abstract {
        public function 
routerStartup(Yaf_Request_Abstract $requestYaf_Response_Abstract $response) {
            
/* before router 
               in this hook,  user can do some url rewrite */
            
var_dump("routerStartup");
        }
        public function 
routerShutdown(Yaf_Request_Abstract $requestYaf_Response_Abstract $response) {
            
/* router complete 
               in this hook, user can do login check */
            
var_dump("routerShutdown");
        }
        public function 
dispatchLoopStartup(Yaf_Request_Abstract $requestYaf_Response_Abstract $response) {
            
var_dump("dispatchLoopStartup");
        }
        public function 
preDispatch(Yaf_Request_Abstract $requestYaf_Response_Abstract $response) {
            
var_dump("preDispatch");
        }
        public function 
postDispatch(Yaf_Request_Abstract $requestYaf_Response_Abstract $response) {
            
var_dump("postDispatch");
        }
        public function 
dispatchLoopShutdown(Yaf_Request_Abstract $requestYaf_Response_Abstract $response) {
            
/* final hook
               in this hook user can do logging or implement layout */
            
var_dump("dispatchLoopShutdown");
        }
   }

   Class 
IndexController extends Yaf_Controller_Abstract {
        public function 
indexAction() {
            return 
FALSE//prevent rendering
        
}
   }

   
$config = array(
       
"application" => array(
           
"directory" => dirname(__FILE__) . "/application/",
       ),
   );
 
   
$app = new Yaf_Application($config);
   
$app->bootstrap()->run();
?>

上の例の出力は、 たとえば以下のようになります。

string(13) "routerStartup"
string(14) "routerShutdown"
string(19) "dispatchLoopStartup"
string(11) "preDispatch"
string(12) "postDispatch"
string(20) "dispatchLoopShutdown"

クラス概要

Yaf_Plugin_Abstract
class Yaf_Plugin_Abstract {
/* メソッド */
public void dispatchLoopShutdown(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response)
public void dispatchLoopStartup(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response)
public void postDispatch(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response)
public void preDispatch(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response)
public void preResponse(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response)
public void routerShutdown(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response)
public void routerStartup(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response)
}

Yaf_Registry クラス

はじめに

Yaf_Registry のすべてのメソッドはstaticメソッドとして宣言されており、 どこからでもアクセスできます。これを使えば、コードのどこからでも必要に応じて 任意のデータを保存したり取得したりすることができます。

クラス概要

Yaf_Registry
class Yaf_Registry {
/* プロパティ */
static $_instance;
protected $_entries;
/* メソッド */
private __construct()
public static void del(string $name)
public static mixed get(string $name)
public static bool has(string $name)
public static bool set(string $name, string $value)
}

プロパティ

_instance

_entries

Yaf_Request_Abstract クラス

はじめに

クラス概要

Yaf_Request_Abstract
class Yaf_Request_Abstract {
/* 定数 */
const string Yaf_Request_Abstract::SCHEME_HTTP = http;
const string Yaf_Request_Abstract::SCHEME_HTTPS = https;
/* プロパティ */
public $module;
public $controller;
public $action;
public $method;
protected $params;
protected $language;
protected $_exception;
protected $_base_uri;
protected $uri;
protected $dispatched;
protected $routed;
/* メソッド */
public bool clearParams()
public void getActionName()
public void getBaseUri()
public void getControllerName()
public void getEnv(string $name, string $default = ?)
public void getException()
public void getLanguage()
public string getMethod()
public void getModuleName()
public mixed getParam(string $name, string $default = ?)
public array getParams()
public void getRequestUri()
public void getServer(string $name, string $default = ?)
public bool isCli()
public bool isDispatched()
public bool isGet()
public bool isHead()
public bool isOptions()
public bool isPost()
public bool isPut()
public bool isRouted()
public bool isXmlHttpRequest()
public void setActionName(string $action, bool $format_name = true)
public bool setBaseUri(string $uir)
public void setControllerName(string $controller, bool $format_name = true)
public void setDispatched()
public void setModuleName(string $module, bool $format_name = true)
public bool setParam(string $name, string $value = ?)
public void setRequestUri(string $uir)
public void setRouted(string $flag = ?)
}

プロパティ

module

controller

action

method

params

language

_exception

_base_uri

uri

dispatched

routed

定義済み定数

Yaf_Request_Abstract::SCHEME_HTTP

Yaf_Request_Abstract::SCHEME_HTTPS

Yaf_Request_Http クラス

はじめに

クライアントからのあらゆるリクエストは、 Yaf_Request_Http として初期化されます。 URI のクエリ文字列や POST パラメータなどの情報は、このクラスのメソッド経由で取得できます。

注意:

セキュリティの観点から、Yaf では $_GET/$_POST が読み込み専用となっています。つまり、 これらのグローバル変数の値を書き換えたとしても、 Yaf_Request_Http::getQueryYaf_Request_Http::getPost からはそれを取得できないということです。

しかし、ユニットテストの際などに、どうしても書き換えが必要になることもあります。 Yaf のビルド時に --enable-yaf-debug を指定すると、ユーザーがスクリプトで設定した値も Yaf で読み込めるようになります。

その際に、Yaf は E_STRICT レベルの警告 「Strict Standards: you are running yaf in debug mode」を発します。

クラス概要

Yaf_Request_Http
class Yaf_Request_Http extends Yaf_Request_Abstract {
/* プロパティ */
/* メソッド */
public __construct(string $request_uri = ?, string $base_uri = ?)
public mixed get(string $name, string $default = ?)
public mixed getCookie(string $name, string $default = ?)
public void getFiles()
public mixed getPost(string $name, string $default = ?)
public mixed getQuery(string $name, string $default = ?)
public mixed getRaw()
public void getRequest()
public bool isXmlHttpRequest()
/* 継承したメソッド */
public bool Yaf_Request_Abstract::clearParams()
public void Yaf_Request_Abstract::getActionName()
public void Yaf_Request_Abstract::getBaseUri()
public void Yaf_Request_Abstract::getControllerName()
public void Yaf_Request_Abstract::getEnv(string $name, string $default = ?)
public void Yaf_Request_Abstract::getException()
public void Yaf_Request_Abstract::getLanguage()
public string Yaf_Request_Abstract::getMethod()
public void Yaf_Request_Abstract::getModuleName()
public mixed Yaf_Request_Abstract::getParam(string $name, string $default = ?)
public array Yaf_Request_Abstract::getParams()
public void Yaf_Request_Abstract::getRequestUri()
public void Yaf_Request_Abstract::getServer(string $name, string $default = ?)
public bool Yaf_Request_Abstract::isCli()
public bool Yaf_Request_Abstract::isDispatched()
public bool Yaf_Request_Abstract::isGet()
public bool Yaf_Request_Abstract::isHead()
public bool Yaf_Request_Abstract::isOptions()
public bool Yaf_Request_Abstract::isPost()
public bool Yaf_Request_Abstract::isPut()
public bool Yaf_Request_Abstract::isRouted()
public bool Yaf_Request_Abstract::isXmlHttpRequest()
public void Yaf_Request_Abstract::setActionName(string $action, bool $format_name = true)
public bool Yaf_Request_Abstract::setBaseUri(string $uir)
public void Yaf_Request_Abstract::setControllerName(string $controller, bool $format_name = true)
public void Yaf_Request_Abstract::setDispatched()
public void Yaf_Request_Abstract::setModuleName(string $module, bool $format_name = true)
public bool Yaf_Request_Abstract::setParam(string $name, string $value = ?)
public void Yaf_Request_Abstract::setRequestUri(string $uir)
public void Yaf_Request_Abstract::setRouted(string $flag = ?)
}

プロパティ

module

controller

action

method

params

language

_exception

_base_uri

uri

dispatched

routed

Yaf_Request_Simple クラス

はじめに

Yaf_Request_Simple は、 テスト用に使うためのクラスです。 つまり、特定のリクエストを CLI モードでシミュレートしたりするときに使います。

クラス概要

Yaf_Request_Simple
class Yaf_Request_Simple extends Yaf_Request_Abstract {
/* 定数 */
const string Yaf_Request_Simple::SCHEME_HTTP = http;
const string Yaf_Request_Simple::SCHEME_HTTPS = https;
/* プロパティ */
/* メソッド */
public __construct(
    string $method = ?,
    string $module = ?,
    string $controller = ?,
    string $action = ?,
    array $params = ?
)
public void get()
public void getCookie()
public void getFiles()
public void getPost()
public void getQuery()
public void getRequest()
public void isXmlHttpRequest()
/* 継承したメソッド */
public bool Yaf_Request_Abstract::clearParams()
public void Yaf_Request_Abstract::getActionName()
public void Yaf_Request_Abstract::getBaseUri()
public void Yaf_Request_Abstract::getControllerName()
public void Yaf_Request_Abstract::getEnv(string $name, string $default = ?)
public void Yaf_Request_Abstract::getException()
public void Yaf_Request_Abstract::getLanguage()
public string Yaf_Request_Abstract::getMethod()
public void Yaf_Request_Abstract::getModuleName()
public mixed Yaf_Request_Abstract::getParam(string $name, string $default = ?)
public array Yaf_Request_Abstract::getParams()
public void Yaf_Request_Abstract::getRequestUri()
public void Yaf_Request_Abstract::getServer(string $name, string $default = ?)
public bool Yaf_Request_Abstract::isCli()
public bool Yaf_Request_Abstract::isDispatched()
public bool Yaf_Request_Abstract::isGet()
public bool Yaf_Request_Abstract::isHead()
public bool Yaf_Request_Abstract::isOptions()
public bool Yaf_Request_Abstract::isPost()
public bool Yaf_Request_Abstract::isPut()
public bool Yaf_Request_Abstract::isRouted()
public bool Yaf_Request_Abstract::isXmlHttpRequest()
public void Yaf_Request_Abstract::setActionName(string $action, bool $format_name = true)
public bool Yaf_Request_Abstract::setBaseUri(string $uir)
public void Yaf_Request_Abstract::setControllerName(string $controller, bool $format_name = true)
public void Yaf_Request_Abstract::setDispatched()
public void Yaf_Request_Abstract::setModuleName(string $module, bool $format_name = true)
public bool Yaf_Request_Abstract::setParam(string $name, string $value = ?)
public void Yaf_Request_Abstract::setRequestUri(string $uir)
public void Yaf_Request_Abstract::setRouted(string $flag = ?)
}

プロパティ

module

controller

action

method

params

language

_exception

_base_uri

uri

dispatched

routed

定義済み定数

Yaf_Request_Simple::SCHEME_HTTP

Yaf_Request_Simple::SCHEME_HTTPS

Yaf_Response_Abstract クラス

はじめに

クラス概要

Yaf_Response_Abstract
class Yaf_Response_Abstract {
/* 定数 */
const string Yaf_Response_Abstract::DEFAULT_BODY = "content";
/* プロパティ */
protected $_header;
protected $_body;
protected $_sendheader;
/* メソッド */
public __construct()
public bool appendBody(string $content, string $key = ?)
public bool clearBody(string $key = ?)
public void clearHeaders()
public mixed getBody(string $key = ?)
public void getHeader()
public bool prependBody(string $content, string $key = ?)
public void response()
protected void setAllHeaders()
public bool setBody(string $content, string $key = ?)
public bool setHeader(string $name, string $value, bool $replace = ?)
public bool setRedirect(string $url)
private string __toString()
public __destruct()
}

プロパティ

_header

_body

_sendheader

Yaf_Route_Interface クラス

はじめに

Yaf_Route_Interface を使うと、開発者がカスタムルートを定義できるようになります。

クラス概要

Yaf_Route_Interface
class Yaf_Route_Interface {
/* メソッド */
abstract public string assemble(array $info, array $query = ?)
abstract public bool route(Yaf_Request_Abstract $request)
}

Yaf_Route_Map クラス

はじめに

Yaf_Route_Map は標準で組み込まれているルートです。 単純に URI エンドポイント (URI の中で、ベース URL の後に続く部分。 Yaf_Request_Abstract::setBaseUri を参照ください) をコントローラ名やアクション名に変換します (どちらになるかは Yaf_Route_Map::__construct に渡すパラメータで決まります)。 その規則は次のとおりです。 A => controller A. A/B/C => controller A_B_C. A/B/C/D/E => controller A_B_C_D_E.

Yaf_Route_Map::__construct の二番目のパラメータを指定すると、 URI の中でデリミタより前の部分だけを使ってルーティングを行います。 デリミタ以降の部分は、ルーティングリクエストのパラメータとして使います ( Yaf_Route_Map::__construct のサンプルを参照ください)。

クラス概要

Yaf_Route_Map
class Yaf_Route_Map implements Yaf_Route_Interface {
/* プロパティ */
protected $_ctl_router;
protected $_delimiter;
/* メソッド */
public __construct(string $controller_prefer = false, string $delimiter = "")
public string assemble(array $info, array $query = ?)
public bool route(Yaf_Request_Abstract $request)
}

プロパティ

_ctl_router

_delimiter

Yaf_Route_Regex クラス

はじめに

Yaf_Route_Regex は、 Yaf の組み込みのルートのなかでも一番柔軟性の高いものです。

クラス概要

Yaf_Route_Regex
class Yaf_Route_Regex extends Yaf_Route_Interface implements Yaf_Route_Interface {
/* プロパティ */
protected $_route;
protected $_default;
protected $_maps;
protected $_verify;
/* メソッド */
public __construct(
    string $match,
    array $route,
    array $map = ?,
    array $verify = ?,
    string $reverse = ?
)
public stringnull assemble(array $info, array $query = ?)
public bool route(Yaf_Request_Abstract $request)
/* 継承したメソッド */
abstract public string Yaf_Route_Interface::assemble(array $info, array $query = ?)
abstract public bool Yaf_Route_Interface::route(Yaf_Request_Abstract $request)
}

プロパティ

_route

_default

_maps

_verify

Yaf_Route_Rewrite クラス

はじめに

使いかたについては、 Yaf_Route_Rewrite::__construct のサンプルを参照ください。

クラス概要

Yaf_Route_Rewrite
class Yaf_Route_Rewrite extends Yaf_Route_Interface implements Yaf_Route_Interface {
/* プロパティ */
protected $_route;
protected $_default;
protected $_verify;
/* メソッド */
public __construct(string $match, array $route, array $verify = ?)
public string assemble(array $info, array $query = ?)
public bool route(Yaf_Request_Abstract $request)
/* 継承したメソッド */
abstract public string Yaf_Route_Interface::assemble(array $info, array $query = ?)
abstract public bool Yaf_Route_Interface::route(Yaf_Request_Abstract $request)
}

プロパティ

_route

_default

_verify

Yaf_Router クラス

はじめに

Yaf_Router は、フレームワークの標準のルーターです。 ルーティングとは、URI エンドポイント (URI の中で、ベース URL の後に続く部分。 Yaf_Request_Abstract::setBaseUri を参照ください) を受け取ってそこからパラメータを抽出し、 リクエストを受け取るモジュールやコントローラそしてアクションを判断する処理のことです。 モジュール、コントローラ、アクション、そしてその他のパラメータは Yaf_Request_Abstract オブジェクトにまとめられ、 そして Yaf_Dispatcher で処理します。 ルーティングが行われるのは一度だけで、リクエストを最初に受け取ってから 最初のコントローラにディスパッチする前に行われます。 Yaf_Router は、mod_rewrite 風の機能を PHP を使って実現できるような設計になっています。 Ruby on Rails のルーティング方式を参考にしており、 ウェブサーバーの URL リライト機能に関する事前知識は不要です。 Apache の場合は、次のような mod_rewrite ルールを書けば使えます。

例1 Apache 用のリライトルール

RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css|html)$ index.php
あるいは、次のようにもできます (こちらのほうを推奨します)。

例2 Apache 用のリライトルール

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Lighttpd の場合は、次のようなリライトルールを指定します。

例3 Lighttpd 用のリライトルール

url.rewrite-once = (
  ".*\?(.*)$" => "/index.php?$1",
  ".*\.(js|ico|gif|jpg|png|css|html)$" => "$0",
  "" => "/index.php"
)
Nginx の場合は、次のようなリライトルールを指定します。

例4 Nginx 用のリライトルール

server {
  listen ****;
  server_name  yourdomain.com;
  root   document_root;
  index  index.php index.html;

  if (!-e $request_filename) {
    rewrite ^/(.*)  /index.php/$1 last;
  }
}

デフォルトのルート

Yaf_Router には設定済みのデフォルトルート Yaf_Route_Static が用意されており、これは controller/action 形式の URI にマッチします。 さらに、モジュール名を最初のパス要素として指定できます。この場合の URI は module/controller/action 形式になります。 また、追加のパラメータを URI に追記できるようになっています。つまり controller/action/var1/value1/var2/value2 といった形式です。

注意:

モジュール名は設定ファイルで定義しておく必要があります。 たとえば application.module="Index,Foo,Bar" とすると、 index、foo、bar だけがモジュール名とみなされます。 設定をしなかった場合は、モジュール名とみなされるのは "Index" だけです。

ルートのマッチングの例を示します。

例5 Yaf_Route_Static のデフォルトルート

// このように設定しているものとします
$conf = array(
   "application" => array(
      "modules" => "Index,Blog",
   ),
);

コントローラのみ
http://example/news
    controller == news
アクションのみ (php.ini で yaf.action_prefer=1 とした場合)
    action  == news
 
モジュール名として無効な場合はコントローラ名とみなします
http://example/foo
    controller == foo
 
モジュール + コントローラ
http://example/blog/archive
    module     == blog
    controller == archive
 
モジュール + コントローラ + アクション
http://example/blog/archive/list
    module     == blog
    controller == archive
    action     == list
 
モジュール + コントローラ + アクション + パラメータ
http://example/blog/archive/list/sort/alpha/date/desc
    module     == blog
    controller == archive
    action     == list
    sort       == alpha
    date       == desc

クラス概要

Yaf_Router
class Yaf_Router {
/* プロパティ */
protected $_routes;
protected $_current;
/* メソッド */
public __construct()
public bool addConfig(Yaf_Config_Abstract $config)
public bool addRoute(string $name, Yaf_Route_Abstract $route)
public string getCurrentRoute()
public Yaf_Route_Interface getRoute(string $name)
public mixed getRoutes()
public bool route(Yaf_Request_Abstract $request)
}

プロパティ

_routes

登録されたルートスタック。

_current

ルーティングを終えた後に、 今回のリクエストで使ったルートの名前がここに入ります。 この名前を取得するには Yaf_Router::getCurrentRoute を使います。

Yaf_Route_Simple クラス

はじめに

Yaf_Route_Simple は、クエリ文字列を見てルート情報を取得します。

Yaf_Route_Simple に対して必要な設定は、 $_GET のどのキーがモジュールでどのキーがコントローラ、 そしてどのキーがアクションなのかを指定することです。

Yaf_Route_Simple::route は常に true を返します。 そこで、Yaf_Route_Simple はルートスタックの先頭に置く必要があります。 そうしないと、他のルートは決して呼ばれなくなります。

クラス概要

Yaf_Route_Simple
class Yaf_Route_Simple implements Yaf_Route_Interface {
/* プロパティ */
protected $controller;
protected $module;
protected $action;
/* メソッド */
public __construct(string $module_name, string $controller_name, string $action_name)
public string assemble(array $info, array $query = ?)
public bool route(Yaf_Request_Abstract $request)
}

プロパティ

controller

module

action

Yaf_Route_Static クラス

はじめに

デフォルトでは、Yaf_Router が持っているルートは Yaf_Route_Static だけです。

Yaf_Route_Static は、 要件の 80% を満たせるように作られています。

Yaf_Route_Static のインスタンスを作る必要はありません。 また、Yaf_Router のルートスタックに追加する必要もありません。 というのもこのクラスは常に Yaf_Router のルートスタックに含まれており、最後に呼ばれるようになっているからです。

クラス概要

Yaf_Route_Static
class Yaf_Route_Static implements Yaf_Router {
/* メソッド */
public string assemble(array $info, array $query = ?)
public void match(string $uri)
public bool route(Yaf_Request_Abstract $request)
}

Yaf_Route_Supervar クラス

はじめに

クラス概要

Yaf_Route_Supervar
class Yaf_Route_Supervar implements Yaf_Route_Interface {
/* プロパティ */
protected $_var_name;
/* メソッド */
public __construct(string $supervar_name)
public string assemble(array $info, array $query = ?)
public bool route(Yaf_Request_Abstract $request)
}

プロパティ

_var_name

Yaf_Session クラス

はじめに

クラス概要

Yaf_Session
class Yaf_Session implements Iterator, ArrayAccess, Countable {
/* プロパティ */
protected static $_instance;
protected $_session;
protected $_started;
/* メソッド */
private __construct()
public void count()
public void current()
public void del(string $name)
public void __get(string $name)
public static void getInstance()
public void has(string $name)
public void __isset(string $name)
public void key()
public void next()
public void offsetExists(string $name)
public void offsetGet(string $name)
public void offsetSet(string $name, string $value)
public void offsetUnset(string $name)
public void rewind()
public void __set(string $name, string $value)
public void start()
public void __unset(string $name)
public void valid()
}

プロパティ

_instance

_session

_started

Yaf_Exception クラス

はじめに

クラス概要

Yaf_Exception
class Yaf_Exception extends Exception {
/* 継承したプロパティ */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private Throwablenull $previous = null;
/* メソッド */
public __construct()
public void getPrevious()
/* 継承したメソッド */
final public string Exception::getMessage()
final public Throwablenull Exception::getPrevious()
final public int Exception::getCode()
final public string Exception::getFile()
final public int Exception::getLine()
final public array Exception::getTrace()
final public string Exception::getTraceAsString()
public string Exception::__toString()
private void Exception::__clone()
}

Yaf_Exception_TypeError クラス

はじめに

クラス概要

Yaf_Exception_TypeError
class Yaf_Exception_TypeError extends Yaf_Exception {
/* プロパティ */
/* メソッド */
/* 継承したメソッド */
public void Yaf_Exception::getPrevious()
}

Yaf_Exception_StartupError クラス

はじめに

クラス概要

Yaf_Exception_StartupError
class Yaf_Exception_StartupError extends Yaf_Exception {
/* プロパティ */
/* メソッド */
/* 継承したメソッド */
public void Yaf_Exception::getPrevious()
}

Yaf_Exception_DispatchFailed クラス

はじめに

クラス概要

Yaf_Exception_DispatchFailed
class Yaf_Exception_DispatchFailed extends Yaf_Exception {
/* プロパティ */
/* メソッド */
/* 継承したメソッド */
public void Yaf_Exception::getPrevious()
}

Yaf_Exception_RouterFailed クラス

はじめに

クラス概要

Yaf_Exception_RouterFailed
class Yaf_Exception_RouterFailed extends Yaf_Exception {
/* プロパティ */
/* メソッド */
/* 継承したメソッド */
public void Yaf_Exception::getPrevious()
}

Yaf_Exception_LoadFailed クラス

はじめに

クラス概要

Yaf_Exception_LoadFailed
class Yaf_Exception_LoadFailed extends Yaf_Exception {
/* プロパティ */
/* メソッド */
/* 継承したメソッド */
public void Yaf_Exception::getPrevious()
}

Yaf_Exception_LoadFailed_Module クラス

はじめに

クラス概要

Yaf_Exception_LoadFailed_Module
class Yaf_Exception_LoadFailed_Module extends Yaf_Exception_LoadFailed {
/* プロパティ */
/* メソッド */
/* 継承したメソッド */
public void Yaf_Exception::getPrevious()
}

Yaf_Exception_LoadFailed_Controller クラス

はじめに

クラス概要

Yaf_Exception_LoadFailed_Controller
class Yaf_Exception_LoadFailed_Controller extends Yaf_Exception_LoadFailed {
/* プロパティ */
/* メソッド */
/* 継承したメソッド */
public void Yaf_Exception::getPrevious()
}

Yaf_Exception_LoadFailed_Action クラス

はじめに

クラス概要

Yaf_Exception_LoadFailed_Action
class Yaf_Exception_LoadFailed_Action extends Yaf_Exception_LoadFailed {
/* プロパティ */
/* メソッド */
/* 継承したメソッド */
public void Yaf_Exception::getPrevious()
}

Yaf_Exception_LoadFailed_View クラス

はじめに

クラス概要

Yaf_Exception_LoadFailed_View
class Yaf_Exception_LoadFailed_View extends Yaf_Exception_LoadFailed {
/* プロパティ */
/* メソッド */
/* 継承したメソッド */
public void Yaf_Exception::getPrevious()
}