Skip to content

Commit 73830b8

Browse files
committed
Created a doc builder, doc config and doc result classes
1 parent 1480a16 commit 73830b8

15 files changed

+346
-367
lines changed

src/Application.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,12 @@
1717
class Application
1818
{
1919
private $application;
20-
private $buildContext;
20+
private $buildConfig;
2121

2222
public function __construct(string $symfonyVersion)
2323
{
2424
$this->application = new BaseApplication();
25-
26-
$configuration = [
27-
'symfony_api_url' => 'https://api.symfony.com/%s',
28-
'php_doc_url' => 'https://secure.php.net/manual/en',
29-
'symfony_doc_url' => 'https://symfony.com/doc/%s',
30-
];
31-
$this->buildContext = new BuildContext(
32-
$symfonyVersion,
33-
sprintf($configuration['symfony_api_url'], $symfonyVersion),
34-
$configuration['php_doc_url'],
35-
sprintf($configuration['symfony_doc_url'], $symfonyVersion)
36-
);
25+
$this->buildConfig = new BuildConfig();
3726
}
3827

3928
public function run(InputInterface $input): int
@@ -46,7 +35,7 @@ public function run(InputInterface $input): int
4635
false === getenv('SYMFONY_VERSION') ? 'master' : getenv('SYMFONY_VERSION')
4736
);
4837
$this->application->getDefinition()->addOption($inputOption);
49-
$this->application->add(new BuildDocsCommand($this->buildContext));
38+
$this->application->add(new BuildDocsCommand($this->buildConfig));
5039

5140
return $this->application->run($input);
5241
}

src/BuildConfig.php

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Docs Builder package.
5+
* (c) Ryan Weaver <ryan@symfonycasts.com>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace SymfonyDocsBuilder;
11+
12+
use Doctrine\RST\Configuration;
13+
use Symfony\Component\Finder\Finder;
14+
15+
class BuildConfig
16+
{
17+
private const PHP_DOC_URL = 'https://secure.php.net/manual/en';
18+
private const SYMFONY_API_URL = 'https://api.symfony.com/{symfonyVersion}';
19+
private const SYMFONY_DOC_URL = 'https://symfony.com/doc/{symfonyVersion}';
20+
21+
private $useBuildCache;
22+
private $theme;
23+
private $symfonyVersion;
24+
private $contentDir;
25+
private $outputDir;
26+
private $cacheDir;
27+
private $imagesDir;
28+
private $imagesPublicPrefix;
29+
private $subdirectoryToBuild;
30+
private $excludedPaths;
31+
private $fileFinder;
32+
33+
public function __construct()
34+
{
35+
$this->useBuildCache = true;
36+
$this->theme = Configuration::THEME_DEFAULT;
37+
$this->symfonyVersion = '4.4';
38+
$this->excludedPaths = [];
39+
$this->imagesPublicPrefix = '/_images';
40+
}
41+
42+
public function createFileFinder(): Finder
43+
{
44+
if (null === $this->fileFinder) {
45+
$this->fileFinder = new Finder();
46+
$this->fileFinder
47+
->in($this->getContentDir())
48+
// TODO - read this from the rst-parser Configuration
49+
->name('*.rst')
50+
->notName('*.rst.inc')
51+
->files()
52+
->exclude($this->excludedPaths);
53+
}
54+
55+
// clone to get a fresh instance and not share state
56+
return clone $this->fileFinder;
57+
}
58+
59+
public function getTheme(): string
60+
{
61+
return $this->theme;
62+
}
63+
64+
public function getSymfonyVersion(): string
65+
{
66+
return $this->symfonyVersion;
67+
}
68+
69+
public function getPhpDocUrl(): string
70+
{
71+
return self::PHP_DOC_URL;
72+
}
73+
74+
public function getSymfonyApiUrl(): string
75+
{
76+
return str_replace('{symfonyVersion}', $this->getSymfonyVersion(), self::SYMFONY_API_URL);
77+
}
78+
79+
public function getSymfonyDocUrl(): string
80+
{
81+
return str_replace('{symfonyVersion}', $this->getSymfonyVersion(), self::SYMFONY_DOC_URL);
82+
}
83+
84+
public function disableBuildCache(): self
85+
{
86+
$this->useBuildCache = false;
87+
88+
return $this;
89+
}
90+
91+
public function isBuildCacheEnabled(): bool
92+
{
93+
return $this->useBuildCache;
94+
}
95+
96+
public function getContentDir(): string
97+
{
98+
if (null === $this->contentDir) {
99+
throw new \InvalidArgumentException('RST contents directory is not defined. Set it with the setContentDir() method.');
100+
}
101+
102+
return $this->contentDir;
103+
}
104+
105+
public function getSubdirectoryToBuild(): ?string
106+
{
107+
return $this->subdirectoryToBuild;
108+
}
109+
110+
public function getOutputDir(): string
111+
{
112+
return $this->outputDir ?: $this->getContentDir().'/output';
113+
}
114+
115+
public function getCacheDir(): string
116+
{
117+
return $this->cacheDir ?: $this->getOutputDir().'/.cache';
118+
}
119+
120+
public function getImagesDir(): string
121+
{
122+
return $this->imagesDir ?: $this->getOutputDir().'/_images';
123+
}
124+
125+
public function getImagesPublicPrefix(): string
126+
{
127+
return $this->imagesPublicPrefix;
128+
}
129+
130+
public function setSymfonyVersion(string $version): self
131+
{
132+
$this->symfonyVersion = $version;
133+
134+
return $this;
135+
}
136+
137+
public function setTheme(string $theme): self
138+
{
139+
$this->theme = $theme;
140+
141+
return $this;
142+
}
143+
144+
public function setContentDir(string $contentDir): self
145+
{
146+
if (!file_exists($contentDir)) {
147+
throw new \InvalidArgumentException(sprintf('RST contents directory "%s" does not exist', $contentDir));
148+
}
149+
150+
$this->contentDir = rtrim(realpath($contentDir), DIRECTORY_SEPARATOR);
151+
152+
return $this;
153+
}
154+
155+
public function setSubdirectoryToBuild(string $contentSubDir): self
156+
{
157+
$this->subdirectoryToBuild = trim($contentSubDir, DIRECTORY_SEPARATOR);
158+
159+
return $this;
160+
}
161+
162+
public function setOutputDir(string $outputDir): self
163+
{
164+
if (!file_exists($outputDir)) {
165+
throw new \InvalidArgumentException(sprintf('Doc builder output directory "%s" does not exist', $outputDir));
166+
}
167+
168+
$this->outputDir = rtrim(realpath($outputDir), DIRECTORY_SEPARATOR);
169+
170+
return $this;
171+
}
172+
173+
public function setCacheDir(string $cacheDir): self
174+
{
175+
if (!file_exists($cacheDir)) {
176+
throw new \InvalidArgumentException(sprintf('Doc builder cache directory "%s" does not exist', $cacheDir));
177+
}
178+
179+
$this->cacheDir = rtrim(realpath($cacheDir), DIRECTORY_SEPARATOR);
180+
181+
return $this;
182+
}
183+
184+
/**
185+
* The directory where the images will be copied to. E.g. use this to
186+
* copy them into the public/ directory of a Symfony application.
187+
*/
188+
public function setImagesDir(string $imagesDir): self
189+
{
190+
if (!file_exists($imagesDir)) {
191+
throw new \InvalidArgumentException(sprintf('Doc builder images directory "%s" does not exist', $imagesDir));
192+
}
193+
194+
$this->imagesDir = rtrim(realpath($imagesDir), DIRECTORY_SEPARATOR);
195+
196+
return $this;
197+
}
198+
199+
/**
200+
* The string prefixes to the `src` attribute of `<img>` tags. This is useful when
201+
* publishing images in a public directory of a Symfony application.
202+
*/
203+
public function setImagesPublicPrefix(string $prefix): self
204+
{
205+
$this->imagesPublicPrefix = $prefix;
206+
207+
return $this;
208+
}
209+
210+
public function setExcludedPaths(array $excludedPaths)
211+
{
212+
if (null !== $this->fileFinder) {
213+
throw new \LogicException('setExcludePaths() cannot be called after getFileFinder() (because the Finder has been initialized).');
214+
}
215+
216+
$this->excludedPaths = $excludedPaths;
217+
}
218+
}

0 commit comments

Comments
 (0)