-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
228 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Twig; | ||
|
||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Twig\Extension\RuntimeExtensionInterface; | ||
|
||
class AppRuntime implements RuntimeExtensionInterface | ||
{ | ||
public function __construct(private UrlGeneratorInterface $urlGenerator, private RequestStack $request) | ||
{ | ||
} | ||
|
||
public function loginTargetPath(): string | ||
{ | ||
if ($masterRequest = $this->request->getCurrentRequest()) { | ||
return $this->urlGenerator->generate('security.login', ['redirect' => $masterRequest->getRequestUri()]); | ||
} | ||
|
||
return $this->urlGenerator->generate('security.login'); | ||
} | ||
} |
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,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Twig; | ||
|
||
use App\Entity\Category; | ||
use App\Entity\Forum; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Twig\Extension\RuntimeExtensionInterface; | ||
|
||
class BreadcrumbRuntime implements RuntimeExtensionInterface | ||
{ | ||
private array $breadcrumbsPaths = []; | ||
|
||
public function __construct(private UrlGeneratorInterface $urlGenerator) | ||
{ | ||
} | ||
|
||
public function appendBreadcrumb(string $label, ?string $path = null): void | ||
{ | ||
$pair = [$label, $path]; | ||
if (!in_array($pair, $this->breadcrumbsPaths, true)) { | ||
$this->breadcrumbsPaths[] = $pair; | ||
} | ||
} | ||
|
||
public function getBreadcrumbs(): array | ||
{ | ||
return $this->breadcrumbsPaths; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getBreadcrumbParts(Category|Forum $item, array &$parts = []): array | ||
{ | ||
if ($item instanceof Category) { | ||
$url = $this->urlGenerator->generate('category.show', ['slug' => $item->getSlug()]); | ||
} else { | ||
$url = $this->urlGenerator->generate('forum.show', ['slug' => $item->getSlug()]); | ||
} | ||
|
||
array_unshift($parts, ['url' => $url, 'title' => $item->getTitle()]); | ||
|
||
if ($item instanceof Forum && (($parent = $item->getParent()) || ($parent = $item->getCategory()))) { | ||
return $this->getBreadcrumbParts($parent, $parts); | ||
} | ||
|
||
return $parts; | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Twig; | ||
|
||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFunction; | ||
|
||
class UserExtension extends AbstractExtension | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getFunctions(): array | ||
{ | ||
return [ | ||
new TwigFunction('user_profile_link', [UserRuntime::class, 'getUserProfileLink'], ['is_safe' => ['html']]), | ||
new TwigFunction('user_profile_role', [UserRuntime::class, 'getUserProfileRole']), | ||
]; | ||
} | ||
} |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Tests\Unit\Twig; | ||
|
||
use App\Twig\AppExtension; | ||
use PHPUnit\Framework\TestCase; | ||
use Twig\TwigFunction; | ||
|
||
class AppExtensionTest extends TestCase | ||
{ | ||
public function testGetFunctions(): void | ||
{ | ||
$functions = (new AppExtension())->getFunctions(); | ||
static::assertContainsOnlyInstancesOf(TwigFunction::class, $functions); | ||
} | ||
} |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Tests\Unit\Twig; | ||
|
||
use App\Twig\BreadcrumbExtension; | ||
use PHPUnit\Framework\TestCase; | ||
use Twig\TwigFunction; | ||
|
||
class BreadcrumbExtensionTest extends TestCase | ||
{ | ||
public function testGetFunctions(): void | ||
{ | ||
$functions = (new BreadcrumbExtension())->getFunctions(); | ||
static::assertContainsOnlyInstancesOf(TwigFunction::class, $functions); | ||
} | ||
} |
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,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Tests\Unit\Twig; | ||
|
||
use App\Entity\Category; | ||
use App\Entity\Forum; | ||
use App\Twig\BreadcrumbRuntime; | ||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | ||
|
||
class BreadcrumbRuntimeTest extends WebTestCase | ||
{ | ||
private BreadcrumbRuntime $runtime; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->runtime = static::getContainer()->get(BreadcrumbRuntime::class); | ||
} | ||
|
||
public function testAppendBreadcrumb(): void | ||
{ | ||
static::assertCount(0, $this->runtime->getBreadcrumbs()); | ||
$this->runtime->appendBreadcrumb('Home', '/'); | ||
static::assertCount(1, $this->runtime->getBreadcrumbs()); | ||
$this->runtime->appendBreadcrumb('About', '/about'); | ||
static::assertCount(2, $this->runtime->getBreadcrumbs()); | ||
|
||
$this->runtime->appendBreadcrumb('About', '/about'); | ||
static::assertCount(2, $this->runtime->getBreadcrumbs()); | ||
} | ||
|
||
public function testGetBreadcrumbs(): void | ||
{ | ||
static::assertIsArray($this->runtime->getBreadcrumbs()); | ||
} | ||
|
||
public function testGetBreadcrumbParts(): void | ||
{ | ||
$category = (new Category()) | ||
->setTitle('Category') | ||
->setSlug('lorem') | ||
; | ||
|
||
$forum = (new Forum()) | ||
->setTitle('Forum') | ||
->setSlug('forum') | ||
->setCategory($category) | ||
; | ||
|
||
$categoryParts = $this->runtime->getBreadcrumbParts($category); | ||
static::assertIsArray($categoryParts); | ||
static::assertCount(1, $categoryParts); | ||
static::assertArrayHasKey('url', $categoryParts[0]); | ||
static::assertArrayHasKey('title', $categoryParts[0]); | ||
|
||
$forumParts = $this->runtime->getBreadcrumbParts($forum); | ||
static::assertIsArray($forumParts); | ||
static::assertCount(2, $forumParts); | ||
static::assertArrayHasKey('url', $forumParts[0]); | ||
static::assertArrayHasKey('title', $forumParts[0]); | ||
static::assertArrayHasKey('url', $forumParts[1]); | ||
static::assertArrayHasKey('title', $forumParts[1]); | ||
} | ||
} |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Tests\Unit\Twig; | ||
|
||
use App\Twig\UserExtension; | ||
use PHPUnit\Framework\TestCase; | ||
use Twig\TwigFunction; | ||
|
||
class UserExtensionTest extends TestCase | ||
{ | ||
public function testGetFunctions(): void | ||
{ | ||
$functions = (new UserExtension())->getFunctions(); | ||
static::assertContainsOnlyInstancesOf(TwigFunction::class, $functions); | ||
} | ||
} |