Skip to content

Commit

Permalink
feat: add php support for models generation (#576)
Browse files Browse the repository at this point in the history
Co-authored-by: Lukasz Gornicki <[email protected]>
  • Loading branch information
Shurtu-gal and derberg authored May 23, 2023
1 parent 784caa9 commit 5b0f793
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
6 changes: 5 additions & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ weight: 40
The AsyncAPI CLI makes it easier to work with AsyncAPI documents.

To get **help**, run this command in your terminal:

```sh
asyncapi --help
```

It should print something similar to this:

```sh
All in one CLI for all AsyncAPI tools

Expand Down Expand Up @@ -39,5 +41,7 @@ COMMANDS
dart generate the models for Dart
rust generate the models for Rust
kotlin generate the models for Kotlin
fromTemplate generate whatever you want using templates compatible with AsyncAPI Generator
php generate the models for PHP
cplusplus generate the models for C++
fromTemplate generate whatever you want using templates compatible with AsyncAPI Generator
```
14 changes: 12 additions & 2 deletions src/commands/generate/models.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CSharpFileGenerator, JavaFileGenerator, JavaScriptFileGenerator, TypeScriptFileGenerator, GoFileGenerator, Logger, DartFileGenerator, PythonFileGenerator, RustFileGenerator, TS_COMMON_PRESET, TS_JSONBINPACK_PRESET, CSHARP_DEFAULT_PRESET, KotlinFileGenerator, TS_DESCRIPTION_PRESET, CplusplusFileGenerator } from '@asyncapi/modelina';
import { CSharpFileGenerator, JavaFileGenerator, JavaScriptFileGenerator, TypeScriptFileGenerator, GoFileGenerator, Logger, DartFileGenerator, PythonFileGenerator, RustFileGenerator, TS_COMMON_PRESET, TS_JSONBINPACK_PRESET, CSHARP_DEFAULT_PRESET, KotlinFileGenerator, TS_DESCRIPTION_PRESET, PhpFileGenerator, CplusplusFileGenerator } from '@asyncapi/modelina';
import { Flags } from '@oclif/core';
import Command from '../../base';
import { load } from '../../models/SpecificationFile';
Expand All @@ -16,6 +16,7 @@ enum Languages {
python = 'python',
rust = 'rust',
kotlin='kotlin',
php='php',
cplusplus='cplusplus'
}
const possibleLanguageValues = Object.values(Languages).join(', ');
Expand Down Expand Up @@ -93,7 +94,7 @@ export default class Models extends Command {
* C++ and C# specific namespace to use for the generated models
*/
namespace: Flags.string({
description: 'C++ and C# specific, define the namespace to use for the generated models. This is required when language is `cplusplus` or `csharp`.',
description: 'C#, C++ and PHP specific, define the namespace to use for the generated models. This is required when language is `csharp`,`c++` or `php`.',
required: false
}),

Expand Down Expand Up @@ -240,6 +241,15 @@ export default class Models extends Command {
packageName
};
break;
case Languages.php:
if (namespace === undefined) {
throw new Error('In order to generate models to PHP, we need to know which namespace they are under. Add `--namespace=NAMESPACE` to set the desired namespace.');
}
fileGenerator = new PhpFileGenerator();
fileOptions = {
namespace
};
break;
default:
throw new Error(`Could not determine generator for language ${language}, are you using one of the following values ${possibleLanguageValues}?`);
}
Expand Down
27 changes: 24 additions & 3 deletions test/commands/generate/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ describe('models', () => {
.stdout()
.command([...generalOptions, 'random', './test/specification.yml', `-o=${ path.resolve(outputDir, './random')}`])
.it('fails when it dont know the language', (ctx, done) => {
expect(ctx.stderr).toEqual('Error: Expected random to be one of: typescript, csharp, golang, java, javascript, dart, python, rust, kotlin, cplusplus\nSee more help with --help\n');
expect(ctx.stderr).toEqual('Error: Expected random to be one of: typescript, csharp, golang, java, javascript, dart, python, rust, kotlin, php, cplusplus\nSee more help with --help\n');
expect(ctx.stdout).toEqual('');
done();
});

test
.stderr()
.stdout()
Expand Down Expand Up @@ -271,7 +270,29 @@ describe('models', () => {
done();
});
});


describe('for PHP', () => {
test
.stderr()
.stdout()
.command([...generalOptions, 'php', './test/specification.yml', `-o=${ path.resolve(outputDir, './php')}`, '--namespace=\'asyncapi.models\''])
.it('works when file path is passed', (ctx, done) => {
expect(ctx.stderr).toEqual('');
expect(ctx.stdout).toContain(
'Successfully generated the following models: '
);
done();
});
test
.stderr()
.stdout()
.command([...generalOptions, 'php', './test/specification.yml', `-o=${ path.resolve(outputDir, './php')}`])
.it('fails when no namespace defined', (ctx, done) => {
expect(ctx.stderr).toEqual('Error: In order to generate models to PHP, we need to know which namespace they are under. Add `--namespace=NAMESPACE` to set the desired namespace.\n');
expect(ctx.stdout).toEqual('');
done();
});
});
describe('with logging diagnostics', () => {
test
.stderr()
Expand Down

0 comments on commit 5b0f793

Please sign in to comment.