-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathMetaModelsAttributeFileExtension.php
More file actions
131 lines (116 loc) · 4.85 KB
/
MetaModelsAttributeFileExtension.php
File metadata and controls
131 lines (116 loc) · 4.85 KB
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
123
124
125
126
127
128
129
130
131
<?php
/**
* This file is part of MetaModels/attribute_file.
*
* (c) 2012-2025 The MetaModels team.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* This project is provided in good faith and hope to be usable by anyone.
*
* @package MetaModels/attribute_file
* @author Christian Schiffler <c.schiffler@cyberspectrum.de>
* @author Sven Baumann <baumann.sv@gmail.com>
* @author Ingolf Steinhardt <info@e-spin.de>
* @copyright 2012-2025 The MetaModels team.
* @license https://github.com/MetaModels/attribute_file/blob/master/LICENSE LGPL-3.0-or-later
* @filesource
*/
namespace MetaModels\AttributeFileBundle\DependencyInjection;
use InspiredMinds\ContaoFileUsage\ContaoFileUsageBundle;
use MetaModels\AttributeFileBundle\EventListener\DcGeneral\Table\DcaSetting\FileWidgetModeOptions;
use MetaModels\ContaoFrontendEditingBundle\MetaModelsContaoFrontendEditingBundle;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader;
use function assert;
use function in_array;
use function is_array;
use function is_bool;
/**
* This is the class that loads and manages the bundle configuration
*/
class MetaModelsAttributeFileExtension extends Extension
{
/**
* {@inheritDoc}
*
* @SuppressWarnings(PHPMD.LongVariable)
*/
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('factory.yml');
$loader->load('event_listener.yml');
$loader->load('services.yml');
$configuration = $this->getConfiguration($configs, $container);
assert($configuration instanceof Configuration);
$config = $this->processConfiguration($configuration, $configs);
$this->buildCacheService($container, $config);
$bundles = $container->getParameter('kernel.bundles');
assert(is_array($bundles));
// Load configuration for the frontend editing extension.
$frontendEditing = false;
if (in_array(MetaModelsContaoFrontendEditingBundle::class, $bundles, true)) {
$frontendEditing = true;
$loader->load('frontend_editing/event_listener.yml');
}
$this->addFrontendEditingArgument($container, $frontendEditing);
// Load configuration for the file usage extension.
if (in_array(ContaoFileUsageBundle::class, $bundles, true) && (bool) ($config['file_usage'] ?? false)) {
$loader->load('file_usage/services.yml');
}
// Schema manager
$typeNames = $container->hasParameter('metamodels.managed-schema-type-names')
? $container->getParameter('metamodels.managed-schema-type-names')
: null;
$managedSchemaTypeNames = is_array($typeNames) ? $typeNames : [];
$managedSchemaTypeNames[] = 'file';
$container->setParameter('metamodels.managed-schema-type-names', $managedSchemaTypeNames);
}
/**
* {@inheritDoc}
*/
public function getConfiguration(array $config, ContainerBuilder $container): ?ConfigurationInterface
{
$debug = $container->getParameter('kernel.debug');
assert(is_bool($debug));
return new Configuration($debug);
}
/**
* Build the cache service.
*
* @param ContainerBuilder $container The container builder.
* @param array $config The configuration.
*
* @return void
*/
private function buildCacheService(ContainerBuilder $container, array $config): void
{
// If cache disabled, swap it out with the dummy cache.
if (!$config['enable_cache']) {
$cache = $container->getDefinition('metamodels.attribute_file.cache_system');
$cache->setClass(ArrayAdapter::class);
$cache->setArguments([]);
$container->setParameter('metamodels.attribute_file.cache_dir', null);
return;
}
$container->setParameter('metamodels.attribute_file.cache_dir', $config['cache_dir']);
}
/**
* Add the frontend editing argument to service, who it used.
*
* @param ContainerBuilder $container The container builder.
* @param bool $frontendEditing Is frontend editing extension installed.
*
* @return void
*/
private function addFrontendEditingArgument(ContainerBuilder $container, bool $frontendEditing): void
{
$container->getDefinition(FileWidgetModeOptions::class)->setArgument('$frontendEditing', $frontendEditing);
}
}