|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Darkilliant\ImportBundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use Darkilliant\ImportBundle\Step\CsvExtractorStep; |
|
6
|
|
|
use Darkilliant\ImportBundle\Step\DoctrinePersisterStep; |
|
7
|
|
|
use Darkilliant\ImportBundle\Step\LoadObjectNormalizedStep; |
|
8
|
|
|
use Darkilliant\ImportBundle\Step\MappingTransformerStep; |
|
9
|
|
|
use Darkilliant\ProcessBundle\Step\DebugStep; |
|
10
|
|
|
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; |
|
11
|
|
|
use JMS\SerializerBundle\JMSSerializerBundle; |
|
12
|
|
|
use Symfony\Component\Config\FileLocator; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
|
16
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @internal |
|
20
|
|
|
* Class DarkilliantImportExtension |
|
21
|
|
|
*/ |
|
22
|
|
|
class DarkilliantImportExtension extends ConfigurableExtension implements PrependExtensionInterface |
|
23
|
|
|
{ |
|
24
|
1 |
|
public function prepend(ContainerBuilder $container) |
|
25
|
|
|
{ |
|
26
|
1 |
|
$configs = $container->getExtensionConfig($this->getAlias()); |
|
27
|
1 |
|
$config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs); |
|
28
|
|
|
|
|
29
|
1 |
|
$processCollection = []; |
|
30
|
1 |
|
foreach ($config['imports'] as $name => $importConfig) { |
|
31
|
|
|
$process = [ |
|
32
|
1 |
|
'steps' => [ |
|
33
|
|
|
[ |
|
34
|
|
|
'service' => CsvExtractorStep::class, |
|
35
|
|
|
'options' => [ |
|
36
|
1 |
|
'filepath' => $this->resolvePath($importConfig['source']), |
|
37
|
|
|
'colums_names' => null, |
|
38
|
1 |
|
'delimiter' => ';', |
|
39
|
|
|
], |
|
40
|
|
|
], |
|
41
|
|
|
[ |
|
42
|
|
|
'service' => DebugStep::class, |
|
43
|
|
|
'options' => [], |
|
44
|
|
|
], |
|
45
|
|
|
[ |
|
46
|
|
|
'service' => MappingTransformerStep::class, |
|
47
|
|
|
'options' => [ |
|
48
|
1 |
|
'mapping' => $importConfig['mapping'], |
|
49
|
|
|
], |
|
50
|
|
|
], |
|
51
|
|
|
[ |
|
52
|
|
|
'service' => LoadObjectNormalizedStep::class, |
|
53
|
|
|
'options' => [ |
|
54
|
1 |
|
'entity_class' => $importConfig['entity_class'], |
|
55
|
|
|
], |
|
56
|
|
|
], |
|
57
|
|
|
[ |
|
58
|
|
|
'service' => DoctrinePersisterStep::class, |
|
59
|
|
|
'options' => [ |
|
60
|
|
|
'batch_count' => 20, |
|
61
|
|
|
], |
|
62
|
|
|
], |
|
63
|
|
|
], |
|
64
|
|
|
]; |
|
65
|
|
|
|
|
66
|
1 |
|
$processCollection[$name] = $process; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
$container->prependExtensionConfig('darkilliant_process', ['process' => $processCollection]); |
|
70
|
1 |
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
protected function loadInternal(array $mergedConfig, ContainerBuilder $container) |
|
73
|
|
|
{ |
|
74
|
1 |
|
$yamlLoader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
75
|
|
|
|
|
76
|
1 |
|
$container->setParameter('darkilliant_import_field_entity_resolver', $mergedConfig['fields_entity_resolver']); |
|
77
|
1 |
|
$container->setParameter('darkilliant_import_entity_resolver_cache', $mergedConfig['entity_resolver_cache']); |
|
78
|
|
|
|
|
79
|
1 |
|
$yamlLoader->load('services.yml'); |
|
80
|
1 |
|
$yamlLoader->load('transformers.yml'); |
|
81
|
|
|
|
|
82
|
|
|
// Only when doctrine is in project |
|
83
|
1 |
|
if (in_array(DoctrineBundle::class, $container->getParameter('kernel.bundles'))) { |
|
84
|
1 |
|
$yamlLoader->load('symfony_serializer.yml'); |
|
85
|
|
|
|
|
86
|
|
|
// Only when jms is in project |
|
87
|
1 |
|
if (in_array(JMSSerializerBundle::class, $container->getParameter('kernel.bundles'))) { |
|
88
|
1 |
|
$yamlLoader->load('jms_serializer.yml'); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
1 |
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
private function resolvePath(string $path): string |
|
94
|
|
|
{ |
|
95
|
1 |
|
return str_replace('file://', '', $path); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|