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\ResourceBundle\DependencyInjection; |
13
|
|
|
|
14
|
|
|
use Sylius\Bundle\ResourceBundle\DependencyInjection\Driver\DriverProvider; |
15
|
|
|
use Sylius\Component\Resource\Metadata\Metadata; |
16
|
|
|
use Symfony\Component\Config\Definition\Processor; |
17
|
|
|
use Symfony\Component\Config\FileLocator; |
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
19
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension; |
20
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
21
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
22
|
|
|
use Symfony\Component\Config\Loader\LoaderInterface; |
23
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
27
|
|
|
* @author Gonzalo Vilaseca <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class SyliusResourceExtension extends Extension |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function load(array $config, ContainerBuilder $container) |
35
|
|
|
{ |
36
|
|
|
$config = $this->processConfiguration($this->getConfiguration($config, $container), $config); |
37
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
38
|
|
|
|
39
|
|
|
$configFiles = [ |
40
|
|
|
'services.xml', |
41
|
|
|
'controller.xml', |
42
|
|
|
'storage.xml', |
43
|
|
|
'routing.xml', |
44
|
|
|
'twig.xml', |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
foreach ($configFiles as $configFile) { |
48
|
|
|
$loader->load($configFile); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$bundles = $container->getParameter('kernel.bundles'); |
52
|
|
|
if (array_key_exists('SyliusGridBundle', $bundles)) { |
53
|
|
|
$loader->load('grid.xml'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($config['translation']['enabled']) { |
57
|
|
|
$loader->load('translation.xml'); |
58
|
|
|
|
59
|
|
|
$container->setParameter('sylius.translation.default_locale', $config['translation']['default_locale']); |
60
|
|
|
$container->setAlias('sylius.translation.locale_provider', $config['translation']['locale_provider']); |
61
|
|
|
$container->setAlias('sylius.translation.available_locales_provider', $config['translation']['available_locales_provider']); |
62
|
|
|
$container->setParameter('sylius.translation.available_locales', $config['translation']['available_locales']); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$container->setParameter('sylius.resource.settings', $config['settings']); |
66
|
|
|
$container->setAlias('sylius.resource_controller.authorization_checker', $config['authorization_checker']); |
67
|
|
|
|
68
|
|
|
$this->loadPersistence($config['drivers'], $config['resources'], $loader); |
69
|
|
|
$this->loadResources($config['resources'], $container); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function loadPersistence(array $enabledDrivers, array $resources, LoaderInterface $loader) |
73
|
|
|
{ |
74
|
|
|
foreach ($resources as $alias => $resource) { |
75
|
|
|
if (!in_array($resource['driver'], $enabledDrivers)) { |
76
|
|
|
throw new InvalidArgumentException(sprintf( |
77
|
|
|
'Resource "%s" uses driver "%s", but this driver has not been enabled.', |
78
|
|
|
$alias, $resource['driver'] |
79
|
|
|
)); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
foreach ($enabledDrivers as $enabledDriver) { |
84
|
|
|
$loader->load(sprintf('driver/%s.xml', $enabledDriver)); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private function loadResources(array $resources, ContainerBuilder $container) |
89
|
|
|
{ |
90
|
|
|
foreach ($resources as $alias => $resourceConfig) { |
91
|
|
|
$metadata = Metadata::fromAliasAndConfiguration($alias, $resourceConfig); |
92
|
|
|
|
93
|
|
|
$resources = $container->hasParameter('sylius.resources') ? $container->getParameter('sylius.resources') : []; |
94
|
|
|
$resources = array_merge($resources, [$alias => $resourceConfig]); |
95
|
|
|
$container->setParameter('sylius.resources', $resources); |
96
|
|
|
|
97
|
|
|
DriverProvider::get($metadata)->load($container, $metadata); |
98
|
|
|
|
99
|
|
|
if ($metadata->hasParameter('translation')) { |
100
|
|
|
$alias = $alias.'_translation'; |
101
|
|
|
$resourceConfig = array_merge(['driver' => $resourceConfig['driver']], $resourceConfig['translation']); |
102
|
|
|
|
103
|
|
|
$resources = $container->hasParameter('sylius.resources') ? $container->getParameter('sylius.resources') : []; |
104
|
|
|
$resources = array_merge($resources, [$alias => $resourceConfig]); |
105
|
|
|
$container->setParameter('sylius.resources', $resources); |
106
|
|
|
|
107
|
|
|
$metadata = Metadata::fromAliasAndConfiguration($alias, $resourceConfig); |
108
|
|
|
|
109
|
|
|
DriverProvider::get($metadata)->load($container, $metadata); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|