|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Form\Extension; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use LAG\AdminBundle\Assets\Registry\ScriptRegistryInterface; |
|
7
|
|
|
use Symfony\Component\Form\AbstractTypeExtension; |
|
8
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FormType; |
|
9
|
|
|
use Symfony\Component\Form\FormInterface; |
|
10
|
|
|
use Symfony\Component\Form\FormView; |
|
11
|
|
|
|
|
12
|
|
|
class AssetsExtension extends AbstractTypeExtension |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var ScriptRegistryInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
private $scriptRegistry; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* AssetsExtension constructor. |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct(ScriptRegistryInterface $scriptRegistry) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->scriptRegistry = $scriptRegistry; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public static function getExtendedTypes(): iterable |
|
28
|
|
|
{ |
|
29
|
|
|
return [ |
|
30
|
|
|
FormType::class, |
|
31
|
|
|
]; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Register the configured scripts using the FormView variables. |
|
36
|
|
|
* |
|
37
|
|
|
* @throws Exception |
|
38
|
|
|
*/ |
|
39
|
|
|
public function finishView(FormView $view, FormInterface $form, array $options) |
|
40
|
|
|
{ |
|
41
|
|
|
if (!array_key_exists('scripts', $view->vars) || !is_array($view->vars['scripts'])) { |
|
42
|
|
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
foreach ($view->vars['scripts'] as $location => $scripts) { |
|
45
|
|
|
if (!is_array($scripts)) { |
|
46
|
|
|
throw new Exception('Assets configuration for location '.$location.' should be an array in form '.$form->getName()); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
foreach ($scripts as $name => $script) { |
|
50
|
|
|
// provide a script name if none is provided |
|
51
|
|
|
if (is_array($script) && !array_key_exists('script', $script)) { |
|
52
|
|
|
$script['script'] = $name; |
|
53
|
|
|
} |
|
54
|
|
|
$this->registerScript($location, $script); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Register a script for a location. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $location |
|
63
|
|
|
* @param string $script |
|
64
|
|
|
*/ |
|
65
|
|
|
private function registerScript($location, $script) |
|
66
|
|
|
{ |
|
67
|
|
|
if (is_string($script)) { |
|
|
|
|
|
|
68
|
|
|
$this |
|
69
|
|
|
->scriptRegistry |
|
70
|
|
|
->register($location, $script) |
|
71
|
|
|
; |
|
72
|
|
|
} elseif (is_array($script)) { |
|
73
|
|
|
if (!array_key_exists('template', $script)) { |
|
74
|
|
|
$script['template'] = null; |
|
75
|
|
|
} |
|
76
|
|
|
if (!array_key_exists('context', $script)) { |
|
77
|
|
|
$script['context'] = []; |
|
78
|
|
|
} |
|
79
|
|
|
$this |
|
80
|
|
|
->scriptRegistry |
|
81
|
|
|
->register($location, $script['script'], $script['template'], $script['context']) |
|
82
|
|
|
; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|