<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/yaf.tutorials.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'yaf.tutorials.plugins.php',
    1 => '插件',
    2 => '插件',
  ),
  'up' => 
  array (
    0 => 'yaf.tutorials.php',
    1 => '示例',
  ),
  'prev' => 
  array (
    0 => 'yaf.tutorials.routes.php',
    1 => '自定义路由',
  ),
  'next' => 
  array (
    0 => 'yaf.appconfig.php',
    1 => '应用程序配置',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/yaf/tutorials.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="yaf.tutorials.plugins" class="section">
  <h2 class="title">插件</h2>

 <div class="example" id="example-1">
  <p><strong>示例 #1 插件</strong></p>
  <div class="example-contents"><p>
   插件通过继承 <span class="classname"><a href="class.yaf-plugin-abstract.php" class="classname">Yaf_Plugin_Abstract</a></span> 并重写其六个
   钩子方法中的一个或多个来挂入分发循环，然后通常在 Bootstrap 中使用
   <span class="methodname"><a href="yaf-dispatcher.registerplugin.php" class="methodname">Yaf_Dispatcher::registerPlugin()</a></span> 注册到分发器上：
  </p></div>
  <div class="example-contents">
<div class="phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
</span><span style="color: #007700">class </span><span style="color: #0000BB">UserPlugin </span><span style="color: #007700">extends </span><span style="color: #0000BB">Yaf_Plugin_Abstract
</span><span style="color: #007700">{
    public function </span><span style="color: #0000BB">routerStartup</span><span style="color: #007700">(
        </span><span style="color: #0000BB">Yaf_Request_Abstract $request</span><span style="color: #007700">, </span><span style="color: #0000BB">Yaf_Response_Abstract $response</span><span style="color: #007700">)
    {
        </span><span style="color: #FF8000">// before routing: inspect or rewrite the raw request URI
    </span><span style="color: #007700">}

    public function </span><span style="color: #0000BB">routerShutdown</span><span style="color: #007700">(
        </span><span style="color: #0000BB">Yaf_Request_Abstract $request</span><span style="color: #007700">, </span><span style="color: #0000BB">Yaf_Response_Abstract $response</span><span style="color: #007700">)
    {
        </span><span style="color: #FF8000">// after routing: module/controller/action are known, good for per-route access control
    </span><span style="color: #007700">}

    public function </span><span style="color: #0000BB">dispatchLoopStartup</span><span style="color: #007700">(
        </span><span style="color: #0000BB">Yaf_Request_Abstract $request</span><span style="color: #007700">, </span><span style="color: #0000BB">Yaf_Response_Abstract $response</span><span style="color: #007700">)
    {
        </span><span style="color: #FF8000">// before the dispatch loop: one-time setup shared by all dispatches
    </span><span style="color: #007700">}

    public function </span><span style="color: #0000BB">preDispatch</span><span style="color: #007700">(
        </span><span style="color: #0000BB">Yaf_Request_Abstract $request</span><span style="color: #007700">, </span><span style="color: #0000BB">Yaf_Response_Abstract $response</span><span style="color: #007700">)
    {
        </span><span style="color: #FF8000">// before each dispatch: filter parameters or redirect before the action runs
    </span><span style="color: #007700">}

    public function </span><span style="color: #0000BB">postDispatch</span><span style="color: #007700">(
        </span><span style="color: #0000BB">Yaf_Request_Abstract $request</span><span style="color: #007700">, </span><span style="color: #0000BB">Yaf_Response_Abstract $response</span><span style="color: #007700">)
    {
        </span><span style="color: #FF8000">// after each dispatch: decorate the response or the rendered view
    </span><span style="color: #007700">}

    public function </span><span style="color: #0000BB">dispatchLoopShutdown</span><span style="color: #007700">(
        </span><span style="color: #0000BB">Yaf_Request_Abstract $request</span><span style="color: #007700">, </span><span style="color: #0000BB">Yaf_Response_Abstract $response</span><span style="color: #007700">)
    {
        </span><span style="color: #FF8000">// after the dispatch loop: final cleanup, logging or flushing
    </span><span style="color: #007700">}
}

class </span><span style="color: #0000BB">Bootstrap </span><span style="color: #007700">extends </span><span style="color: #0000BB">Yaf_Bootstrap_Abstract
</span><span style="color: #007700">{
    public function </span><span style="color: #0000BB">_initPlugin</span><span style="color: #007700">(</span><span style="color: #0000BB">Yaf_Dispatcher $dispatcher</span><span style="color: #007700">)
    {
        </span><span style="color: #0000BB">$dispatcher</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">registerPlugin</span><span style="color: #007700">(new </span><span style="color: #0000BB">UserPlugin</span><span style="color: #007700">());
    }
}
</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
  </div>

 </div>

 </div><?php manual_footer($setup); ?>