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\ReviewBundle\DependencyInjection; |
13
|
|
|
|
14
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\ResourceController; |
15
|
|
|
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository; |
16
|
|
|
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle; |
17
|
|
|
use Sylius\Bundle\ReviewBundle\Form\Type\ReviewType; |
18
|
|
|
use Sylius\Component\Resource\Factory\Factory; |
19
|
|
|
use Sylius\Component\Review\Model\ReviewerInterface; |
20
|
|
|
use Sylius\Component\Review\Model\ReviewInterface; |
21
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
22
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
23
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author Daniel Richter <[email protected]> |
27
|
|
|
* @author Grzegorz Sadowski <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
final class Configuration implements ConfigurationInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function getConfigTreeBuilder() |
35
|
|
|
{ |
36
|
|
|
$treeBuilder = new TreeBuilder(); |
37
|
|
|
$rootNode = $treeBuilder->root('sylius_review'); |
38
|
|
|
|
39
|
|
|
$rootNode |
40
|
|
|
->addDefaultsIfNotSet() |
41
|
|
|
->children() |
42
|
|
|
->scalarNode('driver')->defaultValue(SyliusResourceBundle::DRIVER_DOCTRINE_ORM)->end() |
43
|
|
|
->end() |
44
|
|
|
; |
45
|
|
|
|
46
|
|
|
$this->addResourcesSection($rootNode); |
47
|
|
|
|
48
|
|
|
return $treeBuilder; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param ArrayNodeDefinition $node |
53
|
|
|
*/ |
54
|
|
|
private function addResourcesSection(ArrayNodeDefinition $node) |
55
|
|
|
{ |
56
|
|
|
$node |
|
|
|
|
57
|
|
|
->children() |
58
|
|
|
->arrayNode('resources') |
59
|
|
|
->useAttributeAsKey('name') |
60
|
|
|
->prototype('array') |
61
|
|
|
->children() |
62
|
|
|
->scalarNode('subject')->isRequired()->end() |
63
|
|
|
->arrayNode('review') |
64
|
|
|
->isRequired() |
65
|
|
|
->addDefaultsIfNotSet() |
66
|
|
|
->children() |
67
|
|
|
->arrayNode('classes') |
68
|
|
|
->addDefaultsIfNotSet() |
69
|
|
|
->children() |
70
|
|
|
->scalarNode('model')->isRequired()->end() |
71
|
|
|
->scalarNode('interface')->defaultValue(ReviewInterface::class)->cannotBeEmpty()->end() |
72
|
|
|
->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end() |
73
|
|
|
->scalarNode('repository')->defaultValue(EntityRepository::class)->cannotBeEmpty()->end() |
74
|
|
|
->scalarNode('factory')->defaultValue(Factory::class)->cannotBeEmpty()->end() |
75
|
|
|
->scalarNode('form')->defaultValue(ReviewType::class)->cannotBeEmpty()->end() |
76
|
|
|
->end() |
77
|
|
|
->end() |
78
|
|
|
->end() |
79
|
|
|
->end() |
80
|
|
|
->arrayNode('reviewer') |
81
|
|
|
->addDefaultsIfNotSet() |
82
|
|
|
->children() |
83
|
|
|
->arrayNode('classes') |
84
|
|
|
->addDefaultsIfNotSet() |
85
|
|
|
->children() |
86
|
|
|
->scalarNode('model')->isRequired()->end() |
87
|
|
|
->scalarNode('interface')->defaultValue(ReviewerInterface::class)->cannotBeEmpty()->end() |
88
|
|
|
->scalarNode('repository')->cannotBeEmpty()->end() |
89
|
|
|
->scalarNode('factory')->defaultValue(Factory::class)->cannotBeEmpty()->end() |
90
|
|
|
->end() |
91
|
|
|
->end() |
92
|
|
|
->end() |
93
|
|
|
->end() |
94
|
|
|
->end() |
95
|
|
|
->end() |
96
|
|
|
->end() |
97
|
|
|
; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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: