Skip to content

Commit

Permalink
Allow to generate js enums from PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
ogizanagi committed Jun 9, 2020
1 parent 3b5fc31 commit c0b2726
Show file tree
Hide file tree
Showing 21 changed files with 1,223 additions and 87 deletions.
6 changes: 5 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ charset = utf-8
insert_final_newline = true
trim_trailing_whitespace = true

[*.{php,js}]
[*.php]
indent_style = space
indent_size = 4

[*.js]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
/.travis.yml export-ignore
/phpunit.xml.dist export-ignore
/tests export-ignore
/res export-ignore
/res/img export-ignore
50 changes: 48 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Table of Contents
* [Usage with Alice](#usage-with-alice)
* [Api-Platform](#api-platform)
* [OpenApi / Swagger](#openapi--swagger)
* [JavaScript](#javascript)
* [API](#api)
* [Simple enum](#simple-enum)
* [Readable enum](#readable-enum)
Expand Down Expand Up @@ -123,6 +124,8 @@ Providing our own package inspired from the best ones, on which we'll apply our
- Symfony HttpKernel component integration with an enum resolver for controller arguments.
- Doctrine DBAL integration with abstract classes in order to persist your enumeration in database.
- Faker enum provider to generate random enum instances.
- An API Platform OpenApi/Swagger type for documentation generation.
- JavaScript enums code generation.

# Installation

Expand Down Expand Up @@ -869,11 +872,54 @@ MyEntity:

> 📝 `MISTER` in `<enum(Civility::MISTER)>` refers to a constant defined in the `Civility` enum class, not to a constant's value ('mister' string for instance).

## Api-Platform
## API Platform

### OpenApi / Swagger

The PhpEnums library provides an `Elao\Enum\Bridge\ApiPlatform\Core\JsonSchema\Type\ElaoEnumType` to generate a OpenApi (formally Swagger) documentation based on your enums. This decorator is automatically wired for you when using the Symfony bundle.
The library provides an `Elao\Enum\Bridge\ApiPlatform\Core\JsonSchema\Type\ElaoEnumType` to generate a OpenApi (formally Swagger) documentation based on your enums. This decorator is automatically wired for you when using the Symfony bundle.

## JavaScript

This library allows to generate JS code from your PHP enums using a command:

```bash
bin/elao-enum-dump-js --lib-path "./assets/js/lib/enum.js" --base-dir="./assets/js/modules" \
"App\Auth\Enum\Permissions:auth/Permissions.js" \
"App\Common\Enum\Gender:common/Gender.js"
```

This command generates:
- [library sources](./res/js/Enum.js) at path `assets/js/lib/enums.js` containing the base JS classes
- enums in a base `/assets/js/modules` dir:
- `Permissions` in `/assets/js/modules/auth/Permissions.js`
- `Gender` in `/assets/js/modules/common/Gender.js`

Simple enums, readables & flagged enums are supported.

Note that this is not meant to be used as part of an automatic process updating your code.
There is no BC promise guaranteed on the generated code. Once generated, the code belongs to you.

### In a Symfony app

You can configure the library path, base dir and enum paths in the bundle configuration:

```yaml
elao_enum:
elao_enum:
js:
base_dir: '%kernel.project_dir%/assets/js/modules'
lib_path: '%kernel.project_dir%/assets/js/lib'
paths:
App\Common\Enum\SimpleEnum: 'common/SimpleEnum.js'
App\Common\Enum\Gender: 'common/Gender.js'
App\Auth\Enum\Permissions: 'auth/Permissions.js'
```

Then, use the CLI command to generate the JS files:

```bash
bin/console elao:enum:dump-js [--lib-path] [--base-dir] [<enum:path>...]
```

# API

Expand Down
30 changes: 30 additions & 0 deletions bin/elao-enum-dump-js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env php
<?php

use Elao\Enum\Bridge\Symfony\Console\Command\DumpJsEnumsCommand;
use Symfony\Component\Console\Application;

function includeIfExists(string $file): bool
{
return file_exists($file) && include $file;
}

if (
!includeIfExists(__DIR__ . '/../../../autoload.php') &&
!includeIfExists(__DIR__ . '/../vendor/autoload.php')
) {
fwrite(STDERR, 'Install dependencies using Composer.' . PHP_EOL);
exit(1);
}

if (!class_exists(Application::class)) {
fwrite(STDERR, 'You need the "symfony/console" component in order to run the this command.' . PHP_EOL);
exit(1);
}

(new Application())
->add($command = new DumpJsEnumsCommand())
->getApplication()
->setDefaultCommand($command->getName(), true)
->run()
;
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"homepage": "https://github.com/ogizanagi"
}
],
"bin": [
"bin/elao-enum-dump-js"
],
"autoload": {
"psr-4": {
"Elao\\Enum\\": "src/"
Expand Down
Loading

0 comments on commit c0b2726

Please sign in to comment.