Add install step for dependencies

This commit is contained in:
Maurits van der Schee 2019-09-29 20:35:51 +02:00
commit 52ea4aa8dd
5 changed files with 70 additions and 50 deletions

View file

@ -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

View file

@ -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) {
@ -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) {

11
install.php Normal file
View file

@ -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
patch.php Normal file
View file

@ -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']);

View file

@ -8,51 +8,4 @@ if (!file_exists('composer.phar')) {
}
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';