Skip to content

Commit 12efdf9

Browse files
authored
Merge pull request #157 from ToshY/feature/generator
Generator scripts for converting OpenAPI specs to models
2 parents 7c17792 + 753c3f2 commit 12efdf9

33 files changed

+3370
-127
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/.githooks/ export-ignore
33
/docs/ export-ignore
44
/tests/ export-ignore
5+
/generator/ export-ignore
56
/.gitattributes export-ignore
67
/.gitignore export-ignore
78
/.php-cs-fixer.dist.php export-ignore

.github/workflows/generator.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: Generator
2+
on:
3+
workflow_dispatch:
4+
schedule:
5+
- cron: '0 0 * * 0'
6+
7+
env:
8+
API_SPEC_MANIFEST: https://toshy.github.io/bunnynet-openapi-specification/manifest.json
9+
10+
jobs:
11+
generate:
12+
runs-on: ubuntu-latest
13+
timeout-minutes: 10
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v4
17+
18+
- name: Setup PHP
19+
uses: shivammathur/setup-php@v2
20+
with:
21+
php-version: '8.1'
22+
23+
- name: Install Composer dependencies
24+
uses: ramsey/composer-install@v3
25+
26+
- name: Run map generator
27+
run: php generator/Command/generate-maps.php
28+
29+
- name: Run model generator
30+
run: php generator/Command/generate-models.php --log
31+
32+
- name: Create Pull Request
33+
uses: peter-evans/create-pull-request@v7.0.8
34+
with:
35+
commit-message: Updated API models
36+
title: '[OpenAPI] Updated API models'
37+
body: |
38+
Added/Updated models according to OpenAPI specs.
39+
40+
Checklist:
41+
- [x] Add/Update API models
42+
- [ ] Add/Update API public methods for corresponding models
43+
- [ ] Update documentation
44+
branch: patch/models
45+
branch-suffix: short-commit-hash
46+
assignees: ToshY
47+
reviewers: ToshY
48+
labels: |
49+
OpenAPI
50+
automated

.github/workflows/phpstan.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ jobs:
3232
uses: ramsey/composer-install@v3
3333

3434
- name: PHPStan
35-
run: php vendor/bin/phpstan analyse --error-format=github
35+
run: php vendor/bin/phpstan analyse --error-format=github --memory-limit=256M

.php-cs-fixer.dist.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
$finder = (new PhpCsFixer\Finder())
44
->exclude(__DIR__ . '/vendor')
5-
->in(__DIR__ . '/src')
5+
->in([
6+
__DIR__ . '/src',
7+
__DIR__ . '/tests',
8+
__DIR__ . '/generator',
9+
])
610
->name('*.php');
711

812
return (new PhpCsFixer\Config())
@@ -14,6 +18,7 @@
1418
'array_syntax' => [
1519
'syntax' => 'short',
1620
],
21+
'array_indentation' => true,
1722
'trailing_comma_in_multiline' => [
1823
'elements' => [
1924
'arguments',

Dockerfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,3 @@ RUN apt-get update && apt-get install -y \
1212
&& rm -rf /var/lib/apt/lists/*
1313

1414
COPY --from=composer:2.5 /usr/bin/composer /usr/bin/composer
15-
16-
COPY . .

Taskfile.yml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ env:
66
GID:
77
sh: id -g
88
TTY: ''
9+
API_SPEC_MANIFEST: https://toshy.github.io/bunnynet-openapi-specification/manifest.json
910

1011
tasks:
1112
default:
@@ -63,6 +64,22 @@ tasks:
6364
cmds:
6465
- docker compose run --rm $TTY php composer update {{.PACKAGE}} {{.CLI_ARGS | default "--no-cache"}}
6566

67+
composer:audit:
68+
desc: Composer audit
69+
cmds:
70+
- docker compose run --rm $TTY php composer audit
71+
72+
# Generator
73+
specs:maps:
74+
desc: Generate model mapping files from OpenAPI specs
75+
cmds:
76+
- docker compose run --rm $TTY php php /app/generator/Command/generate-maps.php
77+
78+
specs:models:
79+
desc: Generate model from mapping file
80+
cmds:
81+
- docker compose run --rm $TTY php php /app/generator/Command/generate-models.php {{.CLI_ARGS | default "--log"}}
82+
6683
# Git
6784
git:hooks:
6885
desc: Setup git hooks
@@ -112,7 +129,7 @@ tasks:
112129
phpstan:
113130
desc: PHPStan run
114131
cmds:
115-
- docker compose run --rm $TTY php vendor/bin/phpstan analyse --error-format=table
132+
- docker compose run --rm $TTY php vendor/bin/phpstan analyse --error-format=table --memory-limit=256M
116133

117134
phpstan:baseline:
118135
desc: PHPStan update baseline

compose.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ services:
44
context: .
55
dockerfile: Dockerfile
66
user: "${UID:-1000}:${GID:-1000}"
7+
environment:
8+
API_SPEC_MANIFEST: ${API_SPEC_MANIFEST:-}
79
volumes:
8-
- ./:/app
10+
- ./:/app

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
},
2929
"autoload-dev": {
3030
"psr-4": {
31-
"ToshY\\BunnyNet\\Tests\\": "tests/"
31+
"ToshY\\BunnyNet\\Tests\\": "tests/",
32+
"ToshY\\BunnyNet\\Generator\\": "generator/"
3233
}
3334
},
3435
"require": {
@@ -42,7 +43,9 @@
4243
"phpmd/phpmd": "^2.15",
4344
"friendsofphp/php-cs-fixer": "^3.75",
4445
"phpstan/phpstan": "^2.1",
45-
"phpunit/phpunit": "^10.0"
46+
"phpunit/phpunit": "^10.0",
47+
"nette/php-generator": "^4.1",
48+
"cebe/php-openapi": "^1.8"
4649
},
4750
"config": {
4851
}

docs/stream-api.md

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -320,38 +320,10 @@ $streamApi->setThumbnail(
320320
);
321321
```
322322

323-
#### Set Thumbnail (by body)
324-
325-
```php
326-
/*
327-
* File contents read into string from the local filesystem.
328-
*/
329-
$content = file_get_contents('./thumbnail.jpg');
330-
331-
/*
332-
* File contents handle from a `$filesystem` (Flysystem FtpAdapter).
333-
*/
334-
$content = $filesystem->readStream('./thumbnail.jpg');
335-
336-
// Set video thumbnail by body contents.
337-
$streamApi->setThumbnailByBody(
338-
libraryId: 1,
339-
videoId: 'e7e9b99a-ea2a-434a-b200-f6615e7b6abd',
340-
body: $content,
341-
headers: [
342-
'Content-Type' => 'image/jpeg',
343-
],
344-
);
345-
```
346-
347323
!!! note
348324

349325
- This method allows for uploading a thumbnail based on body contents.
350326

351-
!!! warning
352-
353-
- Adding a thumbnail by uploading body contents is not documented in the official Bunny.net API specification for the [Set Thumbnail](https://docs.bunny.net/reference/video_setthumbnail) endpoint.
354-
355327
#### [Fetch Video](https://docs.bunny.net/reference/video_fetchnewvideo)
356328

357329
```php

generator/Command/generate-maps.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use ToshY\BunnyNet\Generator\Generator\MapGenerator;
6+
use ToshY\BunnyNet\Generator\Utils\FileUtils;
7+
8+
require_once __DIR__ . '/../../vendor/autoload.php';
9+
10+
$apiSpecManifest = getenv('API_SPEC_MANIFEST');
11+
$modelInputDirectory = __DIR__ . '/../../src/Model/API';
12+
$outputDirectory = __DIR__ . '/../Map';
13+
14+
$file = FileUtils::getFile($apiSpecManifest);
15+
if ($file === false) {
16+
throw new RuntimeException(
17+
sprintf(
18+
'Unable to load API spec file `%s`.',
19+
$apiSpecManifest,
20+
),
21+
);
22+
}
23+
$manifests = FileUtils::jsonDecode($file);
24+
25+
$data = [];
26+
foreach ($manifests as $file) {
27+
$description = $file['sourceDescription'];
28+
$key = match (true) {
29+
str_contains($description, 'bunny.net API') => 'Base',
30+
str_contains($description, 'Edge Scripting API') => 'EdgeScripting',
31+
str_contains($description, 'Edge Storage API') => 'EdgeStorage',
32+
str_contains($description, 'Stream API') => 'Stream',
33+
str_contains($description, 'Shield API') => 'Shield',
34+
default => throw new RuntimeException(
35+
sprintf(
36+
'Unknown API type with description: `%s`',
37+
$description,
38+
),
39+
),
40+
};
41+
42+
$ignoreEndpoints = match ($key) {
43+
'Base' => [
44+
/* Changed to EdgeScripting */
45+
'/compute/script',
46+
'/compute/script/{id}',
47+
'/compute/script/{id}/code',
48+
'/compute/script/{id}/releases',
49+
'/compute/script/{id}/publish',
50+
'/compute/script/{id}/publish/{uuid}',
51+
'/compute/script/{id}/variables/add',
52+
'/compute/script/{id}/variables/{variableId}',
53+
],
54+
default => [],
55+
};
56+
57+
$data[$key] = [
58+
'apiSpecPath' => $file['fileUrl'],
59+
'modelDirectory' => $modelInputDirectory . '/' . $key,
60+
'mappingClassName' => $key,
61+
'outputDirectory' => $outputDirectory,
62+
'ignoreEndpoints' => $ignoreEndpoints,
63+
];
64+
}
65+
66+
foreach ($data as $namespace => $config) {
67+
$generator = new MapGenerator(
68+
$config['apiSpecPath'],
69+
$config['outputDirectory'],
70+
$config['mappingClassName'],
71+
$config['modelDirectory'],
72+
$config['ignoreEndpoints'],
73+
);
74+
$generator->generate();
75+
76+
echo "[$namespace] Mapping class generated successfully" . PHP_EOL;
77+
}
78+
79+
// Autofix with php-cs-fixer
80+
shell_exec('php vendor/bin/php-cs-fixer --quiet fix ' . realpath($outputDirectory));

0 commit comments

Comments
 (0)