听起来您需要创建自己的视图助手,扩展标准 Zend FrameworkHeadMeta
查看助手,并实现一个名为appendProperty()
,模仿的行为appendName()
.
自从appendName()
方法似乎是在__call()
方法,看起来你的扩展类可以简单地复制相同的__call()
形成父级,但更改中使用的模式preg_match()
from:
'/^(?P<action>set|(pre|ap)pend|offsetSet)(?P<type>Name|HttpEquiv)$/'
to
'/^(?P<action>set|(pre|ap)pend|offsetSet)(?P<type>Name|HttpEquiv|Property)$/'
[顺便说一句,可能值得向 ZF 跟踪器提出问题,建议从内联代码中提取此正则表达式模式,并将其放置为类的受保护成员。这样,子类(例如您的子类)可以简单地声明一个新模式,而不是“复制”如此多的父代码。但在我向他们建议之前,我必须进行更多的观察和测试。]
无论如何,只是在黑暗中刺一下......
更新:2010-12-17
我发现需要做更多的工作才能使其发挥作用。您需要覆盖受保护的成员$_typeKeys
和受保护的方法_normalizeType()
处理您的新“财产”类型。
您的扩展类可能如下所示:
class Kwis_View_Helper_HeadMeta extends Zend_View_Helper_HeadMeta
{
protected $_typeKeys = array('name', 'http-equiv', 'charset', 'property');
public function __call($method, $args)
{
if (preg_match('/^(?P<action>set|(pre|ap)pend|offsetSet)(?P<type>Name|HttpEquiv|Property)$/', $method, $matches)) {
$action = $matches['action'];
$type = $this->_normalizeType($matches['type']);
$argc = count($args);
$index = null;
if ('offsetSet' == $action) {
if (0 < $argc) {
$index = array_shift($args);
--$argc;
}
}
if (2 > $argc) {
require_once 'Zend/View/Exception.php';
$e = new Zend_View_Exception('Too few arguments provided; requires key value, and content');
$e->setView($this->view);
throw $e;
}
if (3 > $argc) {
$args[] = array();
}
$item = $this->createData($type, $args[0], $args[1], $args[2]);
if ('offsetSet' == $action) {
return $this->offsetSet($index, $item);
}
$this->$action($item);
return $this;
}
return parent::__call($method, $args);
}
protected function _normalizeType($type)
{
switch ($type) {
case 'Property':
return 'property';
default:
return parent::_normalizeType($type);
}
}
}
正如之前所观察到的,如果preg_match()
签入的模式Zend_View_Helper_HeadMeta::__call()
被分解为一个受保护的成员,称为类似的东西$_callPattern
。那么扩展类就不必复制大部分__call()
方法。它只需要覆盖受保护的成员$_typeKeys
and $_callPattern
并实现受保护的方法_normalizeType()
,如上图所示。