teknoo / states-bundle
Symfony bunde 用于创建遵循 PHP 中状态模式的 PHP 类。这可以是一个对象在运行时更改其行为的一种更干净的方法,无需使用大型的单体条件语句,从而提高可维护性。(基于 Teknoo Software States 库构建)
Requires
- php: ~7.0
- container-interop/service-provider: ^0.3.0
- teknoo/states: ^3.0.0
Requires (Dev)
- doctrine/mongodb-odm: ^1.0.0
- doctrine/orm: ~2.2
- pdepend/pdepend: ~2.5
- phploc/phploc: ~3.0
- phpmd/phpmd: ~2.6
- phpunit/phpunit: ~5.4
- sebastian/phpcpd: ~3.0
- squizlabs/php_codesniffer: ~2.8
- symfony/framework-bundle: ~2.8||~3.0,<3.4
- teknoo/states-life-cycle: ^2.0.0
- dev-master
- 3.0.2
- 3.0.1
- 3.0.0
- 3.0.0-beta1
- 3.0.0-alpha8
- 3.0.0-alpha7
- 3.0.0-alpha6
- 3.0.0-alpha5
- 3.0.0-alpha4
- 3.0.0-alpha3
- 3.0.0-alpha2
- 3.0.0-alpha1
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.1
- 2.1.0
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 2.0.0-rc6
- 2.0.0-rc5
- 2.0.0-rc4
- 2.0.0-rc3
- 2.0.0-rc2
- 2.0.0-rc1
- 2.0.0-beta2
- 1.1.3
- 1.1.2
- 1.1.0
- 1.1.0-beta6
- 1.1.0-beta5
- 1.1.0-beta4
- 1.1.0-beta3
- 1.1.0-beta2
- 1.1.0-beta1
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.9.1-rc
- dev-next
- dev-statesBundle-3.0
- dev-statesBundle-2.0
- dev-legacy
This package is not auto-updated.
Last update: 2022-02-01 12:52:27 UTC
README
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 上进行分支