Skip to content

Commit

Permalink
Merge pull request #1 from damianopetrungaro/dp/refactoring
Browse files Browse the repository at this point in the history
Project refactoring
  • Loading branch information
alexmanno committed Nov 25, 2017
2 parents 21c42fa + 0876c99 commit 4669bcf
Show file tree
Hide file tree
Showing 17 changed files with 158 additions and 317 deletions.
37 changes: 19 additions & 18 deletions README.md
Expand Up @@ -34,42 +34,42 @@ To use this package, use Composer:

A Pipeline is a simple SplQueue of Stage.

You can initialize it in this way: `$pipeline = new Remix\Pipelines\Pipeline();`
You can initialize it in this way: `$pipeline = new AlexManno\Remix\Pipelines\Pipeline();`

You can add Stage to Pipeline using `$pipeline->pipe($stage)`

### Stage

A Stage is an object that implements `Remix\Pipelines\Interfaces\StageInterface` and has an `__invoke()` method.
A Stage is an object that implements `AlexManno\Remix\Pipelines\Interfaces\StageInterface` and has an `__invoke()` method.

This object is the smallest part of the pipeline and it should be a single operation.

You can create a class that implements that interface.

Ex.
```php
class MyCoolStage implements Remix\Pipelines\Interfaces\StageInterface
class MyCoolStage implements AlexManno\Remix\Pipelines\Interfaces\StageInterface
{
/**
* @param State $state
* @param Payload $payload
*/
public function __invoke(State $state)
public function __invoke(Payload $payload)
{
$state->append('Hello!');
$payload->setData('Hello!');

return $state;
return $payload;
}
}
```

### State
### Payload

State is an object that implements `StateInterface` and can store any kind of data.
Payload is an object that implements `PayloadInterface` and can store any kind of data.
For example in a web application it can store `Request` and `Response`.

Ex.
```php
class State implements Remix\Pipelines\Interfaces\StateInterface
class Payload implements Remix\Pipelines\Interfaces\PayloadInterface
{
/** @var RequestInterface */
public $request;
Expand All @@ -80,31 +80,32 @@ class State implements Remix\Pipelines\Interfaces\StateInterface

## Compose and run your pipeline

If you have already initialized State object and Stages objects you can compose your pipeline.
If you have already initialized Payload object and Stages objects you can compose your pipeline.

Ex.
```php
// -- Initialized objects: $state, $pipeline, $stage1, $stage2 --
// -- Initialized objects: $payload, $pipeline, $stage1, $stage2 --

$state = $pipeline
$pipeline
->pipe($stage1) // Add $stage1 to queue
->pipe($stage2) // Add $stage2 to queue
->run($state); // Run pipeline: invoke $stage1 and then $stage2 with state from $stage1
->pipe($stage2); // Add $stage2 to queue

$pipeline($payload); // Run pipeline: invoke $stage1 and then $stage2 with payload from $stage1
```

You can also compose two or more pipelines togheter using method `add()`
You can also compose two or more pipelines together using method `add()`

Ex.

```php
// -- Initialized objects: $state, $pipeline1, $pipeline2, $stage1, $stage2 --
// -- Initialized objects: $payload, $pipeline1, $pipeline2, $stage1, $stage2 --

$pipeline1->pipe($stage1); // Add $stage1 to $pipeline1
$pipeline2->pipe($stage2); // Add $stage2 to $pipeline2

$pipeline1->add($pipeline2); // Add stages from $pipeline2

$state = $pipeline1->run($state); // Run pipeline: invoke $stage1 (from $pipeline1) and then $stage2 (from $pipeline2) with state from $stage1
$payload = $pipeline1($payload); // Run pipeline: invoke $stage1 (from $pipeline1) and then $stage2 (from $pipeline2) with payload from $stage1

```

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -12,12 +12,12 @@
"minimum-stability": "stable",
"autoload": {
"psr-4": {
"Remix\\": "src/"
"AlexManno\\Remix\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
"AlexManno\\Remix\\Tests\\": "tests/"
}
},
"require": {
Expand Down
53 changes: 25 additions & 28 deletions examples/example-00.php
Expand Up @@ -2,54 +2,51 @@

namespace Examples;

use Remix\Pipelines\Interfaces\StageInterface;
use Remix\Pipelines\Pipeline;
use Remix\Pipelines\SimpleState;
use AlexManno\Remix\Pipelines\Interfaces\PayloadInterface;
use AlexManno\Remix\Pipelines\Interfaces\StageInterface;
use AlexManno\Remix\Pipelines\Pipeline;
use AlexManno\Remix\Pipelines\SimplePayload;

require_once __DIR__ . '/../vendor/autoload.php';

class TextState extends SimpleState
{

public $data = '';

public function append(string $paragraph)
{
$this->data .= $paragraph . " ";

return $this;
}
}

class SayHello implements StageInterface
{
/**
* @param TextState $state
* @param PayloadInterface $payload
*
* @return PayloadInterface
*/
public function __invoke($state)
public function __invoke(PayloadInterface $payload): PayloadInterface
{
return $state->append('Hello!');
$payload->setData('Hello!');

return $payload;
}
}

class SayMyNameIsAlessandro implements StageInterface
{
/**
* @param TextState $state
* @param PayloadInterface $payload
*
* @return PayloadInterface
*/
public function __invoke($state)
public function __invoke(PayloadInterface $payload): PayloadInterface
{
return $state->append('My name is Alessandro!');
$data = $payload->getData();
$payload->setData($data . ' My name is Alessandro');

return $payload;
}
}


$pipeline = new Pipeline();
$payload = new SimplePayload();

/** @var TextState $state */
$state = $pipeline
->pipe(new SayHello) // Append "Hello!" to TextState
->pipe(new SayMyNameIsAlessandro) // Append "My name is Alessandro" to TextState
->run(new TextState); // Pass an initial State
$pipeline
->pipe(new SayHello) // Append "Hello!" to TextState
->pipe(new SayMyNameIsAlessandro); // Append "My name is Alessandro" to TextState
$payload = $pipeline($payload); // Pass an initial State

echo $state->get(); // Print: Hello! My name is Alessandro!
echo $payload->getData(); // Print: Hello! My name is Alessandro!
46 changes: 0 additions & 46 deletions examples/example-01.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Pipelines/Exceptions/MissingImplementException.php
@@ -1,6 +1,6 @@
<?php

namespace Remix\Pipelines\Exceptions;
namespace AlexManno\Remix\Pipelines\Exceptions;

class MissingImplementException extends \Exception
{
Expand Down
18 changes: 18 additions & 0 deletions src/Pipelines/Interfaces/PayloadInterface.php
@@ -0,0 +1,18 @@
<?php

namespace AlexManno\Remix\Pipelines\Interfaces;

interface PayloadInterface
{
/**
* @return mixed
*/
public function getData();

/**
* @param $data
*
* @return void
*/
public function setData($data): void;
}
12 changes: 6 additions & 6 deletions src/Pipelines/Interfaces/PipelineInterface.php
@@ -1,12 +1,12 @@
<?php

namespace Remix\Pipelines\Interfaces;
namespace AlexManno\Remix\Pipelines\Interfaces;

interface PipelineInterface
{
public function getStages(): \SplQueue;
use SplQueue;

public function pipe(StageInterface $stage): self;
interface PipelineInterface extends StageInterface
{
public function getStages(): SplQueue;

public function run(StateInterface $state): StateInterface;
public function pipe(StageInterface $stage): PipelineInterface;
}
4 changes: 2 additions & 2 deletions src/Pipelines/Interfaces/StageInterface.php
@@ -1,8 +1,8 @@
<?php

namespace Remix\Pipelines\Interfaces;
namespace AlexManno\Remix\Pipelines\Interfaces;

interface StageInterface
{
public function __invoke($state);
public function __invoke(PayloadInterface $payload): PayloadInterface;
}
7 changes: 0 additions & 7 deletions src/Pipelines/Interfaces/StateInterface.php

This file was deleted.

14 changes: 7 additions & 7 deletions src/Pipelines/Pipeline.php
@@ -1,10 +1,10 @@
<?php

namespace Remix\Pipelines;
namespace AlexManno\Remix\Pipelines;

use Remix\Pipelines\Interfaces\PipelineInterface;
use Remix\Pipelines\Interfaces\StageInterface;
use Remix\Pipelines\Interfaces\StateInterface;
use AlexManno\Remix\Pipelines\Interfaces\PipelineInterface;
use AlexManno\Remix\Pipelines\Interfaces\StageInterface;
use AlexManno\Remix\Pipelines\Interfaces\PayloadInterface;
use SplQueue;

class Pipeline implements PipelineInterface
Expand Down Expand Up @@ -50,14 +50,14 @@ public function count(): int
return $this->stages->count();
}

public function run(StateInterface $state): StateInterface
public function __invoke(PayloadInterface $payload): PayloadInterface
{
$this->stages->rewind();
while ($this->stages->valid()) {
$state = $this->stages->current()($state);
$payload = $this->stages->current()($payload);
$this->stages->next();
}

return $state;
return $payload;
}
}

0 comments on commit 4669bcf

Please sign in to comment.