-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Plugin.php
186 lines (158 loc) · 5.93 KB
/
Plugin.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/*
* This file is part of Contao.
*
* Copyright (c) 2005-2017 Leo Feyer
*
* @license LGPL-3.0+
*/
namespace Contao\ManagerBundle\ContaoManager;
use Contao\ManagerPlugin\Bundle\BundlePluginInterface;
use Contao\ManagerPlugin\Bundle\Parser\ParserInterface;
use Contao\ManagerPlugin\Config\ConfigPluginInterface;
use Contao\ManagerPlugin\Config\ContainerBuilder as PluginContainerBuilder;
use Contao\ManagerPlugin\Config\ExtensionPluginInterface;
use Contao\ManagerPlugin\Routing\RoutingPluginInterface;
use Doctrine\DBAL\Exception\ConnectionException;
use Doctrine\DBAL\DriverManager;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Loader\LoaderResolverInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
/**
* Plugin for the Contao Manager.
*
* @author Andreas Schempp <https://github.com/aschempp>
*/
class Plugin implements BundlePluginInterface, ConfigPluginInterface, RoutingPluginInterface, ExtensionPluginInterface
{
/**
* @var string|null
*/
private static $autoloadModules = null;
/**
* {@inheritdoc}
*/
public function getBundles(ParserInterface $parser)
{
$configs = $parser->parse(__DIR__.'/../Resources/contao-manager/bundles.json');
if (null !== static::$autoloadModules && file_exists(static::$autoloadModules)) {
/** @var Finder $modules */
$modules = (new Finder())
->directories()
->depth(0)
->in(static::$autoloadModules)
;
foreach ($modules as $module) {
if (file_exists($module->getPathname().'/.skip')) {
continue;
}
$configs = array_merge($configs, $parser->parse($module->getFilename(), 'ini'));
}
}
return $configs;
}
/**
* {@inheritdoc}
*/
public function registerContainerConfiguration(LoaderInterface $loader, array $managerConfig)
{
$loader->load('@ContaoManagerBundle/Resources/contao-manager/framework.yml');
$loader->load('@ContaoManagerBundle/Resources/contao-manager/security.yml');
$loader->load('@ContaoManagerBundle/Resources/contao-manager/contao.yml');
$loader->load('@ContaoManagerBundle/Resources/contao-manager/twig.yml');
$loader->load('@ContaoManagerBundle/Resources/contao-manager/doctrine.yml');
$loader->load('@ContaoManagerBundle/Resources/contao-manager/swiftmailer.yml');
$loader->load('@ContaoManagerBundle/Resources/contao-manager/monolog.yml');
$loader->load('@ContaoManagerBundle/Resources/contao-manager/lexik_maintenance.yml');
$loader->load('@ContaoManagerBundle/Resources/contao-manager/nelmio_security.yml');
$loader->load(function (ContainerBuilder $container) use ($loader) {
if ('dev' === $container->getParameter('kernel.environment')) {
$loader->load('@ContaoManagerBundle/Resources/contao-manager/web_profiler.yml');
}
});
}
/**
* {@inheritdoc}
*/
public function getRouteCollection(LoaderResolverInterface $resolver, KernelInterface $kernel)
{
if ('dev' !== $kernel->getEnvironment()) {
return null;
}
$collections = [];
$files = [
'_wdt' => '@WebProfilerBundle/Resources/config/routing/wdt.xml',
'_profiler' => '@WebProfilerBundle/Resources/config/routing/profiler.xml',
];
foreach ($files as $prefix => $file) {
/** @var RouteCollection $collection */
$collection = $resolver->resolve($file)->load($file);
$collection->addPrefix($prefix);
$collections[] = $collection;
}
$collection = array_reduce(
$collections,
function (RouteCollection $carry, RouteCollection $item) {
$carry->addCollection($item);
return $carry;
},
new RouteCollection()
);
// Redirect the deprecated install.php file
$collection->add('contao_install_redirect', new Route('/install.php', [
'_scope' => 'backend',
'_controller' => 'FrameworkBundle:Redirect:redirect',
'route' => 'contao_install',
'permanent' => true,
]));
return $collection;
}
/**
* {@inheritdoc}
*/
public function getExtensionConfig($extensionName, array $extensionConfigs, PluginContainerBuilder $container)
{
if ('doctrine' !== $extensionName) {
return $extensionConfigs;
}
$params = [];
foreach ($extensionConfigs as $extensionConfig) {
if (isset($extensionConfig['dbal']['connections']['default'])) {
$params = array_merge($params, $extensionConfig['dbal']['connections']['default']);
}
}
$parameterBag = $container->getParameterBag();
foreach ($params as $key => $value) {
$params[$key] = $parameterBag->resolveValue($value);
}
try {
$connection = DriverManager::getConnection($params);
$connection->connect();
$connection->close();
} catch (ConnectionException $e) {
$extensionConfigs[] = [
'dbal' => [
'connections' => [
'default' => [
'server_version' => '5.1',
],
],
],
];
}
return $extensionConfigs;
}
/**
* Sets path to enable autoloading of legacy Contao modules.
*
* @param string $modulePath
*/
public static function autoloadModules($modulePath)
{
static::$autoloadModules = $modulePath;
}
}