Example #1
0
  /**
   * Uses an existing XSLProcessor (thus template) to transfor the given $xml to a file at location $destination_file.
   *
   * @param DOMDocument   $xml
   * @param XSLTProcessor $proc
   * @param string        $destination_file
   *
   * @return void
   */
  protected function transformTemplateToFile(DOMDocument $xml, XSLTProcessor $proc, $destination_file)
  {
    $files_path = $this->getTargetFilesPath();
    $root       = $this->getRelativeRoot($destination_file);
    $proc->setParameter('', 'search_template', ($this->getSearchObject() !== false) ? $this->getSearchObject()->getXslTemplateName() : 'none');

    // root differs, since most cached files rely on the root we will have to repopulate
    if ($proc->getParameter('', 'root') !== substr($root ? $root : './', 0, -1))
    {
      $proc->setParameter('', 'root', substr($root ? $root : './', 0, -1));
      if (file_exists($this->getThemePath().'/preprocess'))
      {
        $dirs = new DirectoryIterator($this->getThemePath().'/preprocess');

        /** @var DirectoryIterator $file */
        foreach($dirs as $file)
        {
          if (!$file->isFile())
          {
            continue;
          }

          $this->log('  Preprocessing '.$file->getFilename() . ' as XSLT parameter $'.$file->getBasename('.xsl'));
          $xsl2 = new DOMDocument();
          $xsl2->load($this->getThemePath() . '/preprocess/' . $file->getFilename());

          $proc2 = new XSLTProcessor();
          $proc2->importStyleSheet($xsl2);
          $proc2->setParameter('', 'root', substr($root ? $root : './', 0, -1));

          $proc->setParameter('', $file->getBasename('.xsl'), str_replace('\'', '"', $proc2->transformToXml($xml)));
        }
      }
    }

    $proc->transformToURI($xml, 'file://' . $files_path . '/' . $destination_file);
  }