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
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Configuration |
18
|
|
|
* |
19
|
|
|
* Configuration tree. |
20
|
|
|
* |
21
|
|
|
* @package RunOpenCode\Bundle\ExchangeRate\DependencyInjection |
22
|
|
|
*/ |
23
|
|
|
class Configuration implements ConfigurationInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
4 |
|
public function getConfigTreeBuilder() |
29
|
|
|
{ |
30
|
4 |
|
$treeBuilder = new TreeBuilder(); |
31
|
|
|
|
32
|
4 |
|
$rootNode = $treeBuilder->root('run_open_code_exchange_rate'); |
33
|
|
|
|
34
|
|
|
$rootNode |
35
|
4 |
|
->children() |
36
|
4 |
|
->scalarNode('base_currency') |
37
|
4 |
|
->isRequired() |
38
|
4 |
|
->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
|
4 |
|
->end() |
49
|
4 |
|
->end(); |
50
|
|
|
|
51
|
4 |
|
return $treeBuilder; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Build configuration tree for rates. |
56
|
|
|
* |
57
|
|
|
* @return ArrayNodeDefinition |
58
|
|
|
*/ |
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
|
4 |
|
->requiresAtLeastOneElement() |
66
|
4 |
|
->prototype('array') |
67
|
4 |
|
->children() |
68
|
4 |
|
->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
|
4 |
|
->end() |
75
|
4 |
|
->end(); |
76
|
|
|
|
77
|
4 |
|
return $node; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Build configuration tree for processors. |
82
|
|
|
* |
83
|
|
|
* @return ArrayNodeDefinition |
84
|
|
|
*/ |
85
|
4 |
|
protected function getProcessorsDefinition() |
86
|
|
|
{ |
87
|
4 |
|
$node = new ArrayNodeDefinition('processors'); |
88
|
|
|
|
89
|
|
|
$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
|
4 |
|
->end(); |
94
|
|
|
|
95
|
4 |
|
return $node; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Build configuration tree for repository. |
100
|
|
|
* |
101
|
|
|
* @return ArrayNodeDefinition |
102
|
|
|
*/ |
103
|
4 |
|
protected function getFileRepositoryDefinition() |
104
|
|
|
{ |
105
|
4 |
|
$node = new ArrayNodeDefinition('file_repository'); |
106
|
|
|
|
107
|
|
|
$node |
|
|
|
|
108
|
4 |
|
->info('Configuration for file repository (if used).') |
109
|
4 |
|
->addDefaultsIfNotSet() |
110
|
4 |
|
->children() |
111
|
4 |
|
->scalarNode('path') |
112
|
4 |
|
->info('Absolute path to file where database file will be stored.') |
113
|
4 |
|
->defaultValue('%kernel.root_dir%/db/exchange_rates.dat') |
114
|
4 |
|
->end() |
115
|
4 |
|
->end() |
116
|
4 |
|
->end(); |
117
|
|
|
|
118
|
4 |
|
return $node; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Build configuration tree for view (controller). |
123
|
|
|
* |
124
|
|
|
* @return ArrayNodeDefinition |
125
|
|
|
*/ |
126
|
4 |
|
protected function getViewDefinition() |
127
|
|
|
{ |
128
|
4 |
|
$node = new ArrayNodeDefinition('view'); |
129
|
|
|
|
130
|
|
|
$node |
|
|
|
|
131
|
4 |
|
->info('Configuration of administration interface.') |
132
|
4 |
|
->addDefaultsIfNotSet() |
133
|
4 |
|
->children() |
134
|
4 |
|
->scalarNode('base_template') |
135
|
4 |
|
->info('Base decorator template.') |
136
|
4 |
|
->defaultValue('@ExchangeRate/base.html.twig') |
137
|
4 |
|
->end() |
138
|
4 |
|
->scalarNode('list') |
139
|
4 |
|
->info('Template for list view.') |
140
|
4 |
|
->defaultValue('@ExchangeRate/list.html.twig') |
141
|
4 |
|
->end() |
142
|
4 |
|
->scalarNode('new') |
143
|
4 |
|
->info('Template for create new exchange rate view.') |
144
|
4 |
|
->defaultValue('@ExchangeRate/new.html.twig') |
145
|
4 |
|
->end() |
146
|
4 |
|
->scalarNode('edit') |
147
|
4 |
|
->info('Template for edit exchange rate view.') |
148
|
4 |
|
->defaultValue('@ExchangeRate/edit.html.twig') |
149
|
4 |
|
->end() |
150
|
4 |
|
->scalarNode('date_format') |
151
|
4 |
|
->info('Date format in list view.') |
152
|
4 |
|
->defaultValue('Y-m-d') |
153
|
4 |
|
->end() |
154
|
4 |
|
->scalarNode('time_format') |
155
|
4 |
|
->info('Date/time format in list view.') |
156
|
4 |
|
->defaultValue('H:i') |
157
|
4 |
|
->end() |
158
|
4 |
|
->booleanNode('secure')->defaultValue(true)->end() |
159
|
4 |
|
->end() |
160
|
4 |
|
->end(); |
161
|
|
|
|
162
|
4 |
|
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: