delormejonathan / accessible-bundle
一个用于在 Symfony 项目中轻松使用 Accessible 的扩展包。
v1.3.1
2020-05-08 10:23 UTC
Requires
- php: >=5.4.0
- delormejonathan/accessible: >=3.1
README
AccessibleBundle 为您的 Symfony 项目提供了一种 Accessible 集成方式。这将允许您使用强大的注解来定义类行为。
以下是一个(非常)基本的示例
class Customer { use AutomatedBehaviorTrait; /** * @Access({Access::GET, Access::SET}) * @Assert\Email */ private $email; } $bob = new Customer(); $bob->setEmail('bob@example.com'); $bob->getEmail(); // bob@example.com $bob->setEmail('not an email address'); // throws an InvalidArgumentException
在这里,库被用来生成获取器和设置器,但它也可以用来管理构造器、属性初始化、集合以及类之间的关联!
欢迎提出建议和贡献!
文档
此文件包含您使用此扩展包所需的一切。有关库的使用详情,请参阅Accessible 页面。
安装
首先将扩展包添加到您的 Composer 依赖中
composer require antares/accessible-bundle
然后,在您的内核中注册该扩展包
// app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Antares\Bundle\AccessibleBundle\AntaresAccessibleBundle() ); }
为了与 PropertyAccess
组件兼容,您还应在您的配置中添加以下行
# app/config/config.yml framework: property_access: magic_call: true
配置
此扩展包的配置相当简单,请看以下内容
# app/config/config.yml antares_accessible: cache: enable: true # default: false constraints_validation: enable: false # default: true validate_initialize_values: true # default: %kernel.debug%
请注意,您不需要设置配置,因为所有配置都已经是默认配置。
以下是配置值的含义
cache.enable
:您想要使用缓存驱动器吗?constraints_validation.enable
:您想要使用约束验证来设置您的类属性吗?constraints_validation.validate_initialize_values
:您想要验证@Initialize
和@InitializeObject
的值吗?
使用自定义缓存驱动器
默认情况下,使用 Doctrine\Common\Cache\PhpFileCache
的实例。如果您启用了 APC,您应该替换缓存驱动器。您可以这样做
# app/config/services.yml parameters: antares_accessible.cache_driver.class: Doctrine\Common\Cache\ApcCache services: antares_accessible.cache.driver: class: "%antares_accessible.cache_driver.class%" antares_accessible.annotations.cache_driver: class: "%antares_accessible.cache_driver.class%"
antares_accessible.cache.driver
是库使用的缓存驱动器antares_accessible.annotations.cache_driver
是库的注解读取器使用的缓存驱动器
使用自定义注解读取器
您可以使用自定义的注解读取器
# app/config/services.yml services: antares_accessible.annotations.reader: class: Doctrine\Common\Annotations\AnnotationReader
使用自定义验证器
您还可以使用自定义的约束验证器,例如,如果您的项目已经使用了验证器服务,您也可以像这样使用此库
# app/config/services.yml services: antares_accessible.constraints_validation.validator: '@validator'