unialteri / states-bundle

该软件包已被废弃,不再维护。作者建议使用 teknoo/states-bundle 软件包。

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

安装: 521

依赖项: 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)必须使用特性 Teknoo\Bundle\StatesBundle\Entity\IntegratedTrait
    • 或者,您可以从 Teknoo\Bundle\StatesBundle\Entity\IntegratedEntity 继承。
  • 您的 Symfony 的 Doctrine 文档(ODM)必须使用特性 Teknoo\Bundle\StatesBundle\Document\IntegratedTrait
    • 或者,您可以从 Teknoo\Bundle\StatesBundle\Document\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上Fork它