-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add PHP justinrainbow/json-schema implementation #1512
Merged
Julian
merged 3 commits into
bowtie-json-schema:main
from
DannyvdSluijs:add-justin-rainbow-json-schema
Sep 10, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] |
14 changes: 14 additions & 0 deletions
14
implementations/php-justinrainbow-json-schema/bootstrap.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use JsonRainbow\TestHarness; | ||
|
||
ini_set('display_errors', 'stderr'); | ||
require_once 'vendor/autoload.php'; | ||
|
||
$in = fopen('php://stdin', 'rb'); | ||
$out = fopen('php://stdout', 'wb'); | ||
$debug = fopen('php://stderr', 'wb'); | ||
$testHarness = new TestHarness($in, $out, $debug); | ||
$testHarness(); |
33 changes: 33 additions & 0 deletions
33
implementations/php-justinrainbow-json-schema/composer.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "justinrainbow/bowtie-json-schema-test-harness", | ||
"description": "These sources contains the test harness implementation for Bowtie", | ||
"license": "MIT", | ||
"require": { | ||
"justinrainbow/json-schema": "dev-master" | ||
}, | ||
"require-dev": { | ||
"squizlabs/php_codesniffer": "^3.10" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"JsonRainbow\\": "./src" | ||
} | ||
}, | ||
"config": { | ||
"lock": false, | ||
"sort-packages": true | ||
}, | ||
"scripts": { | ||
"code-style": "phpcs . --ignore=*/vendor/* --standard=PSR12", | ||
"build-container": "docker build . -t localhost/php-justinrainbow-json-schema", | ||
"run-interactive-container": "docker run --rm -it localhost/php-justinrainbow-json-schema", | ||
"run-suite-draft3": "bowtie suite -i localhost/php-justinrainbow-json-schema https://github.com/json-schema-org/JSON-Schema-Test-Suite/tree/main/tests/draft3", | ||
"run-suite-draft4": "bowtie suite -i localhost/php-justinrainbow-json-schema https://github.com/json-schema-org/JSON-Schema-Test-Suite/tree/main/tests/draft4" | ||
}, | ||
"scripts-descriptions": { | ||
"build-container": "Build the container imsage using Docker.", | ||
"run-interactive-container": "Run the container interactivly", | ||
"run-suite-draft3": "Run the test suite for draft-3 againt the test harness", | ||
"run-suite-draft4": "Run the test suite for draft-4 againt the test harness" | ||
} | ||
} |
140 changes: 140 additions & 0 deletions
140
implementations/php-justinrainbow-json-schema/src/TestHarness.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JsonRainbow; | ||
|
||
use Composer\InstalledVersions; | ||
use JsonSchema\Constraints\Factory; | ||
use JsonSchema\SchemaStorage; | ||
use JsonSchema\Validator; | ||
use RuntimeException; | ||
use stdClass; | ||
use Throwable; | ||
|
||
class TestHarness | ||
{ | ||
private mixed $in; | ||
private mixed $out; | ||
private mixed $debug; | ||
|
||
public function __construct($in, $out, $debug) | ||
{ | ||
get_resource_type($in) !== 'stream' && throw new RuntimeException('Param #1 $in must be a stream'); | ||
get_resource_type($out) !== 'stream' && throw new RuntimeException('Param #2 $out must be a stream'); | ||
get_resource_type($debug) !== 'stream' && throw new RuntimeException('Param #3 $debug must be a stream'); | ||
|
||
$this->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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If perchance you're interested in a follow up PR, changing this to instead be a fixed version, (perhaps as soon as you publish one if that's not yet happened?) would be nice, and then relying on Dependabot to update it.
As is, right now the version information shown at https://bowtie.report/#/implementations/php-justinrainbow-json-schema (which is live, congrats!) won't be useful to someone, since it just says
dev-master
, but someone won't know what commit that corresponds to even if it was HEAD at some point in time.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the heads up. This is on my radar. Since the latest version is having issues with PHP 8.3 I've decided to use dev-master for now. But your points are very valid. Should be picked up in the near future.