Skip to content

Commit

Permalink
Merge pull request #34 from kalibora/support-redirect
Browse files Browse the repository at this point in the history
Support redirect
  • Loading branch information
koriym authored May 21, 2019
2 parents a525cc7 + 20cae91 commit 6c73406
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/Annotation/TwigRedirectPath.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* This file is part of the Madapaja.TwigModule package.
*
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Madapaja\TwigModule\Annotation;

use Ray\Di\Di\Qualifier;

/**
* @Annotation
* @Target("METHOD")
* @Qualifier
*/
final class TwigRedirectPath
{
public $value;
}
7 changes: 7 additions & 0 deletions src/TwigModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Madapaja\TwigModule\Annotation\TwigLoader;
use Madapaja\TwigModule\Annotation\TwigOptions;
use Madapaja\TwigModule\Annotation\TwigPaths;
use Madapaja\TwigModule\Annotation\TwigRedirectPath;
use Ray\Di\AbstractModule;
use Ray\Di\Scope;
use Twig\Environment;
Expand Down Expand Up @@ -52,6 +53,7 @@ protected function configure()
$this->bindTwigEnvironment();
$this->bindTwigPaths();
$this->bindTwigOptions();
$this->bindTwigRedirectPath();
}

private function bindRender()
Expand Down Expand Up @@ -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);
Expand Down
39 changes: 37 additions & 2 deletions src/TwigRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use BEAR\Resource\Code;
use BEAR\Resource\RenderInterface;
use BEAR\Resource\ResourceObject;
use Madapaja\TwigModule\Annotation\TwigRedirectPath;
use Ray\Aop\WeavedInterface;
use Twig\Environment;
use Twig\Error\LoaderError;
Expand All @@ -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;
}

Expand All @@ -46,7 +56,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;
}
Expand All @@ -65,6 +82,13 @@ private function renderView(ResourceObject $ro)
return $template ? $template->render($this->buildBody($ro)) : '';
}

private function renderRedirectView(ResourceObject $ro)
{
$url = $ro->headers['Location'];

return $this->twig->render($this->redirectPage, ['url' => $url]);
}

/**
* @return null|\Twig\TemplateWrapper
*/
Expand All @@ -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();
Expand Down
1 change: 1 addition & 0 deletions tests/Fake/src/Resource/Page/Redirect.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo is {{ foo }}.
30 changes: 30 additions & 0 deletions tests/Fake/src/Resource/Page/Redirect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* This file is part of the Madapaja.TwigModule package.
*
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Madapaja\TwigModule\Resource\Page;

use BEAR\Resource\Code;
use BEAR\Resource\ResourceObject;

class Redirect extends ResourceObject
{
public function onGet()
{
$this->body = [
'foo' => 'bar',
];

return $this;
}

public function onPost()
{
$this->code = Code::FOUND;
$this->headers['Location'] = '/path/to/baz';

return $this;
}
}
11 changes: 11 additions & 0 deletions tests/Fake/src/Resource/redirect/redirect.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="refresh" content="0;url={{ url }}" />
<title>Redirecting to {{ url }}</title>
</head>
<body>
Redirecting to <a href="{{ url }}">{{ url }}</a>.
</body>
</html>
22 changes: 22 additions & 0 deletions tests/FileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
}
}
11 changes: 11 additions & 0 deletions var/templates/redirect/redirect.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="refresh" content="0;url={{ url }}" />
<title>Redirecting to {{ url }}</title>
</head>
<body>
Redirecting to <a href="{{ url }}">{{ url }}</a>.
</body>
</html>

0 comments on commit 6c73406

Please sign in to comment.