Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/Command/ReportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,25 @@ private function safeReport(string $folder, Report $report, array $forbiddenRela
// Ensure destination directory exists (destination is typically 'report/report.js')
$dir = dirname($destination);
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
if (!mkdir($dir, 0777, true)) {
throw new \RuntimeException(sprintf('Failed to create directory "%s". Check permissions and disk space.', $dir));
}
}

// Write the JS data file (report.js)
file_put_contents($destination, 'const reportData = '.$json.';');
if (false === file_put_contents($destination, 'const reportData = '.$json.';')) {
throw new \RuntimeException(sprintf('Failed to write report data to "%s". Check permissions and disk space.', $destination));
}

// Ensure static assets (chart.js, styles.css) are available in the report directory.
// Prefer packaged templates under resources/report, otherwise create sensible defaults.
$resourceDir = __DIR__.'/../../resources/report';

// chart.js
if (is_file($resourceDir.'/chart.js')) {
copy($resourceDir.'/chart.js', $dir.'/chart.js');
if (!copy($resourceDir.'/chart.js', $dir.'/chart.js')) {
throw new \RuntimeException(sprintf('Failed to copy chart.js from "%s" to "%s". Check permissions and disk space.', $resourceDir.'/chart.js', $dir.'/chart.js'));
}
} else {
// default minimal chart script inspired by tbone's chart.js using ECharts
$defaultChartJs = <<<'JS'
Expand Down Expand Up @@ -291,12 +297,16 @@ classes[category].forEach(c => all.push({ name: c, category }));
}
});
JS;
file_put_contents($dir.'/chart.js', $defaultChartJs);
if (false === file_put_contents($dir.'/chart.js', $defaultChartJs)) {
throw new \RuntimeException(sprintf('Failed to write default chart.js to "%s". Check permissions and disk space.', $dir.'/chart.js'));
}
}

// styles.css
if (is_file($resourceDir.'/styles.css')) {
copy($resourceDir.'/styles.css', $dir.'/styles.css');
if (!copy($resourceDir.'/styles.css', $dir.'/styles.css')) {
throw new \RuntimeException(sprintf('Failed to copy styles.css from "%s" to "%s". Check permissions and disk space.', $resourceDir.'/styles.css', $dir.'/styles.css'));
}
} else {
$defaultCss = <<<'CSS'
body { font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial; margin: 16px; }
Expand All @@ -305,7 +315,9 @@ classes[category].forEach(c => all.push({ name: c, category }));
.chart-container { border:1px solid #ddd; border-radius:8px; padding:6px; }
#classesTableContainer { max-width: 1000px; margin: 0 auto; }
CSS;
file_put_contents($dir.'/styles.css', $defaultCss);
if (false === file_put_contents($dir.'/styles.css', $defaultCss)) {
throw new \RuntimeException(sprintf('Failed to write default styles.css to "%s". Check permissions and disk space.', $dir.'/styles.css'));
}
}

// index.html referencing the static assets. Use ECharts CDN and include chart.js and report.js
Expand Down Expand Up @@ -345,6 +357,8 @@ classes[category].forEach(c => all.push({ name: c, category }));
</html>
HTML;

file_put_contents($dir.'/index.html', $indexHtml);
if (false === file_put_contents($dir.'/index.html', $indexHtml)) {
throw new \RuntimeException(sprintf('Failed to write index.html to "%s". Check permissions and disk space.', $dir.'/index.html'));
}
}
}