Skip to content

Commit

Permalink
Renamed package from StackRunner to Invokator
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianmiu committed Oct 13, 2023
1 parent 1cbd9f0 commit ff50235
Show file tree
Hide file tree
Showing 59 changed files with 324 additions and 325 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "siriusphp/stack_runner",
"description": "Library that",
"name": "siriusphp/invokator",
"description": "Library that implements a unified way to execute a list of commands/callables that are used by various patterns: events, pipelines, middleware etc",
"type": "library",
"license": "MIT",
"keywords": [
"callables",
"events",
"middleware",
"pipes",
"invoker",
"pipelines"
],
"authors": [
Expand All @@ -30,7 +30,7 @@
"src/functions.php"
],
"psr-4": {
"Sirius\\StackRunner\\": "src/"
"Sirius\\Invokator\\": "src/"
}
},
"provide": {
Expand Down
34 changes: 17 additions & 17 deletions docs/1_the_stack.md → docs/1_callable_list.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
---
title: What are stacks in Sirius\StackRunner?
title: What are callable collection in Sirius\Invokator?
---

# The callable stack
# The callable collections

In the `Sirius\StackRunner` lingo a **stack** is a list of callables organized by priority. By default, the priority is determined by the order the callables are added to the stack. The callables are executed in the order of their priority.
In the `Sirius\Invokator` items in the callable collections are organized by priority. By default, the priority is determined by the order the callables are added to the collection. The callables are executed in the order of their priority.

A **callable** is something that can be executed directly or after being interpreted by the [invoker](2_the_invoker.md). For example the `Invoker` class that comes with this library can recognize and execute callables in the form of
`SomeClass@someMethod`

Even though stacks may have different purposes (middleware, events etc), a stack is defined in a single way.
Even though callable collections may have different purposes (middleware, events etc), a collection is defined in a single way.

Below it's an example for a stack designed to run as a pipeline that process a piece of text

```php
use Sirius\StackRunner\Stack;
use Sirius\Invokator\CallableCollection;

$stack = new Stack();
$callables = new CallableCollection();

// add a regular function to the stack
$stack->add('trim');
$callables->add('trim');

// add an anonymous function
$stack->add(function($str) {
$callables->add(function($str) {
return 'hello ' . $str;
});

// add a static method to the stack
$stack->add('Str::toUpper');
$callables->add('Str::toUpper');

// add an object method to the stack,
// object to be retrieved at runtime from the container
// the callable also has a priority of -100
$stack->add('SlackChannel@send', -100);
$callables->add('SlackChannel@send', -100);

// add an object method to the stack
// with a specific priority to be executed
// before the callable that was registered above
$stack->add([$logger, 'info'], -3);
$callables->add([$logger, 'info'], -3);
```

#### Callable priority
Expand All @@ -46,21 +46,21 @@ By default, all callables in a stack have priority **zero** and callables with t

A callable with a higher priority will be executed before a callable with a lower priority.

## Executing a stack
## Executing a collection of callables

The `Sirius\StackRunner` library comes with a few **stack processors** which are act as stack registries/repositories and stack executors.
The `Sirius\Invokator` library comes with a few **stack processors** which are act as stack registries/repositories and stack executors.

```php
use Sirius\StackRunner\Processors\PipelineProcessor;
use Sirius\StackRunner\Invoker;
use Sirius\Invokator\Processors\PipelineProcessor;
use Sirius\Invokator\Invoker;

// this is required for callables like "SomeClass@someMethod"
// and by callables that have dependencies
$invoker = new Invoker($yourChoiceOfDependencyInjectionContainer);
$processor = new PipelineProcessor($invoker);

// execute the $stack created above as a pipeline with one parameter
$processor->processStack($stack, ' world ');
$processor->processCollection($stack, ' world ');

// this will
// 1. create string `HELLO WORLD`,
Expand All @@ -70,4 +70,4 @@ $processor->processStack($stack, ' world ');

Each type of stack processor has its own quirks that you can learn on the next page.

[Next: The stack_processors](2_stack_processors.md)
[Next: The callable_processors](2_callable_processors.md)
27 changes: 27 additions & 0 deletions docs/2_1_simple_collection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
title: Simple callable collections
---

# The simple processor

This processor has the following characteristics:
1. All the parameters are passed down to each of the callables. This means all the callables should have the same signature (although this restriction can be by-passed with **modifiers**)
2. The values returned by the callables are ignored

#### Use case: Reporting/logging stack

```php
use Sirius\Invokator\Invoker;
use Sirius\Invokator\Processors\SimpleCallablesProcessor;

$invoker = new Invoker($psr11Container);
$processor = new SimpleCallablesProcessor($invoker);

$processor->add('log', 'FileLogger@log') // this returns the Stack
->add('SlackNotification@send')
->add('TextNotification@send');

$processor->process('log', $severity, $message, $context);
```

[Next: Pipelines](2_2_pipelines.md)
27 changes: 0 additions & 27 deletions docs/2_1_simple_stacks.md

This file was deleted.

10 changes: 5 additions & 5 deletions docs/2_2_pipelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ This processor has the following characteristics:
#### Use case

```php
use Sirius\StackRunner\Invoker;
use Sirius\StackRunner\Processors\PipelineProcessor;
use Sirius\Invokator\Invoker;
use Sirius\Invokator\Processors\PipelineProcessor;

$invoker = new Invoker($psr11Container);
$processor = new PipelineProcessor($invoker);

$processor->get('tax_report')
->add('ImportCsv@taxReport') // this receives a DTO with a file and a user ID, imports it into a table and returns a DTO with the table name and user ID
->add('GenerateTaxReport@compileExcelFile') // this receives the DTO returned by the previous callable, returns a DTO with the name of the XLS file and user ID
->add('NotifyReportReady@notifyTaxReport') // this receive the DTO from the previous callable and sends an email
->add('ImportCsv@taxReport') // this receives a DTO with a file and a user ID, imports it into a table and returns a DTO with the table name and user ID
->add('GenerateTaxReport@compileExcelFile') // this receives the DTO returned by the previous callable, returns a DTO with the name of the XLS file and user ID
->add('NotifyReportReady@notifyTaxReport'); // this receive the DTO from the previous callable and sends an email

$processor->process('tax_report', new TaxReportDTO('path_to_csv_file', 'user_id') );
```
Expand Down
14 changes: 7 additions & 7 deletions docs/2_3_event_dispatcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ title: PSR-14 Event dispatcher implementation

# PSR-14 Event dispatcher implementation

The `Sirius\StackRunner` library comes with an implementation of the [PSR-14 Event Dispatcher](https://www.php-fig.org/psr/psr-14/).
The `Sirius\Invokator` library comes with an implementation of the [PSR-14 Event Dispatcher](https://www.php-fig.org/psr/psr-14/).

```php
use Sirius\StackRunner\Invoker;
use Sirius\StackRunner\Event\Dispatcher;
use Sirius\StackRunner\Event\ListenerProvider;
use Sirius\Invokator\Invoker;
use Sirius\Invokator\Event\Dispatcher;
use Sirius\Invokator\Event\ListenerProvider;

$invoker = new Invoker($psr11Container);
$listenerProvider = new ListenerProvider();
Expand All @@ -19,7 +19,7 @@ $dispatcher = new Dispatcher($listenerProvider, $invoker);
$listenerProvider->subscribeTo(Event::class, 'some_callable', 0);
$listenerProvider->subscribeOnceTo(Event::class, 'some_callable', 0);

// if you use the Sirius\StackRunner\Event\ListenerProvider
// if you use the Sirius\Invokator\Event\ListenerProvider
// the same results as above can also be achieved with
$dispatcher->subscribeTo(Event::class, 'some_callable', 0);
$dispatcher->subscribeOnceTo(Event::class, 'some_callable', 0);
Expand All @@ -30,7 +30,7 @@ $dispatcher->subscribeOnceTo(Event::class, 'some_callable', 0);
If you want to identify the events by something other than the class name you can make the event classes implement the `HasEventname` interface

```php
use Sirius\StackRunner\Event\HasEventName;
use Sirius\Invokator\Event\HasEventName;

class EventWithName implements HasEventName {
public function getEventName() : string{
Expand All @@ -52,7 +52,7 @@ $dispatcher->dispatch(new EventWithName());
If you want some events to be able to stop the execution of the rest of the callables in the stack you can add the `Stoppable` trait to your event classes

```php
use Sirius\StackRunner\Event\Stoppable;
use Sirius\Invokator\Event\Stoppable;

class StoppableEvent {
use Stoppable;
Expand Down
14 changes: 7 additions & 7 deletions docs/2_3_middlewares.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ This processor has the following characteristics:
#### Use case

```php
use Sirius\StackRunner\Invoker;
use Sirius\StackRunner\Processors\MiddlewareProcessor;
use Sirius\Invokator\Invoker;
use Sirius\Invokator\Processors\MiddlewareProcessor;

$invoker = new Invoker($psr11Container);
$processor = new MiddlewareProcessor($invoker);

$processor->get('dispatcher')
->add('CsrfCheckMiddleware')
->add('TrimStringsMiddleware')
->add('AuthMiddleware')
->add('CacheMiddleware')
->add('RouterMiddleware')
->add('CsrfCheckMiddleware')
->add('TrimStringsMiddleware')
->add('AuthMiddleware')
->add('CacheMiddleware')
->add('RouterMiddleware');

$processor->process('dispatcher', new HttpRequest);
```
Expand Down
10 changes: 5 additions & 5 deletions docs/2_4_wordpress_actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ title: Actions a la Wordpress

This processor is similar to the "Simple stack processor" with the difference that you also have to specify a limit for the arguments passed to each callable.

This means that the callables do not have to have the same signature as for the Simple stack processor. This processor is just a convenience as the same result could be been achieved using the ['limit_arguments' modifier](4_callable_modifiers.md)
This means that the callables do not have to have the same signature as for the SimpleCallables processor. This processor is just a convenience as the same result could be been achieved using the ['limit_arguments' modifier](4_callable_modifiers.md)

#### Use case

```php
use Sirius\StackRunner\Invoker;
use Sirius\StackRunner\Processors\ActionsProcessor;
use Sirius\Invokator\Invoker;
use Sirius\Invokator\Processors\ActionsProcessor;

$invoker = new Invoker($psr11Container);
$processor = new ActionsProcessor($invoker);
Expand All @@ -27,7 +27,7 @@ $processor->process('save_post', $postID, $wpPost, $update);
**Attention!** The processor's `get()` and `add()` method return the Stack object, so you can't chain callables with arguments limit. For example the code below doesn't work as you might expect
```php
$processor->add('save_post', 'validate_taxonomies', 0, 2) // this returns the stack
->add('validate_acf_fields', 0, 2) // this won't place a limit on the arguments for the 'validate_acf_fields' function
->add('validate_acf_fields', 0, 2); // this won't place a limit on the arguments for the 'validate_acf_fields' function
```

[Next: Filters a la Wordpress](2_2_pipelines.md)
[Next: Filters a la Wordpress](2_5_wordpress_filters.md)
7 changes: 4 additions & 3 deletions docs/2_5_wordpress_filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ title: Filters a la Wordpress

This processor is similar to the "Pipeline processor" with the difference that the additional parameters passed to the `process()` method are also passed to the other callbacks.

Just like the "actions processor" you can specify the number of arguments passed to the callbacks
Just like the "actions processor" you have to specify the number of arguments passed to the callbacks

#### Use case

```php
use Sirius\StackRunner\Invoker;
use Sirius\StackRunner\Processors\FiltersProcessor;
use Sirius\Invokator\Invoker;
use Sirius\Invokator\Processors\FiltersProcessor;

$invoker = new Invoker($psr11Container);
$processor = new FiltersProcessor($invoker);
Expand All @@ -24,6 +24,7 @@ $processor->process('the_title', $postTitle, $postID);
```

**Attention!** The processor's `get()` and `add()` method return the Stack object, so you can't chain callables with arguments limit. For example the code below doesn't work as you might expect

```php
$processor->add('the_title', 'add_category_name', 0, 2) // this returns the stack
->add('add_site_name', 0, 2) // this won't place a limit on the arguments for the 'add_site_name' function
Expand Down
6 changes: 3 additions & 3 deletions docs/2_6_custom_processors.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
title: Custom callable processors
---

# Custom stack processor
# Custom callables processor

You can easily build your own custom processor by extending the `SimpleStackSelector` or starting from scratch as the API for a stack processor is very simple.
You can easily build your own custom processor by extending the `SimpleCallablesProcessor` or starting from scratch as the API for a stack processor is very simple.

If you extend the `SimpleStackProcessor` you only need to implement the `processStack()` method.
If you extend the `SimpleCallablesProcessor` you only need to implement the `processCollection()` method.

Here are some ideas:
1. pipelines where all the callbacks receive the same arguments and where the result of a callback becomes the first argument in the list. It would be similar to the "Filters processor" but without having to specify the limit for the arguments.
Expand Down
14 changes: 7 additions & 7 deletions docs/2_stack_processors.md → docs/2_callable_processors.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
---
title: What are stack processors in Sirius\StackRunner?
title: What are callable processors in Sirius\Invokator?
---

# The stack processors
# The callable processors

Stack processors are objects that have 3 objectives:
Callable processors are objects that have 3 objectives:
1. To act as a registry for stacks via `$processor->get('stack_identifier')`
2. To simplify adding callbacks to stacks via `$processor->add('stack_identifier', $callable, $priority)` which is syntactic sugar for `$processor->get('stack_identifier')->add($callable, $priority)`
3. To process stacks stored in the registry via `$processor->process('stack_identifier', $param_1, $param_2)`
4. To process stacks constructed separately via `$processor->processStack($previouslyConstructedStack, $param_1, $param_2)`
4. To process stacks constructed separately via `$processor->processCollection($previouslyConstructedStack, $param_1, $param_2)`

The stack processors depend on the [invoker](3_the_invoker.md) to actually execute the callbacks.
The processors depend on the [invoker](3_the_invoker.md) to actually execute the callbacks.

The `Sirius\StackRunner` library comes with 5 stack processors/runners
1. [simple stacks](2_1_simple_stacks.md)
The `Sirius\Invokator` library comes with 5 callable processors/runners
1. [simple collection](2_1_simple_collection.md)
2. [pipelines](2_2_pipelines.md)
3. [middlewares](2_3_middlewares.md)
4. [actions a la Wordpress](2_4_wordpress_actions.md)
Expand Down

0 comments on commit ff50235

Please sign in to comment.