Skip to content

Commit

Permalink
Add install step for dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
mevdschee committed Sep 29, 2019
1 parent a7403b2 commit 52ea4aa
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 50 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ The following features are supported:

## Compilation

You can install all dependencies of this project using the following command:

php install.php

You can compile all files into a single "`api.php`" file using:

php build.php
Expand Down
6 changes: 4 additions & 2 deletions build.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

// combine src and vendor directories into a single file

function removeIgnored(string $dir, array &$entries, array $ignore)
{
foreach ($entries as $i => $entry) {
Expand Down Expand Up @@ -34,8 +36,8 @@ function runDir(string $base, string $dir, array &$lines, array $ignore): int
$data = preg_replace('/\s*<\?php\s+/s', '', $data, 1);
$data = preg_replace('/^.*?(vendor\/autoload|declare\s*\(\s*strict_types\s*=\s*1).*?$/m', '', $data);
array_push($lines, "// file: $dir/$entry");
if (!preg_match('/^\s*(namespace[^;]*);/', $data)){
$data = "namespace;\n".$data;
if (!preg_match('/^\s*(namespace[^;]*);/', $data)) {
$data = "namespace;\n" . $data;
}
foreach (explode("\n", trim($data)) as $line) {
if ($line) {
Expand Down
11 changes: 11 additions & 0 deletions install.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

// download composer and install dependencies

if (!file_exists('composer.phar')) {
$composer = file_get_contents('https://getcomposer.org/composer.phar');
file_put_contents('composer.phar', $composer);
}
exec('php composer.phar install');

include 'patch.php';
50 changes: 50 additions & 0 deletions patch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

// patch files for PHP 7.0 compatibility

function patchDir(string $base, string $dir): int
{
$count = 0;
$entries = scandir($dir);
foreach ($entries as $entry) {
if ($entry === '.' || $entry === '..') {
continue;
}
$filename = "$base/$dir/$entry";
if (is_dir($filename)) {
$count += patchDir($base, "$dir/$entry");
}
}
foreach ($entries as $entry) {
$filename = "$base/$dir/$entry";
if (is_file($filename)) {
if (substr($entry, -4) != '.php') {
continue;
}
$patched = $original = file_get_contents($filename);
$patched = preg_replace('/\):\s*(\?[a-zA-Z]+|void)\s*\n/', ") /*:$1*/\n", $patched);
$patched = preg_replace('/(private|public|protected) const/', "/*$1*/ const", $patched);
if ($patched && $patched != $original) {
file_put_contents($filename, $patched);
$count++;
}
}
}
return $count;
}

function patch(string $base, array $dirs)
{
$start = microtime(true);
$count = 0;
foreach ($dirs as $dir) {
$count += patchDir($base, $dir);
}
$end = microtime(true);
$time = ($end - $start) * 1000;
if ($count) {
fwrite(STDERR, sprintf("%d files patched in %d ms\n", $count, $time));
}
}

patch(__DIR__, ['vendor']);
49 changes: 1 addition & 48 deletions update.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,4 @@
}
exec('php composer.phar update');

// patch files for PHP 7.0 compatibility

function patchDir(string $base, string $dir): int
{
$count = 0;
$entries = scandir($dir);
foreach ($entries as $entry) {
if ($entry === '.' || $entry === '..') {
continue;
}
$filename = "$base/$dir/$entry";
if (is_dir($filename)) {
$count += patchDir($base, "$dir/$entry");
}
}
foreach ($entries as $entry) {
$filename = "$base/$dir/$entry";
if (is_file($filename)) {
if (substr($entry, -4) != '.php') {
continue;
}
$patched = $original = file_get_contents($filename);
$patched = preg_replace('/\):\s*(\?[a-zA-Z]+|void)\s*\n/', ") /*:$1*/\n", $patched);
$patched = preg_replace('/(private|public|protected) const/', "/*$1*/ const", $patched);
if ($patched && $patched != $original) {
file_put_contents($filename, $patched);
$count++;
}
}
}
return $count;
}

function patch(string $base, array $dirs)
{
$start = microtime(true);
$count = 0;
foreach ($dirs as $dir) {
$count += patchDir($base, $dir);
}
$end = microtime(true);
$time = ($end - $start) * 1000;
if ($count) {
fwrite(STDERR, sprintf("%d files patched in %d ms\n", $count, $time));
}
}

patch(__DIR__, ['vendor']);
include 'patch.php';

0 comments on commit 52ea4aa

Please sign in to comment.