Example #1
0
 /**
  * This method combines the structure.xml and the given target template
  * and creates a static html page at the artifact location.
  *
  * @param \DOMDocument                        $structure      XML source.
  * @param \phpDocumentor\Transformer\Transformation $transformation Transformation.
  *
  * @throws \Exception
  *
  * @return void
  */
 public function transform(\DOMDocument $structure, \phpDocumentor\Transformer\Transformation $transformation)
 {
     if (!class_exists('XSLTProcessor')) {
         throw new \phpDocumentor\Plugin\Core\Exception('The XSL writer was unable to find your XSLTProcessor; ' . 'please check if you have installed the PHP XSL extension');
     }
     $artifact = $transformation->getTransformer()->getTarget() . DIRECTORY_SEPARATOR . $transformation->getArtifact();
     $xsl = new \DOMDocument();
     $xsl->load($transformation->getSourceAsPath());
     $proc = new \XSLTProcessor();
     $proc->importStyleSheet($xsl);
     if (empty($structure->documentElement)) {
         throw new \phpDocumentor\Plugin\Core\Exception('Specified DOMDocument lacks documentElement, cannot transform');
     }
     $proc->setParameter('', 'title', $structure->documentElement->getAttribute('title'));
     $proc->setParameter('', 'root', str_repeat('../', substr_count($transformation->getArtifact(), '/')));
     $proc->setParameter('', 'search_template', $transformation->getParameter('search', 'none'));
     $proc->setParameter('', 'version', \phpDocumentor\Application::VERSION);
     $proc->setParameter('', 'generated_datetime', date('r'));
     // check parameters for variables and add them when found
     $this->setProcessorParameters($transformation, $proc);
     // if a query is given, then apply a transformation to the artifact
     // location by replacing ($<var>} with the sluggified node-value of the
     // search result
     if ($transformation->getQuery() !== '') {
         $xpath = new \DOMXPath($transformation->getTransformer()->getSource());
         /** @var \DOMNodeList $qry */
         $qry = $xpath->query($transformation->getQuery());
         $count = $qry->length;
         foreach ($qry as $key => $element) {
             \phpDocumentor\Event\Dispatcher::getInstance()->dispatch('transformer.writer.xsl.pre', \phpDocumentor\Transformer\Event\PreXslWriterEvent::createInstance($this)->setElement($element)->setProgress(array($key + 1, $count)));
             $proc->setParameter('', $element->nodeName, $element->nodeValue);
             $file_name = $transformation->getTransformer()->generateFilename($element->nodeValue);
             $filename = str_replace('{$' . $element->nodeName . '}', $file_name, $artifact);
             $this->log('Processing the file: ' . $element->nodeValue . ' as ' . $filename);
             if (!file_exists(dirname($filename))) {
                 mkdir(dirname($filename), 0755, true);
             }
             $proc->transformToURI($structure, $this->getXsltUriFromFilename($filename));
         }
     } else {
         if (substr($transformation->getArtifact(), 0, 1) == '$') {
             // not a file, it must become a variable!
             $variable_name = substr($transformation->getArtifact(), 1);
             $this->xsl_variables[$variable_name] = $proc->transformToXml($structure);
         } else {
             if (!file_exists(dirname($artifact))) {
                 mkdir(dirname($artifact), 0755, true);
             }
             $proc->transformToURI($structure, $this->getXsltUriFromFilename($artifact));
         }
     }
 }
Example #2
0
 /**
  * This method combines the structure.xml and the given target template
  * and creates a static html page at the artifact location.
  *
  * @param ProjectDescriptor $project        Document containing the structure.
  * @param Transformation    $transformation Transformation to execute.
  *
  * @throws \RuntimeException if the structure.xml file could not be found.
  * @throws Exception        if the structure.xml file's documentRoot could not be read because of encoding issues
  *    or because it was absent.
  *
  * @return void
  */
 public function transform(ProjectDescriptor $project, Transformation $transformation)
 {
     $structure = $this->loadAst($this->getAstPath($transformation));
     $proc = $this->getXslProcessor($transformation);
     $proc->registerPHPFunctions();
     $this->registerDefaultVariables($transformation, $proc, $structure);
     $this->setProcessorParameters($transformation, $proc);
     $artifact = $this->getArtifactPath($transformation);
     $this->checkForSpacesInPath($artifact);
     // if a query is given, then apply a transformation to the artifact
     // location by replacing ($<var>} with the sluggified node-value of the
     // search result
     if ($transformation->getQuery() !== '') {
         $xpath = new \DOMXPath($structure);
         /** @var \DOMNodeList $qry */
         $qry = $xpath->query($transformation->getQuery());
         $count = $qry->length;
         foreach ($qry as $key => $element) {
             Dispatcher::getInstance()->dispatch('transformer.writer.xsl.pre', PreXslWriterEvent::createInstance($this)->setElement($element)->setProgress(array($key + 1, $count)));
             $proc->setParameter('', $element->nodeName, $element->nodeValue);
             $file_name = $transformation->getTransformer()->generateFilename($element->nodeValue);
             if (!$artifact) {
                 $url = $this->generateUrlForXmlElement($project, $element);
                 if ($url === false || $url[0] !== DIRECTORY_SEPARATOR) {
                     continue;
                 }
                 $filename = $transformation->getTransformer()->getTarget() . str_replace('/', DIRECTORY_SEPARATOR, $url);
             } else {
                 $filename = str_replace('{$' . $element->nodeName . '}', $file_name, $artifact);
             }
             $relativeFileName = substr($filename, strlen($transformation->getTransformer()->getTarget()) + 1);
             $proc->setParameter('', 'root', str_repeat('../', substr_count($relativeFileName, '/')));
             $this->writeToFile($filename, $proc, $structure);
         }
     } else {
         if (substr($transformation->getArtifact(), 0, 1) == '$') {
             // not a file, it must become a variable!
             $variable_name = substr($transformation->getArtifact(), 1);
             $this->xsl_variables[$variable_name] = $proc->transformToXml($structure);
         } else {
             $relativeFileName = substr($artifact, strlen($transformation->getTransformer()->getTarget()) + 1);
             $proc->setParameter('', 'root', str_repeat('../', substr_count($relativeFileName, '/')));
             $this->writeToFile($artifact, $proc, $structure);
         }
     }
 }
Example #3
0
 /**
  * This method combines the structure.xml and the given target template
  * and creates a static html page at the artifact location.
  *
  * @param ProjectDescriptor $project        Document containing the structure.
  * @param Transformation    $transformation Transformation to execute.
  *
  * @return void
  */
 public function transform(ProjectDescriptor $project, Transformation $transformation)
 {
     if (!class_exists('XSLTProcessor')) {
         throw new Exception('The XSL writer was unable to find your XSLTProcessor; ' . 'please check if you have installed the PHP XSL extension');
     }
     $artifact = $transformation->getTransformer()->getTarget() . DIRECTORY_SEPARATOR . $transformation->getArtifact();
     $xsl = new \DOMDocument();
     $xsl->load($transformation->getSourceAsPath());
     $structureFilename = $transformation->getTransformer()->getTarget() . DIRECTORY_SEPARATOR . 'structure.xml';
     if (!is_readable($structureFilename)) {
         throw new RuntimeException('Structure.xml file was not found in the target directory, is the XML writer missing from the ' . 'template definition?');
     }
     // load the structure file (ast)
     $structure = new \DOMDocument('1.0', 'utf-8');
     libxml_use_internal_errors(true);
     $structure->load($structureFilename);
     $proc = new \XSLTProcessor();
     $proc->importStyleSheet($xsl);
     if (empty($structure->documentElement)) {
         $message = 'Specified DOMDocument lacks documentElement, cannot transform.';
         if (libxml_get_last_error()) {
             $message .= PHP_EOL . 'Apparently an error occurred with reading the structure.xml file, the reported ' . 'error was "' . trim(libxml_get_last_error()->message) . '" on line ' . libxml_get_last_error()->line;
         }
         throw new Exception($message);
     }
     $proc->setParameter('', 'title', $structure->documentElement->getAttribute('title'));
     $proc->setParameter('', 'root', str_repeat('../', substr_count($transformation->getArtifact(), '/')));
     $proc->setParameter('', 'search_template', $transformation->getParameter('search', 'none'));
     $proc->setParameter('', 'version', \phpDocumentor\Application::$VERSION);
     $proc->setParameter('', 'generated_datetime', date('r'));
     // check parameters for variables and add them when found
     $this->setProcessorParameters($transformation, $proc);
     // if a query is given, then apply a transformation to the artifact
     // location by replacing ($<var>} with the sluggified node-value of the
     // search result
     if ($transformation->getQuery() !== '') {
         $xpath = new \DOMXPath($structure);
         /** @var \DOMNodeList $qry */
         $qry = $xpath->query($transformation->getQuery());
         $count = $qry->length;
         foreach ($qry as $key => $element) {
             \phpDocumentor\Event\Dispatcher::getInstance()->dispatch('transformer.writer.xsl.pre', \phpDocumentor\Transformer\Event\PreXslWriterEvent::createInstance($this)->setElement($element)->setProgress(array($key + 1, $count)));
             $proc->setParameter('', $element->nodeName, $element->nodeValue);
             $file_name = $transformation->getTransformer()->generateFilename($element->nodeValue);
             $filename = str_replace('{$' . $element->nodeName . '}', $file_name, $artifact);
             if (!file_exists(dirname($filename))) {
                 mkdir(dirname($filename), 0755, true);
             }
             $proc->transformToURI($structure, $this->getXsltUriFromFilename($filename));
         }
     } else {
         if (substr($transformation->getArtifact(), 0, 1) == '$') {
             // not a file, it must become a variable!
             $variable_name = substr($transformation->getArtifact(), 1);
             $this->xsl_variables[$variable_name] = $proc->transformToXml($structure);
         } else {
             if (!file_exists(dirname($artifact))) {
                 mkdir(dirname($artifact), 0755, true);
             }
             $proc->transformToURI($structure, $this->getXsltUriFromFilename($artifact));
         }
     }
 }