Repository navigation
Expand file tree
/
Copy pathbuild-phar.php
More file actions
96 lines (88 loc) · 3.45 KB
/
Copy pathbuild-phar.php
File metadata and controls
96 lines (88 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
$composer = getenv('COMPOSER_BINARY');
if (!$composer) {
fwrite(STDERR, "Run composer phar to build the archive.\n");
exit(1);
}
$dist = __DIR__ . '/dist';
$stage = $dist . '/phar-build-' . bin2hex(random_bytes(8));
if ((!is_dir($dist) && !@mkdir($dist, 0755, true) && !is_dir($dist)) || !mkdir($stage, 0700)) {
throw new RuntimeException('Could not create the PHAR build directory');
}
try {
$manifest = json_decode(file_get_contents(__DIR__ . '/composer.json'), true);
if (!is_array($manifest)) {
throw new RuntimeException('Could not read composer.json');
}
$manifest['config']['autoloader-suffix'] = 'RingCentralSdkPhar';
$manifest['config']['prepend-autoloader'] = false;
$stagedManifest = json_encode($manifest);
if (
$stagedManifest === false
|| file_put_contents($stage . '/composer.json', $stagedManifest) === false
|| !copy(__DIR__ . '/composer.lock', $stage . '/composer.lock')
) {
throw new RuntimeException('Could not stage the Composer manifest and lockfile');
}
// Stage runtime packages so the test dependencies and autoloader stay intact.
putenv('COMPOSER=composer.json');
putenv('COMPOSER_VENDOR_DIR=vendor');
putenv('COMPOSER_BIN_DIR=vendor/bin');
passthru(
escapeshellarg(PHP_BINARY) . ' ' . escapeshellarg($composer) .
' install --working-dir=' . escapeshellarg($stage) .
' --no-dev --prefer-dist --no-interaction --no-scripts --no-plugins',
$status
);
if ($status !== 0) {
throw new RuntimeException('Could not install the PHAR runtime dependencies');
}
$archive = $stage . '/ringcentral.phar';
$phar = new Phar($archive, 0, 'ringcentral.phar');
$phar->startBuffering();
$stub = <<<'PHP'
<?php
Phar::mapPhar();
$loader = require 'phar://' . __FILE__ . '/vendor/autoload.php';
// Let the application load its Guzzle classes together.
spl_autoload_register(static function ($class) use ($loader) {
if (strpos($class, 'GuzzleHttp\\') !== 0) {
$loader->loadClass($class);
}
}, true, true);
__HALT_COMPILER();
PHP;
$phar->setStub($stub);
foreach ([__DIR__ . '/src' => __DIR__, $stage . '/vendor' => $stage] as $directory => $base) {
$files = new RecursiveCallbackFilterIterator(
new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS),
static function (SplFileInfo $file) use ($stage) {
return !$file->isLink()
&& $file->getFilename()[0] !== '.'
&& $file->getPathname() !== $stage . '/vendor' . DIRECTORY_SEPARATOR . 'bin';
}
);
$phar->buildFromIterator(new RecursiveIteratorIterator($files), $base);
}
$phar->addFile(__DIR__ . '/composer.json', 'composer.json');
$phar->addFile($stage . '/composer.lock', 'composer.lock');
$phar->compressFiles(Phar::GZ);
$phar->stopBuffering();
unset($phar);
if (!rename($archive, $dist . '/ringcentral.phar')) {
throw new RuntimeException('Could not publish dist/ringcentral.phar');
}
} finally {
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($stage, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $file) {
if ($file->isDir() && !$file->isLink()) {
rmdir($file->getPathname());
} else {
unlink($file->getPathname());
}
}
rmdir($stage);
}