Skip to content

Commit 0d25560

Browse files
authored
Blueprints parser, mapper, and validator autogenerated from JSON Schema (#23)
Solves the Blueprint parsing problem as follows: * JSON Schema is the source of truth for the Blueprint data structure * Model classes are generated from JSON Schema * Input Validation is done against the JSON Schema * Mapping validated input into model instances is indirectly done based on the JSON Schema ## Summary of the approach We use [Janephp](https://github.com/janephp/janephp/) to infer the PHP model class structure from the JSON schema We then use Jane's output to generate PHP code using [Nette PHP generator](https://github.com/nette/php-generator): * Model classes * Interfaces for groups of related classes: `StepDefinitionInterface`, `ResourceDefinitionInterface`. * Interface resolution map for the [JsonMapper library](https://jsonmapper.net/). * PHP docstrings with more accurate types than Jane to guide the mapping process From there, the data pipeline looks as follows: 1. Parse raw JSON 2. Validate it with Opis 3. Map it into PHP models using JsonMapper Or, if you're consuming the PHP API directly: 1. Create Model instances 2. Validate them with Opis ## Remaining work - [ ] Solve merge conflicts ## Rationale Parsing and mapping JSON [in TypeScript is a simple problem, but in PHP it's surprisingly involved](WordPress/blueprints#13 (comment)). This PR combines three previously explored approaches into a single pipeline: * WordPress/blueprints#17 * WordPress/blueprints#19 * WordPress/blueprints#21
1 parent 6b01e58 commit 0d25560

File tree

97 files changed

+4458
-5641
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+4458
-5641
lines changed

blueprint_compiling.php

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

33
use WordPress\Blueprints\ContainerBuilder;
4-
use WordPress\Blueprints\Model\BlueprintComposer;
4+
use WordPress\Blueprints\Model\BlueprintBuilder;
55
use WordPress\Blueprints\Runtime\NativePHPRuntime;
66

77
require 'vendor/autoload.php';
88

9-
$composer = BlueprintComposer::create()
9+
$blueprint = BlueprintBuilder::create()
1010
->withWordPressVersion( 'https://wordpress.org/latest.zip' )
1111
->withSiteOptions( [
1212
'blogname' => 'My Playground Blog',
@@ -30,14 +30,16 @@
3030
INSERT INTO `tmp_table` VALUES (2);
3131
SQL
3232
)
33-
->withFile( 'wordpress.txt', 'Data' );
33+
->withFile( 'wordpress.txt', 'Data' )
34+
->toBlueprint();
35+
3436

3537
$c = ( new ContainerBuilder() )->build(
3638
new NativePHPRuntime(
3739
__DIR__ . '/new-wp'
3840
)
3941
);
4042

41-
$results = $c['blueprint.engine']->runBlueprint( $composer );
43+
$results = $c['blueprint.engine']->runBlueprint( $blueprint );
4244

4345
var_dump( $results );

composer.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,28 @@
77
"symfony/http-client": "^7.0",
88
"symfony/http-kernel": "^7.0",
99
"pimple/pimple": "^3.0",
10-
"swaggest/json-schema": "^0.12.42",
11-
"swaggest/php-code-builder": "^0.2.41"
10+
"opis/json-schema": "^2.3",
11+
"nette/php-generator": "^4.1",
12+
"json-mapper/json-mapper": "^2.21"
1213
},
1314
"require-dev": {
14-
"pestphp/pest": "^2.33"
15+
"pestphp/pest": "^2.33",
16+
"jane-php/json-schema": "^7.6"
1517
},
1618
"config": {
1719
"optimize-autoloader": true,
1820
"preferred-install": "dist",
1921
"allow-plugins": {
20-
"pestphp/pest-plugin": true
22+
"pestphp/pest-plugin": true,
23+
"php-http/discovery": true
2124
}
2225
},
2326
"autoload": {
2427
"psr-4": {
2528
"WordPress\\": "src/WordPress"
2629
},
2730
"files": [
31+
"src/WordPress/Blueprints/functions.php",
2832
"src/WordPress/Zip/functions.php",
2933
"src/WordPress/Blueprints/functions.php",
3034
"src/WordPress/Streams/stream_str_replace.php"

0 commit comments

Comments
 (0)