码迷,mamicode.com
首页 > Web开发 > 详细

ZendFramework-2.4 源代码 - 关于MVC - View层 - 视图渲染器、视图插件管理器

时间:2017-02-10 20:24:20      阅读:341      评论:0      收藏:0      [点我收藏+]

标签: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层 - 视图渲染器、视图插件管理器

标签:ace   top   json   action   erer   return   route   ati   doctype   

原文地址:http://www.cnblogs.com/xiaoyaogege/p/6387579.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!