Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pages map #49

Merged
merged 2 commits into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"erusev/parsedown": "^1.7",
"erusev/parsedown-extra": "^0.8.1",
"symfony/asset": "^4.4|^5.1",
"symfony/expression-language": "^4.4|^5.1",
"symfony/framework-bundle": "^4.4|^5.1",
"symfony/http-kernel": "^4.4|^5.1",
"symfony/routing": "^4.4|^5.1",
Expand Down
27 changes: 13 additions & 14 deletions spec/Helper/PageHelperSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,38 @@
namespace spec\Mobizel\Bundle\MarkdownDocsBundle\Helper;

use Mobizel\Bundle\MarkdownDocsBundle\Helper\PageHelper;
use Mobizel\Bundle\MarkdownDocsBundle\Template\TemplateHandlerInterface;
use PhpSpec\ObjectBehavior;

class PageHelperSpec extends ObjectBehavior
{
function let(TemplateHandlerInterface $templateHandler): void
function let(): void
{
$this->beConstructedWith($templateHandler);
$this->beConstructedWith([
'first-page' => 'First page',
'foo' => 'Foo fighters',
'last-page' => 'Last page',
]);
}

function it_is_initializable(): void
{
$this->shouldHaveType(PageHelper::class);
}

function it_can_get_page_title(TemplateHandlerInterface $templateHandler): void
function it_can_get_page_title(): void
{
$templateHandler->getTemplateAbsolutePath('foo')->willReturn('tests/docs/foo.md');

$this->getTitle('foo')->shouldReturn('Foo fighters');
}

function it_return_default_title_when_no_title_has_been_found(TemplateHandlerInterface $templateHandler): void
function it_can_get_previous_page(): void
{
$templateHandler->getTemplateAbsolutePath('bar')->willReturn('tests/docs/bar.md');

$this->getTitle('bar')->shouldReturn('Bar');
$this->getPreviousPage('foo')->shouldReturn('first-page');
$this->getPreviousPage('first-page')->shouldReturn(null);
}

function it_return_default_title_when_file_is_empty(TemplateHandlerInterface $templateHandler): void
function it_can_get_next_page(): void
{
$templateHandler->getTemplateAbsolutePath('empty')->willReturn('tests/docs/empty.md');

$this->getTitle('empty')->shouldReturn('Empty');
$this->getNextPage('foo')->shouldReturn('last-page');
$this->getNextPage('last-page')->shouldReturn(null);
}
}
5 changes: 3 additions & 2 deletions src/Controller/PageAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ final class PageAction extends AbstractController
/** @var PageItemDataProvider */
private $pageItemDataProvider;

public function __construct(PageItemDataProvider $pageItemDataProvider)
{
public function __construct(
PageItemDataProvider $pageItemDataProvider
) {
$this->pageItemDataProvider = $pageItemDataProvider;
}

Expand Down
23 changes: 23 additions & 0 deletions src/DataProvider/PageCollectionDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,29 @@ public function __construct(string $docsDir)
$this->docsDir = $docsDir;
}

public function getPagesMap(): array
{
$finder = new Finder();

$finder
->files()
->in($this->docsDir)
//->depth(0)
->sort(PageSorter::sortByTitle());
$pages = [];

foreach ($finder as $file) {
$pageInfo = new PageInfo($file->getPathname(), $file->getRelativePath(), $file->getRelativePathname());

/** @var string $slug */
$slug = preg_replace('/\.md$/', '', $pageInfo->getRelativePathName());

$pages[$slug] = $pageInfo->getTitle();
}

return $pages;
}

public function getRootPages(): iterable
{
$finder = new Finder();
Expand Down
2 changes: 2 additions & 0 deletions src/DataProvider/PageCollectionDataProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

interface PageCollectionDataProviderInterface
{
public function getPagesMap(): array;

public function getRootPages(): iterable;

public function getChildrenPages(string $parentSlug): iterable;
Expand Down
45 changes: 36 additions & 9 deletions src/Helper/PageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,51 @@

namespace Mobizel\Bundle\MarkdownDocsBundle\Helper;

use Mobizel\Bundle\MarkdownDocsBundle\Page\PageInfo;
use Mobizel\Bundle\MarkdownDocsBundle\Template\TemplateHandlerInterface;
use Webmozart\Assert\Assert;

final class PageHelper implements PageHelperInterface
{
/** @var TemplateHandlerInterface */
private $templateHandler;
/** @var array */
private $pagesMap;

public function __construct(TemplateHandlerInterface $templateHandler)
public function __construct(array $pagesMap)
{
$this->templateHandler = $templateHandler;
$this->pagesMap = $pagesMap;
}

public function getTitle(string $slug): string
{
$path = $this->templateHandler->getTemplateAbsolutePath($slug);
$page = new PageInfo($path, '', '');
return $this->pagesMap[$slug] ?? '';
}

public function getPreviousPage(string $slug): ?string
{
$currentPosition = $this->getCurrentPosition($slug);

return $this->getPageFromPosition($currentPosition - 1);
}

public function getNextPage(string $slug): ?string
{
$currentPosition = $this->getCurrentPosition($slug);

return $this->getPageFromPosition($currentPosition + 1);
}

private function getCurrentPosition(string $slug): int
{
/** @var int $currentPosition */
$currentPosition = array_search($slug, array_keys($this->pagesMap));
Assert::notFalse($currentPosition, 'Current position was not found');

return $currentPosition;
}

private function getPageFromPosition(int $position): ?string
{
/** @var string|null $slug */
$slug = array_keys($this->pagesMap)[$position] ?? null;

return $page->getTitle();
return $slug;
}
}
4 changes: 4 additions & 0 deletions src/Helper/PageHelperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@
interface PageHelperInterface
{
public function getTitle(string $slug): string;

public function getPreviousPage(string $slug): ?string;

public function getNextPage(string $slug): ?string;
}
7 changes: 5 additions & 2 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<defaults autowire="false" autoconfigure="true" public="true"/>

Expand All @@ -17,7 +16,7 @@
<service id="Mobizel\Bundle\MarkdownDocsBundle\Template\TemplateHandlerInterface" alias="mobizel_markdown_docs.template.template_handler"/>

<service id="mobizel_markdown_docs.helper.page_helper" class="Mobizel\Bundle\MarkdownDocsBundle\Helper\PageHelper">
<argument key="$templateHandler" type="service" id="mobizel_markdown_docs.template.template_handler"/>
<argument key="$pagesMap" type="expression">service('mobizel_markdown_docs.data_provider.page_collection').getPagesMap()</argument>
</service>

<!-- Data Providers -->
Expand All @@ -39,6 +38,10 @@
</service>
<service id="Mobizel\Bundle\MarkdownDocsBundle\Twig\Extension\PageTitleExtensionInterface" alias="mobizel_markdown_docs.twig.extension.page_title"/>

<service id="mobizel_markdown_docs.twig.extension.pagination" class="Mobizel\Bundle\MarkdownDocsBundle\Twig\Extension\PaginationExtension">
<argument key="$pageHelper" type="service" id="mobizel_markdown_docs.helper.page_helper"/>
</service>
<service id="Mobizel\Bundle\MarkdownDocsBundle\Twig\Extension\PaginationExtensionInterface" alias="mobizel_markdown_docs.twig.extension.pagination"/>

<!-- Controllers -->

Expand Down
47 changes: 47 additions & 0 deletions src/Twig/Extension/PaginationExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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\Twig\Extension;

use Mobizel\Bundle\MarkdownDocsBundle\Helper\PageHelperInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

final class PaginationExtension extends AbstractExtension implements PaginationExtensionInterface
{
/** @var PageHelperInterface */
private $pageHelper;

public function __construct(PageHelperInterface $pageHelper)
{
$this->pageHelper = $pageHelper;
}

public function getFunctions()
{
return [
new TwigFunction('previous_page', [$this, 'previousPage']),
new TwigFunction('next_page', [$this, 'nextPage']),
];
}

public function previousPage(string $slug): ?string
{
return $this->pageHelper->getPreviousPage($slug);
}

public function nextPage(string $slug): ?string
{
return $this->pageHelper->getNextPage($slug);
}
}
21 changes: 21 additions & 0 deletions src/Twig/Extension/PaginationExtensionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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\Twig\Extension;

interface PaginationExtensionInterface
{
public function previousPage(string $slug): ?string;

public function nextPage(string $slug): ?string;
}