PHP code example of tmilos / context

1. Go to this page and download the library: Download tmilos/context library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tmilos / context example snippets

 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' => []]]
 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