4
4
5
5
use Doctrine \RST \Environment ;
6
6
use Doctrine \RST \Meta \MetaEntry ;
7
+ use Doctrine \RST \Meta \Metas ;
7
8
use Symfony \Component \Console \Helper \ProgressBar ;
8
9
use Symfony \Component \DomCrawler \Crawler ;
9
10
use Symfony \Component \Filesystem \Filesystem ;
10
11
use Symfony \Component \Finder \Finder ;
12
+ use SymfonyDocsBuilder \BuildContext ;
11
13
12
14
/**
13
15
* Class JsonGenerator
14
16
*/
15
17
class JsonGenerator
16
18
{
17
- use GeneratorTrait ;
19
+ private $ metas ;
18
20
19
- public function generateJson (array $ documents , ProgressBar $ progressBar )
20
- {
21
- $ this ->extractEnvironmentsAndCachedMetas ($ documents );
21
+ private $ buildContext ;
22
22
23
- $ finder = new Finder ();
24
- $ finder ->in ($ this ->buildContext ->getHtmlOutputDir ())
25
- ->name ('*.html ' )
26
- ->files ();
23
+ public function __construct (Metas $ metas , BuildContext $ buildContext )
24
+ {
25
+ $ this ->metas = $ metas ;
26
+ $ this ->buildContext = $ buildContext ;
27
+ }
27
28
29
+ public function generateJson (ProgressBar $ progressBar )
30
+ {
28
31
$ fs = new Filesystem ();
29
32
30
- foreach ($ finder as $ file ) {
31
- $ parserFilename = $ this ->getParserFilename ($ file ->getRealPath (), $ this ->buildContext ->getHtmlOutputDir ());
32
- $ jsonFilename = str_replace ([$ this ->buildContext ->getHtmlOutputDir (), '.html ' ], [$ this ->buildContext ->getJsonOutputDir (), '.json ' ], $ file ->getRealPath ());
33
-
34
- if ($ this ->useCacheForFile ($ parserFilename )) {
35
- if (!file_exists ($ jsonFilename )) {
36
- throw new \RuntimeException (
37
- sprintf ('File %s does not exist although cache is enabled and related environment is not available ' , $ jsonFilename )
38
- );
39
- }
40
-
41
- continue ;
42
- }
43
-
44
- $ meta = $ this ->getMeta ($ parserFilename );
33
+ foreach ($ this ->metas ->getAll () as $ filename => $ metaEntry ) {
34
+ $ parserFilename = $ filename ;
35
+ $ jsonFilename = $ this ->buildContext ->getJsonOutputDir ().'/ ' .$ filename .'.json ' ;
45
36
46
- $ crawler = new Crawler ($ file -> getContents ( ));
37
+ $ crawler = new Crawler (file_get_contents ( $ this -> buildContext -> getHtmlOutputDir (). ' / ' . $ filename . ' .html ' ));
47
38
48
39
$ data = [
49
40
'body ' => $ crawler ->filter ('body ' )->html (),
50
- 'title ' => $ meta ->getTitle (),
41
+ 'title ' => $ metaEntry ->getTitle (),
51
42
'current_page_name ' => $ parserFilename ,
52
- 'toc ' => $ this ->generateToc ($ meta , current ($ meta ->getTitles ())[1 ]),
43
+ 'toc ' => $ this ->generateToc ($ metaEntry , current ($ metaEntry ->getTitles ())[1 ]),
53
44
'next ' => $ this ->guessNext ($ parserFilename ),
54
45
'prev ' => $ this ->guessPrev ($ parserFilename ),
55
46
'rellinks ' => [
@@ -90,14 +81,14 @@ private function generateToc(MetaEntry $metaEntry, ?array $titles): array
90
81
91
82
private function guessNext (string $ parserFilename ): ?array
92
83
{
93
- $ meta = $ this ->getMeta ($ parserFilename );
84
+ $ meta = $ this ->getMetaEntry ($ parserFilename );
94
85
$ parentFile = $ meta ->getParent ();
95
86
96
87
// if current file is an index, next is the first chapter
97
88
if ('index ' === $ parentFile && \count ($ tocs = $ meta ->getTocs ()) === 1 && \count ($ tocs [0 ]) > 0 ) {
98
89
return [
99
- 'title ' => $ this ->getMeta ($ tocs [0 ][0 ])->getTitle (),
100
- 'link ' => $ this ->getMeta ($ tocs [0 ][0 ])->getUrl (),
90
+ 'title ' => $ this ->getMetaEntry ($ tocs [0 ][0 ])->getTitle (),
91
+ 'link ' => $ this ->getMetaEntry ($ tocs [0 ][0 ])->getUrl (),
101
92
];
102
93
}
103
94
@@ -110,14 +101,14 @@ private function guessNext(string $parserFilename): ?array
110
101
$ nextFileName = $ toc [$ indexCurrentFile + 1 ];
111
102
112
103
return [
113
- 'title ' => $ this ->getMeta ($ nextFileName )->getTitle (),
114
- 'link ' => $ this ->getMeta ($ nextFileName )->getUrl (),
104
+ 'title ' => $ this ->getMetaEntry ($ nextFileName )->getTitle (),
105
+ 'link ' => $ this ->getMetaEntry ($ nextFileName )->getUrl (),
115
106
];
116
107
}
117
108
118
109
private function guessPrev (string $ parserFilename ): ?array
119
110
{
120
- $ meta = $ this ->getMeta ($ parserFilename );
111
+ $ meta = $ this ->getMetaEntry ($ parserFilename );
121
112
$ parentFile = $ meta ->getParent ();
122
113
123
114
// no prev if parent is an index
@@ -130,8 +121,8 @@ private function guessPrev(string $parserFilename): ?array
130
121
// if current file is the first one of the chapter, prev is the direct parent
131
122
if (0 === $ indexCurrentFile ) {
132
123
return [
133
- 'title ' => $ this ->getMeta ($ parentFile )->getTitle (),
134
- 'link ' => $ this ->getMeta ($ parentFile )->getUrl (),
124
+ 'title ' => $ this ->getMetaEntry ($ parentFile )->getTitle (),
125
+ 'link ' => $ this ->getMetaEntry ($ parentFile )->getUrl (),
135
126
];
136
127
}
137
128
@@ -142,21 +133,21 @@ private function guessPrev(string $parserFilename): ?array
142
133
$ prevFileName = $ toc [$ indexCurrentFile - 1 ];
143
134
144
135
return [
145
- 'title ' => $ this ->getMeta ($ prevFileName )->getTitle (),
146
- 'link ' => $ this ->getMeta ($ prevFileName )->getUrl (),
136
+ 'title ' => $ this ->getMetaEntry ($ prevFileName )->getTitle (),
137
+ 'link ' => $ this ->getMetaEntry ($ prevFileName )->getUrl (),
147
138
];
148
139
}
149
140
150
141
private function getNextPrevInformation (string $ parserFilename ): ?array
151
142
{
152
- $ meta = $ this ->getMeta ($ parserFilename );
143
+ $ meta = $ this ->getMetaEntry ($ parserFilename );
153
144
$ parentFile = $ meta ->getParent ();
154
145
155
146
if (!$ parentFile ) {
156
147
return [null , null ];
157
148
}
158
149
159
- $ metaParent = $ this ->getMeta ($ parentFile );
150
+ $ metaParent = $ this ->getMetaEntry ($ parentFile );
160
151
161
152
if (!$ metaParent ->getTocs () || \count ($ metaParent ->getTocs ()) !== 1 ) {
162
153
return [null , null ];
@@ -172,4 +163,15 @@ private function getNextPrevInformation(string $parserFilename): ?array
172
163
173
164
return [$ toc , $ indexCurrentFile ];
174
165
}
166
+
167
+ private function getMetaEntry (string $ parserFilename ): MetaEntry
168
+ {
169
+ $ metaEntry = $ this ->metas ->get ($ parserFilename );
170
+
171
+ if (null === $ metaEntry ) {
172
+ throw new \LogicException (sprintf ('Could not find MetaEntry for file "%s" ' , $ parserFilename ));
173
+ }
174
+
175
+ return $ metaEntry ;
176
+ }
175
177
}
0 commit comments