From a92dc348cecb5f4c2af984e69755bddf0934ddde Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Tue, 10 Sep 2024 12:02:33 +0200 Subject: [PATCH 1/3] feat: add justinrainbow/json-schema implementation --- .../php-justinrainbow-json-schema/.gitignore | 1 + .../php-justinrainbow-json-schema/Dockerfile | 19 +++ .../bootstrap.php | 14 ++ .../composer.json | 33 +++++ .../src/TestHarness.php | 140 ++++++++++++++++++ 5 files changed, 207 insertions(+) create mode 100644 implementations/php-justinrainbow-json-schema/.gitignore create mode 100644 implementations/php-justinrainbow-json-schema/Dockerfile create mode 100644 implementations/php-justinrainbow-json-schema/bootstrap.php create mode 100644 implementations/php-justinrainbow-json-schema/composer.json create mode 100644 implementations/php-justinrainbow-json-schema/src/TestHarness.php diff --git a/implementations/php-justinrainbow-json-schema/.gitignore b/implementations/php-justinrainbow-json-schema/.gitignore new file mode 100644 index 000000000..61ead8666 --- /dev/null +++ b/implementations/php-justinrainbow-json-schema/.gitignore @@ -0,0 +1 @@ +/vendor diff --git a/implementations/php-justinrainbow-json-schema/Dockerfile b/implementations/php-justinrainbow-json-schema/Dockerfile new file mode 100644 index 000000000..c5b4e677b --- /dev/null +++ b/implementations/php-justinrainbow-json-schema/Dockerfile @@ -0,0 +1,19 @@ +FROM composer:2.7 AS builder + +WORKDIR /usr/src/json-schema + +COPY ./src ./src +COPY ./bootstrap.php . +COPY composer.* . +RUN composer install --no-dev --no-scripts --no-interaction --prefer-dist --optimize-autoloader +RUN composer dump-autoload --no-dev --optimize --classmap-authoritative + +FROM php:8.3-alpine + +WORKDIR /usr/src/json-schema + +COPY ./src ./src +COPY ./bootstrap.php . +COPY --from=builder /usr/src/json-schema/vendor /usr/src/json-schema/vendor + +CMD ["php", "bootstrap.php"] diff --git a/implementations/php-justinrainbow-json-schema/bootstrap.php b/implementations/php-justinrainbow-json-schema/bootstrap.php new file mode 100644 index 000000000..e1b820035 --- /dev/null +++ b/implementations/php-justinrainbow-json-schema/bootstrap.php @@ -0,0 +1,14 @@ +in = $in; + $this->out = $out; + $this->debug = $debug; + } + + public function __invoke(): void + { + $this->debug('Test harness is being invoked'); + + while (true) { + $next = fgets($this->in); + if ($next === false || $next === '') { + throw new RuntimeException('Unable to read from input'); + } + $request = json_decode($next, false, 512, JSON_THROW_ON_ERROR); + + $response = match ($request->cmd) { + 'start' => $this->start($request), + 'stop' => $this->stop(), + 'dialect' => $this->dialect(), + 'run' => $this->run($request), + default => [ + 'seq' => $request->seq, + 'results' => [[ + 'errored' => true, + 'context' => [ + 'message' => sprintf('Received unsupported command: %s', $request->cmd), + ], + ]], + ], + }; + + fwrite($this->out, json_encode($response, JSON_THROW_ON_ERROR) . PHP_EOL); + } + } + + private function start(stdClass $request): array + { + if ($request->version !== 1) { + throw new RuntimeException('Unsupported IHOP version'); + } + + $this->debug( + 'Starting with justinrainbow/json-schema version %s', + InstalledVersions::getVersion('justinrainbow/json-schema') + ); + + return [ + 'version' => 1, + 'implementation' => [ + 'language' => 'php', + 'name' => 'justinrainbow-json-schema', + 'version' => InstalledVersions::getVersion('justinrainbow/json-schema'), + 'homepage' => 'https://github.com/jsonrainbow/json-schema', + 'documentation' => 'https://github.com/jsonrainbow/json-schema/wiki', + 'source' => 'https://github.com/jsonrainbow/json-schema', + 'issues' => 'https://github.com/jsonrainbow/json-schema/issues', + 'dialects' => [ + 'http://json-schema.org/draft-04/schema#', + 'http://json-schema.org/draft-03/schema#', + ], + 'os' => PHP_OS, + 'os_version' => php_uname('r'), + 'language_version' => PHP_VERSION, + ], + ]; + } + + private function stop(): never + { + $this->debug('Stopping test harness'); + exit(0); + } + + private function dialect(): array + { + return ['ok' => true]; + } + + private function run(stdClass $request): array + { + $this->debug('Running test case'); + + $schemaStorage = new SchemaStorage(); + $validator = new Validator(new Factory($schemaStorage)); + + $results = []; + + foreach ($request->case->tests as $test) { + $schemaStorage->addSchema('internal://mySchema', $test); + try { + $validator->validate( + $test->instance, + $request->case->schema + ); + $results[] = ['valid' => $validator->isValid()]; + } catch (Throwable $e) { + $results[] = [ + 'errored' => true, + 'context' => [ + 'message' => $e->getMessage(), + 'traceback' => $e->getTraceAsString(), + ], + ]; + } + } + return ['seq' => $request->seq, 'results' => $results]; + } + + private function debug(string $format, ...$values): void + { + fwrite($this->debug, sprintf($format, ...$values) . PHP_EOL); + } +} From 44171492aec27f4f4fe52086b969013453782ba9 Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Tue, 10 Sep 2024 12:02:50 +0200 Subject: [PATCH 2/3] ci: add dependabot configuration --- .github/dependabot.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 44f6f9b3d..2335825b2 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -123,6 +123,11 @@ updates: schedule: interval: "daily" + - package-ecosystem: "composer" + directory: "/implementations/php-justinrainbow-json-schema" + schedule: + interval: "daily" + - package-ecosystem: "cargo" directory: "/implementations/rust-jsonschema" schedule: From e3bcc65bb208dc2fac7bff1f37dc701ec327ff84 Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Tue, 10 Sep 2024 12:11:46 +0200 Subject: [PATCH 3/3] ci: add dependabot config for docker container --- .github/dependabot.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 2335825b2..5c6504e76 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -247,6 +247,11 @@ updates: schedule: interval: "daily" + - package-ecosystem: "docker" + directory: "/implementations/php-justinrainbow-json-schema" + schedule: + interval: "daily" + - package-ecosystem: "docker" directory: "/implementations/python-fastjsonschema" schedule: