Skip to content

Commit 495f934

Browse files
authored
Create packaged release with composer (dompdf#2799)
This entirely replaces our custom autoload mechanism. The added benefit is reduced maintenance for DomPDF itself but also its dependencies. And it opens up the possibility to add new dependency transparently, without the need to manage their autoloading. Fixes dompdf#2608 Closes dompdf#2779 Fixes dompdf/utils#12 Closes dompdf/utils#13
1 parent 116404a commit 495f934

File tree

6 files changed

+126
-62
lines changed

6 files changed

+126
-62
lines changed

Diff for: .gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ VERSION export-subst
2020
/.travis.yml export-ignore
2121
/phpunit.xml.dist export-ignore
2222
/tests export-ignore
23+
/bin export-ignore
2324
/phpcs.xml export-ignore
2425
/CONTRIBUTING.md export-ignore
2526
/SECURITY.md export-ignore

Diff for: CONTRIBUTING.md

+11
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,14 @@ Questions should be asked through
6161
- Add a unit test in the ``test/Dompdf/Tests/`` directory.
6262
- Submit a pull request
6363
([how to create a pull request](https://help.github.com/articles/fork-a-repo))
64+
65+
## Releasing
66+
67+
To create a release:
68+
69+
1. Tag the version and push it
70+
2. Create the packaged release locally via:
71+
```sh
72+
php bin/create-release.php
73+
```
74+
3. Upload the created zip file to GitHub release page

Diff for: bin/create-release.php

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
class Release
4+
{
5+
/**
6+
* Execute a shell command and throw exception if fails.
7+
*/
8+
private function exec(string $command): void
9+
{
10+
$return_var = null;
11+
$fullCommand = "$command 2>&1";
12+
passthru($fullCommand, $return_var);
13+
if ($return_var) {
14+
throw new Exception('FAILED executing: ' . $command);
15+
}
16+
}
17+
18+
private function checkPhpVersion(): void
19+
{
20+
$data = json_decode(file_get_contents('composer.json'), true);
21+
preg_match_all('~\d\.\d~', $data['require']['php'] ?? $data['require']['php-64bit'], $m);
22+
23+
$expectedVersion = min($m[0]);
24+
$actualVersion = PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION;
25+
26+
if ($actualVersion > $expectedVersion) {
27+
throw new Exception("Release must be created with PHP $expectedVersion (the oldest supported version), but you are running the newer PHP $actualVersion.");
28+
}
29+
}
30+
31+
private function getComposerCommand(): string
32+
{
33+
$php = PHP_BINARY;
34+
$composerPath = trim(`which composer`);
35+
36+
return "$php $composerPath --ansi --no-interaction";
37+
}
38+
39+
public function create(): void
40+
{
41+
$this->checkPhpVersion();
42+
$composer = $this->getComposerCommand();
43+
$tempDir = sys_get_temp_dir() . '/dompdf-release';
44+
45+
$buildDirectory = dirname(__DIR__) . "/build";
46+
if (!is_dir($buildDirectory)) {
47+
mkdir($buildDirectory);
48+
}
49+
50+
$this->exec("rm -rf $tempDir");
51+
mkdir($tempDir);
52+
chdir($tempDir);
53+
$this->exec("$composer init --type project");
54+
$this->exec("$composer require --fixed dompdf/dompdf");
55+
$this->exec("rm composer.json composer.lock");
56+
$this->exec("cp vendor/dompdf/dompdf/*.md vendor/dompdf/dompdf/LICENSE.LGPL vendor/dompdf/dompdf/VERSION .");
57+
58+
file_put_contents($tempDir . '/autoload.inc.php', "<?php require (__DIR__ . '/vendor/autoload.php');");
59+
60+
// Create zip
61+
$version = trim(file_get_contents($tempDir . '/VERSION'));
62+
$destination = $buildDirectory . "/dompdf-$version.zip";
63+
$this->zip($tempDir, $destination, 'dompdf');
64+
65+
$this->exec("rm -rf $tempDir");
66+
67+
echo "created $destination" . PHP_EOL;
68+
}
69+
70+
private function zip(string $source, string $destination, string $mainDir): void
71+
{
72+
$zip = new ZipArchive();
73+
$opened = $zip->open($destination, ZipArchive::CREATE | ZipArchive::OVERWRITE);
74+
if (!$opened) {
75+
throw new Exception('Could not create zip file: ' . $opened);
76+
}
77+
78+
$prefix = '';
79+
if ($mainDir) {
80+
$zip->addEmptyDir($mainDir);
81+
$prefix = $mainDir . '/';
82+
}
83+
84+
$source = str_replace('\\', '/', realpath($source));
85+
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST) as $file) {
86+
$file = str_replace('\\', '/', $file);
87+
88+
// Ignore "." and ".." folders
89+
$basename = basename($file);
90+
if (in_array($basename, ['.', '..'])) {
91+
continue;
92+
}
93+
94+
$targetFile = str_replace($source . '/', $prefix, $file);
95+
if (is_dir($file)) {
96+
$zip->addEmptyDir($targetFile);
97+
} else {
98+
$zip->addFile($file, $targetFile);
99+
}
100+
}
101+
102+
$ok = $zip->close();
103+
if (!$ok) {
104+
throw new Exception('Failed to close zip file');
105+
}
106+
}
107+
108+
}
109+
110+
$release = new Release();
111+
$release->create();
112+

Diff for: composer.json

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
"phenx/php-svg-lib": "^0.3.3 || ^0.4.0"
4040
},
4141
"require-dev": {
42+
"ext-json": "*",
43+
"ext-zip": "*",
4244
"phpunit/phpunit": "^7.5 || ^8 || ^9",
4345
"squizlabs/php_codesniffer": "^3.5",
4446
"mockery/mockery": "^1.3"

Diff for: src/Autoloader.php

-42
This file was deleted.

Diff for: tests/AutoloaderTest.php

-20
This file was deleted.

0 commit comments

Comments
 (0)