1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Bundle\ResourceBundle\DependencyInjection; |
13
|
|
|
|
14
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\ResourceController; |
15
|
|
|
use Sylius\Bundle\ResourceBundle\Form\Type\DefaultResourceType; |
16
|
|
|
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle; |
17
|
|
|
use Sylius\Component\Resource\Factory\Factory; |
18
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
19
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
20
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
final class Configuration implements ConfigurationInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function getConfigTreeBuilder() |
31
|
|
|
{ |
32
|
|
|
$treeBuilder = new TreeBuilder(); |
33
|
|
|
$rootNode = $treeBuilder->root('sylius_resource'); |
34
|
|
|
|
35
|
|
|
$this->addResourcesSection($rootNode); |
36
|
|
|
$this->addSettingsSection($rootNode); |
37
|
|
|
$this->addTranslationsSection($rootNode); |
38
|
|
|
$this->addDriversSection($rootNode); |
39
|
|
|
|
40
|
|
|
$rootNode |
41
|
|
|
->children() |
42
|
|
|
->scalarNode('authorization_checker') |
43
|
|
|
->defaultValue('sylius.resource_controller.authorization_checker.disabled') |
44
|
|
|
->cannotBeEmpty() |
45
|
|
|
->end() |
46
|
|
|
->end() |
47
|
|
|
; |
48
|
|
|
|
49
|
|
|
return $treeBuilder; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param ArrayNodeDefinition $node |
54
|
|
|
*/ |
55
|
|
|
private function addResourcesSection(ArrayNodeDefinition $node) |
56
|
|
|
{ |
57
|
|
|
$node |
|
|
|
|
58
|
|
|
->children() |
59
|
|
|
->arrayNode('resources') |
60
|
|
|
->useAttributeAsKey('name') |
61
|
|
|
->prototype('array') |
62
|
|
|
->children() |
63
|
|
|
->scalarNode('driver')->defaultValue(SyliusResourceBundle::DRIVER_DOCTRINE_ORM)->end() |
64
|
|
|
->variableNode('options')->end() |
65
|
|
|
->scalarNode('templates')->cannotBeEmpty()->end() |
66
|
|
|
->arrayNode('classes') |
67
|
|
|
->isRequired() |
68
|
|
|
->addDefaultsIfNotSet() |
69
|
|
|
->children() |
70
|
|
|
->scalarNode('model')->isRequired()->cannotBeEmpty()->end() |
71
|
|
|
->scalarNode('interface')->cannotBeEmpty()->end() |
72
|
|
|
->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end() |
73
|
|
|
->scalarNode('repository')->cannotBeEmpty()->end() |
74
|
|
|
->scalarNode('factory')->defaultValue(Factory::class)->end() |
75
|
|
|
->scalarNode('form')->defaultValue(DefaultResourceType::class)->cannotBeEmpty()->end() |
76
|
|
|
->end() |
77
|
|
|
->end() |
78
|
|
|
->arrayNode('translation') |
79
|
|
|
->children() |
80
|
|
|
->variableNode('options')->end() |
81
|
|
|
->arrayNode('classes') |
82
|
|
|
->isRequired() |
83
|
|
|
->addDefaultsIfNotSet() |
84
|
|
|
->children() |
85
|
|
|
->scalarNode('model')->isRequired()->cannotBeEmpty()->end() |
86
|
|
|
->scalarNode('interface')->cannotBeEmpty()->end() |
87
|
|
|
->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end() |
88
|
|
|
->scalarNode('repository')->cannotBeEmpty()->end() |
89
|
|
|
->scalarNode('factory')->defaultValue(Factory::class)->end() |
90
|
|
|
->scalarNode('form')->defaultValue(DefaultResourceType::class)->cannotBeEmpty()->end() |
91
|
|
|
->end() |
92
|
|
|
->end() |
93
|
|
|
->end() |
94
|
|
|
->end() |
95
|
|
|
->end() |
96
|
|
|
->end() |
97
|
|
|
->end() |
98
|
|
|
->end() |
99
|
|
|
; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param ArrayNodeDefinition $node |
104
|
|
|
*/ |
105
|
|
|
private function addSettingsSection(ArrayNodeDefinition $node) |
106
|
|
|
{ |
107
|
|
|
$node |
|
|
|
|
108
|
|
|
->children() |
109
|
|
|
->arrayNode('settings') |
110
|
|
|
->addDefaultsIfNotSet() |
111
|
|
|
->children() |
112
|
|
|
->variableNode('paginate')->defaultNull()->end() |
113
|
|
|
->variableNode('limit')->defaultNull()->end() |
114
|
|
|
->arrayNode('allowed_paginate') |
115
|
|
|
->prototype('integer')->end() |
116
|
|
|
->defaultValue([10, 20, 30]) |
117
|
|
|
->end() |
118
|
|
|
->integerNode('default_page_size')->defaultValue(10)->end() |
119
|
|
|
->booleanNode('sortable')->defaultFalse()->end() |
120
|
|
|
->variableNode('sorting')->defaultNull()->end() |
121
|
|
|
->booleanNode('filterable')->defaultFalse()->end() |
122
|
|
|
->variableNode('criteria')->defaultNull()->end() |
123
|
|
|
->end() |
124
|
|
|
->end() |
125
|
|
|
->end() |
126
|
|
|
; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param ArrayNodeDefinition $node |
131
|
|
|
*/ |
132
|
|
|
private function addTranslationsSection(ArrayNodeDefinition $node) |
133
|
|
|
{ |
134
|
|
|
$node |
135
|
|
|
->children() |
136
|
|
|
->arrayNode('translation') |
137
|
|
|
->canBeDisabled() |
138
|
|
|
->children() |
139
|
|
|
->scalarNode('locale_provider')->defaultValue('sylius.translation_locale_provider.immutable')->cannotBeEmpty()->end() |
140
|
|
|
->end() |
141
|
|
|
->end() |
142
|
|
|
; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param ArrayNodeDefinition $node |
147
|
|
|
*/ |
148
|
|
|
private function addDriversSection(ArrayNodeDefinition $node) |
149
|
|
|
{ |
150
|
|
|
$node |
|
|
|
|
151
|
|
|
->children() |
152
|
|
|
->arrayNode('drivers') |
153
|
|
|
->defaultValue([SyliusResourceBundle::DRIVER_DOCTRINE_ORM]) |
154
|
|
|
->prototype('enum')->values(SyliusResourceBundle::getAvailableDrivers())->end() |
155
|
|
|
->end() |
156
|
|
|
->end() |
157
|
|
|
; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
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: