I read about W3C Web Components spec and JavaScript's libraries like polymer.js and others, so I tried to find something similar in PHP, but with no luck.
So I wrote a tiny class to work with custom tags in PHP and I'd like to know what do you think about it. Is the idea somehow reasonable?
public static function loadString($string, array $context = array()) {
$context = new Context($context);
//eval the first time
$string = self::evall($string, $context);
//eval 'til there are custom tags
while ($matches = self::matchWebComp($string)) {
$context = new Context($matches);
$context->setArray(Context::fromAttributesString($context->attributesList)->toArray());
$webComp = self::getWebCompDefinition($context->tag);
$webComp = self::evall($webComp, $context);
$string = str_replace($context->match, $webComp, $string);
}
return $string;
}
public static function evall($string, Context $context = NULL) {
ob_start();
eval('; ?>' . $string);
return ob_get_clean();
}
public static function matchWebComp($string) {
//match <a-tag [attributes]>[inner html]</a-tag>
if (preg_match('/<(?P<tag>\w+-[\w\-]+)( *.*)>([\s\S]*)<\/(?P=tag)>/U', $string, $matches)) {
return array(
'match' => $matches[0],
'tag' => $matches[1],
'attributesList' => $matches[2],
'innerHtml' => $matches[3]
);
}
}