From ff50235cae0ec4391bd47571b1c54775e9cec2f3 Mon Sep 17 00:00:00 2001 From: adrianmiu Date: Fri, 13 Oct 2023 08:36:18 +0300 Subject: [PATCH] Renamed package from StackRunner to Invokator --- composer.json | 8 +-- docs/{1_the_stack.md => 1_callable_list.md} | 34 ++++++------- docs/2_1_simple_collection.md | 27 ++++++++++ docs/2_1_simple_stacks.md | 27 ---------- docs/2_2_pipelines.md | 10 ++-- docs/2_3_event_dispatcher.md | 14 +++--- docs/2_3_middlewares.md | 14 +++--- docs/2_4_wordpress_actions.md | 10 ++-- docs/2_5_wordpress_filters.md | 7 +-- docs/2_6_custom_processors.md | 6 +-- ...processors.md => 2_callable_processors.md} | 14 +++--- docs/3_callable_modifiers.md | 49 +++++++++---------- docs/4_the_invoker.md | 19 ++++--- docs/couscous.yml | 14 +++--- docs/index.md | 22 ++++----- readme.md | 28 +++++------ src/ArgumentReference.php | 2 +- src/{Stack.php => CallableCollection.php} | 4 +- src/CallablesRegistryInterface.php | 10 ++++ src/DelayedResult.php | 2 +- src/Event/Dispatcher.php | 8 +-- src/Event/HasEventName.php | 2 +- src/Event/ListenerProvider.php | 20 ++++---- src/Event/ListenerSubscriber.php | 2 +- src/Event/Stoppable.php | 2 +- src/InvalidCallableException.php | 2 +- ...erInterface.php => InvokatorInterface.php} | 6 +-- src/Invoker.php | 2 +- src/InvokerAwareInterface.php | 2 +- src/InvokerReference.php | 2 +- src/InvokerResult.php | 2 +- src/Modifiers/LimitArguments.php | 6 +-- src/Modifiers/Once.php | 6 +-- src/Modifiers/WithArguments.php | 10 ++-- src/Modifiers/Wrap.php | 6 +-- src/PipelinePromise.php | 4 +- src/Processors/ActionsProcessor.php | 30 ++++++------ src/Processors/FiltersProcessor.php | 30 ++++++------ src/Processors/MiddlewareProcessor.php | 12 ++--- src/Processors/PipelineProcessor.php | 14 +++--- ...essor.php => SimpleCallablesProcessor.php} | 28 +++++------ src/StackRegistryInterface.php | 10 ---- src/functions.php | 24 ++++----- tests/phpunit.xml | 2 +- tests/src/Event/DispatcherTest.php | 4 +- tests/src/Event/EventWithName.php | 2 +- tests/src/Event/EventWithoutName.php | 2 +- tests/src/Event/StoppableEvent.php | 2 +- tests/src/Modifiers/OnceTest.php | 10 ++-- tests/src/Modifiers/WithArgumentsTest.php | 18 +++---- tests/src/Modifiers/WrapTest.php | 8 +-- tests/src/Processors/ActionsProcessorTest.php | 6 +-- tests/src/Processors/FiltersProcessorTest.php | 6 +-- .../Processors/MiddlewareProcessorTest.php | 6 +-- .../src/Processors/PipelineProcessorTest.php | 6 +-- .../Processors/SimpleStackProcessorTest.php | 10 ++-- tests/src/StackTest.php | 6 +-- tests/src/TestCase.php | 4 +- tests/src/Utilities/SimpleCallables.php | 6 +-- 59 files changed, 324 insertions(+), 325 deletions(-) rename docs/{1_the_stack.md => 1_callable_list.md} (61%) create mode 100644 docs/2_1_simple_collection.md delete mode 100644 docs/2_1_simple_stacks.md rename docs/{2_stack_processors.md => 2_callable_processors.md} (63%) rename src/{Stack.php => CallableCollection.php} (95%) create mode 100644 src/CallablesRegistryInterface.php rename src/{StackRunnerInterface.php => InvokatorInterface.php} (58%) rename src/Processors/{SimpleStackProcessor.php => SimpleCallablesProcessor.php} (56%) delete mode 100644 src/StackRegistryInterface.php diff --git a/composer.json b/composer.json index 753efda..8294d8b 100644 --- a/composer.json +++ b/composer.json @@ -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": [ @@ -30,7 +30,7 @@ "src/functions.php" ], "psr-4": { - "Sirius\\StackRunner\\": "src/" + "Sirius\\Invokator\\": "src/" } }, "provide": { diff --git a/docs/1_the_stack.md b/docs/1_callable_list.md similarity index 61% rename from docs/1_the_stack.md rename to docs/1_callable_list.md index cf8eae5..e7d2414 100644 --- a/docs/1_the_stack.md +++ b/docs/1_callable_list.md @@ -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 @@ -46,13 +46,13 @@ 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 @@ -60,7 +60,7 @@ $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`, @@ -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) diff --git a/docs/2_1_simple_collection.md b/docs/2_1_simple_collection.md new file mode 100644 index 0000000..91eb520 --- /dev/null +++ b/docs/2_1_simple_collection.md @@ -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) diff --git a/docs/2_1_simple_stacks.md b/docs/2_1_simple_stacks.md deleted file mode 100644 index f192f09..0000000 --- a/docs/2_1_simple_stacks.md +++ /dev/null @@ -1,27 +0,0 @@ ---- -title: Simple stacks ---- - -# The simple processor - -This processor has the following characteristics: -1. All the parameters are passed down to each of the callables as which 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\StackRunner\Invoker; -use Sirius\StackRunner\Processors\SimpleStackProcessor; - -$invoker = new Invoker($psr11Container); -$processor = new SimpleStackProcessor($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) diff --git a/docs/2_2_pipelines.md b/docs/2_2_pipelines.md index 30943b0..e4af5bb 100644 --- a/docs/2_2_pipelines.md +++ b/docs/2_2_pipelines.md @@ -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') ); ``` diff --git a/docs/2_3_event_dispatcher.md b/docs/2_3_event_dispatcher.md index 10850e8..d015463 100644 --- a/docs/2_3_event_dispatcher.md +++ b/docs/2_3_event_dispatcher.md @@ -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(); @@ -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); @@ -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{ @@ -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; diff --git a/docs/2_3_middlewares.md b/docs/2_3_middlewares.md index 69f2008..786aa9b 100644 --- a/docs/2_3_middlewares.md +++ b/docs/2_3_middlewares.md @@ -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); ``` diff --git a/docs/2_4_wordpress_actions.md b/docs/2_4_wordpress_actions.md index 6417321..c76a5c2 100644 --- a/docs/2_4_wordpress_actions.md +++ b/docs/2_4_wordpress_actions.md @@ -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); @@ -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) diff --git a/docs/2_5_wordpress_filters.md b/docs/2_5_wordpress_filters.md index 1cb6802..384efec 100644 --- a/docs/2_5_wordpress_filters.md +++ b/docs/2_5_wordpress_filters.md @@ -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); @@ -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 diff --git a/docs/2_6_custom_processors.md b/docs/2_6_custom_processors.md index 837449d..bc78c0f 100644 --- a/docs/2_6_custom_processors.md +++ b/docs/2_6_custom_processors.md @@ -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. diff --git a/docs/2_stack_processors.md b/docs/2_callable_processors.md similarity index 63% rename from docs/2_stack_processors.md rename to docs/2_callable_processors.md index f1fa039..a436877 100644 --- a/docs/2_stack_processors.md +++ b/docs/2_callable_processors.md @@ -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) diff --git a/docs/3_callable_modifiers.md b/docs/3_callable_modifiers.md index 1a4d2a6..f364c56 100644 --- a/docs/3_callable_modifiers.md +++ b/docs/3_callable_modifiers.md @@ -1,5 +1,5 @@ --- -title: What callable modifiers in Sirius\StackRunner? +title: What callable modifiers in Sirius\Invokator? --- # Callable modifiers @@ -10,16 +10,16 @@ Since a stack is composed of callables, the callable modifiers are actually call The modifiers are composable which means you can wrap them one on top of another. -The `Sirius\StackRunner` library comes with a bunch of modifiers that allow you to simplify how you compose your stack. The examples use function that create the actual modifiers. +The `Sirius\Invokator` library comes with a bunch of modifiers that allow you to simplify how you compose your stack. The examples use function that create the actual modifiers. ## The "limit arguments" modifier This modifier will limit the number of arguments passed to the callables. If your stack starts the execution with 5 arguments and the signature of a callable has only one parameter you have to use the `LimitArguments` modifier ```php -use function Sirius\StackRunner\limit_arguments; -use Sirius\StackRunner\Invoker; -use Sirius\StackRunner\Processors\SimpleStackProcessor; +use function Sirius\Invokator\limit_arguments; +use Sirius\Invokator\Invoker; +use Sirius\Invokator\Processors\SimpleStackProcessor; $invoker = new Invoker($psr11Container); $processor = new SimpleStackProcessor($invoker); @@ -27,7 +27,7 @@ $processor = new SimpleStackProcessor($invoker); $processor->get('stack') ->add(limit_arguments(function($param_1, $param2) { return 'something' - }, 2)); + }, 2)) ->add(limit_arguments('Service@method', 1)); $processor->process('stack', $param_1, $param_2, $param_3, $param_4); @@ -44,14 +44,14 @@ It is useful for an events system where you want a particular listener to be exe **Atention!** The result of the first execution is stored in memory and returned on subsequent calls which might not be what you want. ```php -use function Sirius\StackRunner\once; -$processor->get('event') - ->add(once(function($param_1, $param2) { - return 'something' - }, 2)); - ->add(limit_arguments('Service@method', 1)); - -$processor->process('event', $param_1, $param_2); +use function Sirius\Invokator\once; +$processor->get('add') + ->add(once(function($param_1, $param2) { + return $param_1 + $param2 + })); + +$processor->process('event', 2 + 3); // this returns 5 +$processor->process('event', 8 + 7); // this STILL returns 5 ``` ## The "wrap" modifier @@ -61,33 +61,32 @@ This modifier wraps a callable inside a function that only receives one argument This can be used to override how the callable is actually being executed by passing different arguments. ```php -use function Sirius\StackRunner\wrap; +use function Sirius\Invokator\wrap; $processor->get('stack') - ->add(wrap('Service@method', function(callable $callable) use ($param_1, $param_2) { - return $callable($param_1, $param_2); + ->add(wrap('Service@method', function(callable $callable) use ($param_3, $param_4) { + return $callable($param_3, $param_4); }, 2)); $processor->process('stack', $param_1, $param_2); +// the `Service@method` function will actually receive $param_3 and $param_4 as arguments instead of $param_1 and $param_2 ``` ## The "with arguments" modifier -This modifier can be used when you have a callable that has a specific signature, and you don't want to wrap change its signature nor do you want to wrap it inside an anonymous function (eg: because you want to serialize the stack) +This modifier can be used when you have a callable that has a specific signature, and you don't want to change its signature nor do you want to wrap it inside an anonymous function (eg: because you want to serialize the stack) ```php -use function Sirius\StackRunner\with_arguments; -use function Sirius\StackRunner\ref; -use function Sirius\StackRunner\arg; +use function Sirius\Invokator\with_arguments; +use function Sirius\Invokator\ref; +use function Sirius\Invokator\arg; $processor->get('stack') - ->add(with_arguments('Service@method', [arg(0), 'value', ref('SomeClass'), arg(1)]); + ->add(with_arguments('Service@method', [arg(0), 'value', ref('SomeClass'), arg(1)]); $processor->process('stack', $param_1, $param_2); // This is the same as calling Service@method($param_1, 'value', $container->get('SomeClass'), $param_2) ``` -The `ref()` function generates an `InvokerReference` (more on this on the next page) object which can be either -1. an **integer** in which case it refers to the index of the parameters passed to the stack process method. In this example above the `ref(0)` corresponds to the first parameter. -2. a **string** in which case it refers to an object from the container. In the example above the invoker will replace `rev('SomeClass')` with the value returned by `$container->get('SomeClass')` +You will learn about the `arg()` and `ref()` functions on the [invoker](4_the_invoker.md) page [Next: The callable invoker](4_the_invoker.md) diff --git a/docs/4_the_invoker.md b/docs/4_the_invoker.md index 2497dc0..024d271 100644 --- a/docs/4_the_invoker.md +++ b/docs/4_the_invoker.md @@ -1,5 +1,5 @@ --- -title: What is the callable invoker in Sirius\StackRunner? +title: What is the callable invoker in Sirius\Invokator? --- # The callable invoker @@ -12,7 +12,7 @@ On its own, PHP recognizes a few callables based on how they are referenced: 4. an anonymous function, like `function($number) { return $number + 5}` 5. an instance of an invokable class (i.e. a class that has an `__invoke` method) -However, in the context of modern development these options are not enough. For this reason the `Sirius\StackRunner` library comes with an `Invoker` that can handle: +However, in the context of modern development these options are not enough. For this reason the `Sirius\Invokator` library comes with an `Invoker` that can handle: 1. Callables in the format of `SomeClass@someMethod` 2. Callables in the format of `SomeClass` with the condition that the class is invokable @@ -29,18 +29,17 @@ When executing a callback, the invoker object, with go over the arguments passed This is for when you want to pass a parameter that is actually a reference to an item in the container. It is useful if you don't want to retrieve the item from the container until it is actually needed. -Such an instance is created using the `Sirius\StackRunner\ref($identifier)` function. +Such an instance is created using the `Sirius\Invokator\ref($identifier)` function. ##### 2. instances of the `InvokerResult` class. -This for when you want to use as an argument for a callable the result of a computationally expensive function. Such an instance is created using the `Sirius\StackRunner\result_of($callable, [$param_1, - $param_2])` function +This for when you want to use as an argument the result of a computationally expensive function. In this case you would want to execute that function only when it's needed -##### 3. instances of the `ArgumentReference` class. -This for when you want to use as an argument in a different position than the position that argument was passed on by the processor. Such an instance is created using the `Sirius\StackRunner\arg(2)` function. +Such an instance is created using the `Sirius\Invokator\result_of($callable, [$param_1, $param_2])` function -For an example, check the documentation for the ["with arguments" modifier](3_callable_modifiers.md) +##### 3. instances of the `ArgumentReference` class. +This for when you want to use as an argument in a different position than the position that argument was passed on by the processor. -## Extending the invoker +Such an instance is created using the `Sirius\Invokator\arg(2)` function. +For an example, check the documentation for the ["with arguments" modifier](3_callable_modifiers.md) -[Next: The simple stack runner](3_simple_runner.md) diff --git a/docs/couscous.yml b/docs/couscous.yml index 7eed693..1bc926c 100644 --- a/docs/couscous.yml +++ b/docs/couscous.yml @@ -12,12 +12,12 @@ exclude: # Base URL of the published website (no "/" at the end!) # You are advised to set and use this variable to write your links in the HTML layouts -baseUrl: https://www.sirius.ro/php/sirius/stack_runner +baseUrl: https://www.sirius.ro/php/sirius/invokator paypal: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=SGXDKNJCXFPJU gacode: UA-535999-18 -projectName: Sirius\StackRunner -title: Sirius\StackRunner +projectName: Sirius\Invokator +title: Sirius\Invokator subTitle: Implementation of various stack patterns (pipelines, middleware, events) # The left menu bar @@ -40,13 +40,13 @@ menu: relativeUrl: stack: text: The callable stack - relativeUrl: 1_the_stack.html - stack_processors: + relativeUrl: 1_callable_collection.html + callable_processors: text: The stack processors - relativeUrl: 2_stack_processors.html + relativeUrl: 2_callable_processors.html simple_stacks: text: "- Simple stacks" - relativeUrl: 2_1_simple_stacks.html + relativeUrl: 2_1_simple_collection.html pipelines: text: "- Pipelines" relativeUrl: 2_2_pipelines.html diff --git a/docs/index.md b/docs/index.md index 36d0a56..7c3ebe0 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1,16 +1,16 @@ --- -title: Sirius\StackRunner. PHP library to control your application flow using patterns +title: Sirius\Invokator. PHP library to control your application flow using patterns --- #Sirius Stack Runner -[![Source Code](http://img.shields.io/badge/source-siriusphp/stackrunner-blue.svg)](https://github.com/siriusphp/stackrunner) -[![Latest Version](https://img.shields.io/packagist/v/siriusphp/stack_runner.svg)](https://github.com/siriusphp/stackrunner/releases) -[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://github.com/siriusphp/stackrunner/blob/master/LICENSE) -[![Build Status](https://github.com/siriusphp/stackrunner/workflows/CI/badge.svg)](https://github.com/siriusphp/stackrunner/actions) -[![Total Downloads](https://img.shields.io/packagist/dt/siriusphp/stack_runner.svg)](https://packagist.org/packages/siriusphp/stackrunner) +[![Source Code](http://img.shields.io/badge/source-siriusphp/invokator-blue.svg)](https://github.com/siriusphp/invokator) +[![Latest Version](https://img.shields.io/packagist/v/siriusphp/invokator.svg)](https://github.com/siriusphp/invokator/releases) +[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://github.com/siriusphp/invokator/blob/master/LICENSE) +[![Build Status](https://github.com/siriusphp/invokator/workflows/CI/badge.svg)](https://github.com/siriusphp/invokator/actions) +[![Total Downloads](https://img.shields.io/packagist/dt/siriusphp/invokator.svg)](https://packagist.org/packages/siriusphp/invokator) -Sirius StackRunner is a library that implements various patterns that execute a list of commands: +Sirius Invokator is a library that implements various patterns that execute a list of commands: 1. middlewares 2. pipelines @@ -19,7 +19,7 @@ Sirius StackRunner is a library that implements various patterns that execute a All of the above patterns have in common that they are actually a list of callables that have to be executed in different ways. -In the case of **middlewares**, the starting parameter (eg: a HTTP request) is passed from one callable to the next, each +In the case of **middlewares**, the starting parameter (a HTTP request) is passed from one callable to the next, each callable having the option to terminate with a result or call the next callable in the stack. In the case of **pipelines**, the result of each callable is passed to the next callable and the last callable will return the result of the pipeline. @@ -32,10 +32,10 @@ The Wordpress' **action hooks** are similar to events as the callables are not i ### Install using Composer -Sirius\StackRunner is available on [Packagist](https://packagist.org/packages/siriusphp/stackrunner). To install it run +Sirius\Invokator is available on [Packagist](https://packagist.org/packages/siriusphp/invokator). To install it run ``` -composer require siriusphp/stack_runner +composer require siriusphp/invokator ``` -[Next: The callable stack](1_the_stack.md) +[Next: The callable list](1_callable_collection.md) diff --git a/readme.md b/readme.md index 03223f2..74e1489 100644 --- a/readme.md +++ b/readme.md @@ -1,12 +1,12 @@ -# Sirius StackRunner +# Sirius Invokator -[![Source Code](http://img.shields.io/badge/source-siriusphp/stackrunner-blue.svg)](https://github.com/siriusphp/stackrunner) -[![Latest Version](https://img.shields.io/packagist/v/siriusphp/stack_runner.svg)](https://github.com/siriusphp/stackrunner/releases) -[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://github.com/siriusphp/stackrunner/blob/master/LICENSE) -[![Build Status](https://github.com/siriusphp/stackrunner/workflows/CI/badge.svg)](https://github.com/siriusphp/stackrunner/actions) -[![Total Downloads](https://img.shields.io/packagist/dt/siriusphp/stack_runner.svg)](https://packagist.org/packages/siriusphp/stackrunner) +[![Source Code](http://img.shields.io/badge/source-siriusphp/invokator-blue.svg)](https://github.com/siriusphp/invokator) +[![Latest Version](https://img.shields.io/packagist/v/siriusphp/invokator.svg)](https://github.com/siriusphp/invokator/releases) +[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://github.com/siriusphp/invokator/blob/master/LICENSE) +[![Build Status](https://github.com/siriusphp/invokator/workflows/CI/badge.svg)](https://github.com/siriusphp/invokator/actions) +[![Total Downloads](https://img.shields.io/packagist/dt/siriusphp/invokator.svg)](https://packagist.org/packages/siriusphp/invokator) -Sirius StackRunner is a library that implements various patterns that execute a list of commands: +Sirius Invokator is a library that implements a unified way to execute a list of commands/callables that are used by various patterns: 1. middlewares 2. pipelines @@ -14,27 +14,27 @@ Sirius StackRunner is a library that implements various patterns that execute a 4. actions a la Wordpress 5. filters a la Wordpress -All of the above patterns have in common that they are actually a list of callables and they differ in the way they are executed in different ways. +All of the above patterns have in common that they are actually a list of callables, and they differ in the way they are executed in different ways. In the case of middlewares, the starting parameter (eg: a HTTP request) is passed from one callable to the next, each -callable having the option to terminate with a result or call the next callable in the stack. +callable having the option to terminate with a result or call the next callable in the list. In the case of pipelines, the result of each callable is passed to the next callable and the last callable will return the result of the pipeline -In the case of events, an `event` object is passed through each callable in the stack and each callable is independent. +In the case of events, an `event` object is passed through each callable in the list and each callable is independent. ## Elevator pitch ```php -use Sirius\StackRunner\Invoker; -use Sirius\StackRunner\Processors\PipelineProcessor; -use Sirius\StackRunner\Stack; +use Sirius\Invokator\Invoker; +use Sirius\Invokator\Processors\PipelineProcessor; +use Sirius\Invokator\CallableCollection; $container = app(); // your application DI container $invoker = new Invoker($container) $processor = new PipelineProcessor($invoker); -$stack = new Stack(); +$stack = new CallableCollection(); $stack->add('trim'); $stack->add('Str::toUppercase'); $stack->add(fn($value) => { // anonymous function diff --git a/src/ArgumentReference.php b/src/ArgumentReference.php index e5a90ed..1cf6b5e 100644 --- a/src/ArgumentReference.php +++ b/src/ArgumentReference.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Sirius\StackRunner; +namespace Sirius\Invokator; class ArgumentReference { diff --git a/src/Stack.php b/src/CallableCollection.php similarity index 95% rename from src/Stack.php rename to src/CallableCollection.php index f2b34cc..95cd492 100644 --- a/src/Stack.php +++ b/src/CallableCollection.php @@ -2,9 +2,9 @@ declare(strict_types=1); -namespace Sirius\StackRunner; +namespace Sirius\Invokator; -class Stack extends \SplPriorityQueue +class CallableCollection extends \SplPriorityQueue { protected int $index = 0; diff --git a/src/CallablesRegistryInterface.php b/src/CallablesRegistryInterface.php new file mode 100644 index 0000000..12c4ce8 --- /dev/null +++ b/src/CallablesRegistryInterface.php @@ -0,0 +1,10 @@ +registry->getListenersForEvent($event); /** @var mixed $callable */ diff --git a/src/Event/HasEventName.php b/src/Event/HasEventName.php index c2c47d7..c01bd45 100644 --- a/src/Event/HasEventName.php +++ b/src/Event/HasEventName.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Sirius\StackRunner\Event; +namespace Sirius\Invokator\Event; interface HasEventName { diff --git a/src/Event/ListenerProvider.php b/src/Event/ListenerProvider.php index 9ec07cc..9cbce0e 100644 --- a/src/Event/ListenerProvider.php +++ b/src/Event/ListenerProvider.php @@ -2,21 +2,21 @@ declare(strict_types=1); -namespace Sirius\StackRunner\Event; +namespace Sirius\Invokator\Event; use Psr\EventDispatcher\ListenerProviderInterface; -use Sirius\StackRunner\Stack; -use function Sirius\StackRunner\once; +use Sirius\Invokator\CallableCollection; +use function Sirius\Invokator\once; class ListenerProvider implements ListenerProviderInterface, ListenerSubscriber { /** - * @var array + * @var array */ protected array $registry = []; // @phpstan-ignore-line /** - * @return iterable|Stack + * @return iterable|CallableCollection */ public function getListenersForEvent(object $event): iterable // @phpstan-ignore-line { @@ -25,13 +25,13 @@ public function getListenersForEvent(object $event): iterable // @phpstan-ignore $eventName = $event->getEventName(); } - return $this->registry[$eventName] ?? new Stack(); + return $this->registry[$eventName] ?? new CallableCollection(); } public function subscribeTo(string $eventName, mixed $callable, int $priority = 0): void { - /** @var Stack $stack */ - $stack = $this->registry[$eventName] ?? new Stack(); + /** @var CallableCollection $stack */ + $stack = $this->registry[$eventName] ?? new CallableCollection(); $stack->add($callable, $priority); @@ -40,8 +40,8 @@ public function subscribeTo(string $eventName, mixed $callable, int $priority = public function subscribeOnceTo(string $eventName, mixed $callable, int $priority = 0): void { - /** @var Stack $stack */ - $stack = $this->registry[$eventName] ?? new Stack(); + /** @var CallableCollection $stack */ + $stack = $this->registry[$eventName] ?? new CallableCollection(); $stack->add(once($callable), $priority); diff --git a/src/Event/ListenerSubscriber.php b/src/Event/ListenerSubscriber.php index 03cd3b9..c6cef65 100644 --- a/src/Event/ListenerSubscriber.php +++ b/src/Event/ListenerSubscriber.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Sirius\StackRunner\Event; +namespace Sirius\Invokator\Event; interface ListenerSubscriber { diff --git a/src/Event/Stoppable.php b/src/Event/Stoppable.php index 556a9d2..d81d6c1 100644 --- a/src/Event/Stoppable.php +++ b/src/Event/Stoppable.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Sirius\StackRunner\Event; +namespace Sirius\Invokator\Event; trait Stoppable { diff --git a/src/InvalidCallableException.php b/src/InvalidCallableException.php index 7b6df9b..dd77bea 100644 --- a/src/InvalidCallableException.php +++ b/src/InvalidCallableException.php @@ -1,6 +1,6 @@ $params @@ -14,5 +14,5 @@ public function process(string $name, ...$params): mixed; /** * @param array $params */ - public function processStack(Stack $stack, ...$params): mixed; + public function processCollection(CallableCollection $stack, ...$params): mixed; } diff --git a/src/Invoker.php b/src/Invoker.php index 54c67ac..3ce4a48 100644 --- a/src/Invoker.php +++ b/src/Invoker.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Sirius\StackRunner; +namespace Sirius\Invokator; use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; diff --git a/src/InvokerAwareInterface.php b/src/InvokerAwareInterface.php index 1017a36..9ba501a 100644 --- a/src/InvokerAwareInterface.php +++ b/src/InvokerAwareInterface.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Sirius\StackRunner; +namespace Sirius\Invokator; interface InvokerAwareInterface { diff --git a/src/InvokerReference.php b/src/InvokerReference.php index a9839a7..9bba466 100644 --- a/src/InvokerReference.php +++ b/src/InvokerReference.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Sirius\StackRunner; +namespace Sirius\Invokator; class InvokerReference { diff --git a/src/InvokerResult.php b/src/InvokerResult.php index 272e2d3..b1c3194 100644 --- a/src/InvokerResult.php +++ b/src/InvokerResult.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Sirius\StackRunner; +namespace Sirius\Invokator; class InvokerResult { diff --git a/src/Modifiers/LimitArguments.php b/src/Modifiers/LimitArguments.php index 4859385..8b71cfd 100644 --- a/src/Modifiers/LimitArguments.php +++ b/src/Modifiers/LimitArguments.php @@ -2,10 +2,10 @@ declare(strict_types=1); -namespace Sirius\StackRunner\Modifiers; +namespace Sirius\Invokator\Modifiers; -use Sirius\StackRunner\Invoker; -use Sirius\StackRunner\InvokerAwareInterface; +use Sirius\Invokator\Invoker; +use Sirius\Invokator\InvokerAwareInterface; class LimitArguments implements InvokerAwareInterface { diff --git a/src/Modifiers/Once.php b/src/Modifiers/Once.php index 14212ae..02282c5 100644 --- a/src/Modifiers/Once.php +++ b/src/Modifiers/Once.php @@ -2,10 +2,10 @@ declare(strict_types=1); -namespace Sirius\StackRunner\Modifiers; +namespace Sirius\Invokator\Modifiers; -use Sirius\StackRunner\Invoker; -use Sirius\StackRunner\InvokerAwareInterface; +use Sirius\Invokator\Invoker; +use Sirius\Invokator\InvokerAwareInterface; class Once implements InvokerAwareInterface { diff --git a/src/Modifiers/WithArguments.php b/src/Modifiers/WithArguments.php index f40b0b0..f7023bd 100644 --- a/src/Modifiers/WithArguments.php +++ b/src/Modifiers/WithArguments.php @@ -2,12 +2,12 @@ declare(strict_types=1); -namespace Sirius\StackRunner\Modifiers; +namespace Sirius\Invokator\Modifiers; -use Sirius\StackRunner\ArgumentReference; -use Sirius\StackRunner\Invoker; -use Sirius\StackRunner\InvokerAwareInterface; -use Sirius\StackRunner\InvokerReference; +use Sirius\Invokator\ArgumentReference; +use Sirius\Invokator\Invoker; +use Sirius\Invokator\InvokerAwareInterface; +use Sirius\Invokator\InvokerReference; class WithArguments implements InvokerAwareInterface { diff --git a/src/Modifiers/Wrap.php b/src/Modifiers/Wrap.php index 8d15977..7aed24d 100644 --- a/src/Modifiers/Wrap.php +++ b/src/Modifiers/Wrap.php @@ -2,10 +2,10 @@ declare(strict_types=1); -namespace Sirius\StackRunner\Modifiers; +namespace Sirius\Invokator\Modifiers; -use Sirius\StackRunner\Invoker; -use Sirius\StackRunner\InvokerAwareInterface; +use Sirius\Invokator\Invoker; +use Sirius\Invokator\InvokerAwareInterface; class Wrap implements InvokerAwareInterface { diff --git a/src/PipelinePromise.php b/src/PipelinePromise.php index e8e8442..4c393f4 100644 --- a/src/PipelinePromise.php +++ b/src/PipelinePromise.php @@ -2,14 +2,14 @@ declare(strict_types=1); -namespace Sirius\StackRunner; +namespace Sirius\Invokator; class PipelinePromise { /** * @param array $params */ - public function __construct(public mixed $value, public Stack $remainingStack, public array $params, public int $retryAfter) + public function __construct(public mixed $value, public CallableCollection $remainingStack, public array $params, public int $retryAfter) { } } diff --git a/src/Processors/ActionsProcessor.php b/src/Processors/ActionsProcessor.php index 530d893..fb976d8 100644 --- a/src/Processors/ActionsProcessor.php +++ b/src/Processors/ActionsProcessor.php @@ -2,19 +2,19 @@ declare(strict_types=1); -namespace Sirius\StackRunner\Processors; +namespace Sirius\Invokator\Processors; -use Sirius\StackRunner\Invoker; -use Sirius\StackRunner\Stack; -use Sirius\StackRunner\StackRegistryInterface; -use Sirius\StackRunner\StackRunnerInterface; +use Sirius\Invokator\Invoker; +use Sirius\Invokator\CallableCollection; +use Sirius\Invokator\CallablesRegistryInterface; +use Sirius\Invokator\InvokatorInterface; -use function Sirius\StackRunner\limit_arguments; +use function Sirius\Invokator\limit_arguments; -class ActionsProcessor implements StackRegistryInterface, StackRunnerInterface +class ActionsProcessor implements CallablesRegistryInterface, InvokatorInterface { /** - * @var array + * @var array */ protected $registry = []; @@ -22,7 +22,7 @@ public function __construct(public Invoker $invoker) { } - public function get(string $name): Stack + public function get(string $name): CallableCollection { if (! isset($this->registry[$name])) { $this->registry[$name] = $this->newStack(); @@ -31,17 +31,17 @@ public function get(string $name): Stack return $this->registry[$name]; } - public function add(string $name, mixed $callable, int $priority = 0, int $argumentsLimit = 1): Stack + public function add(string $name, mixed $callable, int $priority = 0, int $argumentsLimit = 1): CallableCollection { return $this->get($name)->add(limit_arguments($callable, $argumentsLimit), $priority); } - protected function newStack(): Stack + protected function newStack(): CallableCollection { - return new Stack(); + return new CallableCollection(); } - protected function getCopy(string $name): Stack + protected function getCopy(string $name): CallableCollection { return clone($this->get($name)); } @@ -51,13 +51,13 @@ protected function getCopy(string $name): Stack */ public function process(string $name, ...$params): mixed { - return $this->processStack($this->getCopy($name), ...$params); + return $this->processCollection($this->getCopy($name), ...$params); } /** * @param array $params */ - public function processStack(Stack $stack, ...$params): mixed + public function processCollection(CallableCollection $stack, ...$params): mixed { $nextCallable = $stack->extract(); diff --git a/src/Processors/FiltersProcessor.php b/src/Processors/FiltersProcessor.php index 6e230ad..2968607 100644 --- a/src/Processors/FiltersProcessor.php +++ b/src/Processors/FiltersProcessor.php @@ -2,19 +2,19 @@ declare(strict_types=1); -namespace Sirius\StackRunner\Processors; +namespace Sirius\Invokator\Processors; -use Sirius\StackRunner\Invoker; -use Sirius\StackRunner\Stack; -use Sirius\StackRunner\StackRegistryInterface; -use Sirius\StackRunner\StackRunnerInterface; +use Sirius\Invokator\Invoker; +use Sirius\Invokator\CallableCollection; +use Sirius\Invokator\CallablesRegistryInterface; +use Sirius\Invokator\InvokatorInterface; -use function Sirius\StackRunner\limit_arguments; +use function Sirius\Invokator\limit_arguments; -class FiltersProcessor implements StackRegistryInterface, StackRunnerInterface +class FiltersProcessor implements CallablesRegistryInterface, InvokatorInterface { /** - * @var array + * @var array */ protected $registry = []; @@ -22,7 +22,7 @@ public function __construct(public Invoker $invoker) { } - public function get(string $name): Stack + public function get(string $name): CallableCollection { if (! isset($this->registry[$name])) { $this->registry[$name] = $this->newStack(); @@ -31,17 +31,17 @@ public function get(string $name): Stack return $this->registry[$name]; } - public function add(string $name, mixed $callable, int $priority = 0, int $argumentsLimit = 1): Stack + public function add(string $name, mixed $callable, int $priority = 0, int $argumentsLimit = 1): CallableCollection { return $this->get($name)->add(limit_arguments($callable, $argumentsLimit), $priority); } - protected function newStack(): Stack + protected function newStack(): CallableCollection { - return new Stack(); + return new CallableCollection(); } - protected function getCopy(string $name): Stack + protected function getCopy(string $name): CallableCollection { return clone($this->get($name)); } @@ -51,13 +51,13 @@ protected function getCopy(string $name): Stack */ public function process(string $name, ...$params): mixed { - return $this->processStack($this->getCopy($name), ...$params); + return $this->processCollection($this->getCopy($name), ...$params); } /** * @param array $params */ - public function processStack(Stack $stack, ...$params): mixed + public function processCollection(CallableCollection $stack, ...$params): mixed { $result = null; $nextCallable = $stack->extract(); diff --git a/src/Processors/MiddlewareProcessor.php b/src/Processors/MiddlewareProcessor.php index e4ac640..d1d4ab2 100644 --- a/src/Processors/MiddlewareProcessor.php +++ b/src/Processors/MiddlewareProcessor.php @@ -2,12 +2,12 @@ declare(strict_types=1); -namespace Sirius\StackRunner\Processors; +namespace Sirius\Invokator\Processors; -use Sirius\StackRunner\InvalidCallableException; -use Sirius\StackRunner\Stack; +use Sirius\Invokator\InvalidCallableException; +use Sirius\Invokator\CallableCollection; -class MiddlewareProcessor extends SimpleStackProcessor +class MiddlewareProcessor extends SimpleCallablesProcessor { /** * @param array $params @@ -16,7 +16,7 @@ class MiddlewareProcessor extends SimpleStackProcessor * @throws \Psr\Container\NotFoundExceptionInterface * @throws InvalidCallableException */ - public function processStack(Stack $stack, ...$params): mixed + public function processCollection(CallableCollection $stack, ...$params): mixed { $result = null; $nextCallable = $stack->extract(); @@ -25,7 +25,7 @@ public function processStack(Stack $stack, ...$params): mixed if ($stack->isEmpty()) { $response = $this->invoker->invoke($nextCallable, ...$params); } else { - $next = fn ($result) => $this->processStack($stack, ...$params); + $next = fn ($result) => $this->processCollection($stack, ...$params); $paramsForNext = [...$params, $next]; $response = $this->invoker->invoke($nextCallable, ...$paramsForNext); } diff --git a/src/Processors/PipelineProcessor.php b/src/Processors/PipelineProcessor.php index 76f25ed..68cf2dd 100644 --- a/src/Processors/PipelineProcessor.php +++ b/src/Processors/PipelineProcessor.php @@ -2,19 +2,19 @@ declare(strict_types=1); -namespace Sirius\StackRunner\Processors; +namespace Sirius\Invokator\Processors; -use Sirius\StackRunner\DelayedResult; -use Sirius\StackRunner\PipelinePromise; -use Sirius\StackRunner\Stack; +use Sirius\Invokator\DelayedResult; +use Sirius\Invokator\PipelinePromise; +use Sirius\Invokator\CallableCollection; -class PipelineProcessor extends SimpleStackProcessor +class PipelineProcessor extends SimpleCallablesProcessor { /** * @param array $params */ - public function processStack(Stack $stack, ...$params): mixed + public function processCollection(CallableCollection $stack, ...$params): mixed { $result = null; $nextCallable = $stack->extract(); @@ -36,7 +36,7 @@ public function processStack(Stack $stack, ...$params): mixed /** * @param array $params */ - public function resumeStack(Stack $remainingStack, mixed $previousValue, ...$params): mixed + public function resumeStack(CallableCollection $remainingStack, mixed $previousValue, ...$params): mixed { return null; } diff --git a/src/Processors/SimpleStackProcessor.php b/src/Processors/SimpleCallablesProcessor.php similarity index 56% rename from src/Processors/SimpleStackProcessor.php rename to src/Processors/SimpleCallablesProcessor.php index 44dc767..54929d8 100644 --- a/src/Processors/SimpleStackProcessor.php +++ b/src/Processors/SimpleCallablesProcessor.php @@ -2,17 +2,17 @@ declare(strict_types=1); -namespace Sirius\StackRunner\Processors; +namespace Sirius\Invokator\Processors; -use Sirius\StackRunner\Invoker; -use Sirius\StackRunner\Stack; -use Sirius\StackRunner\StackRegistryInterface; -use Sirius\StackRunner\StackRunnerInterface; +use Sirius\Invokator\Invoker; +use Sirius\Invokator\CallableCollection; +use Sirius\Invokator\CallablesRegistryInterface; +use Sirius\Invokator\InvokatorInterface; -class SimpleStackProcessor implements StackRegistryInterface, StackRunnerInterface +class SimpleCallablesProcessor implements CallablesRegistryInterface, InvokatorInterface { /** - * @var array + * @var array */ protected $registry = []; @@ -20,7 +20,7 @@ public function __construct(public Invoker $invoker) { } - public function get(string $name): Stack + public function get(string $name): CallableCollection { if (! isset($this->registry[$name])) { $this->registry[$name] = $this->newStack(); @@ -29,17 +29,17 @@ public function get(string $name): Stack return $this->registry[$name]; } - public function add(string $name, mixed $callable, int $priority = 0): Stack + public function add(string $name, mixed $callable, int $priority = 0): CallableCollection { return $this->get($name)->add($callable, $priority); } - protected function newStack(): Stack + protected function newStack(): CallableCollection { - return new Stack(); + return new CallableCollection(); } - protected function getCopy(string $name): Stack + protected function getCopy(string $name): CallableCollection { return clone($this->get($name)); } @@ -49,13 +49,13 @@ protected function getCopy(string $name): Stack */ public function process(string $name, ...$params): mixed { - return $this->processStack($this->getCopy($name), ...$params); + return $this->processCollection($this->getCopy($name), ...$params); } /** * @param array $params */ - public function processStack(Stack $stack, ...$params): mixed + public function processCollection(CallableCollection $stack, ...$params): mixed { $nextCallable = $stack->extract(); while ($nextCallable !== null) { diff --git a/src/StackRegistryInterface.php b/src/StackRegistryInterface.php deleted file mode 100644 index 6383e8f..0000000 --- a/src/StackRegistryInterface.php +++ /dev/null @@ -1,10 +0,0 @@ - $params */ @@ -32,7 +32,7 @@ function result_of(mixed $callable, array $params = []): InvokerResult } } -if (! function_exists('Sirius\StackRunner\with_arguments')) { +if (! function_exists('Sirius\Invokator\with_arguments')) { /** * @param array $arguments */ @@ -42,21 +42,21 @@ function with_arguments(mixed $callable, array $arguments): WithArguments } } -if (! function_exists('Sirius\StackRunner\limit_arguments')) { +if (! function_exists('Sirius\Invokator\limit_arguments')) { function limit_arguments(mixed $callable, int $argumentsLimit): LimitArguments { return new LimitArguments($callable, $argumentsLimit); } } -if (! function_exists('Sirius\StackRunner\once')) { +if (! function_exists('Sirius\Invokator\once')) { function once(mixed $callable): Once { return new Once($callable); } } -if (! function_exists('Sirius\StackRunner\wrap')) { +if (! function_exists('Sirius\Invokator\wrap')) { /** * @param callable $wrapperCallback */ diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 29d1fc4..ebdda95 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -23,7 +23,7 @@ - + ./src/ diff --git a/tests/src/Event/DispatcherTest.php b/tests/src/Event/DispatcherTest.php index ee1f90f..a5c326b 100644 --- a/tests/src/Event/DispatcherTest.php +++ b/tests/src/Event/DispatcherTest.php @@ -1,8 +1,8 @@ getInvoker()); + $processor = new SimpleCallablesProcessor($this->getInvoker()); $processor->add('test', once(function ($param_1, $param_2) { static::$results[] = sprintf("anonymous function(%s, %s)", $param_1, $param_2); })); diff --git a/tests/src/Modifiers/WithArgumentsTest.php b/tests/src/Modifiers/WithArgumentsTest.php index 97579e0..8188cf7 100644 --- a/tests/src/Modifiers/WithArgumentsTest.php +++ b/tests/src/Modifiers/WithArgumentsTest.php @@ -1,13 +1,13 @@ getContainer()->register('test_param', 'C'); - $processor = new SimpleStackProcessor($this->getInvoker()); + $processor = new SimpleCallablesProcessor($this->getInvoker()); $processor->add('test', with_arguments(function ($param_1, $param_2, $param_3, $param_4) { static::$results[] = sprintf("anonymous function(%s, %s, %s, %s)", $param_1, $param_2, $param_3, $param_4); }, [arg(1), arg(0), ref('test_param'), 'D'])); @@ -37,7 +37,7 @@ public function test_modifier_with_refs() public function test_modifier_with_invoker_result() { $this->getContainer()->register('test_param', 'C'); - $processor = new SimpleStackProcessor($this->getInvoker()); + $processor = new SimpleCallablesProcessor($this->getInvoker()); $processor->add('test', with_arguments(function ($param_1, $param_2, $param_3) { static::$results[] = sprintf("anonymous function(%s, %s, %s)", $param_1, $param_2, $param_3); }, [result_of('trim', [' C ']), arg(1), arg(0)])); diff --git a/tests/src/Modifiers/WrapTest.php b/tests/src/Modifiers/WrapTest.php index 91d19b9..11e2de9 100644 --- a/tests/src/Modifiers/WrapTest.php +++ b/tests/src/Modifiers/WrapTest.php @@ -1,9 +1,9 @@ getContainer()->register(SimpleCallables::class, new SimpleCallables); - $processor = new SimpleStackProcessor($this->getInvoker()); + $processor = new SimpleCallablesProcessor($this->getInvoker()); $processor->add('test', wrap(function ($param_1, $param_2) { static::$results[] = sprintf("anonymous function(%s, %s)", $param_1, $param_2); }, function ($next) { diff --git a/tests/src/Processors/ActionsProcessorTest.php b/tests/src/Processors/ActionsProcessorTest.php index 8d4dc82..352b581 100644 --- a/tests/src/Processors/ActionsProcessorTest.php +++ b/tests/src/Processors/ActionsProcessorTest.php @@ -1,9 +1,9 @@ getContainer()->register(SimpleCallables::class, new SimpleCallables); - $processor = new SimpleStackProcessor($this->getInvoker()); + $processor = new SimpleCallablesProcessor($this->getInvoker()); $processor->add('test', function ($param_1, $param_2) { static::$results[] = sprintf("anonymous function(%s, %s)", $param_1, $param_2); }); @@ -29,7 +29,7 @@ public function test_simple_stack_processor() public function test_execution_priority() { $this->getContainer()->register(SimpleCallables::class, new SimpleCallables); - $processor = new SimpleStackProcessor($this->getInvoker()); + $processor = new SimpleCallablesProcessor($this->getInvoker()); $processor->add('test', function ($param_1, $param_2) { static::$results[] = sprintf("anonymous function(%s, %s)", $param_1, $param_2); }); diff --git a/tests/src/StackTest.php b/tests/src/StackTest.php index 3e8920b..39d72b1 100644 --- a/tests/src/StackTest.php +++ b/tests/src/StackTest.php @@ -1,12 +1,12 @@ add('callable_1', 10); $stack->add('callable_2', 10); $stack->add('callable_3', 100); @@ -26,7 +26,7 @@ function test_priorities_are_respected() public function test_serialization_of_simple_stack() { - $stack = new Stack(); + $stack = new CallableCollection(); $stack->add('callable_1', 10); $stack->add('callable_2', 10); $stack->add('callable_3', 100); diff --git a/tests/src/TestCase.php b/tests/src/TestCase.php index ec222e7..1257179 100644 --- a/tests/src/TestCase.php +++ b/tests/src/TestCase.php @@ -1,6 +1,6 @@ getContainer()); } - protected function getCallablesFromStack(Stack $stack) + protected function getCallablesFromStack(CallableCollection $stack) { $callables = []; do { diff --git a/tests/src/Utilities/SimpleCallables.php b/tests/src/Utilities/SimpleCallables.php index 268db48..48e69d4 100644 --- a/tests/src/Utilities/SimpleCallables.php +++ b/tests/src/Utilities/SimpleCallables.php @@ -1,9 +1,9 @@