-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #286 from dotkernel/issue-282
Issue #282: Show current page as active/open in left menu
- Loading branch information
Showing
9 changed files
with
129 additions
and
25 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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
$(document).ready(function () { | ||
$('[data-toggle="tooltip"]').tooltip(); | ||
|
||
$('.current-route').closest('.nav-group').addClass('open'); | ||
}); |
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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Admin\App\Twig\Extension; | ||
|
||
use Dot\DependencyInjection\Attribute\Inject; | ||
use Mezzio\Helper\UrlHelper; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFunction; | ||
|
||
class RouteExtension extends AbstractExtension | ||
{ | ||
#[Inject(UrlHelper::class)] | ||
public function __construct( | ||
private readonly UrlHelper $urlHelper, | ||
) { | ||
} | ||
|
||
public function getFunctions(): array | ||
{ | ||
return [ | ||
new TwigFunction('getCurrentRoute', [$this, 'getCurrentRoute']), | ||
]; | ||
} | ||
|
||
public function getCurrentRoute(): ?string | ||
{ | ||
return $this->urlHelper->getRequest()?->getUri()?->getPath(); | ||
} | ||
} |
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,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AdminTest\Unit\App\Twig\Extension; | ||
|
||
use Admin\App\Twig\Extension\RouteExtension; | ||
use AdminTest\Unit\UnitTest; | ||
use Laminas\Diactoros\ServerRequest; | ||
use Laminas\Diactoros\Uri; | ||
use Mezzio\Helper\UrlHelper; | ||
use Mezzio\Router\RouterInterface; | ||
use PHPUnit\Framework\MockObject\Exception; | ||
use Twig\TwigFunction; | ||
|
||
use function method_exists; | ||
|
||
class RouteExtensionTest extends UnitTest | ||
{ | ||
/** | ||
* @throws Exception | ||
*/ | ||
public function testWillInstantiate(): void | ||
{ | ||
$urlHelper = $this->createMock(UrlHelper::class); | ||
$routeExtension = new RouteExtension($urlHelper); | ||
$this->assertInstanceOf(RouteExtension::class, $routeExtension); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function testWillAddExistingFunctions(): void | ||
{ | ||
$routeExtension = new RouteExtension( | ||
$this->createMock(UrlHelper::class) | ||
); | ||
|
||
$functions = $routeExtension->getFunctions(); | ||
$this->assertCount(1, $functions); | ||
|
||
$twigFunction = $functions[0]; | ||
$this->assertInstanceOf(TwigFunction::class, $twigFunction); | ||
|
||
$callable = $twigFunction->getCallable(); | ||
$this->assertIsArray($callable); | ||
$this->assertCount(2, $callable); | ||
$this->assertInstanceOf(RouteExtension::class, $callable[0]); | ||
$this->assertTrue(method_exists($routeExtension, $callable[1])); | ||
$this->assertSame($twigFunction->getName(), $callable[1]); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function testWillGetCurrentRoute(): void | ||
{ | ||
$router = $this->createMock(RouterInterface::class); | ||
$request = new ServerRequest(uri: new Uri('/test')); | ||
$urlHelper = new UrlHelper($router); | ||
$urlHelper->setRequest($request); | ||
$routeExtension = new RouteExtension($urlHelper); | ||
$this->assertSame('/test', $routeExtension->getCurrentRoute()); | ||
} | ||
} |