tmilos / context
Hierarchical context tree PHP library
Requires
- php: >=5.6.0
Requires (Dev)
- phpunit/phpunit: ~4.8|~5.6
- satooshi/php-coveralls: ^1.0
This package is not auto-updated.
Last update: 2024-12-21 20:57:48 UTC
README
A hierarchical context tree PHP library, with hierarchical parameter bags - context, and actions working with them.
ArrayContext
The ArrayContext
is the default implementation of the Context
interface. It acts as a hierarchical parameter bags, when
items can also be other contexts.
<?php $context = new ArrayContext(); // can set/get/has values like to parameter bag $context->set('foo', 123); $context->get('foo'); // 123 $context->has('foo'); // true // can iterate foreach ($context as $key => $value) { } // can create sub-contexts $subContext = $context->getOrCreate('sub', SomeContext:class); // can callback for creation of sub-context $subContext = $context->getOrCall('sub', function () use ($dependencies) { return new SomeContext($dependencies); }); // can get parent context $subContext->getParent() === $context; // can get root context $leafContext = $subContext->getOrCreate('leaf', ArrayContext:class); $leafContext->getTopParent() === $context; // can dump to array $context->set('bar', ['a' => 1, 'b' => 'x']); $context->toArray(); // ['foo' => 123, 'bar' => ['a' => 1, 'b' => 'x'], 'sub' => ['leaf' => []]]
ExceptionContext
The ExceptionContext
holds single exception, and when other exception is added can chain with other ExceptionContext
.
$context = new ExceptionContext(new \Exception('first')); $context->addException(new \Exception('second')); $context->addException(new \Exception('third')); $context->getException()->getMessage(); // first $context->getLastException()->getMessage(); // third
Action
The Action
interface defines an action that executes on a Context
.
Composite Action
The CompositeAction
compose several actions into one, and when executed calls each child action in the order they were added.
Action Mapper
The ActionMapper
is an invokable interface that gets called to return new instances of actions that will replace the old ones.
Can be used for example to wrap actions to record their execution times and to log details.
<?php $composite = new ArrayCompositeAction(); $composite->add($actionOne); $composite->add($actionTwo); $mapper = new ActionLogWrapper(); // some implementation of the ActionMapper interface $composite->map($mapper); // inner actions gets replaced with return values of the mapper
Catchable Error Action
The CatchableErrorAction
is constructed with two actions. On execute first "main" action is called, and if it throws an
Exception
, an ExceptionContext
is added to the supplied context and second "error" action is called.
Abstract Wrapped Action
The AbstractWrappedAction
is an abstract implementation of the Action
interface, that takes inner action as constructor
argument, and on execute first calls own beforeAction(Context $context)
protected method, then the inner action execute()
method,
and finally own afterAction(Context $context)
.