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 data providers and DTO #48

Merged
merged 2 commits into from
Dec 17, 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
12 changes: 6 additions & 6 deletions spec/Page/PageSpec.php → spec/Page/PageInfoSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,38 @@
use Mobizel\Bundle\MarkdownDocsBundle\Template\TemplateHandlerInterface;
use PhpSpec\ObjectBehavior;

class PageSpec extends ObjectBehavior
class PageInfoSpec extends ObjectBehavior
{
function it_can_get_page_title(): void
{
$this->beConstructedWith('tests/docs/foo.md');
$this->beConstructedWith('tests/docs/foo.md', '', '');
$this->getTitle()->shouldReturn('Foo fighters');
}

function it_return_default_title_when_no_title_has_been_found(TemplateHandlerInterface $templateHandler): void
{
$this->beConstructedWith('tests/docs/bar.md');
$this->beConstructedWith('tests/docs/bar.md', '', '');

$this->getTitle()->shouldReturn('Bar');
}

function it_return_default_title_when_file_is_empty(TemplateHandlerInterface $templateHandler): void
{
$this->beConstructedWith('tests/docs/empty.md');
$this->beConstructedWith('tests/docs/empty.md', '', '');

$this->getTitle()->shouldReturn('Empty');
}

function it_can_get_content(): void
{
$this->beConstructedWith('tests/docs/foo.md');
$this->beConstructedWith('tests/docs/foo.md', '', '');

$this->getContent()->shouldContain('# Foo fighters');
}

function it_can_get_content_without_title(): void
{
$this->beConstructedWith('tests/docs/foo.md');
$this->beConstructedWith('tests/docs/foo.md', '', '');

$this->getContentWithoutTitle()->shouldNotContain('# Foo fighters');
}
Expand Down
52 changes: 11 additions & 41 deletions src/Controller/MenuAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,24 @@

namespace Mobizel\Bundle\MarkdownDocsBundle\Controller;

use Mobizel\Bundle\MarkdownDocsBundle\Page\PageSorter;
use Mobizel\Bundle\MarkdownDocsBundle\DataProvider\PageCollectionDataProviderInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Finder\Exception\DirectoryNotFoundException;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

final class MenuAction extends AbstractController
{
/** @var string */
private $docsDir;
/** @var PageCollectionDataProviderInterface */
private $pageCollectionDataProvider;

public function __construct(string $docsDir)
public function __construct(PageCollectionDataProviderInterface $pageCollectionDataProvider)
{
$this->docsDir = $docsDir;
$this->pageCollectionDataProvider = $pageCollectionDataProvider;
}

public function __invoke(Request $request): Response
{
$menuItems = $this->getMenuItems($request);
$menuItems = $this->getMenuItems();
$currentSubmenuItems = $this->getCurrentSubmenuItems($request);

return $this->render('@MobizelMarkdownDocs/layout/menu.html.twig', [
Expand All @@ -42,26 +40,12 @@ public function __invoke(Request $request): Response
]);
}

private function getMenuItems(Request $request): array
private function getMenuItems(): iterable
{
$finder = new Finder();

$finder->files()->in($this->docsDir)->depth(0)->notName('index.md')->sort(PageSorter::sortByTitle());

$menuItems = [];

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

return $menuItems;
return $this->pageCollectionDataProvider->getRootPages();
}

private function getCurrentSubmenuItems(Request $request): array
private function getCurrentSubmenuItems(Request $request): iterable
{
$currentItem = $request->query->get('current_item');
$currentSubmenuItems = [];
Expand All @@ -70,22 +54,8 @@ private function getCurrentSubmenuItems(Request $request): array
return $currentSubmenuItems;
}

$rootSlug = explode('/', $currentItem)[0];
$finder = new Finder();
try {
$finder->files()->in($this->docsDir.'/'.$rootSlug)->depth(0)->sort(PageSorter::sortByTitle());
} catch (DirectoryNotFoundException $exception) {
return [];
}

foreach ($finder as $file) {
$slug = $rootSlug.'/'.preg_replace('/\.md$/', '', $file->getRelativePathName());
$currentSubmenuItems[] = [
'slug' => $slug,
'path' => $this->generateUrl('mobizel_markdown_docs_page_show', ['slug' => $slug]),
];
}
$parentSlug = explode('/', $currentItem)[0];

return $currentSubmenuItems;
return $this->pageCollectionDataProvider->getChildrenPages($parentSlug);
}
}
31 changes: 12 additions & 19 deletions src/Controller/PageAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,19 @@

namespace Mobizel\Bundle\MarkdownDocsBundle\Controller;

use Mobizel\Bundle\MarkdownDocsBundle\Page\Page;
use Mobizel\Bundle\MarkdownDocsBundle\Template\TemplateHandlerInterface;
use Mobizel\Bundle\MarkdownDocsBundle\DataProvider\PageItemDataProvider;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Twig\Error\LoaderError;

final class PageAction extends AbstractController
{
/** @var TemplateHandlerInterface */
private $templateHandler;
/** @var PageItemDataProvider */
private $pageItemDataProvider;

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

public function __invoke(string $slug): Response
Expand All @@ -38,19 +36,14 @@ public function __invoke(string $slug): Response
return $this->redirectToRoute('mobizel_markdown_docs_page_show', ['slug' => $slug]);
}

try {
$templatePath = $this->templateHandler->getTemplateAbsolutePath($slug);
$page = $this->pageItemDataProvider->getPage($slug);

if (!is_file($templatePath)) {
throw new NotFoundHttpException(sprintf('Template %s does not exist', $templatePath));
}

return $this->render('@MobizelMarkdownDocs/page/show.html.twig', [
'slug' => $slug,
'page' => new Page($templatePath),
]);
} catch (LoaderError $exception) {
throw new NotFoundHttpException($exception->getMessage());
if (null === $page) {
throw new NotFoundHttpException(sprintf('Page "%s" was not found', $slug));
}

return $this->render('@MobizelMarkdownDocs/page/show.html.twig', [
'page' => $page,
]);
}
}
102 changes: 102 additions & 0 deletions src/DataProvider/PageCollectionDataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?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\DataProvider;

use Mobizel\Bundle\MarkdownDocsBundle\Dto\PageOutput;
use Mobizel\Bundle\MarkdownDocsBundle\Page\PageInfo;
use Mobizel\Bundle\MarkdownDocsBundle\Page\PageSorter;
use Symfony\Component\Finder\Exception\DirectoryNotFoundException;
use Symfony\Component\Finder\Finder;

final class PageCollectionDataProvider implements PageCollectionDataProviderInterface
{
/** @var string */
private $docsDir;

public function __construct(string $docsDir)
{
$this->docsDir = $docsDir;
}

public function getRootPages(): iterable
{
$finder = new Finder();

$finder
->files()
->in($this->docsDir)
->notName('index.md')
->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[] = $this->createPage(
$slug,
$pageInfo->getTitle(),
$pageInfo->getContentWithoutTitle()
);
}

return $pages;
}

public function getChildrenPages(string $parentSlug): iterable
{
$finder = new Finder();

try {
$finder
->files()
->in($this->docsDir.'/'.$parentSlug)
->notName('index.md')
->depth(0)
->sort(PageSorter::sortByTitle());
} catch (DirectoryNotFoundException $exception) {
return [];
}

$pages = [];

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

$slug = $parentSlug.'/'.preg_replace('/\.md$/', '', $file->getRelativePathName());

$pages[] = $this->createPage(
$slug,
$pageInfo->getTitle(),
$pageInfo->getContentWithoutTitle()
);
}

return $pages;
}

private function createPage(string $slug, string $title, string $content): PageOutput
{
$page = new PageOutput();
$page->slug = $slug;
$page->title = $title;
$page->content = $content;

return $page;
}
}
21 changes: 21 additions & 0 deletions src/DataProvider/PageCollectionDataProviderInterface.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\DataProvider;

interface PageCollectionDataProviderInterface
{
public function getRootPages(): iterable;

public function getChildrenPages(string $parentSlug): iterable;
}
56 changes: 56 additions & 0 deletions src/DataProvider/PageItemDataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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\DataProvider;

use Mobizel\Bundle\MarkdownDocsBundle\Dto\PageOutput;
use Mobizel\Bundle\MarkdownDocsBundle\Page\PageInfo;
use Mobizel\Bundle\MarkdownDocsBundle\Template\TemplateHandlerInterface;

final class PageItemDataProvider
{
/** @var TemplateHandlerInterface */
private $templateHandler;

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

public function getPage(string $slug): ?PageOutput
{
$templateAbsolutePath = $this->templateHandler->getTemplateAbsolutePath($slug);

if (!is_file($templateAbsolutePath)) {
return null;
}

$pageInfo = new PageInfo($templateAbsolutePath, dirname($slug), $this->templateHandler->getTemplatePath($slug));

return $this->createPage(
$slug,
$pageInfo->getTitle(),
$pageInfo->getContentWithoutTitle(),
);
}

private function createPage(string $slug, string $title, string $content): PageOutput
{
$page = new PageOutput();
$page->slug = $slug;
$page->title = $title;
$page->content = $content;

return $page;
}
}
18 changes: 18 additions & 0 deletions src/DataProvider/PageItemDataProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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\DataProvider;

interface PageItemDataProviderInterface
{
}
Loading