Code Monkey home page Code Monkey logo

effector's Introduction

Effector

Build Status Latest Stable Version Code Climate Coverage Status Code Quality Scrutinizer Code Quality

A Php library to write effect aware code.

In this library you will find a collection of classes, each one representing a single (side) effects. This allows you to write completely functional code treating effects as data, and delegating their execution to another component of your application.

You could find more details about the ideas beyond this approach here.

Install

Add this library to your dependencies using Composer with the command

composer require marcosh/effector

Usage

Every class contained in the src/Effect folder represents a single (side) effect. For example Marcosh\Effector\Effect\Echo_ represents the operation of echoing a string, or Marcosh\Effector\Effect|FileGetContents represents the operation of reading a file.

You can create a new instance of an effects simply with

$effect = new Echo_();

Pay attention to the fact that nothing will actually happen at this moment (except for the creation of the new instance of the class).

To actually perform the effect described by the class, you will need to invoke it

$effect('hello!');

This will actually perform the effect and echo the string passed as argument.

Compose effects

Representing effects as data is useful since you could pass them around, as input parameters or as return values of functions. Still if you had not the ability to create more complex effects from simple ones, they could be quite limiting.

Luckily, composing effects is pretty easy. To do that you could use the Marcosh\Effector\Compose class. This will receive several effects and pieces of logic and combine them in a single complex effect.

For example, if you have an effect that receives an HTTP request and an effect that emits an HTTP response, you could compose them to create a web application. This could be done as follows:

$websiteLogic = function (RequestInterface $request): ResponseInterface { ... }

$app = Compose::pieces(
    new ReceiveRequest(),
    $websiteLogic,
    new EmitResponse()
);

When you compose effects and pieces of logic, you have to be careful the each piece should return the input for the next one.

Examples

Several examples of possible usages and functionalities are provided in the example folder.

Run an example using

php example/ArgvEcho.php

or, if you are using Docker to obtain a PHP 7.1 environment, you could use

docker run --rm -ti -v "$(pwd):/app" --workdir /app php:7.1-cli php example/ArgvEcho.php

The examples contained in Http.php and in SerializeEffect.php are web application, so you need a web server to try them. The easiest option is to use the built in PHP web server with

php -S localhost:8000 example/Http.php

or, if you are using Docker,

docker run --rm -ti -p 8000:8000 -v "$(pwd):/app" php:7.1-cli php -S 0.0.0.0:8000 /app/example/Http.php

and then navigate to localhost:8000 to see it working.

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.