Skip to content

Commit f749a4a

Browse files
committed
[BUGFIX] Set option defaults via guides.xml
1 parent 06b57f0 commit f749a4a

File tree

6 files changed

+190
-51
lines changed

6 files changed

+190
-51
lines changed

guides.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<guides xmlns="https://www.phpdoc.org/guides"
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd">
4+
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd"
5+
input="docs"
6+
output="output"
7+
input-format="rst"
8+
show-progress="true"
9+
theme="bootstrap"
10+
fail-on-log="false"
11+
log-path="php://stder"
12+
>
513
<project title="phpDocumentor Guides"/>
6-
<theme>bootstrap</theme>
14+
<output-format>html</output-format>
15+
<output-format>intersphinx</output-format>
716

817
<extension class="phpDocumentor\Guides\Bootstrap"/>
918
</guides>

packages/guides-cli/resources/schema/guides.xsd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@
1212
<xsd:element name="base-template-path" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
1313
<xsd:element name="theme" type="theme" minOccurs="0" maxOccurs="unbounded"/>
1414
<xsd:element name="extension" type="extension" minOccurs="0" maxOccurs="unbounded"/>
15+
<xsd:element name="output-format" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
1516
</xsd:choice>
1617

18+
<xsd:attribute name="input" type="xsd:string"/>
19+
<xsd:attribute name="output" type="xsd:string"/>
20+
<xsd:attribute name="input-format" type="xsd:string"/>
21+
<xsd:attribute name="log-path" type="xsd:string"/>
22+
<xsd:attribute name="fail-on-log" type="xsd:string"/>
23+
<xsd:attribute name="show-progress" type="xsd:string"/>
1724
<xsd:attribute name="theme" type="xsd:string"/>
1825
</xsd:complexType>
1926

packages/guides-cli/src/Command/Run.php

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use phpDocumentor\Guides\Handlers\RenderCommand;
1919
use phpDocumentor\Guides\Intersphinx\InventoryRepository;
2020
use phpDocumentor\Guides\Nodes\ProjectNode;
21+
use phpDocumentor\Guides\Settings\ProjectSettings;
2122
use phpDocumentor\Guides\Settings\SettingsManager;
2223
use phpDocumentor\Guides\Twig\Theme\ThemeManager;
2324
use RuntimeException;
@@ -55,35 +56,30 @@ public function __construct(
5556
'input',
5657
InputArgument::OPTIONAL,
5758
'Directory to read for files',
58-
'docs',
5959
);
6060
$this->addArgument(
6161
'output',
6262
InputArgument::OPTIONAL,
6363
'Directory to read for files',
64-
'output',
6564
);
6665

6766
$this->addOption(
6867
'input-format',
6968
null,
7069
InputOption::VALUE_REQUIRED,
7170
'Format of the input can be RST, or Markdown',
72-
'rst',
7371
);
7472
$this->addOption(
7573
'output-format',
7674
null,
7775
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
78-
'Format of the input can be html',
79-
['html'],
76+
'Format of the input can be html and or intersphinx',
8077
);
8178
$this->addOption(
8279
'log-path',
8380
null,
8481
InputOption::VALUE_REQUIRED,
8582
'Write log to this path',
86-
'php://stder',
8783
);
8884
$this->addOption(
8985
'fail-on-log',
@@ -104,40 +100,70 @@ public function __construct(
104100
null,
105101
InputOption::VALUE_NEGATABLE,
106102
'Whether to show a progress bar',
107-
true,
108103
);
109104
}
110105

106+
private function getSettingsOverridenWithInput(InputInterface $input): ProjectSettings
107+
{
108+
$settings = $this->settingsManager->getProjectSettings();
109+
if ($input->getArgument('input')) {
110+
$settings->setInput((string) $input->getArgument('input'));
111+
}
112+
113+
if ($input->getArgument('output')) {
114+
$settings->setOutput((string) $input->getArgument('output'));
115+
}
116+
117+
if ($input->getOption('log-path')) {
118+
$settings->setLogPath((string) $input->getOption('log-path'));
119+
}
120+
121+
if ($input->getOption('fail-on-log')) {
122+
$settings->setFailOnError(true);
123+
}
124+
125+
if ($input->getOption('input-format')) {
126+
$settings->setInputFormat((string) $input->getOption('input-format'));
127+
}
128+
129+
if (count($input->getOption('output-format')) > 0) {
130+
$settings->setOutputFormats($input->getOption('output-format'));
131+
}
132+
133+
if ($input->getOption('theme')) {
134+
$settings->setTheme((string) $input->getOption('theme'));
135+
}
136+
137+
return $settings;
138+
}
139+
111140
protected function execute(InputInterface $input, OutputInterface $output): int
112141
{
113-
$inputDir = $this->getAbsolutePath((string) ($input->getArgument('input') ?? ''));
142+
$settings = $this->getSettingsOverridenWithInput($input);
143+
$inputDir = $this->getAbsolutePath($settings->getInput());
114144
if (!is_dir($inputDir)) {
115145
throw new RuntimeException(sprintf('Input directory "%s" was not found! ' . "\n" .
116146
'Run "vendor/bin/guides -h" for information on how to configure this command.', $inputDir));
117147
}
118148

119-
$settings = $this->settingsManager->getProjectSettings();
120-
121149
$projectNode = new ProjectNode(
122150
$settings->getTitle() === '' ? null : $settings->getTitle(),
123151
$settings->getVersion() === '' ? null : $settings->getVersion(),
124152
);
125153
$this->inventoryRepository->initialize($settings->getInventories());
126154

127-
$outputDir = $this->getAbsolutePath((string) ($input->getArgument('output') ?? ''));
128-
$sourceFileSystem = new Filesystem(new Local($input->getArgument('input')));
155+
$outputDir = $this->getAbsolutePath($settings->getOutput());
156+
$sourceFileSystem = new Filesystem(new Local($settings->getInput()));
129157
$sourceFileSystem->addPlugin(new Finder());
130-
$logPath = $input->getOption('log-path') ?? 'php://stder';
158+
$logPath = $settings->getLogPath();
131159
if ($logPath === 'php://stder') {
132160
$this->logger->pushHandler(new ErrorLogHandler(ErrorLogHandler::OPERATING_SYSTEM, Logger::WARNING));
133161
} else {
134162
$this->logger->pushHandler(new StreamHandler($logPath . '/warning.log', Logger::WARNING));
135163
$this->logger->pushHandler(new StreamHandler($logPath . '/error.log', Logger::ERROR));
136164
}
137165

138-
$failOnLog = $input->getOption('fail-on-log') ?? false;
139-
140-
if ($failOnLog) {
166+
if ($settings->isFailOnError()) {
141167
$spyProcessor = new SpyProcessor();
142168
$this->logger->pushProcessor($spyProcessor);
143169
}
@@ -146,28 +172,25 @@ protected function execute(InputInterface $input, OutputInterface $output): int
146172
new ParseDirectoryCommand(
147173
$sourceFileSystem,
148174
'',
149-
$input->getOption('input-format'),
175+
$settings->getInputFormat(),
150176
$projectNode,
151177
),
152178
);
153179

154-
$theme = $input->getOption('theme');
155-
if ($theme) {
156-
$settings->setTheme($theme);
157-
}
180+
158181

159182
$this->themeManager->useTheme($settings->getTheme());
160183

161184
$documents = $this->commandBus->handle(new CompileDocumentsCommand($documents, new CompilerContext($projectNode)));
162185

163186
$destinationFileSystem = new Filesystem(new Local($outputDir));
164187

165-
$outputFormats = $input->getOption('output-format');
188+
$outputFormats = $settings->getOutputFormats();
166189

167190
foreach ($outputFormats as $format) {
168191
$progressBar = null;
169192

170-
if ($output instanceof ConsoleOutputInterface && $input->getOption('progress')) {
193+
if ($output instanceof ConsoleOutputInterface && $settings->isShowProgressBar()) {
171194
$progressBar = new ProgressBar($output->section());
172195
}
173196

@@ -195,7 +218,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
195218
'Successfully placed ' . (is_countable($documents) ? count($documents) : 0) . ' rendered ' . $formatsText . ' files into ' . $outputDir,
196219
);
197220

198-
if ($failOnLog && $spyProcessor->hasBeenCalled()) {
221+
if ($settings->isFailOnError() && $spyProcessor->hasBeenCalled()) {
199222
return Command::FAILURE;
200223
}
201224

packages/guides/src/DependencyInjection/GuidesExtension.php

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ public function getConfigTreeBuilder(): TreeBuilder
5252
->end()
5353
->end()
5454
->scalarNode('theme')->end()
55+
->scalarNode('input')->end()
56+
->scalarNode('output')->end()
57+
->scalarNode('input_format')->end()
58+
->arrayNode('output_format')
59+
->defaultValue([])
60+
->scalarPrototype()->end()
61+
->end()
62+
->scalarNode('log_path')->end()
63+
->scalarNode('fail_on_log')->end()
64+
->scalarNode('show_progress')->end()
5565
->arrayNode('base_template_paths')
5666
->defaultValue([])
5767
->scalarPrototype()->end()
@@ -91,28 +101,50 @@ public function load(array $configs, ContainerBuilder $container): void
91101
$loader->load('command_bus.php');
92102
$loader->load('guides.php');
93103

94-
$projectSettings = [];
104+
$projectSettings = new ProjectSettings();
95105
if (isset($config['project'])) {
96106
if (isset($config['project']['version'])) {
97-
$config['project']['version'] = (string) $config['project']['version'];
107+
$projectSettings->setVersion((string) $config['project']['version']);
98108
}
99109

100-
$projectSettings = $config['project'];
110+
$projectSettings->setTitle((string) $config['project']['title']);
101111
}
102112

103113
if (isset($config['inventories'])) {
104-
$projectSettings['inventories'] = $config['inventories']['inventory'];
114+
$projectSettings->setInventories($config['inventories']['inventory']);
105115
}
106116

107117
if (isset($config['theme'])) {
108-
$projectSettings['theme'] = (string) $config['theme'];
118+
$projectSettings->setTheme((string) $config['theme']);
119+
}
120+
121+
if (isset($config['input'])) {
122+
$projectSettings->setInput((string) $config['input']);
123+
}
124+
125+
if (isset($config['output'])) {
126+
$projectSettings->setOutput((string) $config['output']);
109127
}
110128

111-
if ($projectSettings) {
112-
$container->getDefinition(SettingsManager::class)
113-
->addMethodCall('setProjectSettings', [new ProjectSettings($projectSettings)]);
129+
if (isset($config['input_format'])) {
130+
$projectSettings->setInputFormat((string) $config['input_format']);
114131
}
115132

133+
if (isset($config['output_format']) && is_array($config['output_format'])) {
134+
$projectSettings->setOutputFormats($config['output_format']);
135+
}
136+
137+
if (isset($config['show_progress'])) {
138+
$projectSettings->setShowProgressBar((bool) $config['show_progress']);
139+
}
140+
141+
if (isset($config['fail_on_log'])) {
142+
$projectSettings->setFailOnError((bool) $config['show_progress']);
143+
}
144+
145+
$container->getDefinition(SettingsManager::class)
146+
->addMethodCall('setProjectSettings', [$projectSettings]);
147+
116148
$config['base_template_paths'][] = dirname(__DIR__, 2) . '/resources/template/html';
117149
$container->setParameter('phpdoc.guides.base_template_paths', $config['base_template_paths']);
118150

packages/guides/src/Settings/ProjectSettings.php

Lines changed: 84 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,21 @@
44

55
namespace phpDocumentor\Guides\Settings;
66

7-
use function is_array;
8-
use function is_string;
9-
107
class ProjectSettings
118
{
129
/** @var array<string, string> */
13-
private array $inventories;
14-
private string $title;
15-
private string $version;
16-
private string $theme;
17-
18-
/** @param array<string, string|array<string, string>> $settingsArray */
19-
public function __construct(array $settingsArray)
20-
{
21-
$this->title = isset($settingsArray['title']) && is_string($settingsArray['title']) ? $settingsArray['title'] : '';
22-
$this->version = isset($settingsArray['version']) && is_string($settingsArray['version']) ? $settingsArray['version'] : '';
23-
$this->inventories = isset($settingsArray['inventories']) && is_array($settingsArray['inventories']) ? $settingsArray['inventories'] : [];
24-
$this->theme = isset($settingsArray['theme']) && is_string($settingsArray['theme']) ? $settingsArray['theme'] : 'default';
25-
}
10+
private array $inventories = [];
11+
private string $title = '';
12+
private string $version = '';
13+
private string $theme = 'default';
14+
private string $input = 'docs';
15+
private string $output = 'output';
16+
private string $inputFormat = 'rst';
17+
/** @var string[] */
18+
private array $outputFormats = ['html'];
19+
private string $logPath = 'php://stder';
20+
private bool $failOnError = false;
21+
private bool $showProgressBar = true;
2622

2723
public function getTitle(): string
2824
{
@@ -65,4 +61,76 @@ public function setTheme(string $theme): void
6561
{
6662
$this->theme = $theme;
6763
}
64+
65+
public function getInput(): string
66+
{
67+
return $this->input;
68+
}
69+
70+
public function setInput(string $input): void
71+
{
72+
$this->input = $input;
73+
}
74+
75+
public function getOutput(): string
76+
{
77+
return $this->output;
78+
}
79+
80+
public function setOutput(string $output): void
81+
{
82+
$this->output = $output;
83+
}
84+
85+
public function getInputFormat(): string
86+
{
87+
return $this->inputFormat;
88+
}
89+
90+
public function setInputFormat(string $inputFormat): void
91+
{
92+
$this->inputFormat = $inputFormat;
93+
}
94+
95+
public function getLogPath(): string
96+
{
97+
return $this->logPath;
98+
}
99+
100+
public function setLogPath(string $logPath): void
101+
{
102+
$this->logPath = $logPath;
103+
}
104+
105+
public function isFailOnError(): bool
106+
{
107+
return $this->failOnError;
108+
}
109+
110+
public function setFailOnError(bool $failOnError): void
111+
{
112+
$this->failOnError = $failOnError;
113+
}
114+
115+
public function isShowProgressBar(): bool
116+
{
117+
return $this->showProgressBar;
118+
}
119+
120+
public function setShowProgressBar(bool $showProgressBar): void
121+
{
122+
$this->showProgressBar = $showProgressBar;
123+
}
124+
125+
/** @return string[] */
126+
public function getOutputFormats(): array
127+
{
128+
return $this->outputFormats;
129+
}
130+
131+
/** @param string[] $outputFormats */
132+
public function setOutputFormats(array $outputFormats): void
133+
{
134+
$this->outputFormats = $outputFormats;
135+
}
68136
}

0 commit comments

Comments
 (0)