6
6
use Doctrine \RST \Meta \MetaEntry ;
7
7
use Doctrine \RST \Meta \Metas ;
8
8
use Symfony \Component \Console \Helper \ProgressBar ;
9
+ use Symfony \Component \Console \Style \SymfonyStyle ;
9
10
use Symfony \Component \DomCrawler \Crawler ;
10
11
use Symfony \Component \Filesystem \Filesystem ;
11
12
use Symfony \Component \Finder \Finder ;
12
13
use SymfonyDocsBuilder \BuildContext ;
13
14
14
- /**
15
- * Class JsonGenerator
16
- */
17
15
class JsonGenerator
18
16
{
19
17
private $ metas ;
20
18
21
19
private $ buildContext ;
22
20
21
+ /** @var SymfonyStyle|null */
22
+ private $ output ;
23
+
23
24
public function __construct (Metas $ metas , BuildContext $ buildContext )
24
25
{
25
26
$ this ->metas = $ metas ;
@@ -60,6 +61,11 @@ public function generateJson(ProgressBar $progressBar)
60
61
$ progressBar ->finish ();
61
62
}
62
63
64
+ public function setOutput (SymfonyStyle $ output )
65
+ {
66
+ $ this ->output = $ output ;
67
+ }
68
+
63
69
private function generateToc (MetaEntry $ metaEntry , ?array $ titles ): array
64
70
{
65
71
if (null === $ titles ) {
@@ -81,14 +87,21 @@ private function generateToc(MetaEntry $metaEntry, ?array $titles): array
81
87
82
88
private function guessNext (string $ parserFilename ): ?array
83
89
{
84
- $ meta = $ this ->getMetaEntry ($ parserFilename );
90
+ $ meta = $ this ->getMetaEntry ($ parserFilename , true );
91
+
85
92
$ parentFile = $ meta ->getParent ();
86
93
87
94
// if current file is an index, next is the first chapter
88
95
if ('index ' === $ parentFile && \count ($ tocs = $ meta ->getTocs ()) === 1 && \count ($ tocs [0 ]) > 0 ) {
96
+ $ firstChapterMeta = $ this ->getMetaEntry ($ tocs [0 ][0 ]);
97
+
98
+ if (null === $ firstChapterMeta ) {
99
+ return null ;
100
+ }
101
+
89
102
return [
90
- 'title ' => $ this -> getMetaEntry ( $ tocs [ 0 ][ 0 ]) ->getTitle (),
91
- 'link ' => $ this -> getMetaEntry ( $ tocs [ 0 ][ 0 ]) ->getUrl (),
103
+ 'title ' => $ firstChapterMeta ->getTitle (),
104
+ 'link ' => $ firstChapterMeta ->getUrl (),
92
105
];
93
106
}
94
107
@@ -100,15 +113,21 @@ private function guessNext(string $parserFilename): ?array
100
113
101
114
$ nextFileName = $ toc [$ indexCurrentFile + 1 ];
102
115
116
+ $ nextMeta = $ this ->getMetaEntry ($ nextFileName );
117
+
118
+ if (null === $ nextMeta ) {
119
+ return null ;
120
+ }
121
+
103
122
return [
104
- 'title ' => $ this -> getMetaEntry ( $ nextFileName ) ->getTitle (),
105
- 'link ' => $ this -> getMetaEntry ( $ nextFileName ) ->getUrl (),
123
+ 'title ' => $ nextMeta ->getTitle (),
124
+ 'link ' => $ nextMeta ->getUrl (),
106
125
];
107
126
}
108
127
109
128
private function guessPrev (string $ parserFilename ): ?array
110
129
{
111
- $ meta = $ this ->getMetaEntry ($ parserFilename );
130
+ $ meta = $ this ->getMetaEntry ($ parserFilename, true );
112
131
$ parentFile = $ meta ->getParent ();
113
132
114
133
// no prev if parent is an index
@@ -120,9 +139,15 @@ private function guessPrev(string $parserFilename): ?array
120
139
121
140
// if current file is the first one of the chapter, prev is the direct parent
122
141
if (0 === $ indexCurrentFile ) {
142
+ $ parentMeta = $ this ->getMetaEntry ($ parentFile );
143
+
144
+ if (null === $ parentMeta ) {
145
+ return null ;
146
+ }
147
+
123
148
return [
124
- 'title ' => $ this -> getMetaEntry ( $ parentFile ) ->getTitle (),
125
- 'link ' => $ this -> getMetaEntry ( $ parentFile ) ->getUrl (),
149
+ 'title ' => $ parentMeta ->getTitle (),
150
+ 'link ' => $ parentMeta ->getUrl (),
126
151
];
127
152
}
128
153
@@ -132,15 +157,21 @@ private function guessPrev(string $parserFilename): ?array
132
157
133
158
$ prevFileName = $ toc [$ indexCurrentFile - 1 ];
134
159
160
+ $ prevMeta = $ this ->getMetaEntry ($ prevFileName );
161
+
162
+ if (null === $ prevMeta ) {
163
+ return null ;
164
+ }
165
+
135
166
return [
136
- 'title ' => $ this -> getMetaEntry ( $ prevFileName ) ->getTitle (),
137
- 'link ' => $ this -> getMetaEntry ( $ prevFileName ) ->getUrl (),
167
+ 'title ' => $ prevMeta ->getTitle (),
168
+ 'link ' => $ prevMeta ->getUrl (),
138
169
];
139
170
}
140
171
141
172
private function getNextPrevInformation (string $ parserFilename ): ?array
142
173
{
143
- $ meta = $ this ->getMetaEntry ($ parserFilename );
174
+ $ meta = $ this ->getMetaEntry ($ parserFilename, true );
144
175
$ parentFile = $ meta ->getParent ();
145
176
146
177
if (!$ parentFile ) {
@@ -149,7 +180,7 @@ private function getNextPrevInformation(string $parserFilename): ?array
149
180
150
181
$ metaParent = $ this ->getMetaEntry ($ parentFile );
151
182
152
- if (!$ metaParent ->getTocs () || \count ($ metaParent ->getTocs ()) !== 1 ) {
183
+ if (null === $ metaParent || !$ metaParent ->getTocs () || \count ($ metaParent ->getTocs ()) !== 1 ) {
153
184
return [null , null ];
154
185
}
155
186
@@ -164,12 +195,21 @@ private function getNextPrevInformation(string $parserFilename): ?array
164
195
return [$ toc , $ indexCurrentFile ];
165
196
}
166
197
167
- private function getMetaEntry (string $ parserFilename ): MetaEntry
198
+ private function getMetaEntry (string $ parserFilename, bool $ throwOnMissing = false ): ? MetaEntry
168
199
{
169
200
$ metaEntry = $ this ->metas ->get ($ parserFilename );
170
201
202
+ // this is possible if there are invalid references
171
203
if (null === $ metaEntry ) {
172
- throw new \LogicException (sprintf ('Could not find MetaEntry for file "%s" ' , $ parserFilename ));
204
+ $ message = sprintf ('Could not find MetaEntry for file "%s" ' , $ parserFilename );
205
+
206
+ if ($ throwOnMissing ) {
207
+ throw new \Exception ($ message );
208
+ }
209
+
210
+ if ($ this ->output ) {
211
+ $ this ->output ->note ($ message );
212
+ }
173
213
}
174
214
175
215
return $ metaEntry ;
0 commit comments