项目作者: BolZer

项目描述 :
A php cli tool to transform annotated php constants to typescript constants and enums
高级语言: PHP
项目地址: git://github.com/BolZer/ts-const-enum.git
创建时间: 2021-07-27T21:36:43Z
项目社区:https://github.com/BolZer/ts-const-enum

开源协议:Apache License 2.0

下载


Ts-Const-Enum (PHP)

maintained

Description

This tool will transform PHP Scalar-Constants - which are annotated with the provided attributes - to Typescript Constants and generates Typescript
Enums from annotated PHP one dimensional constant arrays. This may be useful if you want to reference a value in a condition within your Typescript Code. You can easily import the generated constants. This has
many advantages over just stating the value. If the value of the constant changes, your code won’t break easily.
If you change the name of a php constant the typescript code won’t compile unless you changed all occurrences of the old
constant name.

This is especially useful if you use alot of JS/TS-Frameworks and do conditionally rendering in regards to
values / properties. The Constants are by default prefixed with the declaring class followed by __ and the
constant name. You may provide an alias on attribute-level. Keep in mind to have unique names / alias as the
constants will land in one file.

Usecase

Before

  1. if (reflection.type === 16) {
  2. // Do something when true
  3. }

After

  1. import {TARGET_CLASS_CONSTANT} from "./constants";
  2. if (reflection.type === TARGET_CLASS_CONSTANT) {
  3. // Do something when true
  4. }

Installation

  1. Require the dependency
  1. composer require bolzer/ts-const-enum
  1. Create in your root (where the vendor folder is) dir a config file

    1. touch .ts-const-enum-config.php
  2. Add the following content to the previously created file with the output path.
    ```php
    <?php declare(strict_types=1);

use Bolzer\TsConstEnum\Configuration\Config;

return (new Config())
->setOutputPath(DIR . ‘/generated/constants.ts’)
;

  1. 4. Take a look at the config class for more config option.
  2. 5. Annotate some constants and arrays in your php code with the provided attributes `Constant` and `Enum`
  3. **Example**
  4. ```php
  5. <?php declare(strict_types=1);
  6. namespace Test\Example;
  7. use Bolzer\TsConstEnum\Attributes\Constant;use Bolzer\TsConstEnum\Attributes\Enum;
  8. class ExampleClass {
  9. #[Constant(alias: "Test")]
  10. private const TEST = "test";
  11. #[Enum]
  12. private const TEST_2 = [
  13. self::TEST => "value"
  14. ];
  15. }
  1. Run the binary of the tool
    1. composer dump-autoload -o --quiet
    2. php ./vendor/bin/ts-const-enum.php generate
  2. Start using the constants

What’s a makeshift enum in PHP?

Currently there’re no enums in PHP. This will change with the release of PHP 8.2.
However, those enums need to be transformed to typescript enums too. In the meantime
i use constructs like these:

  1. class Membership {
  2. public const FREE = "free";
  3. public const PREMIUM = "premium";
  4. public const TYPES = [
  5. self::FREE,
  6. self::PREMIUM
  7. ];
  8. }

The constant “TYPES” is a makeshift enum. Those constructs will be transformed too. This will translate to the following enum
in Typescript

  1. export enum Membership_TYPES {
  2. 'FREE' = 'free',
  3. 'PREMIUM' = 'premium',
  4. }