teknoo/states-bundle

此包已被废弃且不再维护。作者建议使用 teknoo/states 包。

Symfony bunde 用于创建遵循 PHP 中状态模式的 PHP 类。这可以是一个对象在运行时更改其行为的一种更干净的方法,无需使用大型的单体条件语句,从而提高可维护性。(基于 Teknoo Software States 库构建)

安装次数: 2,768

依赖关系: 0

建议者: 0

安全: 0

星标: 2

关注者: 4

分支: 0

开放问题: 0

类型:symfony-bundle


README

SensioLabsInsight Build Status

States 允许您在 PHP 中创建遵循 状态模式 的 PHP 类。这可以是一个对象在运行时更改其行为的一种更干净的方法,无需使用大型的单体条件语句,从而提高可维护性。

此包将库 States 适配到 Symfony 2+。

简例

/**
 * File States/English.php
 */
class English extends \Teknoo\States\State\AbstractState 
{
    public function sayHello(): string
    {
        return 'Good morning, '.$this->name;
    }

    public function displayDate(\DateTime $now): string 
    {
        return $now->format('%m %d, %Y');
    }
}

/**
 * File States/French.php
 */
class French extends \Teknoo\States\State\AbstractState 
{
    public function sayHello(): string
    {
        return 'Bonjour, '.$this->name;
    }

    public function displayDate(\DateTime $now): string 
    {
        return $now->format('%d %m %Y');
    }
}

/**
 * File MyClass.php
 */
class MyClass extends \Teknoo\Bundle\StatesBundle\Entity\IntegratedEntity
{
    /**
     * @ORM\Column(type="string", length=250)
     * @var string
     */
    private $name;
    
    /**
     * @param string $name
     * @return self
     */
    public function setName(string $name): MyClass
    {
        $this->name = $name;
        
        return $this;
    }
}

$frenchMan = new MyClass();
$frenchMan->switchState('French');
$frenchMan->setName('Roger');

$englishMan = new MyClass();
$englishMan->switchState('Enflish');
$englishMan->setName('Richard');

$now = new \DateTime('2016-07-01');

foreach ([$frenchMan, $englishMan] as $man) {
    echo $man->sayHello().PHP_EOL;
    echo 'Date: '.$man->displayDate($now);
}

//Display
Bonjour Roger
Date: 01 07 2016
Good morning Richard
Date: 07 01, 2016

安装 & 要求

此库需要

* PHP 7+
* Composer
* States 2+
* Symfony 2.7+

使用 Symfony 2+ 安装 States 包的说明: 安装

此库支持 Doctrine,但 Doctrine 不是必需的。(即使不使用 Doctrine,也可以使用声明类)
* Doctrine(ORM 或 Odm/Mongo)

Symfony 使用

对于 States 的主要功能,包是透明的

  • States 包的服务是私有的,不可用。只有加载器可以通过以下方式访问:@teknoo.states.loader
  • 您的 Symfony 的 Doctrine 实体(ORM)必须使用 trait Teknoo\Bundle\StatesBundle\Entity\IntegratedTrait
    • 或者,您可以继承 Teknoo\Bundle\StatesBundle\Entity\IntegratedEntity
  • 您的 Symfony 的 Doctrine 文档(ODM)必须使用 trait Teknoo\Bundle\StatesBundle\Document\IntegratedTrait
    • 或者,您可以继承 Teknoo\Bundle\StatesBundle\Entity\IntegratedDocument

使用 States Life cyclable 扩展

  • 观察器实例以注册声明类并观察它:@teknoo.states.lifecyclable.service.observer
  • 管理器以注册关于声明类的场景:@teknoo.states.lifecyclable.service.manager
  • 原型以创建新的 Yaml 场景构建器:@teknoo.states.lifecyclable.prototype.scenario_yaml_builder
  • 原型以创建新的场景构建器:@teknoo.states.lifecyclable.prototype.scenario_builder
  • 原型以创建新的场景:@teknoo.states.lifecyclable.prototype.scenario

使用 Symfony 2+ 使用 States 的文档:Symfony

快速入门

快速入门以了解如何使用此库: 入门

示例

本库的使用示例可在以下文件夹中找到: 示例

API 文档

使用 PhpDocumentor 生成的库文档: 打开

行为文档

解释该库如何工作的文档: 行为

致谢

Richard Déloge - richarddeloge@gmail.com - 首席开发者。Teknoo Software - http://teknoo.software

关于 Teknoo Software

Teknoo Software 是一家 PHP 软件编辑公司,由 Richard Déloge 创立。Teknoo Software 的 DNA 很简单:为我们的合作伙伴和社区提供一套高质量的服务或软件,分享知识和技能。

许可证

States 采用 MIT 和 GPL3+ 许可证 - 详细信息请参阅许可证文件夹

贡献 :)

欢迎您为此项目做出贡献。 在 Github 上进行分支