Skip to content

Commit

Permalink
Context is now interfaced, in preparation of Shoot/Http
Browse files Browse the repository at this point in the history
  • Loading branch information
victorwelling committed Feb 1, 2018
1 parent 343fce1 commit d351da0
Show file tree
Hide file tree
Showing 17 changed files with 90 additions and 103 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog
All notable changes to Shoot will be documented in this file.

## [0.3.0] - 2018-02-01
- Context is now interfaced, in preparation of the Shoot/Http package. This package will make use of Shoot in an HTTP
context (PSR-7 and PSR-15) easier.

## [0.2.1] - 2018-01-16
- Shoot handles embedded templates by passing through all variables from the parent template.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ available to all middleware and presenters. You'll see how it's used further dow

```php
$app->get('/posts/{post_id}', function ($request, $response) {
$context = [ServerRequestInterface::class => $request];
$context = new Context([ServerRequestInterface::class => $request]);

return $this
->get(Pipeline::class)
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "shoot/shoot",
"description": "Shoot extends Twig with presentation models, presenters and a middleware interface",
"description": "Shoot brings inversion of control to loading template data for Twig",
"type": "library",
"keywords": [
"twig",
Expand Down Expand Up @@ -32,6 +32,9 @@
"require-dev": {
"phpunit/phpunit": "^6.5"
},
"suggest": {
"shoot/http": "Makes use of Shoot in an HTTP context more convenient"
},
"autoload": {
"psr-4": {
"Shoot\\Shoot\\": "src/"
Expand Down
10 changes: 1 addition & 9 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* Provides the context in which middleware processes a view.
*/
final class Context
final class Context implements ContextInterface
{
/** @var mixed[] */
private $attributes;
Expand All @@ -29,12 +29,4 @@ public function getAttribute(string $name, $default = null)
{
return $this->attributes[$name] ?? $default;
}

/**
* @return mixed[] All attributes available in the context.
*/
public function getAttributes(): array
{
return $this->attributes;
}
}
30 changes: 4 additions & 26 deletions src/ContextInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,13 @@

namespace Shoot\Shoot;

/**
* Provides an interface to apply a context to the pipeline as it processes a view.
*/
interface ContextInterface
{
/**
* Apply the given context attributes to the pipeline.
* @param string $name The name of the attribute.
* @param mixed $default A default value is the attribute does not exist.
*
* @param mixed[] $context
*
* @return void
*/
public function applyContext(array $context);

/**
* Clear the current context.
*
* @return void
*/
public function clearContext();

/**
* Applies the given context to the pipeline, executes the given callback, and clears the context. This method
* exists merely for convenience.
*
* @param mixed[] $context
* @param callable $callback
*
* @return mixed The result as returned by the callback (if any).
* @return mixed The value of the attribute, or the default if it does not exist.
*/
public function withContext(array $context, callable $callback);
public function getAttribute(string $name, $default = null);
}
10 changes: 5 additions & 5 deletions src/Middleware/InspectorMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Shoot\Shoot\Middleware;

use Shoot\Shoot\Context;
use Shoot\Shoot\ContextInterface;
use Shoot\Shoot\HasPresenterInterface;
use Shoot\Shoot\MiddlewareInterface;
use Shoot\Shoot\PresentationModel;
Expand All @@ -16,13 +16,13 @@
final class InspectorMiddleware implements MiddlewareInterface
{
/**
* @param View $view The view to be processed by this middleware.
* @param Context $context The context in which to process the view.
* @param callable $next The next middleware to call
* @param View $view The view to be processed by this middleware.
* @param ContextInterface $context The context in which to process the view.
* @param callable $next The next middleware to call
*
* @return View The processed view.
*/
public function process(View $view, Context $context, callable $next): View
public function process(View $view, ContextInterface $context, callable $next): View
{
$this->script();
$this->view($view);
Expand Down
10 changes: 5 additions & 5 deletions src/Middleware/LoggingMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace Shoot\Shoot\Middleware;

use Psr\Log\LoggerInterface;
use Shoot\Shoot\Context;
use Shoot\Shoot\ContextInterface;
use Shoot\Shoot\MiddlewareInterface;
use Shoot\Shoot\View;

Expand All @@ -25,13 +25,13 @@ public function __construct(LoggerInterface $logger)
}

/**
* @param View $view The view to be processed by this middleware.
* @param Context $context The context in which to process the view.
* @param callable $next The next middleware to call
* @param View $view The view to be processed by this middleware.
* @param ContextInterface $context The context in which to process the view.
* @param callable $next The next middleware to call
*
* @return View The processed view.
*/
public function process(View $view, Context $context, callable $next): View
public function process(View $view, ContextInterface $context, callable $next): View
{
/** @var View $view */
$startTime = microtime(true);
Expand Down
11 changes: 5 additions & 6 deletions src/Middleware/PresenterMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Shoot\Shoot\Context;
use Shoot\Shoot\ContextInterface;
use Shoot\Shoot\HasPresenterInterface;
use Shoot\Shoot\MiddlewareInterface;
use Shoot\Shoot\PresenterInterface;
Expand All @@ -28,15 +28,14 @@ public function __construct(ContainerInterface $container)
}

/**
* @param View $view The view to be processed by this middleware.
* @param Context $context The context in which to process the view.
* @param callable $next The next middleware to call
* @param View $view The view to be processed by this middleware.
* @param ContextInterface $context The context in which to process the view.
* @param callable $next The next middleware to call
*
* @throws ContainerExceptionInterface
*
* @return View The processed view.
*/
public function process(View $view, Context $context, callable $next): View
public function process(View $view, ContextInterface $context, callable $next): View
{
$presentationModel = $view->getPresentationModel();

Expand Down
8 changes: 4 additions & 4 deletions src/MiddlewareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
interface MiddlewareInterface
{
/**
* @param View $view The view to be processed by this middleware.
* @param Context $context The context in which to process the view.
* @param callable $next The next middleware to call
* @param View $view The view to be processed by this middleware.
* @param ContextInterface $context The context in which to process the view.
* @param callable $next The next middleware to call
*
* @return View The processed view.
*/
public function process(View $view, Context $context, callable $next): View;
public function process(View $view, ContextInterface $context, callable $next): View;
}
47 changes: 23 additions & 24 deletions src/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
use Twig_Test as TwigTest;
use Twig_TokenParserInterface as TokenParserInterface;

final class Pipeline implements ContextInterface, ExtensionInterface
final class Pipeline implements ExtensionInterface, PipelineInterface
{
/** @var Context */
/** @var ContextInterface */
private $context;

/** @var callable */
Expand Down Expand Up @@ -53,45 +53,44 @@ private function chainMiddleware(array $middleware): callable
}

/**
* Apply the given context attributes to the pipeline.
* Applies the given context to the pipeline, executes the given callback, and clears the context.
*
* @param mixed[] $context
* @param ContextInterface $context
* @param callable $callback
*
* @return void
* @return mixed The result as returned by the callback (if any).
*/
public function applyContext(array $context)
public function withContext(ContextInterface $context, callable $callback)
{
$this->context = new Context($context);
try {
$this->applyContext($context);

return $callback();
} finally {
$this->clearContext();
}
}

/**
* Clear the current context.
* Apply the given context attributes to the pipeline.
*
* @param ContextInterface $context
*
* @return void
*/
public function clearContext()
private function applyContext(ContextInterface $context)
{
$this->applyContext([]);
$this->context = $context;
}

/**
* Applies the given context to the pipeline, executes the given callback, and clears the context. This method
* exists merely for convenience.
*
* @param mixed[] $context
* @param callable $callback
* Clear the current context.
*
* @return mixed The result as returned by the callback (if any).
* @return void
*/
public function withContext(array $context, callable $callback)
private function clearContext()
{
try {
$this->applyContext($context);

return $callback();
} finally {
$this->clearContext();
}
$this->applyContext(new Context());
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/PipelineInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);

namespace Shoot\Shoot;

interface PipelineInterface
{
/**
* Applies the given context to the pipeline, executes the given callback, and clears the context.
*
* @param ContextInterface $context
* @param callable $callback
*
* @return mixed The result as returned by the callback (if any).
*/
public function withContext(ContextInterface $context, callable $callback);
}
4 changes: 2 additions & 2 deletions src/PresenterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
interface PresenterInterface
{
/**
* @param Context $context
* @param ContextInterface $context
* @param PresentationModel $presentationModel
*
* @return PresentationModel
*/
public function present(Context $context, PresentationModel $presentationModel): PresentationModel;
public function present(ContextInterface $context, PresentationModel $presentationModel): PresentationModel;
}
8 changes: 0 additions & 8 deletions tests/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ public function testGetAttributeShouldReturnGivenDefaultValueIfAttributeDoesNotE
$this->assertSame('default', $this->context->getAttribute('non_existing_attribute', 'default'));
}

/**
* @return void
*/
public function testGetAttributesShouldReturnAllAttributes()
{
$this->assertCount(2, $this->context->getAttributes());
}

/**
* @return void
*/
Expand Down
6 changes: 3 additions & 3 deletions tests/Fixtures/ItemListPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@

namespace Shoot\Shoot\Tests\Fixtures;

use Shoot\Shoot\Context;
use Shoot\Shoot\ContextInterface;
use Shoot\Shoot\PresentationModel;
use Shoot\Shoot\PresenterInterface;

final class ItemListPresenter implements PresenterInterface
{
/**
* @param Context $context
* @param ContextInterface $context
* @param PresentationModel $presentationModel
*
* @return PresentationModel
*/
public function present(Context $context, PresentationModel $presentationModel): PresentationModel
public function present(ContextInterface $context, PresentationModel $presentationModel): PresentationModel
{
return $presentationModel->withVariables([
'items' => [
Expand Down
6 changes: 3 additions & 3 deletions tests/Fixtures/ItemPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Shoot\Shoot\Tests\Fixtures;

use Shoot\Shoot\Context;
use Shoot\Shoot\ContextInterface;
use Shoot\Shoot\HasDataTrait;
use Shoot\Shoot\PresentationModel;
use Shoot\Shoot\PresenterInterface;
Expand All @@ -13,12 +13,12 @@ final class ItemPresenter implements PresenterInterface
use HasDataTrait;

/**
* @param Context $context
* @param ContextInterface $context
* @param PresentationModel $presentationModel
*
* @return PresentationModel
*/
public function present(Context $context, PresentationModel $presentationModel): PresentationModel
public function present(ContextInterface $context, PresentationModel $presentationModel): PresentationModel
{
if (!$this->hasData($presentationModel)) {
$presentationModel = $presentationModel->withVariables([
Expand Down
10 changes: 5 additions & 5 deletions tests/Fixtures/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Shoot\Shoot\Tests\Fixtures;

use Shoot\Shoot\Context;
use Shoot\Shoot\ContextInterface;
use Shoot\Shoot\MiddlewareInterface;
use Shoot\Shoot\View;

Expand All @@ -21,13 +21,13 @@ public function __construct(callable $callable = null)
}

/**
* @param View $view
* @param Context $context
* @param callable $next
* @param View $view
* @param ContextInterface $context
* @param callable $next
*
* @return View
*/
public function process(View $view, Context $context, callable $next = null): View
public function process(View $view, ContextInterface $context, callable $next = null): View
{
if ($next === null) {
$next = function (View $view): View {
Expand Down
Loading

0 comments on commit d351da0

Please sign in to comment.