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

Print action #112

Merged
merged 4 commits into from
Apr 20, 2022
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
38 changes: 38 additions & 0 deletions src/Controller/PrintAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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 Mobizel\Bundle\MarkdownDocsBundle\DataProvider\PageCollectionDataProviderInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

/**
* @psalm-suppress PropertyNotSetInConstructor
*/
final class PrintAction extends AbstractController
{
private PageCollectionDataProviderInterface $pageCollectionDataProvider;

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

public function __invoke(): Response
{
return $this->render('@MobizelMarkdownDocs/print/index.html.twig', [
'pages' => $this->pageCollectionDataProvider->getPages(),
]);
}
}
16 changes: 16 additions & 0 deletions src/DataProvider/PageCollectionDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ public function __construct(ReaderContextInterface $readerContext)
$this->readerContext = $readerContext;
}

public function getPages(): iterable
{
$pages = $this->getRootPages();

foreach ($pages as $page) {
yield $page;

$pages = $this->getChildrenPages($page->slug);
}

foreach ($pages as $page) {
yield $page;
}
}

public function getRootPages(): iterable
{
$docsDir = $this->getDocsDir();
Expand Down Expand Up @@ -116,6 +131,7 @@ public function getPagesMap(): array
++$currentPosition;
}

/** @var array<string, string> $pagesMap */
$pagesMap = [];
foreach ($pages as $row) {
$pagesMap[$row['slug']] = $row['title'];
Expand Down
8 changes: 8 additions & 0 deletions src/DataProvider/PageCollectionDataProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@

namespace Mobizel\Bundle\MarkdownDocsBundle\DataProvider;

use Mobizel\Bundle\MarkdownDocsBundle\Dto\PageOutput;

interface PageCollectionDataProviderInterface
{
/** @return iterable<int, PageOutput> */
public function getPages(): iterable;

/** @return iterable<int, PageOutput> */
public function getRootPages(): iterable;

/** @return iterable<int, PageOutput> */
public function getChildrenPages(string $parentSlug): iterable;

/** @return string[] */
public function getPagesMap(): array;

public function getPagesAsTree(): array;
Expand Down
18 changes: 18 additions & 0 deletions src/Helper/RouteHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public function getRouteForSearch(ContextInterface $context): string
return sprintf('mobizel_markdown_docs_%s_search', $context->getName());
}

public function getRouteForPrint(ContextInterface $context): string
{
return sprintf('mobizel_markdown_docs_%s_print', $context->getName());
}

public function getPathForIndex(ContextInterface $context): string
{
$route = $this->getRouteForIndex($context);
Expand All @@ -73,6 +78,19 @@ public function getPathForMenu(ContextInterface $context): string
return $this->router->generate($route, $routeParameters);
}

public function getPathForPrint(ContextInterface $context): string
{
$route = $this->getRouteForPrint($context);

$routeParameters = $this->getRouteParameters();

unset($routeParameters['current_item']);
unset($routeParameters['trailingSlash']);
unset($routeParameters['slug']);

return $this->router->generate($route, $routeParameters);
}

public function getPathForPage(ContextInterface $context, string $slug): string
{
$route = $this->getRouteForPage($context);
Expand Down
4 changes: 4 additions & 0 deletions src/Helper/RouteHelperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ public function getRouteForMenu(ContextInterface $context): string;

public function getRouteForSearch(ContextInterface $context): string;

public function getRouteForPrint(ContextInterface $context): string;

public function getPathForIndex(ContextInterface $context): string;

public function getPathForMenu(ContextInterface $context): string;

public function getPathForPage(ContextInterface $context, string $slug): string;

public function getPathForSearch(ContextInterface $context): string;

public function getPathForPrint(ContextInterface $context): string;
}
8 changes: 8 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@
<argument key="$pageCollectionDataProvider" type="service" id="mobizel_markdown_docs.data_provider.page_collection"/>
</service>

<service id="mobizel_markdown_docs.controller.print_action" class="Mobizel\Bundle\MarkdownDocsBundle\Controller\PrintAction">
<call method="setContainer">
<argument type="service" id="Psr\Container\ContainerInterface"/>
</call>
<tag name="container.service_subscriber"/>
<argument key="$pageCollectionDataProvider" type="service" id="mobizel_markdown_docs.data_provider.page_collection"/>
</service>

<!-- Routing -->

<service id="mobizel_markdown_docs.routing.context_loader" class="Mobizel\Bundle\MarkdownDocsBundle\Routing\ContextLoader">
Expand Down
13 changes: 8 additions & 5 deletions src/Resources/views/layout/navbar.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
value="{{ app.request.get('query') }}"
>
</form>
{# <ul class="navbar-nav px-3">#}
{# <li class="nav-item text-nowrap">#}
{# <a class="nav-link" href="#">Sign out</a>#}
{# </li>#}
{# </ul>#}
<div class="navbar-nav d-none d-lg-block">
<div class="nav-item text-nowrap">
<a class="nav-link px-3 text-secondary" target="_blank" href="{{ markdown_docs_path_for_print() }}">
<span data-feather="printer"></span>
Print
</a>
</div>
</div>
</nav>
32 changes: 32 additions & 0 deletions src/Resources/views/print/index.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{% extends '@MobizelMarkdownDocs/layout.html.twig' %}

{% block body %}
<div class="container-fluid">
<div class="row">
<main role="main" class="col-md-12 ml-sm-auto col-lg-12 px-md-4 px-lg-5">
<div class="content">
{% for page in pages %}
{% set table_of_contents = page.tableOfContents %}

<h1>{{ page.title }}</h1>
{% if table_of_contents is not null %}
{{ table_of_contents|markdown_to_html }}
<hr>
{% endif %}

{{ page.content|markdown_to_html }}

<div data-next-page></div>
{% endfor %}
</div>
</main>
</div>
</div>
{% endblock %}

{% block javascripts %}
{{ parent() }}
<script>
window.print();
</script>
{% endblock %}
28 changes: 20 additions & 8 deletions src/Routing/ContextLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@

final class ContextLoader
{
/** @var ContextRegistryInterface */
private $contextRegistry;
private ContextRegistryInterface $contextRegistry;

/** @var RouteHelperInterface */
private $routeHelper;
private RouteHelperInterface $routeHelper;

public function __construct(ContextRegistryInterface $contextRegistry, RouteHelperInterface $routeHelper)
{
Expand All @@ -38,9 +36,6 @@ public function __invoke(): RouteCollection
{
$routeCollection = new RouteCollection();

/**
* @var ContextInterface $context
*/
foreach ($this->contextRegistry->getAll() as $context) {
$this->addRoutesForContext($routeCollection, $context);
}
Expand All @@ -53,6 +48,7 @@ private function addRoutesForContext(RouteCollection $routeCollection, ContextIn
$this->addRouteForMenu($routeCollection, $context);
$this->addRouteForSearch($routeCollection, $context);
$this->addRouteForIndex($routeCollection, $context);
$this->addRouteForPrint($routeCollection, $context);
$this->addRouteForPage($routeCollection, $context);
}

Expand All @@ -77,7 +73,7 @@ private function addRouteForSearch(RouteCollection $routeCollection, ContextInte
$defaults = ['_controller' => 'mobizel_markdown_docs.controller.search_action'];

$route = new Route(
$context->getPath().'/search',
$context->getPath().'/_search',
$defaults,
$this->buildRequirements($context),
[],
Expand All @@ -88,6 +84,22 @@ private function addRouteForSearch(RouteCollection $routeCollection, ContextInte
$routeCollection->add($this->routeHelper->getRouteForSearch($context), $route);
}

private function addRouteForPrint(RouteCollection $routeCollection, ContextInterface $context): void
{
$defaults = ['_controller' => 'mobizel_markdown_docs.controller.print_action'];

$route = new Route(
$context->getPath().'/_print',
$defaults,
$this->buildRequirements($context),
[],
null,
[],
[Request::METHOD_GET]
);
$routeCollection->add($this->routeHelper->getRouteForPrint($context), $route);
}

private function addRouteForIndex(RouteCollection $routeCollection, ContextInterface $context): void
{
$defaults = ['_controller' => 'mobizel_markdown_docs.controller.index_action'];
Expand Down
6 changes: 6 additions & 0 deletions src/Twig/Extension/ReaderExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function getFunctions(): array
return [
new TwigFunction('markdown_docs_path_for_index', [$this, 'getPathForIndex']),
new TwigFunction('markdown_docs_path_for_menu', [$this, 'getPathForMenu']),
new TwigFunction('markdown_docs_path_for_print', [$this, 'getPathForPrint']),
new TwigFunction('markdown_docs_path_for_page', [$this, 'getPathForPage']),
new TwigFunction('markdown_docs_path_for_search', [$this, 'getPathForSearch']),
new TwigFunction('markdown_docs_metadata', [$this, 'getMetadata']),
Expand All @@ -52,6 +53,11 @@ public function getPathForMenu(): string
return $this->routeHelper->getPathForMenu($this->readerContext->getContext());
}

public function getPathForPrint(): string
{
return $this->routeHelper->getPathForPrint($this->readerContext->getContext());
}

public function getPathForPage(string $slug): string
{
return $this->routeHelper->getPathForPage($this->readerContext->getContext(), $slug);
Expand Down
2 changes: 2 additions & 0 deletions src/Twig/Extension/ReaderExtensionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public function getPathForIndex(): string;

public function getPathForMenu(): string;

public function getPathForPrint(): string;

public function getPathForPage(string $slug): string;

public function getPathForSearch(): string;
Expand Down
2 changes: 1 addition & 1 deletion test/config/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
throw new RuntimeException('Please run "composer require symfony/dotenv" to load the ".env" files configuring the application.');
} else {
// load all the .env files
(new Dotenv(true))->loadEnv(dirname(__DIR__) . '/.env');
(new Dotenv())->loadEnv(dirname(__DIR__) . '/.env');
}

$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = ($_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? null) ?: 'test';
Expand Down
6 changes: 6 additions & 0 deletions test/config/packages/mobizel_markdown_docs.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
mobizel_markdown_docs:
contexts:
sylius_resource_bundle:
path: /sylius-resource-bundle
docs_dir: '/Users/loicfremont/www/sylius/sylius_resource_bundle/docs'
monofony:
path: /monofony
docs_dir: '/Users/loicfremont/www/monofony/docs/current'
current:
path: /current
docs_dir: '%kernel.project_dir%/../tests/docs'
Expand Down
29 changes: 29 additions & 0 deletions tests/Controller/MenuActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

final class MenuActionTest extends WebTestCase
{
public function testMenuAction(): void
{
$client = static::createClient();

$client->request('GET', '/current/_partials/menu');

$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->assertSelectorTextSame('html ul li a', 'Bar');
}
}
Loading