Skip to content

Commit

Permalink
Upgrade for Equip 3.0-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMasterov committed Nov 2, 2016
1 parent b183125 commit 0e655ff
Show file tree
Hide file tree
Showing 26 changed files with 252 additions and 526 deletions.
7 changes: 4 additions & 3 deletions .travis.yml
Expand Up @@ -12,7 +12,7 @@ git:

env:
global:
- PHPUNIT_VERSION="^5.4"
- PHPUNIT_VERSION="^5.5"

matrix:
fast_finish: true
Expand All @@ -21,16 +21,17 @@ matrix:
env:
- COMPOSER_FLAGS="--prefer-stable"
- php: 7
- php: 7.1
- php: hhvm
allow_failures:
- php: hhvm

before_script:
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-dist
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source
- travis_retry composer require phpunit/phpunit:${PHPUNIT_VERSION}

script:
- vendor/bin/phpunit
- vendor/bin/phpunit --report-useless-tests

after_script:
- bash -c '[[ -f "build/logs/clover.xml" ]] && wget https://scrutinizer-ci.com/ocular.phar'
Expand Down
128 changes: 41 additions & 87 deletions README.md
Expand Up @@ -17,16 +17,26 @@ composer require alexmasterov/equip-twig
```

## Configuration
To use the [`TwigFormatter`](https://github.com/AlexMasterov/equip-twig/blob/master/src/TwigFormatter.php) implementation you need to add [`TwigResponderConfiguration`](https://github.com/AlexMasterov/equip-twig/blob/master/src/Configuration/TwigResponderConfiguration.php) into the [application bootstrap](https://equipframework.readthedocs.org/en/latest/#bootstrap):
To use the [`TwigFormatter`](https://github.com/AlexMasterov/equip-twig/blob/master/src/TwigFormatter.php) implementation you need to add [`TwigConfiguration`](https://github.com/AlexMasterov/equip-twig/blob/master/src/Configuration/TwigResponderConfiguration.php) into the [application bootstrap](https://equipframework.readthedocs.org/en/latest/#bootstrap):
```php
Equip\Application::build()
->setConfiguration([
// ...
AlexMasterov\EquipTwig\Configuration\TwigResponderConfiguration::class
AlexMasterov\EquipTwig\Configuration\TwigConfiguration::class
])
// ...
```
### Setting up the Twig environment:
[Optional configuration](https://github.com/equip/framework/blob/master/docs/index.md#setting-the-env-file), via a `.env` file:
```shell
TWIG_TEMPLATES = "../Resources/templates"
TWIG_CACHE = "../var/cache/twig"
TWIG_DEBUG = false
TWIG_AUTO_RELOAD = true
TWIG_STRICT_VARIABLES = false
TWIG_FILE_EXTENSIONS = "html.twig,twig"

```
[Default configuration](https://github.com/equip/framework/blob/master/docs/index.md#dependency-injection-container), via dependency injector:
```php
// src/Configuration/TwigConfiguration.php
Expand All @@ -35,9 +45,9 @@ namespace Acme\Configuration;
use Auryn\Injector;
use Equip\Env;
use Equip\Configuration\ConfigurationInterface;
use AlexMasterov\EquipTwig\Configuration\TwigResponderConfiguration;
use AlexMasterov\EquipTwig\Configuration\TwigConfiguration;

class TwigConfiguration implements ConfigurationInterface
class TwigEnvConfiguration implements ConfigurationInterface
{
public function apply(Injector $injector)
{
Expand All @@ -50,7 +60,7 @@ class TwigConfiguration implements ConfigurationInterface
'TWIG_FILE_EXTENSIONS' => 'html.twig,twig'
]);

$injector->define(TwigResponderConfiguration::class, [
$injector->define(TwigConfiguration::class, [
':env' => $twigEnv
]);
}
Expand All @@ -60,126 +70,70 @@ class TwigConfiguration implements ConfigurationInterface
Equip\Application::build()
->setConfiguration([
// ...
Acme\Configuration\TwigConfiguration::class,
AlexMasterov\EquipTwig\Configuration\TwigResponderConfiguration::class
Acme\Configuration\TwigEnvConfiguration::class,
AlexMasterov\EquipTwig\Configuration\TwigConfiguration::class
])
// ...
```
[Optional configuration](https://github.com/equip/framework/blob/master/docs/index.md#setting-the-env-file), via a `.env` file:
```shell
TWIG_TEMPLATES = "../Resources/templates"
TWIG_CACHE = "../var/cache/twig"
TWIG_DEBUG = false
TWIG_AUTO_RELOAD = true
TWIG_STRICT_VARIABLES = false
TWIG_FILE_EXTENSIONS = "html.twig,twig"

```
### Extensions
[`TwigDefaultExtension`](https://github.com/AlexMasterov/equip-twig/blob/master/src/Configuration/TwigDefaultExtension.php) — provides a Equip specific extensions.

| Variable | Description |
|------------|-------------------------------------------------------------------|
| [`session`](https://github.com/equip/framework/blob/master/docs/session.md#usage) | Provides access to an object instance of [`SessionInterface`]( https://github.com/equip/session/blob/master/src/SessionInterface.php)

### Adding extensions
The easiest way to add an extensions is by using the [`TwigExtensionSet`](https://github.com/AlexMasterov/equip-twig/blob/master/src/Configuration/TwigExtensionSet.php) as in the example below:
```php
// src/Configuration/ExtraTwigExtension.php
namespace Acme\Configuration;

use AlexMasterov\EquipTwig\Configuration\TwigExtensionSet;
use AlexMasterov\EquipTwig\Configuration\TwigDefaultExtension;

class ExtraTwigExtension extends TwigExtensionSet
class AppTwigExtension extends TwigExtensionSet
{
public function __construct()
{
$defaults = (new TwigDefaultExtension)->toArray();
$extra = [
AwesomeExtension::class,
AmazingExtension::class
];

parent::__construct(array_merge($defaults, $extra));
parent::__construct([
AppExtension::class
]);
}
}
```
```php
Equip\Application::build()
->setConfiguration([
// ...
AlexMasterov\EquipTwig\Configuration\TwigResponderConfiguration::class,
Acme\Configuration\ExtraTwigExtension::class
AlexMasterov\EquipTwig\Configuration\TwigConfiguration::class,
Acme\Configuration\AppTwigExtension::class
])
// ...
```
It\`s also possible to expand [`TwigDefaultExtension`](https://github.com/AlexMasterov/equip-twig/blob/master/src/Configuration/TwigDefaultExtension.php) following example of the [Default configuration](#setting-up-the-twig-environment):
```php
// ...
use AlexMasterov\EquipTwig\Configuration\TwigDefaultExtension;

class TwigConfiguration implements ConfigurationInterface
{
public function apply(Injector $injector)
{
// ...
$extensions = [
AwesomeExtension::class,
AmazingExtension::class
];

$injector->define(TwigDefaultExtension::class, [
':extensions' => $extensions
]);
}
}
```
## Usage
Basic example:
```php
namespace Acme\Domain;
namespace Acme\Action;

use Equip\Adr\DomainInterface;
use Equip\Adr\PayloadInterface;
use AlexMasterov\EquipTwig\TwigFormatter;
use Equip\Contract\ActionInterface;

class WidgetDomain implements DomainInterface
class DoItAction implements ActionInterface
{
/**
* @var PayloadInterface
*/
private $payload;
private $formatter;

public function __construct(PayloadInterface $payload)
public function __construct(TwigFormatter $formatter)
{
$this->payload = $payload;
$this->formatter = $formatter;
}

public function __invoke(array $input)
{
return $this->payload
->withStatus(PayloadInterface::STATUS_OK)
->withSetting('template', 'widget.html.twig')
->withOutput([
public function __invoke(
ServerRequestInterface $request,
ResponseInterface $response
) {
$response = $response->withHeader('Content-Type', $formatter->type());
$payload = $this->formatter
->withTemplate('doit')
->format([
'message' => 'Just do it!'
]);
}
}
```

Using [`PayloadRenderTrait`](https://github.com/AlexMasterov/equip-twig/blob/master/src/Traits/PayloadRenderTrait.php) as wrapper for the usual `render` method:
```php
// ...
use AlexMasterov\EquipTwig\Traits\PayloadRenderTrait;
$body = $response->getBody();
$body->write($payload);

class WidgetDomain implements DomainInterface
{
use PayloadRenderTrait;

public function __invoke(array $input)
{
$message = 'Just do it!';
return $this->render('widget.html.twig', compact('message'));
return $response;
}
}
```
6 changes: 3 additions & 3 deletions composer.json
Expand Up @@ -16,8 +16,8 @@
}],
"require": {
"php": "^5.5.9 || ^7.0",
"equip/framework": "^2.0 || ^2.1",
"twig/twig": "^1.25 || ^2.0"
"equip/framework": "3.0.0-alpha1",
"twig/twig": "^1.27"
},
"autoload": {
"psr-4": {
Expand All @@ -26,7 +26,7 @@
},
"autoload-dev": {
"psr-4": {
"AlexMasterov\\EquipTwig\\Tests\\": "tests/"
"AlexMasterov\\EquipTwigTests\\": "tests/"
}
},
"suggest": {
Expand Down
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Expand Up @@ -15,8 +15,8 @@
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<logging>
Expand Down
Expand Up @@ -7,10 +7,10 @@
use Auryn\Injector;
use Equip\Configuration\ConfigurationInterface;
use Equip\Configuration\EnvTrait;
use Equip\Responder\FormattedResponder;
use Equip\Env;
use Twig_Environment;

final class TwigResponderConfiguration implements ConfigurationInterface
final class TwigConfiguration implements ConfigurationInterface
{
use EnvTrait;

Expand All @@ -19,42 +19,29 @@ final class TwigResponderConfiguration implements ConfigurationInterface
*/
public function apply(Injector $injector)
{
$injector->prepare(FormattedResponder::class, function(FormattedResponder $responder) {
return $responder->withValue(TwigFormatter::class, 1.0);
});

$injector->define(FilesystemLoader::class, [
':path' => $this->env->getValue('TWIG_TEMPLATES'),
':fileExtensions' => $this->getEnvFileExtensions()
]);
$env = $this->env;
$options = $this->options($env);

$injector->define(Twig_Environment::class, [
'loader' => FilesystemLoader::class,
':options' => $this->getEnvOptions()
':options' => $options
]);
}

/**
* @return array
*/
public function getEnvFileExtensions()
{
$fileExtensions = $this->env->getValue('TWIG_FILE_EXTENSIONS', ['html.twig', 'twig']);

if (is_string($fileExtensions)) {
$fileExtensions = explode(',', $fileExtensions);
}
list($path,$fileExtensions) = $this->filesystemConfig($env);

return $fileExtensions;
$injector->define(FilesystemLoader::class, [
':path' => $path,
':fileExtensions' => $fileExtensions
]);
}

/**
* @param Env $env
*
* @return array Configuration options from environment variables
*/
public function getEnvOptions()
private function options(Env $env)
{
$env = $this->env;

$options = [
'debug' => $env->getValue('TWIG_DEBUG', false),
'auto_reload' => $env->getValue('TWIG_AUTO_RELOAD', true),
Expand All @@ -64,4 +51,21 @@ public function getEnvOptions()

return $options;
}

/**
* @param Env $env
*
* @return array
*/
private function filesystemConfig(Env $env)
{
$path = $env->getValue('TWIG_TEMPLATES');
$fileExtensions = $env->getValue('TWIG_FILE_EXTENSIONS', ['html.twig', 'twig']);

if (is_string($fileExtensions)) {
$fileExtensions = explode(',', $fileExtensions);
}

return [$path, $fileExtensions];
}
}
25 changes: 0 additions & 25 deletions src/Configuration/TwigDefaultExtension.php

This file was deleted.

6 changes: 4 additions & 2 deletions src/Configuration/TwigExtensionSet.php
Expand Up @@ -26,8 +26,10 @@ public function apply(Injector $injector)
*
* @return void
*/
public function prepareExtension(Twig_Environment $environment, Injector $injector)
{
public function prepareExtension(
Twig_Environment $environment,
Injector $injector
) {
$extensions = $this->toArray();

if ($environment->isDebug()) {
Expand Down

0 comments on commit 0e655ff

Please sign in to comment.