Skip to content

Commit 92093f0

Browse files
committed
use composition over inheritance for SymfonyHTMLFormat
1 parent 5e144d8 commit 92093f0

File tree

6 files changed

+111
-39
lines changed

6 files changed

+111
-39
lines changed

_build/composer.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_build/src/Command/ParseDoc.php

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
namespace SymfonyDocs\Command;
44

55
use Doctrine\RST\Builder;
6-
use Doctrine\RST\Configuration;
6+
use Doctrine\RST\Event\PostBuildRenderEvent;
7+
use Doctrine\RST\Event\PostNodeRenderEvent;
8+
use Doctrine\RST\Event\PostParseDocumentEvent;
9+
use Doctrine\RST\Event\PreBuildParseEvent;
10+
use Doctrine\RST\Event\PreBuildRenderEvent;
11+
use Doctrine\RST\Event\PreBuildScanEvent;
712
use Symfony\Component\Console\Command\Command;
813
use Symfony\Component\Console\Helper\ProgressBar;
914
use Symfony\Component\Console\Input\InputInterface;
@@ -12,9 +17,8 @@
1217
use Symfony\Component\Console\Style\SymfonyStyle;
1318
use Symfony\Component\Filesystem\Filesystem;
1419
use Symfony\Component\Finder\Finder;
15-
use SymfonyDocs\KernelFactory;
16-
use SymfonyDocs\SymfonyDocConfiguration;
1720
use SymfonyDocs\JsonGenerator;
21+
use SymfonyDocs\KernelFactory;
1822

1923
/**
2024
* Class ParseDoc
@@ -29,16 +33,21 @@ class ParseDoc extends Command
2933
private $finder;
3034
/** @var ProgressBar */
3135
private $progressBar;
36+
/** @var Builder */
37+
private $builder;
38+
/** @var OutputInterface */
39+
private $output;
3240
private $sourceDir;
3341
private $htmlOutputDir;
3442
private $jsonOutputDir;
43+
private $parsedFiles = [];
3544

3645
public function __construct()
3746
{
3847
parent::__construct(self::$defaultName);
3948

4049
$this->filesystem = new Filesystem();
41-
$this->finder = new Finder();
50+
$this->finder = new Finder();
4251
}
4352

4453
protected function configure()
@@ -48,13 +57,13 @@ protected function configure()
4857
$this
4958
->addOption('source-dir', null, InputOption::VALUE_REQUIRED, 'RST files Source directory', __DIR__.'/../../..')
5059
->addOption('html-output-dir', null, InputOption::VALUE_REQUIRED, 'HTML files output directory', __DIR__.'/../../html')
51-
->addOption('json-output-dir', null, InputOption::VALUE_REQUIRED, 'JSON files output directory', __DIR__.'/../../json')
52-
;
60+
->addOption('json-output-dir', null, InputOption::VALUE_REQUIRED, 'JSON files output directory', __DIR__.'/../../json');
5361
}
5462

5563
protected function initialize(InputInterface $input, OutputInterface $output)
5664
{
5765
$this->io = new SymfonyStyle($input, $output);
66+
$this->output = $output;
5867

5968
$this->sourceDir = $this->getRealAbsolutePath($input->getOption('source-dir'));
6069
if (!$this->filesystem->exists($this->sourceDir)) {
@@ -70,30 +79,36 @@ protected function initialize(InputInterface $input, OutputInterface $output)
7079
if ($this->filesystem->exists($this->jsonOutputDir)) {
7180
$this->filesystem->remove($this->jsonOutputDir);
7281
}
82+
83+
$this->builder = new Builder(KernelFactory::createKernel());
84+
$eventManager = $this->builder->getConfiguration()->getEventManager();
85+
$eventManager->addEventListener(
86+
[PostParseDocumentEvent::POST_PARSE_DOCUMENT],
87+
$this
88+
);
89+
$eventManager->addEventListener(
90+
[PreBuildRenderEvent::PRE_BUILD_RENDER],
91+
$this
92+
);
7393
}
7494

7595
protected function execute(InputInterface $input, OutputInterface $output)
7696
{
77-
$builder = KernelFactory::createKernel();
78-
79-
// $builder->addHook([$this, 'handleProgressBar']);
80-
8197
$this->finder->in($input->getOption('source-dir'))
8298
->exclude(['_build', '.github', '.platform', '_images'])
8399
->notName('*.rst.inc')
84100
->name('*.rst');
85101

86-
$this->io->note(sprintf('Start parsing into html %d rst files', $this->finder->count()));
102+
$this->io->note(sprintf('Start parsing %d rst files', $this->finder->count()));
87103
$this->progressBar = new ProgressBar($output, $this->finder->count());
88104

89-
$builder->build(
105+
$this->builder->build(
90106
$this->sourceDir,
91107
$this->htmlOutputDir
92108
);
93109

94-
$this->progressBar->finish();
95110
$this->io->newLine(2);
96-
$this->io->success('Parse into html complete');
111+
$this->io->success('HTML rendering complete!');
97112

98113
foreach ($this->finder as $file) {
99114
$htmlFile = str_replace([$this->sourceDir, '.rst'], [$this->htmlOutputDir, '.html'], $file->getRealPath());
@@ -104,7 +119,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
104119

105120
$this->io->note('Start transforming doc into json files');
106121
$this->progressBar = new ProgressBar($output, $this->finder->count());
107-
$jsonGenerator = new JsonGenerator($builder->getDocuments()->getAll());
122+
$jsonGenerator = new JsonGenerator($this->builder->getDocuments()->getAll());
108123
$jsonGenerator->generateJson($this->htmlOutputDir, $this->jsonOutputDir, $this->progressBar);
109124
$this->io->newLine(2);
110125
$this->io->success('Parse process complete');
@@ -125,4 +140,27 @@ private function getRealAbsolutePath(string $path): string
125140
)
126141
);
127142
}
143+
144+
public function postParseDocument(PostParseDocumentEvent $postParseDocumentEvent)
145+
{
146+
$file = $postParseDocumentEvent->getDocumentNode()->getEnvironment()->getCurrentFileName();
147+
if (!\in_array($file, $this->parsedFiles)) {
148+
$this->parsedFiles[] = $postParseDocumentEvent->getDocumentNode()->getEnvironment()->getCurrentFileName();
149+
$this->progressBar->advance();
150+
}
151+
}
152+
153+
public function preBuildRender()
154+
{
155+
$eventManager = $this->builder->getConfiguration()->getEventManager();
156+
$eventManager->removeEventListener(
157+
[PostParseDocumentEvent::POST_PARSE_DOCUMENT],
158+
$this
159+
);
160+
161+
$this->progressBar->finish();
162+
163+
$this->io->newLine(2);
164+
$this->io->note('Start rendering in HTML...');
165+
}
128166
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace SymfonyDocs\Directive;
4+
5+
use Doctrine\RST\Directives\SubDirective;
6+
7+
class RoleDirective extends SubDirective
8+
{
9+
public function getName() : string
10+
{
11+
return 'role';
12+
}
13+
}

_build/src/KernelFactory.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Doctrine\RST\Configuration;
66
use Doctrine\RST\Kernel;
7-
use SymfonyDocs\Directive as SymfonyDoirectives;
7+
use SymfonyDocs\Directive as SymfonyDirectives;
88
use SymfonyDocs\Reference as SymfonyRefernces;
99

1010
/**
@@ -17,7 +17,12 @@ public static function createKernel(): Kernel
1717
$configuration = new Configuration();
1818
$configuration->setCustomTemplateDirs([__DIR__.'/Templates']);
1919
$configuration->setCacheDir(__DIR__.'/../var/cache');
20-
$configuration->addFormat(new SymfonyFormat($configuration->getTemplateRenderer()));
20+
$configuration->addFormat(
21+
new SymfonyHTMLFormat(
22+
$configuration->getTemplateRenderer(),
23+
$configuration->getFormat()
24+
)
25+
);
2126

2227
return new Kernel(
2328
$configuration,
@@ -29,17 +34,18 @@ public static function createKernel(): Kernel
2934
private static function getDirectives(): array
3035
{
3136
return [
32-
new SymfonyDoirectives\CautionDirective(),
33-
new SymfonyDoirectives\ClassDirective(),
34-
new SymfonyDoirectives\CodeBlockDirective(),
35-
new SymfonyDoirectives\ConfigurationBlockDirective(),
36-
new SymfonyDoirectives\IndexDirective(),
37-
new SymfonyDoirectives\NoteDirective(),
38-
new SymfonyDoirectives\SeeAlsoDirective(),
39-
new SymfonyDoirectives\SidebarDirective(),
40-
new SymfonyDoirectives\TipDirective(),
41-
new SymfonyDoirectives\VersionAddedDirective(),
42-
new SymfonyDoirectives\BestPracticeDirective(),
37+
new SymfonyDirectives\CautionDirective(),
38+
new SymfonyDirectives\ClassDirective(),
39+
new SymfonyDirectives\CodeBlockDirective(),
40+
new SymfonyDirectives\ConfigurationBlockDirective(),
41+
new SymfonyDirectives\IndexDirective(),
42+
new SymfonyDirectives\RoleDirective(),
43+
new SymfonyDirectives\NoteDirective(),
44+
new SymfonyDirectives\SeeAlsoDirective(),
45+
new SymfonyDirectives\SidebarDirective(),
46+
new SymfonyDirectives\TipDirective(),
47+
new SymfonyDirectives\VersionAddedDirective(),
48+
new SymfonyDirectives\BestPracticeDirective(),
4349
];
4450
}
4551

_build/src/SymfonyFormat.php renamed to _build/src/SymfonyHTMLFormat.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace SymfonyDocs;
44

5+
use Doctrine\RST\Formats\Format;
56
use Doctrine\RST\HTML\HTMLFormat;
67
use Doctrine\RST\Nodes\CodeNode;
78
use Doctrine\RST\Nodes\SpanNode;
@@ -10,25 +11,37 @@
1011
use Doctrine\RST\Templates\TemplateRenderer;
1112

1213
/**
13-
* Class SymfonyFormat
14+
* Class SymfonyHTMLFormat
1415
*/
15-
final class SymfonyFormat extends HTMLFormat
16+
final class SymfonyHTMLFormat implements Format
1617
{
1718
/** @var TemplateRenderer */
1819
protected $templateRenderer;
20+
/** @var HTMLFormat */
21+
private $htmlFormat;
1922

20-
public function __construct(TemplateRenderer $templateRenderer)
23+
public function __construct(TemplateRenderer $templateRenderer, Format $HTMLFormat)
2124
{
22-
parent::__construct($templateRenderer);
2325
$this->templateRenderer = $templateRenderer;
26+
$this->htmlFormat = $HTMLFormat;
27+
}
28+
29+
public function getFileExtension(): string
30+
{
31+
return Format::HTML;
32+
}
33+
34+
public function getDirectives(): array
35+
{
36+
return $this->htmlFormat->getDirectives();
2437
}
2538

2639
/**
2740
* @return NodeRendererFactory[]
2841
*/
2942
public function getNodeRendererFactories(): array
3043
{
31-
$nodeRendererFactories = parent::getNodeRendererFactories();
44+
$nodeRendererFactories = $this->htmlFormat->getNodeRendererFactories();
3245

3346
$nodeRendererFactories[CodeNode::class] = new CallableNodeRendererFactory(
3447
function (CodeNode $node) {

reference/configuration/framework.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Configuration
7070
* `lifetime`_ (deprecated since 2.8)
7171
* `matcher`_
7272

73-
* `ip`_
73+
* :ref:`ip <reference-profiler-matcher-ip>`
7474
* :ref:`path <reference-profiler-matcher-path>`
7575
* `service`_
7676

@@ -680,13 +680,15 @@ matcher
680680
.......
681681

682682
Matcher options are configured to dynamically enable the profiler. For
683-
instance, based on the `ip`_ or :ref:`path <reference-profiler-matcher-path>`.
683+
instance, based on the :ref:`ip <reference-profiler-matcher-ip>` or :ref:`path <reference-profiler-matcher-path>`.
684684

685685
.. seealso::
686686

687687
See :doc:`/profiler/matchers` for more information about using
688688
matchers to enable/disable the profiler.
689689

690+
.. _reference-profiler-matcher-ip:
691+
690692
ip
691693
""
692694

0 commit comments

Comments
 (0)