diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/SimpleController.php b/src/Symfony/Bundle/FrameworkBundle/Controller/SimpleController.php new file mode 100644 index 0000000000000..922129b8d220c --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/SimpleController.php @@ -0,0 +1,43 @@ + + * + * 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 + */ +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; + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/SimpleControllerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/SimpleControllerTest.php new file mode 100644 index 0000000000000..bd4fdf4bb6677 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/SimpleControllerTest.php @@ -0,0 +1,36 @@ + + * + * 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 + */ +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')); + } +}