teknoo/states 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

teknoo/states

Composer 安装命令:

composer require teknoo/states

包简介

Library to create classes following the State pattern in PHP. This can be a cleaner way for an object to change its behavior at runtime without resorting to large monolithic conditional statements and this improve maintainability and workflow writing.

README 文档

README

Latest Stable Version Latest Unstable Version Total Downloads License PHPStan

States allows you to create PHP classes following the State Pattern in PHP. This can be a cleaner way for an object to change its behavior at runtime without resorting to large monolithic conditional statements and this improve maintainability and workflows writing.

Features

  • Create Several States : Split classes in states to avoid un-understandable large monolithic statements.
  • Inherit States and Classes : Complete and factorize states thanks to inheritance.
    • Stated classes can be also inherited.
  • Automate States Switching : Define states switching rules based on object's properties.
  • Implement Every Where: Thanks to traits and interfaces, use this pattern on your existing code.
    • Compatible with Doctrine.

A complete documentation is available in documentation/README.md

Quick Example

<?php

declare(strict_types=1);

require 'vendor/autoload.php';

use Closure;
use DateTime;
use Teknoo\States\Attributes\Assertion\Property as PropertyAssertion;
use Teknoo\States\Attributes\StateClass;
use Teknoo\States\Automated\Assertion\Property;
use Teknoo\States\Automated\Assertion\Property\IsEqual;
use Teknoo\States\Automated\AutomatedInterface;
use Teknoo\States\Automated\AutomatedTrait;
use Teknoo\States\Proxy\ProxyInterface;
use Teknoo\States\Proxy\ProxyTrait;
use Teknoo\States\State\AbstractState;

class English extends AbstractState
{
    public function sayHello(): Closure
    {
        return function(): string {
            return 'Good morning, '.$this->name;
        };
    }

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

class French extends AbstractState
{
    public function sayHello(): Closure
    {
        return function(): string {
            return 'Bonjour, '.$this->name;
        };
    }

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

#[StateClass(English::class)]
#[StateClass(French::class)]
#[PropertyAssertion(English::class, ['country', IsEqual::class, 'en'])]
#[PropertyAssertion(French::class, ['country', IsEqual::class, 'fr'])]
class Person implements ProxyInterface, AutomatedInterface
{
    use ProxyTrait;
    use AutomatedTrait;

    private string $name;

    private string $country;

    public function __construct()
    {
        $this->initializeStateProxy();
    }

    public function setName(string $name): Person
    {
        $this->name = $name;

        return $this;
    }

    public function setCountry(string $country): Person
    {
        $this->country = $country;
        $this->updateStates();

        return $this;
    }
}

$frenchMan = new Person();
$frenchMan->setCountry('fr');
$frenchMan->setName('Roger');

$englishMan = new Person();
$englishMan->setCountry('en');
$englishMan->setName('Richard');

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

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

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

Full Example

An example of using this library is available in the folder : Demo.

Support this project

This project is free and will remain free. It is fully supported by the activities of the EIRL. If you like it and help me maintain it and evolve it, don't hesitate to support me on Patreon or Github.

Thanks :) Richard.

Credits

EIRL Richard Déloge - https://deloge.io - Lead developer. SASU Teknoo Software - https://teknoo.software

About Teknoo Software

Teknoo Software is a PHP software editor, founded by Richard Déloge, as part of EIRL Richard Déloge. Teknoo Software's goals : Provide to our partners and to the community a set of high quality services or software, sharing knowledge and skills.

License

State is licensed under the 3-Clause BSD License - see the licenses folder for details.

Installation & Requirements

To install this library with composer, run this command :

composer require teknoo/states

This library requires :

* PHP 8.1+
* A PHP autoloader (Composer is recommended)
* Teknoo/Immutable (for Automated features).

A complete documentation is available in documentation/README.md

News from Teknoo State 6.0

This library requires PHP 8.1 or newer. Some change causes bc breaks :

  • Replace StateInterface::VISIBILITY_* by Enum Visibility in same namespace.
  • Use readonly behavior on immutables objects' classes.
  • Prevent bug of mutability on automated features with Property and ConstraintsSet.
  • ProxyInterface::DEFAULT_STATE_NAME is now final

News from Teknoo State 5.0

This library requires PHP 8.0 or newer. Some change causes bc breaks :

  • Constructor Property Promotion
  • Non-capturing catches
  • Some optimisations on array functions to limit O(n)

News from Teknoo State 4.0

This library requires PHP 7.4 or newer. Some change causes bc breaks :

  • PHP 7.4 is the minimum required
  • Most methods have been updated to include type hints where applicable. Please check your extension points to make sure the function signatures are correct. _ All files use strict typing. Please make sure to not rely on type coercion.
  • Switch to typed properties
  • Remove some PHP useless DockBlocks
  • Replace array_merge by "..." operators
  • Enable PHPStan in QA Tools and disable PHPMd
  • Add PHPStan extension dedicated to support Stated classes analyze and avoid false positive.

Quick How-to to implement your first stated class

Quick How-to to learn how to use this library : Startup.

Evolutions in 3.x versions

From the version 3.2, the internal api has been redesigned to

  • Following #East programming rules.
  • Remove all public "getter" able to return the internal state of the object.
  • Clean dead code and simplify the behavior of the library.
  • Method are bound and executed by states managing object instead of object itself, but result is injected into the object.
  • This behavior allows developers to execute several implementations for a called method (but only one result must be injected).
  • Import from the extension teknoo/states-life-cyclable all automated feature. This implementation follows also the #east programming.
  • teknoo/states-life-cyclable is deprecated and not compatible with this library since 3.2.

From the version 3.1, this library provide base implementation for doctrine from teknoo/statesBundle.

  • teknoo/statesBundle is deprecated and not compatible with this library since 3.1.

From the version 3.0, this library has been redesigned to

  • States's method are now builders of closure : They must return a closure, bindable with \Closure::call(). The Reflection API is no longer used to get a closure.
  • The library uses \Closure::call() instead of \Closure::rebindTo(), more efficient.
  • States's class must be referenced declared in the proxy class, via the static method statesListDeclaration().
  • Factories and Loaders are removed, they have become useless.
  • Proxy standard can be now directly instantiate. Integrated proxy are also removed.

From the version 2.0, this library has been redesigned to

  • Reuse all composer's autoloader features instead internal autoloader.
  • Reduce the number of necessary components to the internal functioning of this library (Dependency Injector, Closure Injector).
  • Forbid the usage of slows functions like call_user_func.
  • Use Scalar Type Hinting to use PHP Engine's check instead if statements.

Contribute :)

You are welcome to contribute to this project. Fork it on Github

teknoo/states 适用场景与选型建议

teknoo/states 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 40.29k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2015 年 10 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「pattern」 「workflow」 「class」 「states」 「state pattern」 「behavioral software design pattern」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 teknoo/states 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 teknoo/states 我们能提供哪些服务?
定制开发 / 二次开发

基于 teknoo/states 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 40.29k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 11
  • 点击次数: 24
  • 依赖项目数: 12
  • 推荐数: 0

GitHub 信息

  • Stars: 10
  • Watchers: 3
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2015-10-27