Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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?

Here is the Github repo.

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]
          );
      }
}
share|improve this question
    
I'm not sure if there is much point to this library - the 'magic' of Web Components is partly that elements have methods, and that the inside code of an element can be hidden.Replacing a custom element with its Shadow DOM representation before render kinda defeats the point with my understanding anyway! –  Rich Bradshaw Mar 19 '14 at 20:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.