Skip to content

Commit dfcf15c

Browse files
committed
Move first method from pdfexport module into the hook
It made little sense that we ask a concrete hook implementation for the first implementation of a hook. It essentially limited us to exactly one possible pdfexport extension (our own), which is not how hooks are supposed to work.
1 parent b74c281 commit dfcf15c

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

library/Icinga/Application/Hook/PdfexportHook.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,41 @@
55

66
namespace Icinga\Application\Hook;
77

8+
use Icinga\Application\Hook;
9+
use Icinga\Application\Logger;
10+
use Icinga\Exception\IcingaException;
11+
use RuntimeException;
12+
use Throwable;
13+
814
/**
915
* Base class for the PDF Export Hook
1016
*/
1117
abstract class PdfexportHook
1218
{
19+
/**
20+
* Get the first hook
21+
*
22+
* @return static
23+
*/
24+
public static function first()
25+
{
26+
foreach (Hook::all('Pdfexport') as $exporter) {
27+
try {
28+
if ($exporter->isSupported()) {
29+
return $exporter;
30+
}
31+
} catch (Throwable $e) {
32+
Logger::error(
33+
"PDF exporter reported an error during support check: %s\n%s",
34+
$e,
35+
IcingaException::getConfidentialTraceAsString($e),
36+
);
37+
}
38+
}
39+
40+
throw new RuntimeException('No supported PDF exporter available');
41+
}
42+
1343
/**
1444
* Get whether PDF export is supported
1545
*

library/Icinga/Common/PdfExport.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
namespace Icinga\Common;
77

8+
use Icinga\Application\Hook\PdfexportHook;
89
use Icinga\Application\Icinga;
910
use Icinga\Date\DateFormatter;
1011
use Icinga\Exception\ConfigurationError;
@@ -26,7 +27,8 @@ trait PdfExport
2627
* Export the requested action to PDF and send it
2728
*
2829
* @return never
29-
* @throws ConfigurationError If the pdfexport module is not available
30+
*
31+
* @throws ConfigurationError If no pdfexport module is available
3032
*/
3133
protected function sendAsPdf()
3234
{
@@ -71,7 +73,7 @@ protected function sendAsPdf()
7173
$doc->getAttributes()->add('class', 'icinga-module module-' . $moduleName);
7274
}
7375

74-
\Icinga\Module\Pdfexport\ProvidedHook\Pdfexport::first()->streamPdfFromHtml($doc, sprintf(
76+
PdfexportHook::first()->streamPdfFromHtml($doc, sprintf(
7577
'%s-%s',
7678
$this->view->title ?: $this->getRequest()->getActionName(),
7779
$time

0 commit comments

Comments
 (0)