1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Everlution\AjaxcomBundle\Service; |
6
|
|
|
|
7
|
|
|
use Everlution\Ajaxcom\Handler; |
8
|
|
|
use Everlution\AjaxcomBundle\DataObject\Callback as AjaxCallback; |
9
|
|
|
use Everlution\AjaxcomBundle\Mutation\MutatorInterface; |
10
|
|
|
use Everlution\AjaxcomBundle\Mutation\RenderableInterface; |
11
|
|
|
use Everlution\AjaxcomBundle\Mutation\Container; |
12
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Ajaxcom. |
16
|
|
|
* |
17
|
|
|
* @author Ivan Barlog <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class Ajaxcom |
20
|
|
|
{ |
21
|
|
|
const AJAX_COM_HEADER = 'HTTP_X_AJAXCOM'; |
22
|
|
|
const AJAX_COM_CACHE_CONTROL = ['Cache-Control' => 'no-cache,max-age=0,must-revalidate,no-store']; |
23
|
|
|
|
24
|
|
|
/** @var Handler */ |
25
|
|
|
private $handler; |
26
|
|
|
/** @var Container */ |
27
|
|
|
private $container; |
28
|
|
|
|
29
|
|
|
public function __construct(Handler $handler, Container $container) |
30
|
|
|
{ |
31
|
|
|
$this->handler = $handler; |
32
|
|
|
$this->container = $container; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function handle(string $view, array $parameters = []): JsonResponse |
36
|
|
|
{ |
37
|
|
|
/** @var MutatorInterface $mutator */ |
38
|
|
|
foreach ($this->container->getMutators() as $mutator) { |
39
|
|
|
if ($mutator instanceof RenderableInterface) { |
40
|
|
|
$mutator->setView($view); |
41
|
|
|
$mutator->setParameters($parameters); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$mutator->mutate($this->handler); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return new JsonResponse($this->handler->respond(), JsonResponse::HTTP_OK, self::AJAX_COM_CACHE_CONTROL); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function renderBlock(string $id): self |
51
|
|
|
{ |
52
|
|
|
$this->addBlocks->add($id); |
|
|
|
|
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function removeBlock(string $selector): self |
58
|
|
|
{ |
59
|
|
|
$this->removeBlocks->add($selector); |
|
|
|
|
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function addCallback(AjaxCallback $callback): self |
65
|
|
|
{ |
66
|
|
|
$this->callbacks->add($callback); |
|
|
|
|
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function replaceClass(string $selector, string $class): self |
72
|
|
|
{ |
73
|
|
|
$this->replaceClass->add($selector, $class); |
|
|
|
|
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function doNotChangeUrl(): self |
79
|
|
|
{ |
80
|
|
|
$this->changeUrl->doNotChangeUrl(); |
|
|
|
|
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|