-
Notifications
You must be signed in to change notification settings - Fork 5
/
VueExtension.php
113 lines (100 loc) · 3.6 KB
/
VueExtension.php
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
<?php
declare(strict_types=1);
namespace K3ssen\SymfonyVuetified\Vue;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
class VueExtension extends AbstractExtension
{
/**
* @var VueDataStorage
*/
protected $vueDataStorage;
public function __construct(VueDataStorage $vueDataStorage)
{
$this->vueDataStorage = $vueDataStorage;
}
public function getFunctions(): array
{
return [
new TwigFunction('vue_data', [$this, 'addVueData']),
new TwigFunction('vue_prop', [$this, 'addVueDataProp']),
new TwigFunction('get_vue_data', [$this, 'getVueData']),
new TwigFunction('vue_store', [$this, 'addVueStore']),
new TwigFunction('get_vue_store', [$this, 'getVueStore']),
new TwigFunction('vue_form', [$this, 'getVueForm']),
];
}
public function getFilters(): array
{
return [
new TwigFilter('vue', [$this, 'addVueDataPropFilter'], ['needs_context' => true]),
];
}
/**
* Add data to vue-instance.
* Effectively this would be similar to using <script>vue.data = Object.assign(vue.data, {key: value})</script>
* However, using script within rendered vue components is not allowed.
* By using this addVueData method the data can be rendered outside the rendered component, using getVueData
*
* @param String $key
* @param $value
*/
public function addVueData(String $key, $value): void
{
$this->vueDataStorage->addData($key, $value);
}
/**
* Same as addVueData, but returns the key
*/
public function addVueDataProp(String $key, $value): string
{
$this->vueDataStorage->addData($key, $value);
return $key;
}
public function addVueDataPropFilter($context, $value, ?string $key = null): string
{
// Default behaviour would be same as using {{ vue_prop(key, value) }}
if ($key) {
return $this->addVueDataProp($key, $value);
}
// If there's no key and the value is a string, then treat the value as a key and use null as value.
if (is_string($value)) {
return $this->addVueDataProp($value, null);
}
// if there's no key and the value is an object, then use the key found in context.
if (is_object($value)) {
foreach ($context as $contextKey => $contextValue) {
if ($value === $contextValue) {
return $this->addVueDataProp($contextKey, $value);
}
}
}
// If so far no key could be determined, then generate a random key.
$key = 'vue_prop_' . bin2hex(random_bytes(5));
return $this->addVueDataProp($key, $value);
}
public function getVueData(): string
{
return $this->vueDataStorage->getJson();
}
public function addVueStore(String $key, $value): void
{
$this->vueDataStorage->addData($key, $value, VueDataStorage::TYPE_STORE);
}
public function getVueStore(): string
{
return $this->vueDataStorage->getJson(VueDataStorage::TYPE_STORE);
}
public function getVueForm($form): VueForm
{
if ($form instanceof FormView) {
return new VueForm($form);
} else if ($form instanceof FormInterface) {
return VueForm::create($form);
}
throw new \InvalidArgumentException(sprintf('Provided form must be either a FormView or FormInterface; got %s instead', get_class($form)));
}
}