Skip to content
This repository was archived by the owner on Nov 21, 2019. It is now read-only.

Commit 5d8b471

Browse files
Add the grid (#47)
* Add grid * Not meant * Match changes * Updates * Pass context around in the view * Update to committed version * Patch Twig * Add context * Add event tests * Add MainListenerTest * Not needed * Revert * Use constants * Move grid areas to constants * Use Twig release * 👋 * Updates
1 parent f94b3cf commit 5d8b471

35 files changed

+1120
-268
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"symfony/translation": "^4.2",
4242
"symfony/twig-bundle": "^4.1",
4343
"symfony/yaml": "^4.1",
44-
"twig/twig": "^2.5"
44+
"twig/twig": "^2.7"
4545
},
4646
"require-dev": {
4747
"csa/guzzle-cache-middleware": "^1.0",

composer.lock

Lines changed: 18 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

templates/page.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
<body>
2020

21-
{% include '@LiberoPatterns/text.html.twig' with {nodes: content} %}
21+
{% include '@LiberoPatterns/page-grid.html.twig' with content only %}
2222

2323
</body>
2424

vendor-extra/ContentPageBundle/src/Event/CreateContentPageEvent.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Libero\ContentPageBundle\Event;
66

77
use FluentDOM\DOM\Document;
8+
use Libero\ViewsBundle\Views\View;
89
use Symfony\Component\EventDispatcher\Event;
910

1011
final class CreateContentPageEvent extends Event
@@ -27,9 +28,9 @@ public function getContent() : array
2728
return $this->content;
2829
}
2930

30-
public function addContent($content) : void
31+
public function setContent(string $area, View $view) : void
3132
{
32-
$this->content[] = $content;
33+
$this->content[$area] = $view;
3334
}
3435

3536
public function getContext() : array
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Libero\ContentPageBundle\Event;
6+
7+
use FluentDOM\DOM\Document;
8+
use Libero\ViewsBundle\Views\View;
9+
use Symfony\Component\EventDispatcher\Event;
10+
use function end;
11+
use function is_array;
12+
use function is_string;
13+
use function key;
14+
15+
final class CreateContentPagePartEvent extends Event
16+
{
17+
private $content = [];
18+
private $context;
19+
private $item;
20+
private $template;
21+
22+
public static function name(string $part) : string
23+
{
24+
return "libero.page.content.${part}";
25+
}
26+
27+
public function __construct(string $template, Document $item, array $context = [])
28+
{
29+
$this->template = $template;
30+
$this->item = $item;
31+
$this->context = $context;
32+
}
33+
34+
public function getContent() : array
35+
{
36+
return $this->content;
37+
}
38+
39+
public function addContent(View ...$views) : void
40+
{
41+
foreach ($views as $view) {
42+
$area = $view->getContext('area');
43+
44+
if (!is_string($area)) {
45+
$this->content[] = $view;
46+
47+
continue;
48+
}
49+
50+
$last = end($this->content);
51+
if (is_array($last) && $area === $last['area']) {
52+
$key = key($this->content);
53+
$this->content[$key]['content'][] = $view;
54+
55+
continue;
56+
}
57+
58+
$this->content[] = ['area' => $area, 'content' => [$view]];
59+
}
60+
}
61+
62+
public function getContext() : array
63+
{
64+
return $this->context;
65+
}
66+
67+
public function setContext(string $key, $value) : void
68+
{
69+
$this->context[$key] = $value;
70+
}
71+
72+
public function getItem() : Document
73+
{
74+
return $this->item;
75+
}
76+
77+
public function getTemplate() : string
78+
{
79+
return $this->template;
80+
}
81+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Libero\ContentPageBundle\EventListener;
6+
7+
use Libero\ContentPageBundle\Event\CreateContentPageEvent;
8+
use Libero\ContentPageBundle\Event\CreateContentPagePartEvent;
9+
use Libero\ViewsBundle\Views\View;
10+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
11+
use function array_merge;
12+
use function count;
13+
use const Libero\LiberoPatternsBundle\CONTENT_GRID_MAIN;
14+
use const Libero\LiberoPatternsBundle\PAGE_GRID_MAIN;
15+
16+
final class MainListener
17+
{
18+
private $dispatcher;
19+
20+
public function __construct(EventDispatcherInterface $dispatcher)
21+
{
22+
$this->dispatcher = $dispatcher;
23+
}
24+
25+
public function onCreatePage(CreateContentPageEvent $event) : void
26+
{
27+
$part = new CreateContentPagePartEvent(
28+
$grid = '@LiberoPatterns/content-grid.html.twig',
29+
$event->getItem(),
30+
array_merge($event->getContext(), ['area' => CONTENT_GRID_MAIN])
31+
);
32+
33+
$this->dispatcher->dispatch($part::name('main'), $part);
34+
35+
if (0 === count($part->getContent())) {
36+
return;
37+
}
38+
39+
$event->setContent(
40+
PAGE_GRID_MAIN,
41+
new View($part->getTemplate(), ['content' => $part->getContent()], $part->getContext())
42+
);
43+
}
44+
}

vendor-extra/ContentPageBundle/src/Resources/config/services.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
<tag name="kernel.event_listener" event="libero.page.content" method="onCreatePage" priority="1000"/>
1616
</service>
1717

18+
<service id="Libero\ContentPageBundle\EventListener\MainListener"
19+
class="Libero\ContentPageBundle\EventListener\MainListener">
20+
<argument type="service" id="event_dispatcher"/>
21+
<tag name="kernel.event_listener" event="libero.page.content" method="onCreatePage"/>
22+
</service>
23+
1824
<service id="Libero\ContentPageBundle\Routing\ContentPageRouteLoader"
1925
class="Libero\ContentPageBundle\Routing\ContentPageRouteLoader">
2026
<argument type="collection"/>

vendor-extra/ContentPageBundle/tests/Controller/ContentControllerTest.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use GuzzleHttp\Psr7\Response as Psr7Response;
1010
use Libero\ContentPageBundle\Controller\ContentController;
1111
use Libero\ContentPageBundle\Event\CreateContentPageEvent;
12+
use Libero\ViewsBundle\Views\View;
1213
use PHPUnit\Framework\TestCase;
1314
use Symfony\Component\EventDispatcher\EventDispatcher;
1415
use Symfony\Component\HttpFoundation\Request;
@@ -69,7 +70,12 @@ public function pageProvider() : iterable
6970
'dir' => 'ltr',
7071
],
7172
'title' => 'title',
72-
'content' => ['content'],
73+
'content' => [
74+
'area' => [
75+
'template' => 'template',
76+
'arguments' => [],
77+
],
78+
],
7379
],
7480
];
7581

@@ -86,7 +92,12 @@ public function pageProvider() : iterable
8692
'dir' => 'ltr',
8793
],
8894
'title' => 'title',
89-
'content' => ['content'],
95+
'content' => [
96+
'area' => [
97+
'template' => 'template',
98+
'arguments' => [],
99+
],
100+
],
90101
],
91102
];
92103

@@ -103,7 +114,12 @@ public function pageProvider() : iterable
103114
'dir' => 'rtl',
104115
],
105116
'title' => 'title',
106-
'content' => ['content'],
117+
'content' => [
118+
'area' => [
119+
'template' => 'template',
120+
'arguments' => [],
121+
],
122+
],
107123
],
108124
];
109125
}
@@ -150,7 +166,7 @@ private function createContentController(
150166
function (CreateContentPageEvent $event) : void {
151167
$event->setContext('context', $event->getContext());
152168
$event->setTitle('title');
153-
$event->addContent('content');
169+
$event->setContent('area', new View('template'));
154170
}
155171
);
156172

0 commit comments

Comments
 (0)