bolzer/ts-const-enum
Composer 安装命令:
composer require bolzer/ts-const-enum
包简介
A command line tool to generate typescript constants from your php constants
README 文档
README
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
if (reflection.type === 16) { // Do something when true }
After
import {TARGET_CLASS_CONSTANT} from "./constants"; if (reflection.type === TARGET_CLASS_CONSTANT) { // Do something when true }
Installation
- Require the dependency
composer require bolzer/ts-const-enum
- Create in your root (where the vendor folder is) dir a config file
touch .ts-const-enum-config.php
- Add the following content to the previously created file with the output path.
<?php declare(strict_types=1); use Bolzer\TsConstEnum\Configuration\Config; return (new Config()) ->setOutputPath(__DIR__ . '/generated/constants.ts') ;
- Take a look at the config class for more config option.
- Annotate some constants and arrays in your php code with the provided attributes
ConstantandEnum
Example
<?php declare(strict_types=1); namespace Test\Example; use Bolzer\TsConstEnum\Attributes\Constant;use Bolzer\TsConstEnum\Attributes\Enum; class ExampleClass { #[Constant(alias: "Test")] private const TEST = "test"; #[Enum] private const TEST_2 = [ self::TEST => "value" ]; }
- Run the binary of the tool
composer dump-autoload -o --quiet php ./vendor/bin/ts-const-enum.php generate
- 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:
class Membership { public const FREE = "free"; public const PREMIUM = "premium"; public const TYPES = [ self::FREE, self::PREMIUM ]; }
The constant "TYPES" is a makeshift enum. Those constructs will be transformed too. This will translate to the following enum in Typescript
export enum Membership_TYPES { 'FREE' = 'free', 'PREMIUM' = 'premium', }
bolzer/ts-const-enum 适用场景与选型建议
bolzer/ts-const-enum 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 07 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 bolzer/ts-const-enum 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bolzer/ts-const-enum 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2021-07-27