ZF-10839: Subclasses of HeadMeta view helper could implement new meta types more easily by refactoring a single preg_match() pattern into a protected member.
Description
A recent question on StackOverflow asked about how to make the HeadMeta view helper output a tag like:
<meta property="mykey" content="myvalue">
Essentially, adding a new "property" as a new type of meta key.
The solution - subclassing HeadMeta - is straight-forward enough:
- Modify the preg_match pattern used in the __call() method
- Override the protected member $_keyTypes
- Override the protected method _normalizeType().
But the preg_match pattern used in Zend_View_Helper_HeadMeta::__call() method is inline. To override it, the subclass ends up duplicating nearly the entire __call method, simply to change the pattern from:
'/^(?Pset|(pre|ap)pend|offsetSet)(?PName|HttpEquiv)$/'
to
'/^(?Pset|(pre|ap)pend|offsetSet)(?PName|HttpEquiv|Property)$/'
It would be easier if this pattern were refactored out into a protected member. Then subclass would only have to override the protected member rather than duplicate so much of the parent's__call() method.
Just a thought...
Thanks and cheers! ;-)
Comments
Posted by David Weinraub (papayasoft) on 2010-12-16T23:14:00.000+0000
I have a patch (based on what I currently see in trunk, r23526) that implements the change:
This patch file was generated by NetBeans IDE
It uses platform neutral UTF-8 encoding and \n newlines.
--- Base (BASE) +++ Locally Modified (Based On LOCAL) @@ -42,6 +42,7 @@ protected $_typeKeys = array('name', 'http-equiv', 'charset'); protected $_requiredKeys = array('content'); protected $_modifierKeys = array('lang', 'scheme'); + protected $_callPattern = '/^(?Pset|(pre|ap)pend|offsetSet)(?PName|HttpEquiv)$/';
@@ -125,7 +126,7 @@ */ public function __call($method, $args) { - if (preg_match('/^(?Pset|(pre|ap)pend|offsetSet)(?PName|HttpEquiv)$/', $method, $matches)) { + if (preg_match($this->_callPattern, $method, $matches)) { $action = $matches['action']; $type = $this->_normalizeType($matches['type']); $argc = count($args);
But I confess to being such a noob that I'm not sure that I even created/submitted the patch right. And I regret that I do not have unit-testing for the framework all set up on my dev environment. Please be kind. ;-)
Posted by David Weinraub (papayasoft) on 2010-12-16T23:21:33.000+0000
I obviously botched the patch submission. Obviously needed some wiki markup. D'oh.
Trying again. As before, patch relative to r23526:
# This patch file was generated by NetBeans IDE # It uses platform neutral UTF-8 encoding and \n newlines. --- Base (BASE) +++ Locally Modified (Based On LOCAL) @@ -42,6 +42,7 @@ protected $_typeKeys = array('name', 'http-equiv', 'charset'); protected $_requiredKeys = array('content'); protected $_modifierKeys = array('lang', 'scheme'); + protected $_callPattern = '/^(?Pset|(pre|ap)pend|offsetSet)(?PName|HttpEquiv)$/'; /** * @var string registry key @@ -125,7 +126,7 @@ */ public function __call($method, $args) { - if (preg_match('/^(?Pset|(pre|ap)pend|offsetSet)(?PName|HttpEquiv)$/', $method, $matches)) { + if (preg_match($this->_callPattern, $method, $matches)) { $action = $matches['action']; $type = $this->_normalizeType($matches['type']); $argc = count($args);Aaah, well that's a little better. ;-)
Posted by David Weinraub (papayasoft) on 2010-12-30T21:37:36.000+0000
Correct spelling error in the title. (In the title, for crying out loud! I need to be more careful)
Posted by Weber Chris (chris.lu) on 2011-02-13T11:44:25.000+0000
There is a method "appendProperty" in the headMeta helper that could be used for this Link to documentation that shows an example: http://framework.zend.com/manual/en/…
Unfotunatly it doesn't work because the helper only allows this for xhtml1_rdfa doctypes, if you want to use it on your html5 website because for example you want to include the facebook iLike button you should extend the headMeta helper and override the _isValid Method
i replaced those lines:
with these lines:
you could use this code:
Posted by David Weinraub (papayasoft) on 2011-02-13T21:03:18.000+0000
Well, I'm clearly a total bonehead. I was looking at an earlier version.
[Side note: Pleased to see that the implementation of "appendProperty()" is pretty much as I was suggesting: per modification of the regex in the "__call()" method.]
Still, I guess this resolves the issue, eh?
Thanks and cheers!