99use flight \Engine ;
1010
1111class DocsLogic {
12- public function __construct (protected Engine $ app ) {
13- //
14- }
15-
16- /**
17- * Renders a page using the specified Latte template file and parameters.
18- *
19- * @param string $latte_file The path to the Latte template file to be rendered.
20- * @param array $params An optional array of parameters to be passed to the template.
21- */
12+ const AVAILABLE_LANGUAGES = [
13+ 'en ' ,
14+ 'es ' ,
15+ 'fr ' ,
16+ 'lv ' ,
17+ 'pt ' ,
18+ 'de ' ,
19+ 'ru ' ,
20+ 'zh ' ,
21+ 'ja ' ,
22+ 'ko ' ,
23+ 'uk ' ,
24+ 'id '
25+ ];
26+
27+ public function __construct (protected Engine $ app ) {
28+ }
29+
30+ /**
31+ * Renders a page using the specified Latte template file and parameters.
32+ *
33+ * @param string $latte_file The path to the Latte template file to be rendered.
34+ * @param array $params An optional array of parameters to be passed to the template.
35+ */
2236 public function renderPage (string $ latte_file , array $ params = []) {
2337 $ request = $ this ->app ->request ();
2438 $ uri = $ request ->url ;
@@ -33,89 +47,109 @@ public function renderPage(string $latte_file, array $params = []) {
3347 $ this ->app ->latte ()->render ($ latte_file , $ params );
3448 }
3549
36- /**
37- * Sets up the translator service with the specified language and version.
38- *
39- * @param string $language The language to be used by the translator.
40- * @param string $version The version of the translation service.
41- * @return Translator The configured translator service.
42- */
43- public function setupTranslatorService (string $ language , string $ version ): Translator {
44- $ Translator = $ this ->app ->translator ();
45- $ Translator ->setLanguage ($ language );
46- $ Translator ->setVersion ($ version );
50+ /**
51+ * Sets up the translator service with the specified language and version.
52+ *
53+ * @param string $language The language to be used by the translator.
54+ * @param string $version The version of the translation service.
55+ * @return Translator The configured translator service.
56+ */
57+ public function setupTranslatorService (string $ language , string $ version ): Translator {
58+ $ Translator = $ this ->app ->translator ();
59+ $ Translator ->setLanguage ($ language );
60+ $ Translator ->setVersion ($ version );
4761 return $ Translator ;
48- }
49-
50- /**
51- * Compiles a single page based on the specified language, version, and section.
52- *
53- * @param string $language The language of the page to compile.
54- * @param string $version The version of the page to compile.
55- * @param string $section The section of the page to compile.
56- *
57- * @return void
58- */
62+ }
63+
64+ /**
65+ * Compiles a single page based on the specified language, version, and section.
66+ *
67+ * @param string $language The language of the page to compile.
68+ * @param string $version The version of the page to compile.
69+ * @param string $section The section of the page to compile.
70+ *
71+ * @return void
72+ */
5973 public function compileSinglePage (string $ language , string $ version , string $ section ) {
6074 $ app = $ this ->app ;
6175
62- $ Translator = $ this ->setupTranslatorService ($ language , $ version );
76+ // Check if the language is valid
77+ if ($ this ->checkValidLanguage ($ language ) === false ) {
78+ $ language = 'en ' ;
79+ }
80+
81+ // Check if the version is valid
82+ if ($ this ->checkValidVersion ($ version ) === false ) {
83+ $ version = 'v3 ' ;
84+ }
85+
86+ $ Translator = $ this ->setupTranslatorService ($ language , $ version );
6387
64- $ cacheStartTime = microtime (true );
65- $ cacheHit = true ;
66- $ cacheKey = $ section . '_html_ ' . $ language . '_ ' . $ version ;
67- $ markdown_html = $ app ->cache ()->retrieve ($ cacheKey );
68- if ($ markdown_html === null ) {
69- $ cacheHit = false ;
70- $ markdown_html = $ app ->parsedown ()->text ($ Translator ->getMarkdownLanguageFile ($ section . '.md ' ));
71- $ markdown_html = Text::addClassesToElements ($ markdown_html );
72- $ app ->cache ()->store ($ cacheKey , $ markdown_html , 86400 ); // 1 day
73- }
88+ $ cacheStartTime = microtime (true );
89+ $ cacheHit = true ;
90+ $ cacheKey = $ section . '_html_ ' . $ language . '_ ' . $ version ;
91+ $ markdown_html = $ app ->cache ()->retrieve ($ cacheKey );
92+ if ($ markdown_html === null ) {
93+ $ cacheHit = false ;
94+ $ markdown_html = $ app ->parsedown ()->text ($ Translator ->getMarkdownLanguageFile ($ section . '.md ' ));
95+ $ markdown_html = Text::addClassesToElements ($ markdown_html );
96+ $ app ->cache ()->store ($ cacheKey , $ markdown_html , 86400 ); // 1 day
97+ }
7498
75- $ app ->eventDispatcher ()->trigger ('flight.cache.checked ' , 'compile_single_page_ ' . $ cacheKey , $ cacheHit , microtime (true ) - $ cacheStartTime );
99+ $ app ->eventDispatcher ()->trigger ('flight.cache.checked ' , 'compile_single_page_ ' . $ cacheKey , $ cacheHit , microtime (true ) - $ cacheStartTime );
76100
77101 $ markdown_html = $ this ->wrapContentInDiv ($ markdown_html );
78102
79103 $ this ->renderPage ('single_page.latte ' , [
80104 'page_title ' => $ section ,
81105 'markdown ' => $ markdown_html ,
82- 'version ' => $ version ,
106+ 'version ' => $ version ,
83107 ]);
84108 }
85109
86- /**
87- * Compiles the Scrollspy page based on the provided language, version, section, and sub-section.
88- *
89- * @param string $language The language of the documentation.
90- * @param string $version The version of the documentation.
91- * @param string $section The main section of the documentation.
92- * @param string $sub_section The sub-section of the documentation.
93- */
110+ /**
111+ * Compiles the Scrollspy page based on the provided language, version, section, and sub-section.
112+ *
113+ * @param string $language The language of the documentation.
114+ * @param string $version The version of the documentation.
115+ * @param string $section The main section of the documentation.
116+ * @param string $sub_section The sub-section of the documentation.
117+ */
94118 public function compileScrollspyPage (string $ language , string $ version , string $ section , string $ sub_section ) {
95119 $ app = $ this ->app ;
96120
97- $ Translator = $ this ->setupTranslatorService ($ language , $ version );
121+ // Check if the language is valid
122+ if ($ this ->checkValidLanguage ($ language ) === false ) {
123+ $ language = 'en ' ;
124+ }
125+
126+ // Check if the version is valid
127+ if ($ this ->checkValidVersion ($ version ) === false ) {
128+ $ version = 'v3 ' ;
129+ }
130+
131+ $ Translator = $ this ->setupTranslatorService ($ language , $ version );
98132
99- $ section_file_path = str_replace ('_ ' , '- ' , $ section );
133+ $ section_file_path = str_replace ('_ ' , '- ' , $ section );
100134 $ sub_section_underscored = str_replace ('- ' , '_ ' , $ sub_section );
101135 $ heading_data = $ app ->cache ()->retrieve ($ sub_section_underscored . '_heading_data_ ' . $ language . '_ ' . $ version );
102136
103- $ cacheStartTime = microtime (true );
104- $ cacheHit = true ;
105- $ cacheKey = $ sub_section_underscored . '_html_ ' . $ language . '_ ' . $ version ;
106- $ markdown_html = $ app ->cache ()->retrieve ($ cacheKey );
107- if ($ markdown_html === null ) {
108- $ cacheHit = false ;
109- $ markdown_html = $ app ->parsedown ()->text ($ Translator ->getMarkdownLanguageFile ('/ ' . $ section_file_path . '/ ' . $ sub_section_underscored . '.md ' ));
137+ $ cacheStartTime = microtime (true );
138+ $ cacheHit = true ;
139+ $ cacheKey = $ sub_section_underscored . '_html_ ' . $ language . '_ ' . $ version ;
140+ $ markdown_html = $ app ->cache ()->retrieve ($ cacheKey );
141+ if ($ markdown_html === null ) {
142+ $ cacheHit = false ;
143+ $ markdown_html = $ app ->parsedown ()->text ($ Translator ->getMarkdownLanguageFile ('/ ' . $ section_file_path . '/ ' . $ sub_section_underscored . '.md ' ));
110144
111145 $ heading_data = [];
112- $ markdown_html = Text::generateAndConvertHeaderListFromHtml ($ markdown_html , $ heading_data , 'h2 ' , $ section_file_path. '/ ' . $ sub_section );
113- $ markdown_html = Text::addClassesToElements ($ markdown_html );
146+ $ markdown_html = Text::generateAndConvertHeaderListFromHtml ($ markdown_html , $ heading_data , 'h2 ' , $ section_file_path . '/ ' . $ sub_section );
147+ $ markdown_html = Text::addClassesToElements ($ markdown_html );
114148 $ app ->cache ()->store ($ sub_section_underscored . '_heading_data_ ' . $ language . '_ ' . $ version , $ heading_data , 86400 ); // 1 day
115- $ app ->cache ()->store ($ cacheKey , $ markdown_html , 86400 ); // 1 day
116- }
149+ $ app ->cache ()->store ($ cacheKey , $ markdown_html , 86400 ); // 1 day
150+ }
117151
118- $ app ->eventDispatcher ()->trigger ('flight.cache.checked ' , 'compile_scrollspy_page_ ' . $ cacheKey , $ cacheHit , microtime (true ) - $ cacheStartTime );
152+ $ app ->eventDispatcher ()->trigger ('flight.cache.checked ' , 'compile_scrollspy_page_ ' . $ cacheKey , $ cacheHit , microtime (true ) - $ cacheStartTime );
119153
120154 // pull the title out of the first h1 tag
121155 $ page_title = '' ;
@@ -133,17 +167,17 @@ public function compileScrollspyPage(string $language, string $version, string $
133167 'custom_page_title ' => ($ page_title ? $ page_title . ' - ' : '' ) . $ Translator ->translate ($ section ),
134168 'markdown ' => $ markdown_html ,
135169 'heading_data ' => $ heading_data ,
136- 'relative_uri ' => '/ ' . $ section_file_path ,
137- 'version ' => $ version ,
170+ 'relative_uri ' => '/ ' . $ section_file_path ,
171+ 'version ' => $ version ,
138172 ]);
139173 }
140174
141- /**
175+ /**
142176 * This is necessary to encapsulate contents (<p>, <pre>, <ol>, <ul>)
143177 * in a div which can be then styled with CSS thanks to the class name `flight-block`
144- *
145- * @param string $html
146- * @return string
178+ *
179+ * @param string $html
180+ * @return string
147181 */
148182 protected function wrapContentInDiv (string $ html ): string {
149183 $ dom = new DOMDocument ;
@@ -189,8 +223,8 @@ protected function wrapContentInDiv(string $html): string {
189223 && $ element ->nodeName !== 'ol '
190224 && $ element ->nodeName !== 'ul '
191225 && $ element ->nodeName !== 'blockquote '
192- && $ element ->nodeName !== 'table '
193- && $ element ->nodeName !== 'dl '
226+ && $ element ->nodeName !== 'table '
227+ && $ element ->nodeName !== 'dl '
194228 && is_null ($ div ) === false
195229 ) {
196230 $ d .= '</div> ' ;
@@ -208,4 +242,24 @@ protected function wrapContentInDiv(string $html): string {
208242
209243 return $ d ;
210244 }
245+
246+ /**
247+ * Checks if the provided language is valid.
248+ *
249+ * @param string $language The language code to check.
250+ * @return bool True if the language is valid, false otherwise.
251+ */
252+ public function checkValidLanguage (string $ language ): bool {
253+ return in_array ($ language , self ::AVAILABLE_LANGUAGES , true ) === true ;
254+ }
255+
256+ /**
257+ * Checks if the provided version is valid.
258+ *
259+ * @param string $version The version code to check.
260+ * @return bool True if the version is valid, false otherwise.
261+ */
262+ public function checkValidVersion (string $ version ): bool {
263+ return in_array ($ version , ['v3 ' , 'v2 ' ], true ) === true ;
264+ }
211265}
0 commit comments