Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions VariadicController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the Symfony-Util package.
*
* (c) Jean-Bernard Addor
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace SymfonyUtil\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Templating\EngineInterface;

class VariadicController
{
protected $templating;
protected $template;

public function __construct(EngineInterface $templating, $template = 'index.html.twig')
{
$this->templating = $templating;
$this->template = $template;
}

public function __invoke(...$arguments) // PHP 5.6+
{
// Variadic $arguments apparently not supported when empty by Symfony 3.3.6 ArgumentResolver
// and viewed as a regular array by knplabs/rad-resource-resolver v2.1

return new Response($this->templating->render($this->template, $arguments));
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"symfony/templating": "^2.0 || ^3.0 || ^4.0"
},
"require-dev": {
"php": "^5.6",
"symfony/twig-bridge": "^2.0 || ^3.0 || ^4.0",
"symfony/var-dumper": "^2.0 || ^3.0 || ^4.0",
"twig/twig": "^1.0 || ^2.0"
Expand Down
68 changes: 68 additions & 0 deletions tests/VariadicControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the Symfony-Util package.
*
* (c) Jean-Bernard Addor
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Twig\TwigEngine;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Templating\TemplateNameParser;
use SymfonyUtil\Controller\VariadicController;

/**
* @covers \SymfonyUtil\Controller\VariadicController
* @requires PHP 5.6.0
*/
final class VariadicControllerTest extends TestCase
{
public function testCanBeCreated()
{
$this->assertInstanceOf(
// VariadicController::class, // 5.4 < php (code is checked even if not executed!)
'SymfonyUtil\Controller\VariadicController',
new VariadicController(new TwigEngine(
new Twig_Environment(new Twig_Loader_Array(['index.html.twig' => 'Hello World!'])),
new TemplateNameParser()
))
);
}

public function testEmptyReturnsResponse()
{
$this->assertInstanceOf(
// Response::class, // 5.4 < php (code is checked even if not executed!)
'Symfony\Component\HttpFoundation\Response',
(new VariadicController(new TwigEngine(
new Twig_Environment(new Twig_Loader_Array(['index.html.twig' => 'Hello World!'])),
new TemplateNameParser()
)))->__invoke()
);
}

public function testArrayReturnsResponse()
{
$this->assertInstanceOf(
// Response::class, // 5.4 < php (code is checked even if not executed!)
'Symfony\Component\HttpFoundation\Response',
(new VariadicController(new TwigEngine(
new Twig_Environment(new Twig_Loader_Array([
'index.html.twig' => '<ul>{% for item in 0 %}<li>{{ item }}</li>{% endfor %}</ul>',
])),
new TemplateNameParser()
)))->__invoke([
'One',
'Two',
'Three',
])
);
}
}

// http://api.symfony.com/3.3/Symfony/Bridge/Twig/TwigEngine.html
// http://api.symfony.com/3.3/Symfony/Bundle/TwigBundle/TwigEngine.html