-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGuideController.php
More file actions
97 lines (82 loc) · 3.04 KB
/
GuideController.php
File metadata and controls
97 lines (82 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
namespace Webfactory\ShortcodeBundle\Controller;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Twig\Environment;
use Twig_Environment;
/**
* Guide for the configured shortcodes showing a list overview and detail pages with the rendered shortcode.
*/
final class GuideController
{
/**
* @var array
*
* Example structure: [
* [
* 'shortcode' => 'img'
* 'example' (optional key) => '[img src="https://upload.wikimedia.org/wikipedia/en/f/f7/RickRoll.png"]'
* 'description' (optional key) => 'Embeds the imgage located at {src}.'
* ]
* ]
*/
private $shortcodeTags;
/**
* @var Environment|Twig_Environment
*/
private $twig;
/**
* @var ?FormFactoryInterface
*/
private $formFactory;
/**
* @param Twig_Environment|Environment $twig
*/
public function __construct(array $shortcodeTags, $twig, ?FormFactoryInterface $formFactory = null)
{
$this->shortcodeTags = array_combine(array_map(function (array $definition): string { return $definition['shortcode']; }, $shortcodeTags), $shortcodeTags);
$this->twig = $twig;
$this->formFactory = $formFactory;
}
public function listAction(): Response
{
return new Response($this->twig->render('@WebfactoryShortcode/Guide/list.html.twig', ['shortcodeTags' => $this->shortcodeTags]));
}
public function detailAction(string $shortcode, Request $request): Response
{
if (!isset($this->shortcodeTags[$shortcode])) {
throw new NotFoundHttpException();
}
$shortcodeTag = $this->shortcodeTags[$shortcode];
// if custom parameters are provided, replace the example
$customParameters = $request->query->get('customParameters');
if ($customParameters) {
$shortcodeTag['example'] = $shortcode.' '.$customParameters;
}
$example = '['.($shortcodeTag['example'] ?? $shortcode).']';
if ($this->formFactory) {
$formBuilder = $this->formFactory->createBuilder(FormType::class, ['example' => $example], ['method' => 'GET']);
$formBuilder->add('example', TextareaType::class);
$form = $formBuilder->getForm();
$form->handleRequest($request);
if ($form->isSubmitted()) {
$example = $form->getData()['example'];
}
} else {
$form = null;
}
return new Response(
$this->twig->render(
'@WebfactoryShortcode/Guide/detail.html.twig', [
'shortcodeTag' => $shortcodeTag,
'example' => $example,
'form' => $form ? $form->createView() : null,
]
)
);
}
}