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 2eecbd4
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 69 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -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
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
@@ -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
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
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);
}
45 changes: 22 additions & 23 deletions src/Pipeline.php
Expand Up @@ -12,7 +12,7 @@
use Twig_Test as TwigTest;
use Twig_TokenParserInterface as TokenParserInterface;

final class Pipeline implements ContextInterface, ExtensionInterface
final class Pipeline implements ExtensionInterface, PipelineInterface
{
/** @var Context */
private $context;
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
@@ -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);
}
8 changes: 0 additions & 8 deletions tests/ContextTest.php
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
3 changes: 2 additions & 1 deletion tests/PipelineTest.php
Expand Up @@ -67,8 +67,9 @@ public function testWithContextShouldClearContext()
});

$pipeline = new Pipeline([$middleware]);
$context = new Context(['string_attribute' => 'value']);

$pipeline->withContext(['string_attribute' => 'value'], function () use ($pipeline, $view) {
$pipeline->withContext($context, function () use ($pipeline, $view) {
$pipeline->process($view);
});

Expand Down

0 comments on commit 2eecbd4

Please sign in to comment.