Skip to content

Commit 0aa8da1

Browse files
committed
refs #8 Added git hash to colophon
1 parent d8561df commit 0aa8da1

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

assets/colophon.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<p style="margin: 0; font-weight: 600; font-size: 12pt; margin-top: 0.15em;">Черняев Александр</p>
55
<p style="margin: 0; font-size: 10pt; margin-top: 0.5em;">Версия 1.0, издание первое</p>
66
<p style="margin: 0; font-size: 10pt; margin-top: 0.3em;">ISBN 000-0-00000-000-0</p>
7-
<p style="margin: 0; font-weight: 700; font-size: 12pt; margin-top: 1em;">Липецк 2025</p>
7+
<p style="margin: 0; font-size: 10pt; margin-top: 0.3em;">Коммит [commit]</p>
8+
<p style="margin: 0; font-weight: 700; font-size: 12pt; margin-top: 1em;">Липецк [year]</p>
89
</div>
910
</div>

src/Book.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Dandy\Book;
44

5+
use Illuminate\Support\Facades\Date;
56
use Illuminate\Support\Str;
67
use Mpdf\Config\ConfigVariables;
78
use Mpdf\Config\FontVariables;
@@ -78,7 +79,12 @@ public function withTheme(string $themeHtml): static
7879
*/
7980
public function withColophon(string $html): static
8081
{
81-
$this->pdf->WriteHTML($html);
82+
$this->pdf->WriteHTML(
83+
Str::of($html)
84+
->replace('[commit]', self::getCurrentGitCommitHash())
85+
->replace('[year]', Date::now()->format('Y'))
86+
->toString()
87+
);
8288

8389
return $this;
8490
}
@@ -227,4 +233,23 @@ protected function addPageBreak(): void
227233
// $this->pdf->AddPage();
228234
$this->pdf->WriteHTML('<div style="page-break-after: always;"></div>');
229235
}
236+
237+
/**
238+
* Attempt to retrieve the current Git commit hash in PHP.
239+
* Note: This method assumes the project is using Git for version control.
240+
*
241+
* @return string|null
242+
*/
243+
public static function getCurrentGitCommitHash(): ?string
244+
{
245+
$gitPath = '.git/';
246+
247+
if (! file_exists($gitPath)) {
248+
return null;
249+
}
250+
251+
$head = trim(substr(file_get_contents($gitPath.'HEAD'), 4));
252+
253+
return Str::of(file_get_contents($gitPath.$head))->trim()->limit(7, '');
254+
}
230255
}

0 commit comments

Comments
 (0)