HosipLan

Untitled

Jan 9th, 2012
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.30 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * This file is part of the Kdyby (http://www.kdyby.org)
  5.  *
  6.  * Copyright (c) 2008, 2011 Filip Procházka ([email protected])
  7.  *
  8.  * @license http://www.kdyby.org/license
  9.  */
  10.  
  11. namespace Kdyby\Components\Header;
  12.  
  13. use Kdyby;
  14. use Kdyby\Templates\LatteHelpers;
  15. use Nette;
  16. use Nette\Application\UI;
  17. use Nette\Latte;
  18. use Nette\Templating\Template;
  19.  
  20.  
  21.  
  22. /**
  23.  * @author Filip Procházka <[email protected]>
  24.  */
  25. class HeadMacro extends Nette\Object implements Latte\IMacro
  26. {
  27.  
  28.     /**
  29.      * @param \Nette\Latte\Parser $parser
  30.      */
  31.     public static function install(Latte\Parser $parser)
  32.     {
  33.         $parser->addMacro('head', new static());
  34.     }
  35.  
  36.  
  37.  
  38.     /**
  39.      * Initializes before template parsing.
  40.      * @return void
  41.      */
  42.     public function initialize()
  43.     {
  44.  
  45.     }
  46.  
  47.  
  48.  
  49.     /**
  50.      * Finishes template parsing.
  51.      * @return array(prolog, epilog)
  52.      */
  53.     public function finalize()
  54.     {
  55.  
  56.     }
  57.  
  58.  
  59.  
  60.     /**
  61.      * @param \Nette\Latte\MacroNode $node
  62.      *
  63.      * @return bool|string
  64.      */
  65.     public function nodeOpened(Latte\MacroNode $node)
  66.     {
  67.         return Latte\PhpWriter::using($node)
  68.             ->write('<?php Kdyby\Components\Header\HeadMacro::headBegin($presenter); ?>');
  69.     }
  70.  
  71.  
  72.  
  73.     /**
  74.      * @param \Nette\Latte\MacroNode $node
  75.      *
  76.      * @return string
  77.      */
  78.     public function nodeClosed(Latte\MacroNode $node)
  79.     {
  80.         $writer = Latte\PhpWriter::using($node);
  81.         $node->content = $this->wrapTags(Template::optimizePhp($node->content), $writer);
  82.         return $writer->write('<?php Kdyby\Components\Header\HeadMacro::headEnd($presenter); ?>');
  83.     }
  84.  
  85.  
  86.  
  87.     /**
  88.      * @param string $content
  89.      *
  90.      * @return string
  91.      */
  92.     private function wrapTags($content, Latte\PhpWriter $writer)
  93.     {
  94.         $code = NULL;
  95.         foreach ($parts = LatteHelpers::splitPhp($content) as $item) {
  96.             if (substr($item, 0, 5) === '<?php') {
  97.                 $code .= $item;
  98.                 continue;
  99.             }
  100.  
  101.             $code .= Nette\Utils\Strings::replace($item, array(
  102.                 '~<([^<\s]+)\s+~' => $writer->write('<?php ob_start(); ?>') . '<\\1 ',
  103.                 '~\s+/>~' => " />" . $writer->write('<?php Kdyby\Components\Header\HeadMacro::tagCaptureEnd($presenter); ?>'),
  104.             ));
  105.  
  106.         }
  107.  
  108.         echo "<hr>";
  109.         echo nl2br(htmlspecialchars($code));
  110.         echo "<hr>";
  111.         return $content;
  112.     }
  113.  
  114.  
  115.  
  116.     /**
  117.      * @param \Nette\Application\UI\Presenter $presenter
  118.      */
  119.     public static function headBegin(UI\Presenter $presenter)
  120.     {
  121.         $head = static::getHead($presenter);
  122.         echo $head->getElement()->startTag();
  123.         $head->renderContent();
  124.     }
  125.  
  126.  
  127.  
  128.     /**
  129.      * @param \Nette\Application\UI\Presenter $presenter
  130.      */
  131.     public static function headEnd(UI\Presenter $presenter)
  132.     {
  133.         $head = static::getHead($presenter);
  134.         echo $head->getElement()->startTag();
  135.     }
  136.  
  137.  
  138.  
  139.     /**
  140.      * @param \Nette\Application\UI\Presenter $presenter
  141.      */
  142.     public static function tagCaptureEnd(UI\Presenter $presenter)
  143.     {
  144.         $head = static::getHead($presenter);
  145.         $tag = ob_end_clean();
  146.         dump($tag);
  147.     }
  148.  
  149.  
  150.  
  151.     /**
  152.      * @param \Nette\ComponentModel\Container $component
  153.      * @return \Kdyby\Components\Header\HeaderControl
  154.      */
  155.     private static function getHead(Nette\ComponentModel\Container $component)
  156.     {
  157.         if (!$component->getComponent('head', FALSE)) {
  158.             throw new Kdyby\InvalidStateException('You have to register Kdyby\Components\Header\HeaderControl as presenter component named "head".');
  159.         }
  160.  
  161.         return $component->getComponent('head');
  162.     }
  163.  
  164. }
Advertisement
Add Comment
Please, Sign In to add comment