|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Exchange Rate Bundle, an RunOpenCode project. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) 2016 RunOpenCode |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace RunOpenCode\Bundle\ExchangeRate\DependencyInjection; |
|
11
|
|
|
|
|
12
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
|
13
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
|
14
|
4 |
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
|
15
|
|
|
|
|
16
|
4 |
|
/** |
|
17
|
|
|
* Class Configuration |
|
18
|
4 |
|
* |
|
19
|
|
|
* Configuration tree. |
|
20
|
|
|
* |
|
21
|
4 |
|
* @package RunOpenCode\Bundle\ExchangeRate\DependencyInjection |
|
22
|
4 |
|
*/ |
|
23
|
4 |
|
class Configuration implements ConfigurationInterface |
|
24
|
4 |
|
{ |
|
25
|
4 |
|
/** |
|
26
|
4 |
|
* {@inheritdoc} |
|
27
|
4 |
|
*/ |
|
28
|
4 |
|
public function getConfigTreeBuilder() |
|
29
|
4 |
|
{ |
|
30
|
|
|
$treeBuilder = new TreeBuilder(); |
|
31
|
4 |
|
|
|
32
|
|
|
$rootNode = $treeBuilder->root('run_open_code_exchange_rate'); |
|
33
|
|
|
|
|
34
|
4 |
|
$rootNode |
|
35
|
|
|
->children() |
|
36
|
4 |
|
->scalarNode('base_currency') |
|
37
|
|
|
->isRequired() |
|
38
|
|
|
->info('Set base currency in which you are doing your business activities.') |
|
39
|
4 |
|
->end() |
|
40
|
4 |
|
->scalarNode('repository') |
|
41
|
4 |
|
->defaultValue('run_open_code.exchange_rate.repository.file_repository') |
|
42
|
4 |
|
->info('Service ID which is in charge for rates persistence.') |
|
43
|
4 |
|
->end() |
|
44
|
4 |
|
->append($this->getRatesDefinition()) |
|
45
|
4 |
|
->append($this->getProcessorsDefinition()) |
|
46
|
4 |
|
->append($this->getFileRepositoryDefinition()) |
|
47
|
4 |
|
->append($this->getViewDefinition()) |
|
48
|
|
|
->end() |
|
49
|
4 |
|
->end(); |
|
50
|
|
|
|
|
51
|
|
|
return $treeBuilder; |
|
52
|
4 |
|
} |
|
53
|
|
|
|
|
54
|
4 |
|
/** |
|
55
|
|
|
* Build configuration tree for rates. |
|
56
|
|
|
* |
|
57
|
4 |
|
* @return ArrayNodeDefinition |
|
58
|
4 |
|
*/ |
|
59
|
4 |
|
protected function getRatesDefinition() |
|
60
|
|
|
{ |
|
61
|
4 |
|
$node = new ArrayNodeDefinition('rates'); |
|
62
|
|
|
|
|
63
|
|
|
$node |
|
|
|
|
|
|
64
|
4 |
|
->info('Configuration of each individual rate with which you intend to work with.') |
|
65
|
|
|
->requiresAtLeastOneElement() |
|
66
|
4 |
|
->prototype('array') |
|
67
|
|
|
->children() |
|
68
|
|
|
->scalarNode('currency_code')->isRequired()->end() |
|
69
|
4 |
|
->scalarNode('rate_type')->isRequired()->end() |
|
70
|
4 |
|
->scalarNode('source')->isRequired()->end() |
|
71
|
4 |
|
->scalarNode('alias')->defaultValue(null)->end() |
|
72
|
4 |
|
->arrayNode('extra')->end() |
|
73
|
4 |
|
->end() |
|
74
|
|
|
->end() |
|
75
|
4 |
|
->end(); |
|
76
|
|
|
|
|
77
|
|
|
return $node; |
|
78
|
4 |
|
} |
|
79
|
|
|
|
|
80
|
4 |
|
/** |
|
81
|
|
|
* Build configuration tree for processors. |
|
82
|
|
|
* |
|
83
|
4 |
|
* @return ArrayNodeDefinition |
|
84
|
4 |
|
*/ |
|
85
|
4 |
|
protected function getProcessorsDefinition() |
|
86
|
4 |
|
{ |
|
87
|
4 |
|
$node = new ArrayNodeDefinition('processors'); |
|
88
|
4 |
|
|
|
89
|
4 |
|
$node |
|
|
|
|
|
|
90
|
4 |
|
->info('List of processors which ought to be executed after fetch process.') |
|
91
|
4 |
|
->useAttributeAsKey('name') |
|
92
|
4 |
|
->prototype('scalar')->end() |
|
93
|
|
|
->end(); |
|
94
|
4 |
|
|
|
95
|
|
|
return $node; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Build configuration tree for repository. |
|
100
|
|
|
* |
|
101
|
|
|
* @return ArrayNodeDefinition |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function getFileRepositoryDefinition() |
|
104
|
|
|
{ |
|
105
|
|
|
$node = new ArrayNodeDefinition('file_repository'); |
|
106
|
|
|
|
|
107
|
|
|
$node |
|
|
|
|
|
|
108
|
|
|
->info('Configuration for file repository (if used).') |
|
109
|
|
|
->addDefaultsIfNotSet() |
|
110
|
|
|
->children() |
|
111
|
|
|
->scalarNode('path') |
|
112
|
|
|
->info('Absolute path to file where database file will be stored.') |
|
113
|
|
|
->defaultValue('%kernel.root_dir%/db/exchange_rates.dat') |
|
114
|
|
|
->end() |
|
115
|
|
|
->end() |
|
116
|
|
|
->end(); |
|
117
|
|
|
|
|
118
|
|
|
return $node; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Build configuration tree for view (controller). |
|
123
|
|
|
* |
|
124
|
|
|
* @return ArrayNodeDefinition |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function getViewDefinition() |
|
127
|
|
|
{ |
|
128
|
|
|
$node = new ArrayNodeDefinition('view'); |
|
129
|
|
|
|
|
130
|
|
|
$node |
|
|
|
|
|
|
131
|
|
|
->info('Configuration of administration interface.') |
|
132
|
|
|
->addDefaultsIfNotSet() |
|
133
|
|
|
->children() |
|
134
|
|
|
->scalarNode('base_template') |
|
135
|
|
|
->info('Base decorator template.') |
|
136
|
|
|
->defaultValue('@ExchangeRate/base.html.twig') |
|
137
|
|
|
->end() |
|
138
|
|
|
->scalarNode('list') |
|
139
|
|
|
->info('Template for list view.') |
|
140
|
|
|
->defaultValue('@ExchangeRate/list.html.twig') |
|
141
|
|
|
->end() |
|
142
|
|
|
->scalarNode('new') |
|
143
|
|
|
->info('Template for create new exchange rate view.') |
|
144
|
|
|
->defaultValue('@ExchangeRate/new.html.twig') |
|
145
|
|
|
->end() |
|
146
|
|
|
->scalarNode('edit') |
|
147
|
|
|
->info('Template for edit exchange rate view.') |
|
148
|
|
|
->defaultValue('@ExchangeRate/edit.html.twig') |
|
149
|
|
|
->end() |
|
150
|
|
|
->scalarNode('date_format') |
|
151
|
|
|
->info('Date format in list view.') |
|
152
|
|
|
->defaultValue('Y-m-d') |
|
153
|
|
|
->end() |
|
154
|
|
|
->scalarNode('time_format') |
|
155
|
|
|
->info('Date/time format in list view.') |
|
156
|
|
|
->defaultValue('H:i') |
|
157
|
|
|
->end() |
|
158
|
|
|
->booleanNode('secure')->defaultValue(true)->end() |
|
159
|
|
|
->end() |
|
160
|
|
|
->end(); |
|
161
|
|
|
|
|
162
|
|
|
return $node; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: