-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAjaxBlockExtension.php
More file actions
190 lines (164 loc) · 5.74 KB
/
AjaxBlockExtension.php
File metadata and controls
190 lines (164 loc) · 5.74 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace ITE\Js\AjaxBlock;
use ITE\Js\AjaxBlock\Annotation\AjaxBlock;
use ITE\JsBundle\EventListener\Event\AjaxRequestEvent;
use ITE\JsBundle\SF\SFExtension;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Templating\TemplateReference;
/**
* Class AjaxBlockExtension
*
* @author sam0delkin <t.samodelkin@gmail.com>
*/
class AjaxBlockExtension extends SFExtension
{
/**
* @var AjaxBlockRenderer
*/
protected $renderer;
/**
* @var array
*/
protected $options;
/**
* @param AjaxBlockRenderer $renderer
* @param array $options
*/
public function __construct(AjaxBlockRenderer $renderer, array $options)
{
$this->renderer = $renderer;
$this->options = $options;
}
/**
* {@inheritdoc}
*/
public function getJavascripts()
{
return [__DIR__ . '/Resources/public/js/sf.ajax_block.js'];
}
/**
* {@inheritdoc}
*/
public function onAjaxRequest(AjaxRequestEvent $event)
{
$request = $event->getRequest();
if (!$request->attributes->has('_ajax_block')) {
return;
}
$annotations = $request->attributes->get('_ajax_block');
//$single = 1 === count($annotations);
$blocks = [];
/** @var AjaxBlock $annotation */
foreach ($annotations as $annotation) {
$content = $this->renderer->render(
$this->getTemplate($request, $annotation),
$annotation->getBlockName(),
$event->getControllerResult(),
$annotation->isOptional()
);
if (false === $content && $annotation->isOptional()) {
continue;
}
if (!$annotation->getSelector()) {
$event->setContent($content);
continue;
}
// we don't need to re-render the template
if (null === $annotation->getTemplate()) {
if (empty($event->getAjaxDataBag()->getOriginalResponse())) {
$event->getAjaxDataBag()->setOriginalResponse($this->renderer->getOriginalContent());
}
}
if (!$annotation->getSelector()) {
throw new \InvalidArgumentException('You should specify selector for multiple ajaxBlock annotations.');
}
/** @var AjaxBlock $annotation */
$blocks[$annotation->getSelector()] = [
'content' => $content,
'show_animation' => [
'type' => null === $annotation->getShowAnimation()
? $this->options['show_animation']['type']
: $annotation->getShowAnimation(),
'length' => null === $annotation->getShowLength()
? $this->options['show_animation']['length']
: $annotation->getShowLength()
],
];
}
if (!empty($blocks)) {
$event->getAjaxDataBag()->addBodyData('blocks', $blocks);
$event->stopParentEventPropagation();
}
}
/**
* @inheritdoc
*/
public function loadConfiguration(array $config, ContainerBuilder $container)
{
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/Resources/config'));
if ($config['extensions']['ajax_block']['enabled']) {
$container->setParameter('ite_js.ajax_block.options', $config['extensions']['ajax_block']);
$loader->load('ajax_block.yml');
}
}
/**
* @inheritdoc
*/
public function getConfiguration(ContainerBuilder $container)
{
$builder = new TreeBuilder();
$rootNode = $builder->root('ajax_block');
$rootNode
->canBeEnabled()
->children()
->arrayNode('show_animation')
->addDefaultsIfNotSet()
->children()
->enumNode('type')
->defaultValue('show')
->values(['show', 'slide', 'fade'])
->info('animation type')
->end()
->integerNode('length')
->defaultValue(0)
->min(0)
->info('time in ms')
->end()
->end()
->end()
->end()
->end()
;
return $rootNode;
}
/**
* @param Request $request
* @param AjaxBlock $configuration
* @return string
*/
protected function getTemplate(Request $request, AjaxBlock $configuration)
{
if ($configuration->getTemplate()) {
return $configuration->getTemplate();
}
/** @var TemplateReference $template */
$template = $request->attributes->get('_template');
if (!$template) {
throw new \InvalidArgumentException('You should set template for render ajax_block.');
}
if (is_string($template)) {
return $template;
}
$originalFormat = $template->get('format');
$template->set('format', 'html');
$templateName = $template->getPath();
$template->set('format', $originalFormat);
return $templateName;
}
}