示例#1
0
 /**
  * Copies files or folders to the Artifact location.
  *
  * @param \phpDocumentor\Transformer\Transformation $transformation Transformation
  *     to use as data source.
  *
  * @throws \Exception
  *
  * @return void
  */
 public function executeQueryCopy(\phpDocumentor\Transformer\Transformation $transformation)
 {
     $path = $transformation->getSourceAsPath();
     if (!is_readable($path)) {
         throw new \phpDocumentor\Transformer\Exception('Unable to read the source file: ' . $path);
     }
     if (!is_writable($transformation->getTransformer()->getTarget())) {
         throw new \phpDocumentor\Transformer\Exception('Unable to write to: ' . dirname($transformation->getArtifact()));
     }
     $transformation->getTransformer()->copyRecursive($path, $transformation->getArtifact());
 }
示例#2
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\Plugin\EventDispatcher::getInstance()->dispatch('transformer.writer.xsl.pre', \phpDocumentor\Transformer\Events\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));
         }
     }
 }
示例#3
0
 /**
  * Copies files or folders to the Artifact location.
  *
  * @param Transformation $transformation Transformation to use as data source.
  *
  * @throws Exception
  *
  * @return void
  */
 public function executeQueryCopy(Transformation $transformation)
 {
     $path = $transformation->getSourceAsPath();
     if (!is_readable($path)) {
         throw new Exception('Unable to read the source file: ' . $path);
     }
     if (!is_writable($transformation->getTransformer()->getTarget())) {
         throw new Exception('Unable to write to: ' . dirname($transformation->getArtifact()));
     }
     $filesystem = new Filesystem();
     if (is_file($path)) {
         $filesystem->copy($path, $transformation->getArtifact());
     } else {
         $filesystem->mirror($path, $transformation->getArtifact());
     }
 }
示例#4
0
 /**
  *
  *
  * @param Transformation $transformation
  *
  * @return \XSLTCache|\XSLTProcessor
  */
 protected function getXslProcessor(Transformation $transformation)
 {
     $xslTemplatePath = $transformation->getSourceAsPath();
     $this->logger->debug('Loading XSL template: ' . $xslTemplatePath);
     if (!file_exists($xslTemplatePath)) {
         throw new Exception('Unable to find XSL template "' . $xslTemplatePath . '"');
     }
     if (extension_loaded('xslcache')) {
         $proc = new \XSLTCache();
         $proc->importStyleSheet($xslTemplatePath, true);
         return $proc;
     } else {
         $xsl = new \DOMDocument();
         $xsl->load($xslTemplatePath);
         $proc = new \XSLTProcessor();
         $proc->importStyleSheet($xsl);
         return $proc;
     }
 }
示例#5
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));
         }
     }
 }