teknoo / states-life-cycle
3.0.0-beta4
2017-10-02 21:16 UTC
Requires
- php: ~7.0
- php-di/php-di: ^5.4
- psr/container-implementation: ^1.0.0
- teknoo/states: ~3.1.0-beta3
Requires (Dev)
- pdepend/pdepend: ~2.5
- phploc/phploc: ~4.0
- phpmd/phpmd: ~2.6
- phpunit/phpunit: ~6.2
- sebastian/phpcpd: ~3.0
- squizlabs/php_codesniffer: ~3.0
- symfony/event-dispatcher: ~2.7||~3.0
- symfony/yaml: ~2.7||~3.0
Suggests
- symfony/yaml: To able to use Scenario in Yaml format
- dev-master
- 3.0.0-beta4
- 3.0.0-beta3
- 3.0.0-beta2
- 3.0.0-beta1
- 2.0.1
- 2.0.0
- 2.0.0-beta1
- 2.0.0-alpha4
- 2.0.0-alpha3
- 2.0.0-alpha2
- 2.0.0-alpha1
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 1.0.0-RC1
- 1.0.0-beta5
- 1.0.0-beta4
- 1.0.0-beta3
- 1.0.0-beta2
- 1.0.0-alpha1
- dev-feature-east
- dev-states-life-cycle-3.0
- dev-states-life-cycle-2.0
- dev-states-life-cycle-1.0
This package is not auto-updated.
Last update: 2022-02-01 12:54:53 UTC
README
此扩展为您的声明类提供了一些新行为
- 自动声明类:根据类中定义的断言验证规则自动切换到状态
- 可生命周期声明类:将声明类到观察者的状态更新进行调度
- 可追踪声明类:记录声明类在生命周期中的状态演变
- 场景:在PHP或yaml中轻松创建场景,场景在声明类实例和其他应用组件之间
简短示例
/**
* File Person/States/English.php
*/
class English extends \Teknoo\States\State\AbstractState
{
// ...
}
/**
* File Person/States/French.php
*/
class French extends \Teknoo\States\State\AbstractState
{
// ...
}
/**
* File Person.php
*/
class Person extends \Teknoo\States\Proxy\Standard implements AutomatedInterface, LifeCyclableInterface
{
private $nationality;
public function setNationality(string $nationality): Person
{
$this->nationality = $nationality;
//To update states of this instance according to its assertions and
//dispatch states change to observer
$this->updateStates();
return $this;
}
public function setTravel(Travel $travel): Person
{
// ...
}
public function getTravel(): Travel
{
// ...
}
/**
* @return AssertionInterface[]
*/
public function getStatesAssertions(): array
{
return [
(new Assertion([French::class]))->with('nationality', new IsEqual('Fr')),
(new Assertion([English::class]))->with('nationality', new IsNotEqual('Fr'))
];
}
}
/**
* File Travel/States/Schengen.php
*/
class Schengen extends \Teknoo\States\State\AbstractState
{
// ...
}
/**
* File Travel/States/Uk.php
*/
class Uk extends \Teknoo\States\State\AbstractState
{
// ...
}
/**
* File Travel.php
*/
class Travel extends \Teknoo\States\Proxy\Standard
{
// ..
}
//Scenario
//Use the helper generator to get needed instance of observer and event dispatcher, it's not a mandatory tool
$di = include 'src/generator.php';
//Scenario to enable Schengen state to travel of French man
$di->get(ManagerInterface::class)
->registerScenario(
(new ScenarioBuilder($di->get(TokenizerInterface::class)))
->towardStatedClass('demo\Person')
->onIncomingState('French')
->run(function (EventInterface $event) {
$person = $event->getObserved();
$person->getTravel()->switchState('Schengen');
})
->build(new Scenario())
);
//Scenario to enable UK state to travel of English man
$di->get(ManagerInterface::class)
->registerScenario(
(new ScenarioBuilder($di->get(TokenizerInterface::class)))
->towardStatedClass('demo\Person')
->onIncomingState('English')
->run(function (EventInterface $event) {
$person = $event->getObserved();
$person->getTravel()->switchState('UK');
})
->build(new Scenario())
);
//Demo
$frenchMan = new Person();
$travel1 = new Travel();
$frenchMan->setTravel($travel1);
print_r($travel1->listEnabledStates()); //Print []
$frenchMan->setNationality('Fr');
print_r($travel1->listEnabledStates()); //Print ['Schengen"]
$englishMan = new Person();
$travel2 = new Travel();
$frenchMan->setTravel($travel2);
print_r($travel2->listEnabledStates()); //Print []
$englishMan->setNationality('En');
print_r($travel2->listEnabledStates()); //Print ['UK"]
$englishMan->setNationality('Fr');
print_r($travel2->listEnabledStates()); //Print ['Schengen"]
完整示例
此库的用法示例可在demo文件夹中找到。
安装 & 要求
要使用composer安装此库,请运行以下命令
composer require teknoo/states-life-cycle
您必须安装一个事件分发器,例如symfony/event-dispatcher。
composer require symfony/event-dispatcher
库提供对Symfony Event Dispatcher
的原生支持。如果您使用其他事件分发器,您必须编写自己的EventDispatcherBridgeInterface
。
接下来,您必须使用在demo/EventDispatcherBridge.php
中定义的事件分发器桥配置生成器
$di = include 'src/generator.php';
在src/di.php
中提供PHP-DI配置。您可以使用PHP-DI与Symfony配合使用PHP-DI Symfony Bridge。
此库需要
* PHP 7+
* Teknoo Software States 3.1+
* PHP-DI
* Symfony event dispatcher (not required)
* Symfony yaml to parse yaml scenarii (not required)
如何创建一个被观察的声明类和场景
快速了解如何使用此库的指南: 启动。
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 Life Cycle采用MIT许可证 - 有关详细信息,请参阅许可证文件夹
贡献 :)
欢迎您为此项目做出贡献。在Github上Fork它