Skip to content

Commit 52ea4aa

Browse files
committed
Add install step for dependencies
1 parent a7403b2 commit 52ea4aa

File tree

5 files changed

+70
-50
lines changed

5 files changed

+70
-50
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ The following features are supported:
119119

120120
## Compilation
121121

122+
You can install all dependencies of this project using the following command:
123+
124+
php install.php
125+
122126
You can compile all files into a single "`api.php`" file using:
123127

124128
php build.php

build.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
// combine src and vendor directories into a single file
4+
35
function removeIgnored(string $dir, array &$entries, array $ignore)
46
{
57
foreach ($entries as $i => $entry) {
@@ -34,8 +36,8 @@ function runDir(string $base, string $dir, array &$lines, array $ignore): int
3436
$data = preg_replace('/\s*<\?php\s+/s', '', $data, 1);
3537
$data = preg_replace('/^.*?(vendor\/autoload|declare\s*\(\s*strict_types\s*=\s*1).*?$/m', '', $data);
3638
array_push($lines, "// file: $dir/$entry");
37-
if (!preg_match('/^\s*(namespace[^;]*);/', $data)){
38-
$data = "namespace;\n".$data;
39+
if (!preg_match('/^\s*(namespace[^;]*);/', $data)) {
40+
$data = "namespace;\n" . $data;
3941
}
4042
foreach (explode("\n", trim($data)) as $line) {
4143
if ($line) {

install.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
// download composer and install dependencies
4+
5+
if (!file_exists('composer.phar')) {
6+
$composer = file_get_contents('https://getcomposer.org/composer.phar');
7+
file_put_contents('composer.phar', $composer);
8+
}
9+
exec('php composer.phar install');
10+
11+
include 'patch.php';

patch.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
// patch files for PHP 7.0 compatibility
4+
5+
function patchDir(string $base, string $dir): int
6+
{
7+
$count = 0;
8+
$entries = scandir($dir);
9+
foreach ($entries as $entry) {
10+
if ($entry === '.' || $entry === '..') {
11+
continue;
12+
}
13+
$filename = "$base/$dir/$entry";
14+
if (is_dir($filename)) {
15+
$count += patchDir($base, "$dir/$entry");
16+
}
17+
}
18+
foreach ($entries as $entry) {
19+
$filename = "$base/$dir/$entry";
20+
if (is_file($filename)) {
21+
if (substr($entry, -4) != '.php') {
22+
continue;
23+
}
24+
$patched = $original = file_get_contents($filename);
25+
$patched = preg_replace('/\):\s*(\?[a-zA-Z]+|void)\s*\n/', ") /*:$1*/\n", $patched);
26+
$patched = preg_replace('/(private|public|protected) const/', "/*$1*/ const", $patched);
27+
if ($patched && $patched != $original) {
28+
file_put_contents($filename, $patched);
29+
$count++;
30+
}
31+
}
32+
}
33+
return $count;
34+
}
35+
36+
function patch(string $base, array $dirs)
37+
{
38+
$start = microtime(true);
39+
$count = 0;
40+
foreach ($dirs as $dir) {
41+
$count += patchDir($base, $dir);
42+
}
43+
$end = microtime(true);
44+
$time = ($end - $start) * 1000;
45+
if ($count) {
46+
fwrite(STDERR, sprintf("%d files patched in %d ms\n", $count, $time));
47+
}
48+
}
49+
50+
patch(__DIR__, ['vendor']);

update.php

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,4 @@
88
}
99
exec('php composer.phar update');
1010

11-
// patch files for PHP 7.0 compatibility
12-
13-
function patchDir(string $base, string $dir): int
14-
{
15-
$count = 0;
16-
$entries = scandir($dir);
17-
foreach ($entries as $entry) {
18-
if ($entry === '.' || $entry === '..') {
19-
continue;
20-
}
21-
$filename = "$base/$dir/$entry";
22-
if (is_dir($filename)) {
23-
$count += patchDir($base, "$dir/$entry");
24-
}
25-
}
26-
foreach ($entries as $entry) {
27-
$filename = "$base/$dir/$entry";
28-
if (is_file($filename)) {
29-
if (substr($entry, -4) != '.php') {
30-
continue;
31-
}
32-
$patched = $original = file_get_contents($filename);
33-
$patched = preg_replace('/\):\s*(\?[a-zA-Z]+|void)\s*\n/', ") /*:$1*/\n", $patched);
34-
$patched = preg_replace('/(private|public|protected) const/', "/*$1*/ const", $patched);
35-
if ($patched && $patched != $original) {
36-
file_put_contents($filename, $patched);
37-
$count++;
38-
}
39-
}
40-
}
41-
return $count;
42-
}
43-
44-
function patch(string $base, array $dirs)
45-
{
46-
$start = microtime(true);
47-
$count = 0;
48-
foreach ($dirs as $dir) {
49-
$count += patchDir($base, $dir);
50-
}
51-
$end = microtime(true);
52-
$time = ($end - $start) * 1000;
53-
if ($count) {
54-
fwrite(STDERR, sprintf("%d files patched in %d ms\n", $count, $time));
55-
}
56-
}
57-
58-
patch(__DIR__, ['vendor']);
11+
include 'patch.php';

0 commit comments

Comments
 (0)