|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrs\SonataImportBundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
6
|
|
|
use Symfony\Component\Config\FileLocator; |
|
7
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* This is the class that loads and manages your bundle configuration |
|
12
|
|
|
* |
|
13
|
|
|
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} |
|
14
|
|
|
*/ |
|
15
|
|
|
class DoctrsSonataImportExtension extends Extension |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var ContainerBuilder |
|
20
|
|
|
*/ |
|
21
|
|
|
private $container; |
|
22
|
|
|
/** |
|
23
|
|
|
* {@inheritdoc} |
|
24
|
|
|
*/ |
|
25
|
|
|
public function load(array $configs, ContainerBuilder $container) |
|
26
|
|
|
{ |
|
27
|
|
|
$configuration = new Configuration(); |
|
28
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
|
29
|
|
|
$this->prepairConfig($config, $container); |
|
30
|
|
|
|
|
31
|
|
|
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
|
32
|
|
|
$loader->load('services.xml'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param array $config |
|
37
|
|
|
* @param ContainerBuilder $container |
|
38
|
|
|
*/ |
|
39
|
|
|
private function prepairConfig(array $config, ContainerBuilder $container){ |
|
40
|
|
|
$this->container = $container; |
|
41
|
|
|
|
|
42
|
|
|
$this->prepareConfigMappings($config); |
|
43
|
|
|
$this->prepareConfigEncode($config); |
|
44
|
|
|
$this->prepareConfigUploadDir($config); |
|
45
|
|
|
|
|
46
|
|
|
$parametersArray = [ |
|
47
|
|
|
'doctrs_sonata_import.mappings' => $config['mappings'], |
|
48
|
|
|
'doctrs_sonata_import.upload_dir' => $config['upload_dir'], |
|
49
|
|
|
'doctrs_sonata_import.class_loaders' => $config['class_loaders'], |
|
50
|
|
|
'doctrs_sonata_import.encode.default' => $config['encode']['default'], |
|
51
|
|
|
'doctrs_sonata_import.encode.list' => $config['encode']['list'] |
|
52
|
|
|
]; |
|
53
|
|
|
|
|
54
|
|
|
foreach($parametersArray as $parameterKey => $value){ |
|
55
|
|
|
$container->setParameter($parameterKey, $value); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param array $config |
|
61
|
|
|
*/ |
|
62
|
|
|
private function prepareConfigUploadDir(array &$config){ |
|
63
|
|
|
$config['upload_dir'] = $config['upload_dir'] ? |
|
64
|
|
|
$config['upload_dir'] : $this->container->get('kernel')->getRootDir() . '/../web/uploads'; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param array $config |
|
69
|
|
|
*/ |
|
70
|
|
|
private function prepareConfigEncode(array &$config){ |
|
71
|
|
|
if (!isset($config['encode'])) { |
|
72
|
|
|
$config['encode'] = [ |
|
73
|
|
|
'default' => 'utf8', |
|
74
|
|
|
'list' => [] |
|
75
|
|
|
]; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param array $config |
|
81
|
|
|
*/ |
|
82
|
|
|
private function prepareConfigMappings(array &$config){ |
|
83
|
|
|
$config['mappings'] = array_merge($config['mappings'], [[ |
|
84
|
|
|
'name' => 'date', |
|
85
|
|
|
'class' => 'doctrs.type.datetime' |
|
86
|
|
|
],[ |
|
87
|
|
|
'name' => 'datetime', |
|
88
|
|
|
'class' => 'doctrs.type.datetime' |
|
89
|
|
|
],[ |
|
90
|
|
|
'name' => 'boolean', |
|
91
|
|
|
'class' => 'doctrs.type.boolean' |
|
92
|
|
|
],[ |
|
93
|
|
|
'name' => 'integer', |
|
94
|
|
|
'class' => 'doctrs.type.integer' |
|
95
|
|
|
],[ |
|
96
|
|
|
'name' => 'entity', |
|
97
|
|
|
'class' => 'doctrs.type.entity' |
|
98
|
|
|
],[ |
|
99
|
|
|
'name' => 'choice', |
|
100
|
|
|
'class' => 'doctrs.type.entity' |
|
101
|
|
|
]]); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|