|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of AppName. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Monofony |
|
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 App\Fixture; |
|
13
|
|
|
|
|
14
|
|
|
use App\Fixture\Factory\ExampleFactoryInterface; |
|
15
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
16
|
|
|
use Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface; |
|
17
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
|
18
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
|
19
|
|
|
use Symfony\Component\OptionsResolver\Options; |
|
20
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
21
|
|
|
|
|
22
|
|
|
abstract class AbstractResourceFixture implements FixtureInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var ObjectManager |
|
26
|
|
|
*/ |
|
27
|
|
|
private $objectManager; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var ExampleFactoryInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $exampleFactory; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var OptionsResolver |
|
36
|
|
|
*/ |
|
37
|
|
|
private $optionsResolver; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param ObjectManager $objectManager |
|
41
|
|
|
* @param ExampleFactoryInterface $exampleFactory |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(ObjectManager $objectManager, ExampleFactoryInterface $exampleFactory) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->objectManager = $objectManager; |
|
46
|
|
|
$this->exampleFactory = $exampleFactory; |
|
47
|
|
|
|
|
48
|
|
|
$this->optionsResolver = |
|
49
|
|
|
(new OptionsResolver()) |
|
50
|
|
|
->setDefault('random', 0) |
|
51
|
|
|
->setAllowedTypes('random', 'int') |
|
52
|
|
|
->setDefault('prototype', []) |
|
53
|
|
|
->setAllowedTypes('prototype', 'array') |
|
54
|
|
|
->setDefault('custom', []) |
|
55
|
|
|
->setAllowedTypes('custom', 'array') |
|
56
|
|
|
->setNormalizer('custom', function (Options $options, array $custom) { |
|
57
|
|
|
if ($options['random'] <= 0) { |
|
58
|
|
|
return $custom; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return array_merge($custom, array_fill(0, $options['random'], $options['prototype'])); |
|
62
|
|
|
}) |
|
63
|
|
|
; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param array $options |
|
68
|
|
|
*/ |
|
69
|
|
|
final public function load(array $options): void |
|
70
|
|
|
{ |
|
71
|
|
|
$options = $this->optionsResolver->resolve($options); |
|
72
|
|
|
|
|
73
|
|
|
$i = 0; |
|
74
|
|
|
foreach ($options['custom'] as $resourceOptions) { |
|
75
|
|
|
$resource = $this->exampleFactory->create($resourceOptions); |
|
76
|
|
|
|
|
77
|
|
|
$this->objectManager->persist($resource); |
|
78
|
|
|
|
|
79
|
|
|
++$i; |
|
80
|
|
|
|
|
81
|
|
|
if (0 === ($i % 10)) { |
|
82
|
|
|
$this->objectManager->flush(); |
|
83
|
|
|
$this->objectManager->clear(); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$this->objectManager->flush(); |
|
88
|
|
|
$this->objectManager->clear(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
|
|
final public function getConfigTreeBuilder() |
|
95
|
|
|
{ |
|
96
|
|
|
$treeBuilder = new TreeBuilder(); |
|
97
|
|
|
$optionsNode = $treeBuilder->root($this->getName()); |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
$optionsNode->children()->integerNode('random')->min(0)->defaultValue(0); |
|
100
|
|
|
|
|
101
|
|
|
/** @var ArrayNodeDefinition $resourcesNode */ |
|
102
|
|
|
$resourcesNode = $optionsNode->children()->arrayNode('custom'); |
|
103
|
|
|
|
|
104
|
|
|
/** @var ArrayNodeDefinition $resourceNode */ |
|
105
|
|
|
$resourceNode = $resourcesNode->requiresAtLeastOneElement()->prototype('array'); |
|
106
|
|
|
$this->configureResourceNode($resourceNode); |
|
107
|
|
|
|
|
108
|
|
|
return $treeBuilder; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param ArrayNodeDefinition $resourceNode |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function configureResourceNode(ArrayNodeDefinition $resourceNode) |
|
115
|
|
|
{ |
|
116
|
|
|
// empty |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.