Skip to content

Commit

Permalink
Merge pull request #18 from victorwelling/master
Browse files Browse the repository at this point in the history
Changes for v2.0
  • Loading branch information
victorwelling authored Mar 4, 2019
2 parents 45ab73f + 04571b3 commit eabe48a
Show file tree
Hide file tree
Showing 17 changed files with 102 additions and 77 deletions.
20 changes: 19 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
# Changelog
All notable changes to Shoot will be documented in this file.

## [2.0.0] - 2019-03-04
### Added
- Documentation on nesting presentation models and the `optional` tag.
- An `Installer` class which sets up Shoot for an instance of Twig.

### Changed
- Shoot now requires PHP 7.2.
- The Twig dependency has been bumped to v2.6.
- PHPUnit has been bumped to v8.0, and all tests have been updated accordingly.
- The `SuppressionMiddleware` is no longer enabled by the default. You'll have to pass it to the `Pipeline` constructor
along with any other middleware you use.
- The `LoggingMiddleware` now also logs errors. Kind of odd it did not do that before.

### Fixed
- The optional tag would still output any contents from before the exception was thrown. This is now fixed.
- Models now work as expected when using extends, embed and blocks – removing what was previously a limitation of Shoot.


## [1.0.0] - 2018-08-27
### Added
- The `optional` tag was added. This allows runtime exceptions to be suppressed so parts which are not essential to the
Expand All @@ -14,7 +32,7 @@ page can be left out in case of failure.
- The `getPresenter` method of `HasPresenterInterface` was renamed to `getPresenterName` as it more accurately describes
its purpose.
- `HasDataTrait` has been moved to a `Utilities` namespace.
- The Twig dependency has been bumped to v2.5
- The Twig dependency has been bumped to v2.5.
- Lots of housekeeping in code and documentation.

### Deprecated
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Shoot is an extension for [Twig][link-twig], a popular template engine for PHP.
your templates more manageable. Think of Shoot as a DI container for template data.

## Prerequisites
Shoot assumes you're using PHP 7 and Twig to render templates in a [PSR-7][link-psr7] HTTP context. It also needs a
Shoot assumes you're using PHP 7.2 and Twig to render templates in a [PSR-7][link-psr7] HTTP context. It also needs a
[PSR-11][link-psr11] compatible DI container.

Although not a requirement, a framework with support for [PSR-15][link-psr15] HTTP middleware does make your life a
Expand Down Expand Up @@ -39,7 +39,7 @@ rendering your templates and Shoot loads the data as needed. Enjoy this ASCII il
```

For this to work, Shoot introduces a few concepts:
* _Presentation models_ – Think of them as data contracts for your templates, ie. _Views_.
* _Presentation models_ – Think of them as data contracts for your templates, i.e. _Views_.
* _Presenters_ – These do the actual work. A presenter is coupled to a specific presentation model, and loads just the
data it needs. These presenters are automatically invoked by Shoot as your templates are rendered.
* _Middleware_ – As each template is rendered, it passes through Shoot's middleware pipeline. Invoking the presenters is
Expand Down
2 changes: 1 addition & 1 deletion src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(Pipeline $pipeline)
*
* @internal
*/
public function process(View $view)
public function process(View $view): void
{
$this->pipeline->process($view);
}
Expand Down
20 changes: 10 additions & 10 deletions src/Middleware/InspectorMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function process(View $view, ServerRequestInterface $request, callable $n
*
* @return void
*/
private function value($value, string $label = '')
private function value($value, string $label = ''): void
{
if (is_scalar($value)) {
$this->scalar($value, $label);
Expand All @@ -57,7 +57,7 @@ private function value($value, string $label = '')
*
* @return void
*/
private function scalar($scalar, string $label = '')
private function scalar($scalar, string $label = ''): void
{
if (is_bool($scalar)) {
$scalar = $scalar ? 'true' : 'false';
Expand All @@ -80,7 +80,7 @@ private function scalar($scalar, string $label = '')
*
* @return void
*/
private function iterable(array $iterable, string $label = '')
private function iterable(array $iterable, string $label = ''): void
{
if ($label === '') {
$label = '...';
Expand All @@ -105,7 +105,7 @@ private function iterable(array $iterable, string $label = '')
*
* @return void
*/
private function object($object, string $label = '')
private function object($object, string $label = ''): void
{
if ($object instanceof PresentationModel) {
$this->presentationModel($object, $label);
Expand All @@ -121,7 +121,7 @@ private function object($object, string $label = '')
*
* @return void
*/
private function view(View $view)
private function view(View $view): void
{
$this->group($view->getName(), false);
$this->presentationModel($view->getPresentationModel(), 'presentationModel');
Expand All @@ -134,7 +134,7 @@ private function view(View $view)
*
* @return void
*/
private function presentationModel(PresentationModel $presentationModel, string $label = '')
private function presentationModel(PresentationModel $presentationModel, string $label = ''): void
{
if ($label === '') {
$label = $presentationModel->getName();
Expand Down Expand Up @@ -167,7 +167,7 @@ private function escape(string $string): string
*
* @return void
*/
private function group(string $label, bool $collapsed = true)
private function group(string $label, bool $collapsed = true): void
{
$label = $this->escape($label);

Expand All @@ -181,23 +181,23 @@ private function group(string $label, bool $collapsed = true)
/**
* @return void
*/
private function groupEnd()
private function groupEnd(): void
{
echo 'console.groupEnd();';
}

/**
* @return void
*/
private function script()
private function script(): void
{
echo '<script>';
}

/**
* @return void
*/
private function scriptEnd()
private function scriptEnd(): void
{
echo '</script>';
}
Expand Down
24 changes: 1 addition & 23 deletions src/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ final class Pipeline
*/
public function __construct(array $middleware = [])
{
$middleware = $this->addSuppressionMiddleware($middleware);

$this->middleware = $this->chainMiddleware($middleware);
}

Expand Down Expand Up @@ -58,7 +56,7 @@ public function withRequest(ServerRequestInterface $request, callable $callback)
*
* @internal
*/
public function process(View $view)
public function process(View $view): void
{
if ($this->request === null) {
throw new MissingRequestException('Cannot process a view without a request set. This method should be called from the callback passed to Pipeline::withRequest');
Expand All @@ -67,26 +65,6 @@ public function process(View $view)
call_user_func($this->middleware, $view);
}

/**
* @param MiddlewareInterface[] $middleware
*
* @return MiddlewareInterface[]
*
* @deprecated 2.0.0 Should not have been default behaviour. Will be removed as of the next major release.
*/
private function addSuppressionMiddleware(array $middleware): array
{
foreach ($middleware as $instance) {
if ($instance instanceof SuppressionMiddleware) {
return $middleware;
}
}

$middleware[] = new SuppressionMiddleware();

return $middleware;
}

/**
* @param MiddlewareInterface[] $middleware
*
Expand Down
2 changes: 1 addition & 1 deletion src/PresentationModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ final public function withVariables(array $variables): self
*
* @return void
*/
private function setVariables(array $variables)
private function setVariables(array $variables): void
{
foreach ($variables as $variable => $value) {
if ($this->variableExists($variable)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Twig/Node/DisplayEndNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(ModuleNode $module)
*
* @return void
*/
public function compile(Compiler $compiler)
public function compile(Compiler $compiler): void
{
if ($this->module->hasAttribute('is_embedded')) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/Twig/Node/DisplayStartNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(ModuleNode $module, FindPresentationModelInterface $
*
* @return void
*/
public function compile(Compiler $compiler)
public function compile(Compiler $compiler): void
{
if ($this->module->hasAttribute('is_embedded')) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/Twig/Node/OptionalNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(Node $body, int $lineNumber, string $tag)
*
* @return void
*/
public function compile(Compiler $compiler)
public function compile(Compiler $compiler): void
{
$runtimeErrorClass = RuntimeError::class;
$suppressedExceptionClass = SuppressedException::class;
Expand Down
2 changes: 1 addition & 1 deletion src/Twig/NodeVisitor/ModelNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function getPriority(): int
*
* @throws LogicException
*/
private function assign(string $view, string $presentationModel)
private function assign(string $view, string $presentationModel): void
{
if (isset($this->presentationModels[$view])) {
throw new ModelAlreadyAssignedException("A presentation model has already been assigned to {$view}");
Expand Down
6 changes: 3 additions & 3 deletions src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct(string $name, PresentationModel $presentationModel,
*
* @internal
*/
public function render()
public function render(): void
{
call_user_func($this->callback, $this->presentationModel->getVariables());
}
Expand Down Expand Up @@ -94,7 +94,7 @@ public function withPresentationModel(PresentationModel $presentationModel): sel
*
* @return Throwable|null
*/
public function getSuppressedException()
public function getSuppressedException(): ?Throwable
{
return $this->suppressedException;
}
Expand All @@ -116,7 +116,7 @@ public function hasSuppressedException(): bool
*
* @return View
*/
public function withSuppressedException(Throwable $exception = null): self
public function withSuppressedException(?Throwable $exception): self
{
$new = clone $this;
$new->suppressedException = $exception;
Expand Down
8 changes: 6 additions & 2 deletions tests/Integration/Composition/CompositionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@

final class CompositionTest extends IntegrationTestCase
{
/** @var string */
protected $templateDirectory = __DIR__ . '/Templates';
protected function setUp(): void
{
$this->setTemplateDirectory(__DIR__ . '/Templates');

parent::setUp();
}

public function testVariablesFilterShouldPassOnModelVariablesToIncludedTemplate(): void
{
Expand Down
4 changes: 1 addition & 3 deletions tests/Integration/Embedding/EmbeddingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ final class EmbeddingTest extends IntegrationTestCase
/** @var PresenterInterface|MockObject */
private $pagePresenter;

/** @var string */
protected $templateDirectory = __DIR__ . '/Templates';

protected function setUp(): void
{
$this->pagePresenter = $this->createMock(PresenterInterface::class);
Expand All @@ -23,6 +20,7 @@ protected function setUp(): void
->will($this->returnArgument(1));

$this->addToContainer('PagePresenter', $this->pagePresenter);
$this->setTemplateDirectory(__DIR__ . '/Templates');

parent::setUp();
}
Expand Down
16 changes: 11 additions & 5 deletions tests/Integration/ErrorSuppression/ErrorSuppressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@

namespace Shoot\Shoot\Tests\Integration\ErrorSuppression;

use Shoot\Shoot\Middleware\SuppressionMiddleware;
use Shoot\Shoot\Tests\Integration\IntegrationTestCase;

final class ErrorSuppressionTest extends IntegrationTestCase
{
/** @var string */
protected $templateDirectory = __DIR__ . '/Templates';
protected function setUp(): void
{
$this->addMiddleware(new SuppressionMiddleware());
$this->setTemplateDirectory(__DIR__ . '/Templates');

parent::setUp();
}

public function testTemplateShouldRenderIfNoExceptionIsThrown(): void
{
Expand All @@ -25,7 +31,7 @@ public function testIncludedTemplateShouldThrowAnException(): void
{
$this->expectExceptionMessage('item_exception');

$this->request
$this->getRequestMock()
->method('getAttribute')
->will($this->returnValueMap([
['throw_logic_exception', 'n', 'n'],
Expand All @@ -37,7 +43,7 @@ public function testIncludedTemplateShouldThrowAnException(): void

public function testOptionalBlocksShouldDiscardTheirContentsOnRuntimeExceptions(): void
{
$this->request
$this->getRequestMock()
->method('getAttribute')
->will($this->returnValueMap([
['throw_logic_exception', 'n', 'n'],
Expand All @@ -57,7 +63,7 @@ public function testOptionalBlocksShouldNotSuppressLogicExceptions(): void
{
$this->expectExceptionMessage('Variable "unknown_variable" does not exist');

$this->request
$this->getRequestMock()
->method('getAttribute')
->will($this->returnValueMap([
['throw_logic_exception', 'n', 'y'],
Expand Down
8 changes: 6 additions & 2 deletions tests/Integration/Inheritance/InheritanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@

final class InheritanceTest extends IntegrationTestCase
{
/** @var string */
protected $templateDirectory = __DIR__ . '/Templates';
protected function setUp(): void
{
$this->setTemplateDirectory(__DIR__ . '/Templates');

parent::setUp();
}

public function testShouldRenderBaseTemplateWithPlaceholders(): void
{
Expand Down
Loading

0 comments on commit eabe48a

Please sign in to comment.