antares/accessible-bundle

一个用于在 Symfony 项目中轻松使用 Accessible 的包。

v1.2 2016-04-26 22:03 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:04:19 UTC


README

SensioLabsInsight Dependency Status Latest Stable Version License

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

在这里,该库用于生成 getter 和 setter,但它也可以用于管理构造函数、属性初始化、集合和类之间的关联!

欢迎提出建议和贡献!

文档

此文件包含您使用此包所需的一切。有关库的详细信息,请参阅 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:您希望您的类 setter 使用约束验证吗?
  • 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'