|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SymfonyDocsBuilder; |
| 4 | + |
| 5 | +use Doctrine\RST\Builder; |
| 6 | +use Doctrine\RST\Configuration; |
| 7 | +use Symfony\Component\Console\Output\NullOutput; |
| 8 | +use Symfony\Component\Filesystem\Filesystem; |
| 9 | +use SymfonyDocsBuilder\CI\MissingFilesChecker; |
| 10 | +use SymfonyDocsBuilder\Generator\HtmlForPdfGenerator; |
| 11 | +use SymfonyDocsBuilder\Generator\JsonGenerator; |
| 12 | + |
| 13 | +final class DocBuilder |
| 14 | +{ |
| 15 | + private $useBuildCache = true; |
| 16 | + private $symfonyVersion = '4.4'; |
| 17 | + private $theme = Configuration::THEME_DEFAULT;// 'rtd'; // also: Configuration::THEME_DEFAULT |
| 18 | + private const PHP_DOC_URL = 'https://secure.php.net/manual/en'; |
| 19 | + private const SYMFONY_API_URL = 'https://api.symfony.com/{symfonyVersion}'; |
| 20 | + private const SYMFONY_DOC_URL = 'https://symfony.com/doc/{symfonyVersion}'; |
| 21 | + |
| 22 | + public function buildDir(string $rstContentsDir/*, BuildConfig $config*/, string $outputDir, string $publicImagesDir = null, string $publicImagesPrefix = null): BuildResult |
| 23 | + { |
| 24 | + return $this->doBuild($rstContentsDir, $outputDir, $publicImagesDir ?? $outputDir.'/_images', $publicImagesPrefix ?? $publicImagesPrefix.'/_images'); |
| 25 | + } |
| 26 | + |
| 27 | + public function buildDocument(string $rstContent, BuildConfig $config): BuildResult |
| 28 | + { |
| 29 | + // TODO |
| 30 | + } |
| 31 | + |
| 32 | + private function doBuild(string $rstContentsDir, string $htmlOutputDir, string $publicImagesDir, string $publicImagesPrefix, ?string $rstBookSubDir = null): BuildResult |
| 33 | + { |
| 34 | + if (!file_exists($rstContentsDir)) { |
| 35 | + throw new \InvalidArgumentException(sprintf('RST contents directory "%s" does not exist', $rstContentsDir)); |
| 36 | + } |
| 37 | + |
| 38 | + $filesystem = new Filesystem(); |
| 39 | + if (!$this->useBuildCache && $filesystem->exists($htmlOutputDir)) { |
| 40 | + $filesystem->remove($htmlOutputDir); |
| 41 | + } |
| 42 | + $filesystem->mkdir($htmlOutputDir); |
| 43 | + |
| 44 | + if ($rstBookSubDir && !file_exists($rstContentsDir.'/'.$rstBookSubDir)) { |
| 45 | + throw new \InvalidArgumentException(sprintf('Given book directory "%s" is not a subdirectory of the RST contents dir "%s".', $rstBookSubDir, $rstContentsDir)); |
| 46 | + } |
| 47 | + |
| 48 | + $buildContext = new BuildContext( |
| 49 | + $this->symfonyVersion, |
| 50 | + str_replace('{symfonyVersion}', $this->symfonyVersion, self::SYMFONY_API_URL), |
| 51 | + self::PHP_DOC_URL, |
| 52 | + str_replace('{symfonyVersion}', $this->symfonyVersion, self::SYMFONY_DOC_URL) |
| 53 | + ); |
| 54 | + $buildContext->initializeRuntimeConfig($rstContentsDir, $htmlOutputDir, $publicImagesDir, $publicImagesPrefix, $rstBookSubDir, !$this->useBuildCache, $this->theme); |
| 55 | + |
| 56 | + $configFileParser = new ConfigFileParser($buildContext, new NullOutput()); |
| 57 | + $configFileParser->processConfigFile($rstContentsDir); |
| 58 | + |
| 59 | + $builder = new Builder(KernelFactory::createKernel($buildContext)); |
| 60 | + $builder->build($buildContext->getSourceDir(), $buildContext->getOutputDir()); |
| 61 | + |
| 62 | + $buildResult = new BuildResult($builder->getErrorManager()->getErrors()); |
| 63 | + |
| 64 | + $missingFilesChecker = new MissingFilesChecker($buildContext); |
| 65 | + $missingFiles = $missingFilesChecker->getMissingFiles(); |
| 66 | + foreach ($missingFiles as $missingFile) { |
| 67 | + $buildResult->appendError(sprintf('Missing file "%s"', $missingFile)); |
| 68 | + } |
| 69 | + |
| 70 | + if (!$buildResult->isSuccessful()) { |
| 71 | + $buildResult->prependError(sprintf('Build errors from "%s"', date('Y-m-d h:i:s'))); |
| 72 | + $filesystem->dumpFile($htmlOutputDir.'/build_errors.txt', implode("\n", $buildResult->getErrors())); |
| 73 | + } |
| 74 | + |
| 75 | + $metas = $builder->getMetas(); |
| 76 | + if ($buildContext->getParseSubPath()) { |
| 77 | + $htmlForPdfGenerator = new HtmlForPdfGenerator($metas, $buildContext); |
| 78 | + $htmlForPdfGenerator->generateHtmlForPdf(); |
| 79 | + } else { |
| 80 | + $jsonGenerator = new JsonGenerator($metas, $buildContext); |
| 81 | + $jsonGenerator->generateJson(); |
| 82 | + } |
| 83 | + |
| 84 | + return $buildResult; |
| 85 | + } |
| 86 | +} |
0 commit comments