-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractResourceFixture.php
122 lines (100 loc) · 3.36 KB
/
AbstractResourceFixture.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/*
* This file is part of the PhpMob package.
*
* (c) Ishmael Doss <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace PhpMob\DataFixture;
use Doctrine\Common\Persistence\ObjectManager;
use Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* @author Kamil Kokot <[email protected]>
*/
abstract class AbstractResourceFixture implements FixtureInterface
{
/**
* @var ObjectManager
*/
private $objectManager;
/**
* @var ExampleFactoryInterface
*/
private $exampleFactory;
/**
* @var OptionsResolver
*/
private $optionsResolver;
/**
* @param ObjectManager $objectManager
* @param ExampleFactoryInterface $exampleFactory
*/
public function __construct(ObjectManager $objectManager, ExampleFactoryInterface $exampleFactory)
{
$this->objectManager = $objectManager;
$this->exampleFactory = $exampleFactory;
$this->optionsResolver =
(new OptionsResolver())
->setDefault('random', 0)
->setAllowedTypes('random', 'int')
->setDefault('prototype', [])
->setAllowedTypes('prototype', 'array')
->setDefault('custom', [])
->setAllowedTypes('custom', 'array')
->setNormalizer('custom', function (Options $options, array $custom) {
if ($options['random'] <= 0) {
return $custom;
}
return array_merge($custom, array_fill(0, $options['random'], $options['prototype']));
})
;
}
/**
* @param array $options
*/
final public function load(array $options): void
{
$options = $this->optionsResolver->resolve($options);
$i = 0;
foreach ($options['custom'] as $resourceOptions) {
$resource = $this->exampleFactory->create($resourceOptions);
$this->objectManager->persist($resource);
++$i;
if (0 === ($i % 10)) {
$this->objectManager->flush();
$this->objectManager->clear();
}
}
$this->objectManager->flush();
$this->objectManager->clear();
}
/**
* {@inheritdoc}
*/
final public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$optionsNode = $treeBuilder->root($this->getName());
$optionsNode->children()->integerNode('random')->min(0)->defaultValue(0);
/** @var ArrayNodeDefinition $resourcesNode */
$resourcesNode = $optionsNode->children()->arrayNode('custom');
/** @var ArrayNodeDefinition $resourceNode */
$resourceNode = $resourcesNode->requiresAtLeastOneElement()->prototype('array');
$this->configureResourceNode($resourceNode);
return $treeBuilder;
}
/**
* @param ArrayNodeDefinition $resourceNode
*/
protected function configureResourceNode(ArrayNodeDefinition $resourceNode)
{
// empty
}
}