Skip to content

Commit

Permalink
Merge pull request #16 from loic425/feature/use-prefix-for-routes
Browse files Browse the repository at this point in the history
Use prefix for routes
  • Loading branch information
loic425 authored Sep 29, 2020
2 parents 2e2a7f7 + 6802827 commit fc246e7
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 18 deletions.
4 changes: 2 additions & 2 deletions spec/Template/TemplateHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ function it_is_initializable(): void

function it_can_get_template_path(): void
{
$this->getTemplatePath('docs/foo')->shouldReturn('docs/foo.md');
$this->getTemplatePath('foo/bar')->shouldReturn('foo/bar.md');
}

function it_can_get_template_absolute_path(): void
{
$this->getTemplateAbsolutePath('docs/foo')->shouldReturn('/path/to/docs/foo.md');
$this->getTemplateAbsolutePath('foo/bar')->shouldReturn('/path/to/docs/foo/bar.md');
}
}
25 changes: 25 additions & 0 deletions src/Controller/IndexAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Mobizel package.
*
* (c) Mobizel
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Mobizel\Bundle\MarkdownDocsBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

final class IndexAction extends AbstractController
{
public function __invoke(): Response
{
return $this->redirectToRoute('mobizel_markdown_docs_page_show', ['slug' => 'index']);
}
}
5 changes: 2 additions & 3 deletions src/Controller/MenuAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ public function __invoke(Request $request): Response
$finder->files()->in($this->docsDir)->depth(0)->notName('index.md');

$menuItems = [];
$docs = basename($this->docsDir);

foreach($finder as $file) {
$slug = $docs.'/'.rtrim($file->getRelativePathName(),'.md');
$slug = rtrim($file->getRelativePathName(),'.md');
$menuItems[] = [
'slug' => $slug,
'path' => '/'.$slug,
'path' => $this->generateUrl('mobizel_markdown_docs_page_show', ['slug' => $slug]),
];
}

Expand Down
12 changes: 5 additions & 7 deletions src/Resources/config/routes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
xsi:schemaLocation="http://symfony.com/schema/routing
http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="mobizel_markdown_docs_index" path="docs" controller="Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction">
<default key="path">/docs/index</default>
</route>

<route id="mobizel_markdown_docs_page_show" path="{slug}" controller="mobizel_markdown_docs.controller.page_action">
<requirement key="slug">docs/.+</requirement>
</route>
<route id="mobizel_markdown_docs_index" path="/" controller="mobizel_markdown_docs.controller.index_action"/>

<route id="mobizel_markdown_docs_search" path="search" controller="mobizel_markdown_docs.controller.search_action">
<requirement key="query">.+</requirement>
</route>

<route id="mobizel_markdown_docs_menu" path="menu" controller="mobizel_markdown_docs.controller.menu_action"/>

<route id="mobizel_markdown_docs_page_show" path="{slug}" controller="mobizel_markdown_docs.controller.page_action">
<requirement key="slug">.+</requirement>
</route>
</routes>
7 changes: 7 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
</service>
<service id="Mobizel\Bundle\MarkdownDocsBundle\Twig\Extension\PageTitleExtensionInterface" alias="mobizel_markdown_docs.twig.extension.page_title"/>

<service id="mobizel_markdown_docs.controller.index_action" class="Mobizel\Bundle\MarkdownDocsBundle\Controller\IndexAction">
<call method="setContainer">
<argument type="service" id="Psr\Container\ContainerInterface"/>
</call>
<tag name="container.service_subscriber"/>
</service>

<service id="mobizel_markdown_docs.controller.search_action" class="Mobizel\Bundle\MarkdownDocsBundle\Controller\SearchAction">
<call method="setContainer">
<argument type="service" id="Psr\Container\ContainerInterface"/>
Expand Down
4 changes: 1 addition & 3 deletions src/Template/TemplateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ public function getTemplatePath(string $slug): string

public function getTemplateAbsolutePath(string $slug): string
{
$pathName = ltrim($this->getTemplatePath($slug), 'docs');

return $this->docsDir.$pathName;
return $this->docsDir.'/'.$this->getTemplatePath($slug);
}
}
6 changes: 3 additions & 3 deletions tests/Controller/PageActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testRedirection()
{
$client = static::createClient();

$client->request('GET', '/docs/index.md');
$client->request('GET', 'index.md');

$this->assertEquals(302, $client->getResponse()->getStatusCode());
}
Expand All @@ -31,7 +31,7 @@ public function testShowPage()
{
$client = static::createClient();

$client->request('GET', '/docs/index');
$client->request('GET', 'index');

$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->assertSelectorTextContains('html h1', 'Documentation');
Expand All @@ -41,7 +41,7 @@ public function testNotFoundPage()
{
$client = static::createClient();

$client->request('GET', '/docs/not-found');
$client->request('GET', 'not-found');

$this->assertEquals(404, $client->getResponse()->getStatusCode());
}
Expand Down

0 comments on commit fc246e7

Please sign in to comment.