<?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层 - 视图渲染器、视图插件管理器的更多相关文章

  1. ZendFramework-2.4 源代码 - 关于MVC - View层 - 控制器返回值

    <?php class ReturnController extends AbstractActionController { public function returnAction() { ...

  2. ZendFramework-2.4 源代码 - 关于MVC - View层 - 在模板内渲染子模板

    <?php // 方式一: // 1.在模板内直接编写如下内容即可 $viewModel = new ViewModel(); $viewModel->setTemplate('album ...

  3. ZendFramework-2.4 源代码 - 关于MVC - Controller层

    // 1.控制器管理器 class ServiceManager implements ServiceLocatorInterface { public function __construct(Co ...

  4. 订单业务楼层化 view管理器和model管理器进行了model和view的全面封装处理 三端不得不在每个业务入口上线时约定好降级开关,于是代码中充满了各种各样的降级开关字段

    京东APP订单业务楼层化技术实践解密 原创 杜丹 留成 博侃 京东零售技术 2020-09-29 https://mp.weixin.qq.com/s/2oExMjh70Kyveiwh8wOBVA 用 ...

  5. ZendFramework-2.4 源代码 - 关于MVC - Model层类图

  6. ZendFramework-2.4 源代码 - 关于MVC - Model层

    所谓的谓词Predicate // ------ 所谓的谓词 ------ // 条件 case.3 $where = new \Zend\Db\Sql\Where(); $expression = ...

  7. spring mvc DispatcherServlet详解之四---视图渲染过程

    整个spring mvc的架构如下图所示: 现在来讲解DispatcherServletDispatcherServlet的最后一步:视图渲染.视图渲染的过程是在获取到ModelAndView后的过程 ...

  8. MVC+EF 随笔小计——NuGet程序包管理

    安装EF 打开 工具-库程序包管理器-程序包管理器控制台 输入 install-package entityframework 去MSDN上查看下EF的架构图:http://msdn.microsof ...

  9. 《ASP.NET MVC 5 破境之道》:第一境 ASP.Net MVC5项目初探 — 第三节:View层简单改造

    第一境 ASP.Net MVC5项目初探 — 第三节:View层简单改造 MVC默认模板的视觉设计从MVC1到MVC3都没有改变,比较陈旧了:在MVC4中做了升级,好看些,在不同的分辨率下,也能工作得 ...

随机推荐

  1. javaweb项目导入myecplise出错

    项目导入出错:移动项目的时候.classpath .project不要删 build path -> use for source floder 把某一文件夹当作源码文件夹 bulid path ...

  2. 用 Java 实现断点续传参考 (HTTP)

    断点续传的原理 其实断点续传的原理很简单,就是在 Http 的请求上和一般的下载有所不同而已.        打个比方,浏览器请求服务器上的一个文时,所发出的请求如下:        假设服务器域名为 ...

  3. C#面试常见题

    1. 简述 private. protected. public. internal 修饰符的访问权限. 答: private : 私有成员, 在类的内部才可以访问. protected : 保护成员 ...

  4. SpringBoot | 第二十六章:邮件发送

    前言 讲解了日志相关的知识点后.今天来点相对简单的,一般上,我们在开发一些注册功能.发送验证码或者订单服务时,都会通过短信或者邮件的方式通知消费者,注册或者订单的相关信息.而且基本上邮件的内容都是模版 ...

  5. xenserver 更新源

    在xenserver上安装vnc软件时,报错 [root@cloud yum-3.4.3]# ./yummain.py install yumThere are no enabled repos.Ru ...

  6. JavaScript初始

    一.JavaScript基础 1.引入方式 <!--1 直接编写--> <script> alert('hello yuan') </script> <!-- ...

  7. JSP的使用

    JSP全称为(Java server page),之所以出现JSP,最主要的目的是抽离出Servlet中输出HTML的部分,由于之前响应客户端的时候,是直接在Servlet中利用response.ge ...

  8. Windows8 64位运行Silverlight程序不能访问WCF的解决方案

    公司的项目是Silverlight+WCF,而我的本本是Win8 64位系统,一直无法正常运行Silverlight程序,一个同事找到了方案,现分享出来 一种情况是,Vs2010运行程序时,报无法加载 ...

  9. 【Android车载系统 News | Tech 5】车载设计开发

    1. 基于Android的车载移动终端系统的研究与开发 http://wenku.baidu.com/link?url=hIKlQ2myEmR8N0CA28a_SLzLA2Q9R5Xpk20OJ53h ...

  10. 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:6.技术简介之Protobuf

    欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 protocolbuffer(以下简称Protobuf)是google 的一种数据交换的格式,它独立于语言,独立于平 ...