mtarld / symbok-bundle
此包已被废弃且不再维护。未建议替代包。
Symbok 注解包
2.2.1
2020-09-25 17:06 UTC
Requires
- php: ~7.2.5|^7.3|^7.4
- doctrine/annotations: ^1.8
- doctrine/collections: ^1.5
- doctrine/dbal: ^2.9
- doctrine/orm: ^2.7
- nikic/php-parser: ^4.2
- phpdocumentor/reflection-docblock: ^4.3|^5.0
- symfony/inflector: ^5.0
Requires (Dev)
- doctrine/doctrine-bundle: ^2.0
- friendsofphp/php-cs-fixer: ^2.15
- mikey179/vfsstream: ~1.6.8
- phpmd/phpmd: ^2.8
- phpunit/phpunit: ^8.4
- psalm/plugin-phpunit: ^0.9.2
- rskuipers/php-assumptions: ^0.8.0
- sebastian/phpcpd: ^4.1|^5.0
- symfony/framework-bundle: ^4.4|^5.0
- symfony/monolog-bundle: ^3.5
- symfony/phpunit-bridge: ^5.0
- symfony/yaml: ^4.4|^5.0
- vimeo/psalm: ^3.10
Conflicts
- symfony/framework-bundle: <4.0|>=6.0
README
随着 PHP 8.1 及公共只读属性的出现,此包仓库已归档,不再维护。
如果您确实需要生成 getter 和 setter,可以查看 lombok-php。
Symbok 注解包
Symfony 的运行时代码生成包。
- 检测使用 Symbok 注解的类,生成相关方法,并加载生成的类而不是原始类。
- 将生成的类存储在 Symfony 缓存中,以便 Symbok 只编译一次。
- 读取基本的 Doctrine 注解来处理属性的类型、可空状态和实体关系。
最初受 Plumbok 启发。
兼容 Symfony 4 和 5
Symbok 是什么?
👋 再见,无穷无尽的 PHP 类!
Symbok 提供注解以动态生成可预测且重复的方法。
可用的注解有
- AllArgsConstructor
- Data
- ToString
- Getter
- Setter
- Nullable
Symbok 还解析 doctrine 属性注解,如 Column
、JoinColumn
、OneToOne
、OneToMany
、ManyToOne
、ManyToMany
,以自动发现属性类型、可空状态并适应生成的方法。
您可以在 文档 中找到关于 Symbok 包的更详细信息。
入门
安装
您可以通过 composer 轻松安装 Symbok。
$ composer require mtarld/symbok-bundle
然后,应注册此包。只需确认 config\bundles.php
包含
Mtarld\SymbokBundle\SymbokBundle::class => ['all' => true]
配置
Symbok 安装后,您应配置它以适应您的需求。
要这样做,请编辑 config/packages/symbok.yaml
# config/packages/symbok.yaml symbok: # Namespaces that you wanna be processed namespaces: - 'App\Entity' - 'App\Model' defaults: getter: ~ # If getters are nullable by default (default true) nullable: ~ setter: ~ # If setters are fluent by default (default true) fluent: ~ # If setters are nullable by default (default true) nullable: ~ # If setters should update other side when relation is detected (default true) updateOtherSide: ~ constructor: # If constructor uses nullable parameters (default true) nullable: ~
然后您就可以出发了!🚀
基本示例
在配置文件中注册您的命名空间
# config/packages/symbok.yaml symbok: namespaces: - 'App\Entity'
然后通过添加注解编辑您的类
<?php // src/Entity/Product.php namespace App\Entity; use Mtarld\SymbokBundle\Annotation\Getter; class Product { /** * @Getter */ private int $id; }
然后,该类将按以下方式执行
<?php namespace App\Entity; use Mtarld\SymbokBundle\Annotation\Getter; class Product { /** * @Getter */ private int $id; public function getId(): ?int { return $this->id; } }
提供的命令
使用 symbok:update:classes
更新原始文件
$ php bin/console symbok:update:classes
运行此命令时,原始类的 docblock 将更新为好的 @method
标签,以便 IDE 可以知道存在新方法。
例如,类
<?php // src/Entity/Product.php namespace App\Entity; use Mtarld\SymbokBundle\Annotation\Getter; class Product { /** * @var int * @Getter */ private $id; }
将被重写为
<?php // src/Entity/Product.php namespace App\Entity; use Mtarld\SymbokBundle\Annotation\Getter; /** * @method int getId() */ class Product { /** * @var int * @Getter */ private $id; }
使用 symbok:preview
预览结果
$ php bin/console symbok:preview [-s|--compilationStrategy COMPILATIONSTRATEGY] <class path>
通过使用该命令,您可以直接在您的 CLI 中预览 Symbok 编译结果。
编译策略表示将应用在目标类上的编译。可以是以下两种之一:
runtime
预览将在运行时执行的 PHP 代码saved
预览在执行symbok:update:classes
命令时将写入的 PHP 代码
文档
详细的文档可在此处查阅:此处
贡献
请阅读 CONTRIBUTING.md 了解我们的行为准则以及向我们提交拉取请求的流程。
在编写您的修复/功能后,您可以运行以下命令以确保一切正常。
# Install dev dependencies $ composer install # Running tests locally $ make test
作者
- Mathias Arlaud - mtarld - <mathias(dot)arlaud@gmail(dot)com>