|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Field\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use JK\Configuration\Configuration; |
|
7
|
|
|
use LAG\AdminBundle\Application\Configuration\ApplicationConfigurationAwareInterface; |
|
8
|
|
|
use LAG\AdminBundle\Application\Configuration\ApplicationConfigurationStorage; |
|
9
|
|
|
use LAG\AdminBundle\Field\FieldInterface; |
|
10
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
11
|
|
|
|
|
12
|
|
|
class ConfigurationFactory |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var ApplicationConfigurationStorage |
|
16
|
|
|
*/ |
|
17
|
|
|
private $applicationConfigurationStorage; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* ConfigurationFactory constructor. |
|
21
|
|
|
* |
|
22
|
|
|
* @param ApplicationConfigurationStorage $applicationConfigurationStorage |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(ApplicationConfigurationStorage $applicationConfigurationStorage) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->applicationConfigurationStorage = $applicationConfigurationStorage; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Create a new field Configuration object. |
|
31
|
|
|
* |
|
32
|
|
|
* @param FieldInterface $field |
|
33
|
|
|
* @param array $configuration |
|
34
|
|
|
* |
|
35
|
|
|
* @return Configuration |
|
36
|
|
|
* |
|
37
|
|
|
* @throws Exception |
|
38
|
|
|
*/ |
|
39
|
|
|
public function create(FieldInterface $field, array $configuration = []) |
|
40
|
|
|
{ |
|
41
|
|
|
$resolver = new OptionsResolver(); |
|
42
|
|
|
|
|
43
|
|
|
$class = $field->getConfigurationClass(); |
|
44
|
|
|
$fieldConfiguration = new $class(); |
|
45
|
|
|
|
|
46
|
|
|
if (!$fieldConfiguration instanceof Configuration) { |
|
47
|
|
|
throw new Exception('The field configuration should be an instance of '.Configuration::class); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if ($fieldConfiguration instanceof ApplicationConfigurationAwareInterface) { |
|
51
|
|
|
$fieldConfiguration->setApplicationConfiguration( |
|
52
|
|
|
$this |
|
53
|
|
|
->applicationConfigurationStorage |
|
54
|
|
|
->getApplicationConfiguration() |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
$fieldConfiguration->configureOptions($resolver); |
|
58
|
|
|
$fieldConfiguration->setParameters($resolver->resolve($configuration)); |
|
59
|
|
|
|
|
60
|
|
|
return $fieldConfiguration; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|