Пример #1
0
 public function __construct(PHPSTLTemplateProvider $provider, $resource, $identifier)
 {
     parent::__construct($provider, $resource, $identifier);
     // Stash away the current path for when we render
     $this->currentPath = CurrentPath::get();
 }
 protected function createTemplate($resource, $data, $identifier = null)
 {
     return parent::createTemplate($resource, $data, 'file://' . CurrentPath::get() . '/' . $resource);
 }
 /**
  * Adds assets to the page
  *
  * Expects child elements like:
  *   <script href="some.js" />
  *   <stylesheet href="some.css" />
  *   <link href="some/resource" rel="something" type="some/mime" />
  *   <alternate href="some.rss" type="application/rss+xml" title="RSS Feed" />
  *
  * If any href is a simple string (i.e. no ${...} expression), and it is
  * relative, it will be passed to CurrentPath->url->down to form an
  * absolute url.
  *
  * Full gory attribute details:
  *   script: a HTMLPageScript asset
  *     href string required
  *     type string optional default 'text/javascript'
  *
  *   stylesheet: a HTMLPageStylesheet asset
  *     href      string  required
  *     alternate boolean optional default false
  *     title     string  optional default null
  *     media     string  optional default null
  *
  *   alternate: a HTMLPageAlternateLink asset
  *     href  string required
  *     type  string required
  *     title string optional default null
  *
  *   link: a HTMLPageLinkedResource asset
  *     href  string required
  *     rel   string required
  *     type  string required
  *     title string optional default null
  *
  * @param DOMElement element the tag such as <ui:pageBuffer />
  * @return void
  */
 public function handleElementAddAssets(DOMElement $element)
 {
     if ($element->hasAttribute('base')) {
         $base = 'new StupidPath(' . $this->getAttr($element, 'base') . ')';
     } else {
         $base = 'CurrentPath::get()->url';
     }
     $this->compiler->write("<?php if (! \$page instanceof HTMLPage) {\n" . "  throw new RuntimeException('Can only add html assets to an html page');\n" . '} ?>');
     $assets = array();
     $baseVar = '$__base' . uniqid();
     foreach ($element->childNodes as $n) {
         if ($n->nodeType == XML_ELEMENT_NODE) {
             $href = $this->requiredAttr($n, 'href');
             $path = CurrentPath::get();
             $href = "(string) {$baseVar}->rel2abs({$href})";
             switch ($n->tagName) {
                 case 'script':
                     $asset = array('HTMLPageScript', $href);
                     $type = $this->getAttr($n, 'type');
                     if (isset($type)) {
                         array_push($asset, $type);
                     }
                     break;
                 case 'stylesheet':
                     $alt = $this->getBooleanAttr($n, 'alternate');
                     $title = $this->getAttr($n, 'title');
                     $media = $this->getAttr($n, 'media');
                     $asset = array('HTMLPageStylesheet', $href);
                     array_push($asset, $alt ? 'true' : 'false');
                     if (isset($title)) {
                         array_push($asset, $title);
                     } elseif (isset($media)) {
                         array_push($asset, 'null');
                     }
                     if (isset($media)) {
                         array_push($asset, $media);
                     }
                     break;
                 case 'link':
                     $rel = $this->requiredAttr($n, 'rel');
                     $type = $this->requiredAttr($n, 'type');
                     $title = $this->getAttr($n, 'title');
                     $asset = array('HTMLPageLinkedResource', $href, $type, $rel);
                     if (isset($title)) {
                         array_push($asset, $title);
                     }
                     break;
                 case 'alternate':
                     $type = $this->requiredAttr($n, 'type');
                     $title = $this->getAttr($n, 'title');
                     $asset = array('HTMLPageAlternateLink', $href, $type);
                     if (isset($title)) {
                         array_push($asset, $title);
                     }
                     break;
                 default:
                     throw new RuntimeException('Unknown asset element ' . $n->tagName);
             }
             array_push($assets, sprintf('new %s(%s)', array_shift($asset), implode(', ', $asset)));
         }
     }
     if (count($assets)) {
         $buffer = '';
         foreach ($assets as $asset) {
             $buffer .= "\$page->addAsset({$asset});\n";
         }
         $this->compiler->write("<?php\n" . "{$baseVar} = {$base};\n" . $buffer . "?>");
     }
 }