PHPackages                             teknoo/states-life-cycle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. teknoo/states-life-cycle

Abandoned → [teknoo/recipe](/?search=teknoo%2Frecipe)ArchivedLibrary

teknoo/states-life-cycle
========================

Component to extend the behavior of teknoo/states and provide a service to manage life cycles of stated class instances and automated their behavior

2.0.1(9y ago)15.4k2MITPHPPHP ~7.0

Since Nov 29Pushed 8y ago2 watchersCompare

[ Source](https://github.com/TeknooSoftware/states-life-cycle)[ Packagist](https://packagist.org/packages/teknoo/states-life-cycle)[ Docs](http://teknoo.software/states)[ RSS](/packages/teknoo-states-life-cycle/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (11)Versions (26)Used By (2)

Teknoo Software - States Life Cyclable extension
================================================

[](#teknoo-software---states-life-cyclable-extension)

[![SensioLabsInsight](https://camo.githubusercontent.com/8b60e801e1bb15cd922296ffce029fd414f3f9092dd40e575720ebddf36f9932/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f63656531316434332d383162312d343937342d613338382d3838306135333261326334662f6d696e692e706e67)](https://insight.sensiolabs.com/projects/cee11d43-81b1-4974-a388-880a532a2c4f) [![Build Status](https://camo.githubusercontent.com/53ddd3095b32c0503e6757b8e31aea04671419be05013864a30331a2aaa1f144/68747470733a2f2f7472617669732d63692e6f72672f54656b6e6f6f536f6674776172652f7374617465732d6c6966652d6379636c652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/TeknooSoftware/states-life-cycle)

This extention provides some new behavior to your stated class :

- Automated stated class : to switch automatically to states defined by validation rules defined in class by assertions
- Lifecycable stated class : to dispatch states updates from a stated class to observer
- Traceable stated class : to keep evolution of states in the lifecycle of your stated class
- Scenario : to create easily scenarii in php or yaml between stated class instances and others application's components

Shorts Examples
---------------

[](#shorts-examples)

```
/**
 * 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"]

```

Full Example
------------

[](#full-example)

Examples of using this library is available in the folder demo.

Installation &amp; Requirements
-------------------------------

[](#installation--requirements)

To install this library with composer, run this command :

```
composer require teknoo/states-life-cycle

```

You must install a event dispatcher, like symfony/event-dispatcher.

```
composer require symfony/event-dispatcher

```

The library provide a native support of `Symfony Event Dispatcher`. If you use another event dispatcher, you must write you own `EventDispatcherBridgeInterface`.

Next, you must configure the generator, with the event dispatcher bridge defined in `demo/EventDispatcherBridge.php` :

```
$di = include 'src/generator.php';

```

A PHP-DI configuration is available into `src/di.php`. You can use PHP-DI with Symfony with [PHP-DI Symfony Bridge](http://php-di.org/doc/frameworks/symfony2.html).

This library requires :

```
* PHP 7+
* Teknoo Software States 3.1+
* PHP-DI
* Symfony event dispatcher (not required)
* Symfony yaml to parse yaml scenarii (not required)

```

How to create an observed stated class and Scenarri
---------------------------------------------------

[](#how-to-create-an-observed-stated-class-and-scenarri)

Quick How-to to learn how use this library : [Startup](docs/quick-startup.md).

API Documentation
-----------------

[](#api-documentation)

Generated documentation from the library with PhpDocumentor : Coming soon

Credits
-------

[](#credits)

Richard Déloge -  - Lead developer. Teknoo Software -

About Teknoo Software
---------------------

[](#about-teknoo-software)

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

License
-------

[](#license)

States Life Cycle is licensed under the MIT License - see the licenses folder for details

Contribute :)
-------------

[](#contribute-)

You are welcome to contribute to this project. [Fork it on Github](CONTRIBUTING.md)

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~33 days

Recently: every ~57 days

Total

21

Last Release

3144d ago

Major Versions

1.0.3 → 2.0.0-alpha12016-10-07

2.0.1 → 3.0.0-beta12017-07-05

PHP version history (2 changes)1.0.0-alpha1PHP &gt;5.6.0

1.0.0-beta2PHP ~7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/e78f8fa644d9bbbd96dc92b2210e1290250522e56365345442c56eb8d042fb53?d=identicon)[frenchcomp](/maintainers/frenchcomp)

---

Top Contributors

[![frenchcomp](https://avatars.githubusercontent.com/u/1397905?v=4)](https://github.com/frenchcomp "frenchcomp (58 commits)")

---

Tags

classpatternstateslife cyclestate patternbehavioral software design pattern

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/teknoo-states-life-cycle/health.svg)

```
[![Health](https://phpackages.com/badges/teknoo-states-life-cycle/health.svg)](https://phpackages.com/packages/teknoo-states-life-cycle)
```

###  Alternatives

[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.4k5.6M651](/packages/sylius-sylius)[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.

1138.5k18](/packages/teknoo-states)[nette/robot-loader

🍀 Nette RobotLoader: high performance and comfortable autoloader that will search and autoload classes within your application.

89152.7M321](/packages/nette-robot-loader)[daveawb/understated

A PHP Finite State Machine for Laravel 5+

302.0k](/packages/daveawb-understated)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
