ZendFramework-2.4 原始碼 - 關於MVC - View層 - 視圖渲染器、視圖外掛程式管理器

來源:互聯網
上載者:User

標籤:ace   top   json   action   erer   return   route   ati   doctype   

 

<?php// 1. 視圖渲染器class PhpRenderer implements Renderer, TreeRendererInterface{    /**     * 外掛程式管理器     */    public function getHelperPluginManager()    {        if (null === $this->__helpers) {// false            $this->setHelperPluginManager(new HelperPluginManager());        }        return $this->__helpers;    }        /**     * 擷取外掛程式     */    public function plugin($name, array $options = null)    {        // Zend\Mvc\Service\ViewHelperManagerFactory        // Zend\View\HelperPluginManager        return $this->getHelperPluginManager()->get($name, $options);    }        /**     * 渲染     */    public function render($nameOrModel, $values = null)    {        // ....        try {            ob_start();            $includeReturn = include $this->__file; // 執行模板            $this->__content = ob_get_clean();        } catch (\Exception $ex) {            ob_end_clean();            throw $ex;        }        // ....    }}// 2. 外掛程式管理器// 外掛程式管理器建立工廠class ViewHelperManagerFactory extends AbstractPluginManagerFactory{    const PLUGIN_MANAGER_CLASS = ‘Zend\View\HelperPluginManager‘;        protected $defaultHelperMapClasses = array(        ‘Zend\Form\View\HelperConfig‘,        ‘Zend\I18n\View\HelperConfig‘,        ‘Zend\Navigation\View\HelperConfig‘    );        public function createService(ServiceLocatorInterface $serviceLocator)    {        $plugins = parent::createService($serviceLocator);        // 注入外掛程式管理器的預設配置        foreach ($this->defaultHelperMapClasses as $configClass) {            if (is_string($configClass) && class_exists($configClass)) {                $config = new $configClass;                $config->configureServiceManager($plugins);            }        }                // url外掛程式的建立工廠 Configure URL view helper with router        $plugins->setFactory(‘url‘, function () use ($serviceLocator) {            $helper = new ViewHelper\Url; // Zend\View\Helper\Url             $router = Console::isConsole() ? ‘HttpRouter‘ : ‘Router‘;            $helper->setRouter($serviceLocator->get($router)); // 注入路由對象            $match = $serviceLocator->get(‘application‘)                ->getMvcEvent()                ->getRouteMatch()            ;            if ($match instanceof RouteMatch) {                $helper->setRouteMatch($match); // 注入匹配到的路由對象            }            return $helper;        });        // basepath外掛程式的建立工廠        $plugins->setFactory(‘basepath‘, function () use ($serviceLocator) {            //...            return $basePathHelper;        });        // doctype外掛程式的建立工廠        $plugins->setFactory(‘doctype‘, function () use ($serviceLocator) {            //...            return $doctypeHelper;        });    }}// 外掛程式管理器class ServiceManager implements ServiceLocatorInterface{    public function __construct(ConfigInterface $config = null)    {        if ($config) {            $config->configureServiceManager($this);        }    }}abstract class AbstractPluginManager extends ServiceManager implements ServiceLocatorAwareInterface{    public function __construct(ConfigInterface $configuration = null)    {        parent::__construct($configuration);        $self = $this;        $this->addInitializer(function ($instance) use ($self) {//!!! 給外掛程式對象注入外掛程式管理器            if ($instance instanceof ServiceLocatorAwareInterface) {                $instance->setServiceLocator($self);            }        });    }}class HelperPluginManager extends AbstractPluginManager{    protected $factories = array(        ‘flashmessenger‘ => ‘Zend\View\Helper\Service\FlashMessengerFactory‘,        ‘identity‘       => ‘Zend\View\Helper\Service\IdentityFactory‘,    );    protected $invokableClasses = array(        ‘basepath‘            => ‘Zend\View\Helper\BasePath‘,        ‘cycle‘               => ‘Zend\View\Helper\Cycle‘,        ‘declarevars‘         => ‘Zend\View\Helper\DeclareVars‘,        ‘doctype‘             => ‘Zend\View\Helper\Doctype‘, // overridden by a factory in ViewHelperManagerFactory        ‘escapehtml‘          => ‘Zend\View\Helper\EscapeHtml‘,        ‘escapehtmlattr‘      => ‘Zend\View\Helper\EscapeHtmlAttr‘,        ‘escapejs‘            => ‘Zend\View\Helper\EscapeJs‘,        ‘escapecss‘           => ‘Zend\View\Helper\EscapeCss‘,        ‘escapeurl‘           => ‘Zend\View\Helper\EscapeUrl‘,        ‘gravatar‘            => ‘Zend\View\Helper\Gravatar‘,        ‘htmltag‘             => ‘Zend\View\Helper\HtmlTag‘,        ‘headlink‘            => ‘Zend\View\Helper\HeadLink‘,        ‘headmeta‘            => ‘Zend\View\Helper\HeadMeta‘,        ‘headscript‘          => ‘Zend\View\Helper\HeadScript‘,        ‘headstyle‘           => ‘Zend\View\Helper\HeadStyle‘,        ‘headtitle‘           => ‘Zend\View\Helper\HeadTitle‘,        ‘htmlflash‘           => ‘Zend\View\Helper\HtmlFlash‘,        ‘htmllist‘            => ‘Zend\View\Helper\HtmlList‘,        ‘htmlobject‘          => ‘Zend\View\Helper\HtmlObject‘,        ‘htmlpage‘            => ‘Zend\View\Helper\HtmlPage‘,        ‘htmlquicktime‘       => ‘Zend\View\Helper\HtmlQuicktime‘,        ‘inlinescript‘        => ‘Zend\View\Helper\InlineScript‘,        ‘json‘                => ‘Zend\View\Helper\Json‘,        ‘layout‘              => ‘Zend\View\Helper\Layout‘,        ‘paginationcontrol‘   => ‘Zend\View\Helper\PaginationControl‘,        ‘partialloop‘         => ‘Zend\View\Helper\PartialLoop‘,        ‘partial‘             => ‘Zend\View\Helper\Partial‘,        ‘placeholder‘         => ‘Zend\View\Helper\Placeholder‘,        ‘renderchildmodel‘    => ‘Zend\View\Helper\RenderChildModel‘,        ‘rendertoplaceholder‘ => ‘Zend\View\Helper\RenderToPlaceholder‘,        ‘serverurl‘           => ‘Zend\View\Helper\ServerUrl‘,        ‘url‘                 => ‘Zend\View\Helper\Url‘,        ‘viewmodel‘           => ‘Zend\View\Helper\ViewModel‘,    );            public function __construct(ConfigInterface $configuration = null)    {        parent::__construct($configuration);            $this->addInitializer(array($this, ‘injectRenderer‘)) // 給外掛程式對象注入視圖渲染器            ->addInitializer(array($this, ‘injectTranslator‘)); // 給外掛程式對象注入語言翻譯器    }        // 視圖渲染器    public function getRenderer()    {        return $this->renderer;    }}?>//3.外掛程式管理器的使用// 在模板中 moduel1/ctrl1/action1.phtml中使用<div>hello,this is view plugin demo:</div><?php     // view_model 視圖外掛程式    $helper = $this->plugin(‘view_model‘);    $helper->setCurrent($model);        // url 視圖外掛程式    // case.0    $helper = $this->plugin(‘url‘);    echo $helper(‘album‘, array(‘action‘=>‘add‘));    // case.1    echo $this->url(‘album‘, array(‘action‘=>‘add‘));        // basepath 視圖外掛程式    $helper = $this->plugin(‘basepath‘);    $helper->setCurrent($model);        ?>

 

ZendFramework-2.4 原始碼 - 關於MVC - View層 - 視圖渲染器、視圖外掛程式管理器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.