Skip to content

Commit 73e0baa

Browse files
committed
Merge master
1 parent b421340 commit 73e0baa

File tree

10 files changed

+437
-45
lines changed

10 files changed

+437
-45
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ biome.*
1111
node_modules/
1212
tests/cache/
1313
.coverage
14+
.runway-config.json
15+
apm.db*

.runway-config-sample.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"apm": {
3+
"source_type": "sqlite",
4+
"dest_db_dsn": "sqlite::memory:"
5+
}
6+
}

app/config/services.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

33
use app\utils\Translator;
4+
use flight\Apm;
5+
use flight\apm\logger\LoggerFactory;
46
use Latte\Engine as LatteEngine;
57
use Latte\Essential\TranslatorExtension;
68
use Latte\Loaders\FileLoader;
@@ -29,3 +31,8 @@
2931

3032
// Parsedown is a markdown parser
3133
Flight::register('parsedown', Parsedown::class);
34+
35+
// Register the APM
36+
$ApmLogger = LoggerFactory::create(__DIR__ . '/../../.runway-config.json');
37+
$Apm = new Apm($ApmLogger);
38+
$Apm->bindEventsToFlightInstance(Flight::app());

app/controllers/DocsController.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use app\utils\DocsLogic;
77
use app\utils\Text;
88
use Exception;
9+
use flight\core\EventDispatcher;
910
use RecursiveDirectoryIterator;
1011
use RecursiveIteratorIterator;
1112

@@ -171,17 +172,23 @@ public function singlePageGet(string $language, string $version) {
171172

172173
$Translator = $this->DocsLogic->setupTranslatorService($language, $version);
173174

174-
$markdown_html = $app->cache()->refreshIfExpired('single_page_html_' . $language . '_' . $version, function () use ($app, $sections, $Translator) {
175-
$markdown_html = '';
176-
177-
foreach ($sections as $section) {
175+
$cacheHit = true;
176+
$cacheStartTime = microtime(true);
177+
$cacheKey = 'single_page_html_' . $language . '_' . $version;
178+
$markdown_html = $app->cache()->retrieve($cacheKey);
179+
if ($markdown_html === null) {
180+
$cacheHit = false;
181+
$markdown_html = '';
182+
foreach ($sections as $section) {
178183
$slugged_section = Text::slugify($section);
179184
$markdown_html .= '<h1><a href="/' . $section . '" id="' . $slugged_section . '">' . ucwords($section) . '</a> <a href="/' . $section . '#' . $slugged_section . '" class="bi bi-link-45deg" title="Permalink to this heading"></a></h1>';
180185
$markdown_html .= $app->parsedown()->text($Translator->getMarkdownLanguageFile($section . '.md'));
181186
}
182187

183-
return $markdown_html;
184-
}, 86400); // 1 day
188+
$app->cache()->store($cacheKey, $markdown_html, 86400); // 1 day
189+
}
190+
191+
$app->eventDispatcher()->trigger('flight.cache.checked', 'single_page_get_'.$cacheKey, $cacheHit, microtime(true) - $cacheStartTime);
185192

186193
$this->DocsLogic->renderPage('single_page.latte', [
187194
'page_title' => 'single_page_documentation',

app/utils/CustomFlight.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace app\utils;
4+
5+
use flight\Engine;
6+
use flight\Cache;
7+
use Latte\Engine as LatteEngine;
8+
use Parsedown;
9+
use app\utils\Translator;
10+
11+
/**
12+
* This is only for autocomplete help.
13+
*
14+
* @method Cache cache()
15+
* @method LatteEngine latte()
16+
* @method Parsedown parsedown()
17+
* @method Translator translator()
18+
*/
19+
class CustomEngine extends Engine {
20+
}

app/utils/DocsLogic.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use app\middleware\HeaderSecurityMiddleware;
66
use DOMDocument;
77
use DOMXPath;
8+
use flight\core\EventDispatcher;
89
use flight\Engine;
910

1011
class DocsLogic {
@@ -60,11 +61,18 @@ public function compileSinglePage(string $language, string $version, string $sec
6061

6162
$Translator = $this->setupTranslatorService($language, $version);
6263

63-
$markdown_html = $app->cache()->refreshIfExpired($section . '_html_' . $language . '_' . $version, function() use ($app, $section, $Translator) {
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;
6470
$markdown_html = $app->parsedown()->text($Translator->getMarkdownLanguageFile($section . '.md'));
6571
$markdown_html = Text::addClassesToElements($markdown_html);
66-
return $markdown_html;
67-
}, 86400); // 1 day
72+
$app->cache()->store($cacheKey, $markdown_html, 86400); // 1 day
73+
}
74+
75+
$app->eventDispatcher()->trigger('flight.cache.checked', 'compile_single_page_'.$cacheKey, $cacheHit, microtime(true) - $cacheStartTime);
6876

6977
$markdown_html = $this->wrapContentInDiv($markdown_html);
7078

@@ -88,19 +96,26 @@ public function compileScrollspyPage(string $language, string $version, string $
8896

8997
$Translator = $this->setupTranslatorService($language, $version);
9098

91-
$section_file_path = str_replace('_', '-', $section);
99+
$section_file_path = str_replace('_', '-', $section);
92100
$sub_section_underscored = str_replace('-', '_', $sub_section);
93101
$heading_data = $app->cache()->retrieve($sub_section_underscored . '_heading_data_' . $language . '_' . $version);
94-
$markdown_html = $app->cache()->refreshIfExpired($sub_section_underscored . '_html_' . $language . '_' . $version, function () use ($app, $section_file_path, $sub_section, $sub_section_underscored, &$heading_data, $language, $Translator, $version) {
95-
$parsed_text = $app->parsedown()->text($Translator->getMarkdownLanguageFile('/' . $section_file_path . '/' . $sub_section_underscored . '.md'));
102+
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'));
96110

97111
$heading_data = [];
98-
$parsed_text = Text::generateAndConvertHeaderListFromHtml($parsed_text, $heading_data, 'h2', $section_file_path.'/'.$sub_section);
99-
$parsed_text = Text::addClassesToElements($parsed_text);
112+
$markdown_html = Text::generateAndConvertHeaderListFromHtml($markdown_html, $heading_data, 'h2', $section_file_path.'/'.$sub_section);
113+
$markdown_html = Text::addClassesToElements($markdown_html);
100114
$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+
}
101117

102-
return $parsed_text;
103-
}, 86400); // 1 day
118+
$app->eventDispatcher()->trigger('flight.cache.checked', 'compile_scrollspy_page_'.$cacheKey, $cacheHit, microtime(true) - $cacheStartTime);
104119

105120
// pull the title out of the first h1 tag
106121
$page_title = '';

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"php": "^8.2",
1919
"ext-json": "*",
2020
"erusev/parsedown": "^1.7",
21+
"flightphp/apm": "^0.1.1",
2122
"flightphp/cache": "^1.0",
2223
"flightphp/container": "^1.1",
2324
"flightphp/core": "^3.15",
@@ -37,7 +38,8 @@
3738
"composer check-platform-reqs"
3839
],
3940
"post-autoload-dump": [
40-
"php -r \"if (!file_exists('app/config/config.php')) copy('app/config/config_sample.php', 'app/config/config.php');\""
41+
"php -r \"if (!file_exists('app/config/config.php')) copy('app/config/config_sample.php', 'app/config/config.php');\"",
42+
"php -r \"if (!file_exists('.runway-config.json')) copy('.runway-config-sample.json', '.runway-config.json');\""
4143
],
4244
"pre-update-cmd": [
4345
"composer check-platform-reqs"

0 commit comments

Comments
 (0)