Example #1
0
 /**
  * Run the Lexer and gather the block objects
  * @param  blob 	$source   	template contents
  * @param  string 	$template 	path to template
  * @return object|string        BlockSet object or path to cached template
  */
 public static function run($template, $data = array(), array $options = array())
 {
     /**
      * Lets check for cache, if exists then we'll return the cache file name
      * @var Cache
      */
     $options += array('type' => 'html');
     $_cache = new Cache();
     $file = static::_template($template);
     $_cacheFile = sha1($file . $options['type']);
     if ($_isCached = $_cache->file($_cacheFile) and $_cache->cache()) {
         return $_isCached;
     }
     /**
      * Guess there was no cache file, lets lex this mother.
      */
     $source = self::_read($template);
     $_blockSet = static::$_blockSet;
     $template = self::_template($template);
     // Set the final template, this gets reset on every iteration to the current template
     // landing, finally, on the last.
     static::$_blockSet->templates($template);
     foreach (static::$_terminals as $pattern => $terminal) {
         // attempt to match block/parent regex
         $_preg = $terminal == "T_BLOCK" ? preg_match_all($pattern, $source, $matches) : preg_match($pattern, $source, $matches);
         if ($_preg) {
             // Blocks, load them in the BlockSet
             if ($terminal == "T_BLOCK") {
                 foreach ($matches[2] as $index => $name) {
                     /**
                      * Block parameters
                      * Options that can be set to modify how the block is rendered
                      * @var [type]
                      */
                     $params = explode(',', $matches[3][$index]);
                     $params = array_map(function ($param) {
                         return trim($param);
                     }, $params);
                     static::$_blockSet->push($name, $matches[4][$index], $template, $params);
                 }
                 // Parent templates, read these and throw them back to the lexer.
             } else {
                 if (!empty(static::$_hierarchy)) {
                     list($type, $file) = array_slice($matches, 2);
                     if (empty($type)) {
                         $type = 'template';
                     } else {
                         $type = trim($type);
                     }
                     // override the controller if more than simple file name is passed in. First directory becomes controller
                     $file = explode('/', ltrim($file, '/'));
                     if (count($file) > 1) {
                         $options['controller'] = array_shift($file);
                     }
                     // set filename to template/layout
                     $file = implode('/', $file);
                     preg_match('/(' . implode('|', Media::types()) . ')\\.php$/', $file, $extensions);
                     if (count($extensions) == 2) {
                         // remove .{:type}.php
                         $file = substr($file, 0, strlen($file) - strlen($extensions[0]) - 1);
                         // set type to render as extension specified
                         $options['type'] = $extensions[1];
                     }
                     $options[$type] = $file;
                     // template path from \lithium\template\view\adapter\File
                     $_template = static::$_hierarchy->template($type, $options);
                     // if $_hierarchy not passed in, use template as is
                 } else {
                     $_template = $matches[3];
                 }
                 static::run($_template, $data, $options);
             }
         }
     }
     return static::$_blockSet;
 }
Example #2
0
 /**
  * Replaces blocks with content from `BlockSet`
  * @param  blob $source template source
  * @param  bool $final `true` if this template is this the last template
  * @param  array $options
  * @return blob         template source, replaced with blocks
  */
 private static function _replace($source, $i, $options)
 {
     $cache = new Cache();
     $cachePath = $cache->cacheDir() . "/template/";
     $pattern = "/{:(block) \"({:block})\"(?: \\[(.+)\\])?}(.*){\\1:}/msU";
     preg_match_all(static::$_terminals['T_BLOCK'], $source, $matches);
     $_blocks = null;
     foreach ($matches[2] as $index => $block) {
         $_pattern = String::insert($pattern, array('block' => $block));
         $_block = static::$_blocks->blocks("{$block}");
         $_blocks = static::$_blocks;
         /**
          * Top level content for block
          * @var string
          */
         $content = trim($_block->content());
         /**
          * The request for block content in the final template
          * @var string
          */
         $request = $matches[4][$index];
         /**
          * Parent/child matches/replacement
          */
         $_parents = function ($block) use(&$content, &$_parents, &$matches, &$index, &$_pattern) {
             $parent = $block->parent();
             if (preg_match("/{:parent:}/msU", $content, $_matches)) {
                 if ($parent) {
                     $content = preg_replace("/{:parent:}/msU", $parent->content(), $content);
                     // go again
                     return $_parents($block->parent(), $content);
                 } else {
                     // no parent, remove the request
                     $content = preg_replace("/{:parent:}/msU", "", $content);
                 }
             }
             return $content;
         };
         /**
          * Parse the block and check to see if it's parents request it.
          * @var method
          */
         $_children = function ($block, $content = null) use(&$_children, &$matches, &$index, &$_pattern, &$_blocks) {
             $content = $content == null ? $block->content() : $content;
             $_block = $_blocks->blocks($block->name());
             /**
              * If the block has a child then we're not at the bottom of the chain.
              * We need to move up until we cant
              * @var mixed `object` or `false`
              */
             $child = $block->child();
             /**
              * If the block has a parent then we cant be at the top of the chain.
              * As long as there's a parent we need to keep moving. 
              * @var mixed `object` or `false`
              */
             $parent = $block->parent();
             if (preg_match("/{:child:}/msU", $content)) {
                 // Block doesn't have a child block
                 if (!$child) {
                     // Also has no parent
                     if (!$parent) {
                         // clear the entire block
                         $content = "";
                     } else {
                         // Has a parent, still no child tho
                         // just remove the call for child block
                         $content = preg_replace("/{:child:}/msU", "", $content);
                         return $_children($block, $content);
                     }
                 }
                 // not asking for a child
             } else {
                 // Has a parent
                 if ($parent) {
                     if (preg_match("/{:child:}/msU", $parent->content())) {
                         $content = preg_replace("/{:child:}/msU", $content, $parent->content());
                         return $_children($parent, $content);
                     }
                 }
             }
             // must return content so we dont muck up parent
             return $content;
         };
         // parse children
         $content = $_children($_block);
         // parse parents
         $content = $_parents($_block);
         $source = preg_replace($_pattern, $content, $source);
     }
     // 0 should always be the final template
     if ($i == 0) {
         if ($cacheable = $cache->write($source, static::$_blocks->templates(0), $_blocks, $options)) {
             static::$_template = $cacheable;
         }
     }
 }