Giter VIP home page Giter VIP logo

state's Introduction

Build Status

PHP Unified State

This package provides a standard for tracking states of applications in PHP. The package aims to create a multi-use standard.

This package contains interfaces and a simple implementations for tracking states.

Installation

composer require php-unified/state

Usage

Provided interfaces

PhpUnified\State\Common\StateTrackerInterface

This interface is an indication that an optional StateInterface interface can be supplied. The class that would implement this interface is one that keeps track of value for a state (optionally).

PhpUnified\State\Common\StateInterface

This interface provides a standard for implementing state tracking into applications. The following functions are expected to be implemented.

For this interface, an implementation is provided. See: PhpUnified\State\State.

Setters and getters

The object has 2 setters and 2 getters. These methods are:

  • setValue and getValue for setting and getting the value of the object.
  • setIdentifier and getIdentifier for setting and getting the identifier of the object.
Exporting states
String conversion

The output of the method __toString should convert a state to a human readable string. The result of this method with a single state could look like:

version: 1.0.0

Where version is the set identifier and 1.0.0 the set version.

For a state with multiple sub-states, output could look like:

packages:
    name: php-unified/state
    version: 1.0.0

Every line is a new state, with their respective identifier: value pairs.

Array conversion

The method for exporting states to an array is exportState. The output of the method could look like:

$output = [
    'packages' => '',
    'states' => [
        'name' => 'php-unified/state',
        'version' => '1.0.0'
    ]
]
Adding a sub-state

To add a state to a state, the method addState is expected to be implemented. These states should be iterated over in both of the exporting states implementations.

Change log

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Tips

Using nested states

By taking the approach of using nested states, the application can actively update their values. This would result a single repository of states, which can be used in logging. And thus creating more insight for debugging purposes.

For example, the application could monitor the last action from a database connection like so:

use PhpUnified\State\State;
use Application\Database\Connection;
use Application\Logger\Logger;

$systemState = new State();
$systemState->setIdentifier('system-state');

$databaseTransactionState = new State();
$databaseTransactionState->setIdentifier('last-db-transaction-state');

$systemState->addState($databaseTransactionState);

$logger = new Logger();
$connection = new Connection($databaseTransactionState);
try {
    $connection->doQuery();
} catch (Throwable $e) {
    $logger->log('warning', $e->getMessage());
    $logger->info($systemState->__toString());
}

Then the (fictional) Connection class could look like:

namespace Application\Database;

use PhpUnified\State\Common\StateInterface;

class Connection
{
    /**
     * State tracker
     *
     * @var StateInterface
     */
    private $state;

    /**
     * Constructor
     *
     * @param StateInterface $state
     */
    public function __construct(StateInterface $state)
    {
        $this->state = $state;
    }

    /**
     * Fictional doQuery function
     *
     * @return void
     */
    public function doQuery(): void
    {
        $state->setValue('Starting database connection');
        $connection = connectToDatabase();

        $state->setValue('Starting transaction');
        $connection->startTransaction();

        $state->setValue('Executing transaction');
        if($connection->doTransaction()) {
            $state->setValue(
                sprintf(
                    'Transaction with id %d transaction success',
                    $connection->getLastTransactionId()
                )
            );
            return true;
        }

        $state->setValue('Rolling back transaction');
        $connection->rollBackTransaction();
        $state->setValue('Rollback executed');
    }
}

If any of the above function calls in the doQuery function would result in an exception being thrown, the latest state can still be logged.

MIT License

Copyright (c) 2019 GrizzIT

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

state's People

Contributors

mfrankruijter avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.