Skip to content

[FrameworkBundle] Add a controller to send simple HTTP responses #30514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
43 changes: 43 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Controller/SimpleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Controller;

use Symfony\Component\HttpFoundation\Response;

/**
* Generates a simple HTTP Response.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
final class SimpleController
{
public function __invoke(string $content = '', int $status = 200, array $headers = [], int $maxAge = null, int $sharedAge = null, bool $private = null): Response
{
$response = new Response($content, $status, $headers);

if ($maxAge) {
$response->setMaxAge($maxAge);
}

if ($sharedAge) {
$response->setSharedMaxAge($sharedAge);
}

if ($private) {
$response->setPrivate();
} elseif (false === $private || (null === $private && ($maxAge || $sharedAge))) {
$response->setPublic();
}

return $response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Controller;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Controller\SimpleController;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class SimpleControllerTest extends TestCase
{
public function testSimpleResponse()
{
$response = (new SimpleController())('content', 203, ['Content-Type' => 'text/plain'], 10, 20);
$this->assertSame('content', $response->getContent());
$this->assertSame(203, $response->getStatusCode());
$this->assertSame('text/plain', $response->headers->get('Content-Type'));
$this->assertSame('max-age=10, public, s-maxage=20', $response->headers->get('Cache-Control'));
}

public function testPrivateResponse()
{
$response = (new SimpleController())('', 200, [], null, null, true);
$this->assertSame('private', $response->headers->get('Cache-Control'));
}
}