From c9ac8c41830be0fa5de894a8be67d9456f4561dc Mon Sep 17 00:00:00 2001 From: Toshiyuki FUJITA Date: Fri, 19 Apr 2019 18:52:41 +0900 Subject: [PATCH 1/3] support redirect --- src/TwigRenderer.php | 37 ++++++++++++++++++- .../Fake/src/Resource/Page/Redirect.html.twig | 1 + tests/Fake/src/Resource/Page/Redirect.php | 30 +++++++++++++++ tests/FileLoaderTest.php | 22 +++++++++++ 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 tests/Fake/src/Resource/Page/Redirect.html.twig create mode 100644 tests/Fake/src/Resource/Page/Redirect.php diff --git a/src/TwigRenderer.php b/src/TwigRenderer.php index 62f8921..d26bf7a 100644 --- a/src/TwigRenderer.php +++ b/src/TwigRenderer.php @@ -46,7 +46,14 @@ public function __construct(Environment $twig, TemplateFinderInterface $template public function render(ResourceObject $ro) { $this->setContentType($ro); - $ro->view = $this->isNoContent($ro) ? '' : $this->renderView($ro); + + if ($this->isNoContent($ro)) { + $ro->view = ''; + } elseif ($this->isRedirect($ro)) { + $ro->view = $this->renderRedirectView($ro); + } else { + $ro->view = $this->renderView($ro); + } return $ro->view; } @@ -65,6 +72,23 @@ private function renderView(ResourceObject $ro) return $template ? $template->render($this->buildBody($ro)) : ''; } + private function renderRedirectView(ResourceObject $ro) + { + $url = $ro->headers['Location']; + + return sprintf(' + + + + + Redirecting to %1$s + + + Redirecting to %1$s. + +', htmlspecialchars($url, ENT_QUOTES, 'UTF-8')); + } + /** * @return null|\Twig\TemplateWrapper */ @@ -84,6 +108,17 @@ private function isNoContent(ResourceObject $ro) : bool return $ro->code === Code::NO_CONTENT || $ro->view === ''; } + private function isRedirect(ResourceObject $ro) : bool + { + return in_array($ro->code, [ + Code::MOVED_PERMANENTLY, + Code::FOUND, + Code::SEE_OTHER, + Code::TEMPORARY_REDIRECT, + Code::PERMANENT_REDIRECT, + ], true) && isset($ro->headers['Location']); + } + private function loadTemplate(ResourceObject $ro) : TemplateWrapper { $loader = $this->twig->getLoader(); diff --git a/tests/Fake/src/Resource/Page/Redirect.html.twig b/tests/Fake/src/Resource/Page/Redirect.html.twig new file mode 100644 index 0000000..77f5bf4 --- /dev/null +++ b/tests/Fake/src/Resource/Page/Redirect.html.twig @@ -0,0 +1 @@ +foo is {{ foo }}. diff --git a/tests/Fake/src/Resource/Page/Redirect.php b/tests/Fake/src/Resource/Page/Redirect.php new file mode 100644 index 0000000..48d1749 --- /dev/null +++ b/tests/Fake/src/Resource/Page/Redirect.php @@ -0,0 +1,30 @@ +body = [ + 'foo' => 'bar', + ]; + + return $this; + } + + public function onPost() + { + $this->code = Code::FOUND; + $this->headers['Location'] = '/path/to/baz'; + + return $this; + } +} diff --git a/tests/FileLoaderTest.php b/tests/FileLoaderTest.php index 7cfa4e6..c31147d 100644 --- a/tests/FileLoaderTest.php +++ b/tests/FileLoaderTest.php @@ -10,6 +10,7 @@ use Madapaja\TwigModule\Resource\Page\Index; use Madapaja\TwigModule\Resource\Page\NoTemplate; use Madapaja\TwigModule\Resource\Page\Page; +use Madapaja\TwigModule\Resource\Page\Redirect; use PHPUnit_Framework_TestCase; use Ray\Di\Injector; @@ -122,4 +123,25 @@ public function testIndexTemplateWithoutPaths() $ro->onGet(); $this->assertSame('Hello, BEAR.Sunday!', \trim((string) $ro)); } + + public function testNoRedirectOnGet() + { + $injector = $this->getInjector(); + $ro = $injector->getInstance(Redirect::class); + $ro->onGet(); + + $this->assertSame(Code::OK, $ro->code); + $this->assertSame('foo is bar.', \trim((string) $ro)); + } + + public function testRedirectOnPost() + { + $injector = $this->getInjector(); + $ro = $injector->getInstance(Redirect::class); + $ro->onPost(); + + $this->assertSame(Code::FOUND, $ro->code); + $this->assertSame('/path/to/baz', $ro->headers['Location']); + $this->assertRegexp('#^.*Redirecting to /path/to/baz.*$#s', \trim((string) $ro)); + } } From 4bd4ddd7c67c27eae07b8d45925166a0db99173e Mon Sep 17 00:00:00 2001 From: Toshiyuki FUJITA Date: Sat, 20 Apr 2019 19:19:50 +0900 Subject: [PATCH 2/3] Inject redirect page path --- src/Annotation/TwigRedirectPath.php | 19 +++++++++++++++ src/TwigModule.php | 7 ++++++ src/TwigRenderer.php | 24 +++++++++---------- .../src/Resource/redirect/redirect.html.twig | 11 +++++++++ var/templates/redirect/redirect.html.twig | 11 +++++++++ 5 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 src/Annotation/TwigRedirectPath.php create mode 100644 tests/Fake/src/Resource/redirect/redirect.html.twig create mode 100644 var/templates/redirect/redirect.html.twig diff --git a/src/Annotation/TwigRedirectPath.php b/src/Annotation/TwigRedirectPath.php new file mode 100644 index 0000000..79bdd2e --- /dev/null +++ b/src/Annotation/TwigRedirectPath.php @@ -0,0 +1,19 @@ +bindTwigEnvironment(); $this->bindTwigPaths(); $this->bindTwigOptions(); + $this->bindTwigRedirectPath(); } private function bindRender() @@ -116,6 +118,11 @@ private function bindTwigOptions() $this->bind()->annotatedWith(TwigOptions::class)->toProvider(OptionProvider::class); } + private function bindTwigRedirectPath() + { + $this->bind()->annotatedWith(TwigRedirectPath::class)->toInstance('/redirect/redirect.html.twig'); + } + private function isNotEmpty($var) { return \is_array($var) && ! empty($var); diff --git a/src/TwigRenderer.php b/src/TwigRenderer.php index d26bf7a..70a8c4c 100644 --- a/src/TwigRenderer.php +++ b/src/TwigRenderer.php @@ -10,6 +10,7 @@ use BEAR\Resource\RenderInterface; use BEAR\Resource\ResourceObject; use Ray\Aop\WeavedInterface; +use Madapaja\TwigModule\Annotation\TwigRedirectPath; use Twig\Environment; use Twig\Error\LoaderError; use Twig\Loader\FilesystemLoader; @@ -29,14 +30,23 @@ class TwigRenderer implements RenderInterface */ public $twig; + /** + * @var string + */ + private $redirectPage; + /** * @var TemplateFinderInterface */ private $templateFinder; - public function __construct(Environment $twig, TemplateFinderInterface $templateFinder = null) + /** + * @TwigRedirectPath("redirectPage") + */ + public function __construct(Environment $twig, string $redirectPage, TemplateFinderInterface $templateFinder = null) { $this->twig = $twig; + $this->redirectPage = $redirectPage; $this->templateFinder = $templateFinder ?: new TemplateFinder; } @@ -76,17 +86,7 @@ private function renderRedirectView(ResourceObject $ro) { $url = $ro->headers['Location']; - return sprintf(' - - - - - Redirecting to %1$s - - - Redirecting to %1$s. - -', htmlspecialchars($url, ENT_QUOTES, 'UTF-8')); + return $this->twig->render($this->redirectPage, ['url' => $url]); } /** diff --git a/tests/Fake/src/Resource/redirect/redirect.html.twig b/tests/Fake/src/Resource/redirect/redirect.html.twig new file mode 100644 index 0000000..da82d89 --- /dev/null +++ b/tests/Fake/src/Resource/redirect/redirect.html.twig @@ -0,0 +1,11 @@ + + + + + + Redirecting to {{ url }} + + + Redirecting to {{ url }}. + + diff --git a/var/templates/redirect/redirect.html.twig b/var/templates/redirect/redirect.html.twig new file mode 100644 index 0000000..da82d89 --- /dev/null +++ b/var/templates/redirect/redirect.html.twig @@ -0,0 +1,11 @@ + + + + + + Redirecting to {{ url }} + + + Redirecting to {{ url }}. + + From 20cae91577bb54a5434066c05a734b9cf853f5b7 Mon Sep 17 00:00:00 2001 From: Toshiyuki FUJITA Date: Sat, 20 Apr 2019 19:25:10 +0900 Subject: [PATCH 3/3] cs-fix --- src/TwigRenderer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/TwigRenderer.php b/src/TwigRenderer.php index 70a8c4c..4953481 100644 --- a/src/TwigRenderer.php +++ b/src/TwigRenderer.php @@ -9,8 +9,8 @@ use BEAR\Resource\Code; use BEAR\Resource\RenderInterface; use BEAR\Resource\ResourceObject; -use Ray\Aop\WeavedInterface; use Madapaja\TwigModule\Annotation\TwigRedirectPath; +use Ray\Aop\WeavedInterface; use Twig\Environment; use Twig\Error\LoaderError; use Twig\Loader\FilesystemLoader; @@ -110,7 +110,7 @@ private function isNoContent(ResourceObject $ro) : bool private function isRedirect(ResourceObject $ro) : bool { - return in_array($ro->code, [ + return \in_array($ro->code, [ Code::MOVED_PERMANENTLY, Code::FOUND, Code::SEE_OTHER,