-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 impl custom TwigExtensions and UrlGenerator
- Loading branch information
Showing
9 changed files
with
199 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Routing; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
|
||
/** | ||
* @property ContainerInterface $container | ||
*/ | ||
trait ReturnToAwareControllerTrait | ||
{ | ||
protected function redirectOrReturn(string $url, int $status = 302): RedirectResponse | ||
{ | ||
if ($returnTo = $this->container->get('request_stack')->getCurrentRequest()->query->get('returnTo')) { | ||
return new RedirectResponse($returnTo, $status); | ||
} | ||
|
||
return new RedirectResponse($url, $status); | ||
} | ||
|
||
protected function redirectToRouteOrReturn(string $route, array $parameters = [], int $status = 302): RedirectResponse | ||
{ | ||
return $this->redirectOrReturn($this->container->get('router')->generate($route, $parameters), $status); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Routing; | ||
|
||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
|
||
class ReturnToAwareUrlGenerator | ||
{ | ||
private RequestStack $requestStack; | ||
private UrlGeneratorInterface $generator; | ||
|
||
public function __construct(RequestStack $requestStack, UrlGeneratorInterface $generator) | ||
{ | ||
$this->requestStack = $requestStack; | ||
$this->generator = $generator; | ||
} | ||
|
||
public function generate(string $name, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string | ||
{ | ||
if ($returnTo = $this->requestStack->getCurrentRequest()->query->get('returnTo')) { | ||
return $returnTo; | ||
} | ||
|
||
return $this->generator->generate($name, $parameters, $referenceType); | ||
} | ||
|
||
public function generateWithReturnTo(string $name, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string | ||
{ | ||
$request = $this->requestStack->getCurrentRequest(); | ||
|
||
$parameters = array_merge_recursive($parameters, ['returnTo' => $request->query->get('returnTo') ?? $request->getUri()]); | ||
|
||
return $this->generator->generate($name, $parameters, $referenceType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Twig; | ||
|
||
use App\Security\RoleManager; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFilter; | ||
use Twig\TwigFunction; | ||
|
||
class AppExtension extends AbstractExtension | ||
{ | ||
private RoleManager $rm; | ||
private TranslatorInterface $translator; | ||
private RequestStack $reqestStack; | ||
|
||
public function __construct(RoleManager $rm, TranslatorInterface $translator, RequestStack $requestStack) | ||
{ | ||
$this->rm = $rm; | ||
$this->translator = $translator; | ||
$this->reqestStack = $requestStack; | ||
} | ||
|
||
public function getFunctions() | ||
{ | ||
return [ | ||
new TwigFunction('roles', [$this, 'roles'], ['is_safe' => ['html']]), | ||
]; | ||
} | ||
|
||
public function getFilters() | ||
{ | ||
return [ | ||
new TwigFilter('datetime', [$this, 'datetime']), | ||
]; | ||
} | ||
|
||
public function roles(UserInterface $user): string | ||
{ | ||
$badges = []; | ||
|
||
foreach ($this->rm->getReachableRoles($user) as $role) { | ||
$badges[] = sprintf('<span class="badge badge-secondary">%s</span>', $this->translator->trans($role)); | ||
} | ||
|
||
return implode(' ', $badges); | ||
} | ||
|
||
public function datetime(?\DateTimeInterface $datetime): string | ||
{ | ||
if ($this->reqestStack->getCurrentRequest()->getLocale() === 'ja') { | ||
$days = ['日', '月', '火', '水', '木', '金', '土']; | ||
|
||
return $datetime === null ? '' : sprintf($datetime->format('Y/m/d(%\s) H:i:s'), $days[(int) $datetime->format('w')]); | ||
} | ||
|
||
return $datetime === null ? '' : sprintf($datetime->format('Y/m/d D H:i:s')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Twig; | ||
|
||
use App\Routing\ReturnToAwareUrlGenerator; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFunction; | ||
|
||
class RoutingExtension extends AbstractExtension | ||
{ | ||
private ReturnToAwareUrlGenerator $returnToAwareUrlGenerator; | ||
|
||
public function __construct(ReturnToAwareUrlGenerator $returnToAwareUrlGenerator) | ||
{ | ||
$this->returnToAwareUrlGenerator = $returnToAwareUrlGenerator; | ||
} | ||
|
||
public function getFunctions() | ||
{ | ||
return [ | ||
new TwigFunction('pathOrReturnTo', [$this, 'pathOrReturnTo']), | ||
new TwigFunction('pathWithReturnTo', [$this, 'pathWithReturnTo']), | ||
]; | ||
} | ||
|
||
/** | ||
* @see \Symfony\Bridge\Twig\Extension\RoutingExtension::getPath() | ||
*/ | ||
public function pathOrReturnTo(string $name, array $parameters = [], bool $relative = false) | ||
{ | ||
return $this->returnToAwareUrlGenerator->generate($name, $parameters, $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH); | ||
} | ||
|
||
public function pathWithReturnTo(string $name, array $parameters = [], bool $relative = false) | ||
{ | ||
return $this->returnToAwareUrlGenerator->generateWithReturnTo($name, $parameters, $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters