Skip to content

Commit 39f2cef

Browse files
committed
Move support log data into cached view models
1 parent c4f8807 commit 39f2cef

10 files changed

+209
-82
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ html/archives/
22
html/files/xdebug-latest.tgz
33
html/files/xdebug-vc11-latest.tgz
44
html/files/xdebug-vc9-latest.tgz
5+
cache
56

67
vendor/
78

Dockerfile

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ COPY docker/lighttpd/fastcgi.conf /etc/lighttpd/conf-enabled/15-fastcgi-php.conf
1616
RUN mkdir -p /run/lighttpd/
1717
RUN chown www-data. /run/lighttpd/
1818

19+
RUN mkdir /var/www/cache
20+
RUN chown www-data. /var/www/cache
21+
1922
EXPOSE 80
2023

2124
CMD php-fpm -D && lighttpd -D -f /etc/lighttpd/lighttpd.conf

html/router.php

+2-12
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@
1515
} elseif ($requested_uri === '/updates') {
1616
$contents = XdebugDotOrg\Controller\HomeController::updates()->render();
1717
} elseif ($requested_uri === '/download') {
18-
$contents = XdebugDotOrg\Core\ContentsCache::fetch(
19-
function() {
20-
return XdebugDotOrg\Controller\HomeController::download()->render();
21-
},
22-
'download'
23-
);
18+
$contents = XdebugDotOrg\Controller\HomeController::download()->render();
2419
} elseif (preg_match('/^\/docs(\/([a-z_]+))?/', $requested_uri, $matches)) {
2520
$pages = [
2621
'install', 'basic', 'display', 'stack_trace', 'execution_trace',
@@ -50,12 +45,7 @@ function() {
5045
} elseif ($requested_uri === '/reporting-bugs') {
5146
$contents = XdebugDotOrg\Controller\SupportController::reporting_bugs()->render();
5247
} elseif ($requested_uri === '/log') {
53-
$contents = XdebugDotOrg\Core\ContentsCache::fetch(
54-
function() {
55-
return XdebugDotOrg\Controller\SupportController::log()->render();
56-
},
57-
'log'
58-
);
48+
$contents = XdebugDotOrg\Controller\SupportController::log()->render();
5949
} elseif ($requested_uri === '/wizard') {
6050
$contents = XdebugDotOrg\Controller\WizardController::index()->render();
6151
} elseif (preg_match('/^\/ci(\?r=.*)?/', $requested_uri, $matches)) {

src/Controller/HomeController.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static function license() : HtmlResponse
2323
return new HtmlResponse(null, 'home/license.php');
2424
}
2525

26-
public static function download() : HtmlResponse
26+
public static function getDownloadsModel() : Downloads
2727
{
2828
// open the files dir, and scan
2929
$d = dir(dirname(__DIR__, 2) . '/html/files' );
@@ -94,7 +94,21 @@ public static function download() : HtmlResponse
9494
);
9595
}
9696

97-
return new HtmlResponse(new Downloads($downloads), 'home/download.php');
97+
return new Downloads($downloads);
98+
}
99+
100+
public static function download() : HtmlResponse
101+
{
102+
return new HtmlResponse(
103+
\XdebugDotOrg\Core\ContentsCache::fetchModel(
104+
Downloads::class,
105+
function() : Downloads {
106+
return self::getDownloadsModel();
107+
},
108+
'downloads'
109+
),
110+
'home/download.php'
111+
);
98112
}
99113

100114
public static function contributing() : HtmlResponse

src/Controller/SupportController.php

+55-54
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
use XdebugDotOrg\Core\HtmlResponse;
55

66
use XdebugDotOrg\Model\SupportLog;
7+
use XdebugDotOrg\Model\SupportLogDayReport;
8+
use XdebugDotOrg\Model\SupportLogMonthReport;
79

810
class SupportController
911
{
@@ -19,55 +21,59 @@ public static function reporting_bugs() : HtmlResponse
1921

2022
public static function log() : HtmlResponse
2123
{
22-
$d = Dir( 'reports' );
24+
return new HtmlResponse(
25+
\XdebugDotOrg\Core\ContentsCache::fetchModel(
26+
SupportLog::class,
27+
function() : SupportLog {
28+
return self::getLogModel();
29+
},
30+
'log'
31+
),
32+
'support/log.php'
33+
);
34+
}
35+
36+
public static function getLogModel() : SupportLog
37+
{
38+
$d = \dir( 'reports' );
39+
40+
$files = [];
2341

24-
$files = array();
25-
while ( false !== ( $entry = $d->read() ) )
26-
{
42+
while ( false !== ( $entry = $d->read() ) ) {
2743
if (preg_match( '@^20[0-9][0-9]-[01][0-9]\.txt$@', $entry, $m)) {
2844
$files[] = $entry;
2945
}
3046
}
3147

32-
rsort($files);
48+
\rsort($files);
3349

34-
35-
36-
return new HtmlResponse(
37-
new SupportLog($files, self::get_supporters()),
38-
'support/log.php'
50+
return new SupportLog(
51+
array_map(
52+
function($file) {
53+
return self::getMonthReport($file);
54+
},
55+
$files
56+
),
57+
self::get_supporters()
3958
);
4059
}
4160

42-
public static function get_report( string $file ) : string
61+
private static function getMonthReport(string $file) : SupportLogMonthReport
4362
{
4463
$f = file( 'reports/'. $file );
4564
$summary = array_shift($f);
4665

4766
preg_match( '/[0-9]{4}-[0-9]{2}/', $file, $matches );
4867
$d = new \DateTimeImmutable( "{$matches[0]}-01" );
4968

50-
$html = "<h2>" . $d->format( "F Y" ) . "</h2>\n";
51-
52-
// var_dump( $summary, $f );
53-
54-
list( $patreon, $basic, $company, $others ) = explode( "\t", trim( $summary) );
69+
list($patreon, $basic, $company, $others) = explode("\t", trim($summary));
5570
$total = (int) $patreon + (int) $basic + (int) $company + (int) $others;
5671

57-
$html .= "
58-
<div class='funding'>
59-
<div class='others' style='width: {$others}%'></div>
60-
<div class='company' style='width: {$company}%'></div>
61-
<div class='basic' style='width: {$basic}%'></div>
62-
<div class='patreon' style='width: {$patreon}%'></div>
63-
<div class='comment'>Time Funded</div>
64-
</div>";
65-
6672
$totalHours = [];
67-
$logTable = "<table class='log'>\n";
68-
$logTable .= "<tr><th class='day'>Day</th><th class='type'>Type</th><th class='description'>Description</th><th class='hours'>Hours</th></tr>\n";
69-
foreach( $f as $line )
70-
{
73+
74+
$days = [];
75+
76+
foreach ($f as $line) {
7177
$line = trim( $line );
7278
if ( $line == '' ) {
7379
continue;
@@ -81,33 +87,28 @@ public static function get_report( string $file ) : string
8187
$type = 'generic';
8288
};
8389

84-
$logTable .= "<tr><td class='day'>{$day}</td><td class='type'><div class='type-{$type}'>{$type}</div></td><td>{$description}</td><td class='hours'>{$hours}</td></tr>\n";
90+
$days[] = new SupportLogDayReport(
91+
$day,
92+
$type,
93+
$description,
94+
$hours
95+
);
8596

8697
$totalHours[$type] += $hours;
87-
}
88-
$logTable .= "</table>\n";
89-
90-
krsort( $totalHours );
91-
$spendBar = "<div class='spend'>\n";
92-
foreach( $totalHours as $type => $value )
93-
{
94-
$spendBar .= "<div class='type-{$type}' style='width: {$value}%'></div>\n";
95-
}
96-
$spendBar .= "<div class='comment'>Time Spent</div>\n";
97-
$spendBar .= "</div>\n";
98-
99-
$html .= $spendBar;
100-
$html .= $logTable;
101-
102-
$url = strtolower( $d->format( 'F-Y' ) );
103-
104-
$from = $d->modify( '+40 days' );
105-
if (new \DateTimeImmutable() > $from )
106-
{
107-
$html .="<p>For additional information, please see the <a href='https://derickrethans.nl/xdebug-update-{$url}.html'>monthly</a> report.</p>\n";
108-
}
109-
110-
return $html;
98+
};
99+
100+
krsort($totalHours);
101+
102+
return new SupportLogMonthReport(
103+
$d,
104+
$others,
105+
$company,
106+
$basic,
107+
$patreon,
108+
$days,
109+
$totalHours,
110+
new \DateTimeImmutable() > $d->modify( '+40 days' ) ? strtolower($d->format( 'F-Y' )) : null
111+
);
111112
}
112113

113114
/**

src/Core/ContentsCache.php

+19-4
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,40 @@
33

44
class ContentsCache
55
{
6-
static public function fetch(\Closure $callback, string $cache_id, int $expiryS = 3600)
6+
/**
7+
* @template T
8+
* @param class-string<T> $modelClass
9+
* @param \Closure():T $callback
10+
* @return T
11+
*/
12+
static public function fetchModel(string $modelClass, \Closure $callback, string $cache_id, int $expiryS = 3600) : object
713
{
814
$cache_directory = dirname(__DIR__, 2) . '/cache';
915
$cache_file = $cache_directory . '/' . $cache_id;
1016

1117
if (!file_exists($cache_directory)) {
12-
@mkdir($cache_directory, 0700);
18+
@mkdir($cache_directory, 0700, true);
1319
}
1420

1521
$stat = @stat($cache_file);
1622
if ($stat) {
1723
if (time() <= $stat['ctime'] + $expiryS) {
18-
return file_get_contents($cache_file);
24+
$serialized_contents = file_get_contents($cache_file);
25+
if ($serialized_contents) {
26+
$contents = unserialize($serialized_contents);
27+
28+
if (get_class($contents) === $modelClass) {
29+
return $contents;
30+
}
31+
}
1932
}
2033
}
2134

2235
$contents = $callback();
2336

24-
file_put_contents($cache_file, $contents);
37+
if (file_exists($cache_directory)) {
38+
file_put_contents($cache_file, serialize($contents));
39+
}
2540

2641
return $contents;
2742
}

src/Model/SupportLog.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
*/
77
class SupportLog
88
{
9-
public $files;
9+
public $reports;
1010
public $supporters;
1111

1212
/**
13-
* @param string[] $files
13+
* @param SupportLogMonthReport[] $reports
1414
* @param array<int, array{0: string, 1: string}> $supporters
1515
*/
1616
public function __construct(
17-
array $files,
17+
array $reports,
1818
array $supporters
1919
) {
20-
$this->files = $files;
20+
$this->reports = $reports;
2121
$this->supporters = $supporters;
2222
}
2323
}

src/Model/SupportLogDayReport.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
namespace XdebugDotOrg\Model;
3+
4+
/**
5+
* @psalm-immutable
6+
*/
7+
class SupportLogDayReport
8+
{
9+
public $day;
10+
public $type;
11+
public $description;
12+
public $hours;
13+
14+
public function __construct(
15+
int $day,
16+
string $type,
17+
string $description,
18+
string $hours
19+
) {
20+
$this->day = $day;
21+
$this->type = $type;
22+
$this->description = $description;
23+
$this->hours = $hours;
24+
}
25+
}
26+
?>

src/Model/SupportLogMonthReport.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
namespace XdebugDotOrg\Model;
3+
4+
/**
5+
* @psalm-immutable
6+
*/
7+
class SupportLogMonthReport
8+
{
9+
public $start;
10+
public $others;
11+
public $company;
12+
public $basic;
13+
public $patreon;
14+
public $days;
15+
public $totalHours;
16+
public $url;
17+
18+
/**
19+
* @param array<SupportLogDayReport> $days
20+
* @param array<string, string> $totalHours
21+
*/
22+
public function __construct(
23+
\DateTimeImmutable $start,
24+
string $others,
25+
string $company,
26+
string $basic,
27+
string $patreon,
28+
array $days,
29+
array $totalHours,
30+
?string $url
31+
) {
32+
$this->start = $start;
33+
$this->others = $others;
34+
$this->company = $company;
35+
$this->basic = $basic;
36+
$this->patreon = $patreon;
37+
$this->days = $days;
38+
$this->totalHours = $totalHours;
39+
$this->url = $url;
40+
}
41+
}
42+
?>

0 commit comments

Comments
 (0)