sonata-project /
SonataMediaBundle
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | /* |
||
| 6 | * This file is part of the Sonata Project package. |
||
| 7 | * |
||
| 8 | * (c) Thomas Rabaix <[email protected]> |
||
| 9 | * |
||
| 10 | * For the full copyright and license information, please view the LICENSE |
||
| 11 | * file that was distributed with this source code. |
||
| 12 | */ |
||
| 13 | |||
| 14 | namespace Sonata\MediaBundle\DependencyInjection; |
||
| 15 | |||
| 16 | use Sonata\ClassificationBundle\Model\CategoryInterface; |
||
| 17 | use Sonata\Doctrine\Mapper\Builder\OptionsBuilder; |
||
| 18 | use Sonata\Doctrine\Mapper\DoctrineCollector; |
||
| 19 | use Sonata\EasyExtendsBundle\Mapper\DoctrineCollector as DeprecatedDoctrineCollector; |
||
| 20 | use Symfony\Component\Config\Definition\Processor; |
||
| 21 | use Symfony\Component\Config\FileLocator; |
||
| 22 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
||
| 23 | use Symfony\Component\DependencyInjection\Definition; |
||
| 24 | use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
||
| 25 | use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
||
| 26 | use Symfony\Component\DependencyInjection\Reference; |
||
| 27 | use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @final since sonata-project/media-bundle 3.21.0 |
||
| 31 | * |
||
| 32 | * @author Thomas Rabaix <[email protected]> |
||
| 33 | */ |
||
| 34 | class SonataMediaExtension extends Extension implements PrependExtensionInterface |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | private $bundleConfigs; |
||
| 40 | |||
| 41 | public function load(array $configs, ContainerBuilder $container): void |
||
| 42 | { |
||
| 43 | $processor = new Processor(); |
||
| 44 | $configuration = new Configuration(); |
||
| 45 | $config = $processor->processConfiguration($configuration, $configs); |
||
| 46 | |||
| 47 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
||
| 48 | $loader->load('provider.xml'); |
||
| 49 | $loader->load('media.xml'); |
||
| 50 | $loader->load('twig.xml'); |
||
| 51 | $loader->load('security.xml'); |
||
| 52 | $loader->load('extra.xml'); |
||
| 53 | $loader->load('form.xml'); |
||
| 54 | $loader->load('gaufrette.xml'); |
||
| 55 | $loader->load('validators.xml'); |
||
| 56 | $loader->load('serializer.xml'); |
||
| 57 | $loader->load('command.xml'); |
||
| 58 | |||
| 59 | $bundles = $container->getParameter('kernel.bundles'); |
||
| 60 | |||
| 61 | if (isset($bundles['FOSRestBundle'], $bundles['NelmioApiDocBundle'])) { |
||
| 62 | $loader->load(sprintf('api_form_%s.xml', $config['db_driver'])); |
||
| 63 | |||
| 64 | if ('doctrine_orm' === $config['db_driver']) { |
||
| 65 | $loader->load('api_controllers.xml'); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | if (isset($bundles['SonataNotificationBundle'])) { |
||
| 70 | $loader->load('consumer.xml'); |
||
| 71 | } |
||
| 72 | |||
| 73 | if (isset($bundles['SonataFormatterBundle'])) { |
||
| 74 | $loader->load('formatter.xml'); |
||
| 75 | } |
||
| 76 | |||
| 77 | if (isset($bundles['SonataBlockBundle'])) { |
||
| 78 | $loader->load('block.xml'); |
||
| 79 | } |
||
| 80 | |||
| 81 | if (isset($bundles['SonataSeoBundle'])) { |
||
| 82 | $loader->load('seo_block.xml'); |
||
| 83 | } |
||
| 84 | |||
| 85 | if (!isset($bundles['LiipImagineBundle'])) { |
||
| 86 | $container->removeDefinition('sonata.media.thumbnail.liip_imagine'); |
||
| 87 | } |
||
| 88 | |||
| 89 | if ($this->isClassificationEnabled($config)) { |
||
| 90 | $loader->load('category.xml'); |
||
| 91 | $categoryManagerAlias = 'sonata.media.manager.category'; |
||
| 92 | $container->setAlias($categoryManagerAlias, $config['category_manager']); |
||
| 93 | $categoryManager = $container->getAlias($categoryManagerAlias); |
||
| 94 | $categoryManager->setPublic(true); |
||
| 95 | } |
||
| 96 | |||
| 97 | if (!\array_key_exists($config['default_context'], $config['contexts'])) { |
||
| 98 | throw new \InvalidArgumentException(sprintf('SonataMediaBundle - Invalid default context : %s, available : %s', $config['default_context'], json_encode(array_keys($config['contexts'])))); |
||
| 99 | } |
||
| 100 | |||
| 101 | $loader->load(sprintf('%s.xml', $config['db_driver'])); |
||
| 102 | |||
| 103 | if (isset($bundles['SonataAdminBundle'])) { |
||
| 104 | $loader->load(sprintf('%s_admin.xml', $config['db_driver'])); |
||
| 105 | |||
| 106 | $sonataAdminConfig = $this->bundleConfigs['SonataAdminBundle']; |
||
| 107 | |||
| 108 | $sonataRoles = []; |
||
| 109 | if (isset($sonataAdminConfig['security']['role_admin'])) { |
||
| 110 | $sonataRoles[] = $sonataAdminConfig['security']['role_admin']; |
||
| 111 | } else { |
||
| 112 | $sonataRoles[] = 'ROLE_ADMIN'; |
||
| 113 | } |
||
| 114 | |||
| 115 | if (isset($sonataAdminConfig['security']['role_super_admin'])) { |
||
| 116 | $sonataRoles[] = $sonataAdminConfig['security']['role_super_admin']; |
||
| 117 | } else { |
||
| 118 | $sonataRoles[] = 'ROLE_SUPER_ADMIN'; |
||
| 119 | } |
||
| 120 | |||
| 121 | $container->getDefinition('sonata.media.security.superadmin_strategy') |
||
| 122 | ->replaceArgument(2, $sonataRoles); |
||
| 123 | } |
||
| 124 | |||
| 125 | $this->configureFilesystemAdapter($container, $config); |
||
| 126 | $this->configureCdnAdapter($container, $config); |
||
| 127 | |||
| 128 | $pool = $container->getDefinition('sonata.media.pool'); |
||
| 129 | $pool->replaceArgument(0, $config['default_context']); |
||
| 130 | |||
| 131 | $strategies = []; |
||
| 132 | |||
| 133 | foreach ($config['contexts'] as $name => $settings) { |
||
| 134 | $formats = []; |
||
| 135 | |||
| 136 | foreach ($settings['formats'] as $format => $value) { |
||
| 137 | $formats[$name.'_'.$format] = $value; |
||
| 138 | } |
||
| 139 | |||
| 140 | $strategies[] = $settings['download']['strategy']; |
||
| 141 | $pool->addMethodCall('addContext', [$name, $settings['providers'], $formats, $settings['download']]); |
||
| 142 | } |
||
| 143 | |||
| 144 | $container->setParameter('sonata.media.admin_format', $config['admin_format']); |
||
| 145 | |||
| 146 | $strategies = array_unique($strategies); |
||
| 147 | |||
| 148 | foreach ($strategies as $strategyId) { |
||
| 149 | $pool->addMethodCall('addDownloadStrategy', [$strategyId, new Reference($strategyId)]); |
||
| 150 | } |
||
| 151 | |||
| 152 | if ('doctrine_orm' === $config['db_driver']) { |
||
| 153 | if (isset($bundles['SonataDoctrineBundle'])) { |
||
| 154 | $this->registerSonataDoctrineMapping($config); |
||
| 155 | } else { |
||
| 156 | // NEXT MAJOR: Remove next line and throw error when not registering SonataDoctrineBundle |
||
| 157 | $this->registerDoctrineMapping($config); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | $container->setParameter('sonata.media.resizer.simple.adapter.mode', $config['resizer']['simple']['mode']); |
||
| 162 | $container->setParameter('sonata.media.resizer.square.adapter.mode', $config['resizer']['square']['mode']); |
||
| 163 | |||
| 164 | $this->configureParameterClass($container, $config); |
||
| 165 | $this->configureExtra($container, $config); |
||
| 166 | $this->configureBuzz($container, $config); |
||
| 167 | $this->configureProviders($container, $config); |
||
| 168 | $this->configureAdapters($container, $config); |
||
| 169 | $this->configureResizers($container, $config); |
||
| 170 | } |
||
| 171 | |||
| 172 | public function configureProviders(ContainerBuilder $container, array $config): void |
||
| 173 | { |
||
| 174 | $container->getDefinition('sonata.media.provider.image') |
||
| 175 | ->replaceArgument(5, array_map('strtolower', $config['providers']['image']['allowed_extensions'])) |
||
| 176 | ->replaceArgument(6, $config['providers']['image']['allowed_mime_types']) |
||
| 177 | ->replaceArgument(7, new Reference($config['providers']['image']['adapter'])) |
||
| 178 | ; |
||
| 179 | |||
| 180 | $container->getDefinition('sonata.media.provider.file') |
||
| 181 | ->replaceArgument(5, $config['providers']['file']['allowed_extensions']) |
||
| 182 | ->replaceArgument(6, $config['providers']['file']['allowed_mime_types']) |
||
| 183 | ; |
||
| 184 | |||
| 185 | $container->getDefinition('sonata.media.provider.youtube')->replaceArgument(7, $config['providers']['youtube']['html5']); |
||
| 186 | } |
||
| 187 | |||
| 188 | public function configureBuzz(ContainerBuilder $container, array $config): void |
||
| 189 | { |
||
| 190 | $container->getDefinition('sonata.media.buzz.browser') |
||
| 191 | ->replaceArgument(0, new Reference($config['buzz']['connector'])); |
||
| 192 | |||
| 193 | foreach ([ |
||
| 194 | 'sonata.media.buzz.connector.curl', |
||
| 195 | 'sonata.media.buzz.connector.file_get_contents', |
||
| 196 | ] as $connector) { |
||
| 197 | $container->getDefinition($connector) |
||
| 198 | ->addMethodCall('setIgnoreErrors', [$config['buzz']['client']['ignore_errors']]) |
||
| 199 | ->addMethodCall('setMaxRedirects', [$config['buzz']['client']['max_redirects']]) |
||
| 200 | ->addMethodCall('setTimeout', [$config['buzz']['client']['timeout']]) |
||
| 201 | ->addMethodCall('setVerifyPeer', [$config['buzz']['client']['verify_peer']]) |
||
| 202 | ->addMethodCall('setProxy', [$config['buzz']['client']['proxy']]); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | public function configureParameterClass(ContainerBuilder $container, array $config): void |
||
| 207 | { |
||
| 208 | $container->setParameter('sonata.media.admin.media.entity', $config['class']['media']); |
||
| 209 | $container->setParameter('sonata.media.admin.gallery.entity', $config['class']['gallery']); |
||
| 210 | $container->setParameter('sonata.media.admin.gallery_item.entity', $config['class']['gallery_item']); |
||
| 211 | |||
| 212 | $container->setParameter('sonata.media.media.class', $config['class']['media']); |
||
| 213 | $container->setParameter('sonata.media.gallery.class', $config['class']['gallery']); |
||
| 214 | |||
| 215 | $container->getDefinition('sonata.media.form.type.media')->replaceArgument(1, $config['class']['media']); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * NEXT_MAJOR: Remove this method. |
||
| 220 | */ |
||
| 221 | public function registerDoctrineMapping(array $config): void |
||
| 222 | { |
||
| 223 | @trigger_error( |
||
|
0 ignored issues
–
show
|
|||
| 224 | 'Using SonataEasyExtendsBundle is deprecated since sonata-project/media-bundle 3.26. Please register SonataDoctrineBundle as a bundle instead.', |
||
| 225 | E_USER_DEPRECATED |
||
| 226 | ); |
||
| 227 | |||
| 228 | $collector = DeprecatedDoctrineCollector::getInstance(); |
||
| 229 | |||
| 230 | $collector->addAssociation($config['class']['media'], 'mapOneToMany', [ |
||
| 231 | 'fieldName' => 'galleryItems', |
||
| 232 | 'targetEntity' => $config['class']['gallery_item'], |
||
| 233 | 'cascade' => [ |
||
| 234 | 'persist', |
||
| 235 | ], |
||
| 236 | 'mappedBy' => 'media', |
||
| 237 | 'orphanRemoval' => false, |
||
| 238 | ]); |
||
| 239 | |||
| 240 | $collector->addAssociation($config['class']['gallery_item'], 'mapManyToOne', [ |
||
| 241 | 'fieldName' => 'gallery', |
||
| 242 | 'targetEntity' => $config['class']['gallery'], |
||
| 243 | 'cascade' => [ |
||
| 244 | 'persist', |
||
| 245 | ], |
||
| 246 | 'mappedBy' => null, |
||
| 247 | 'inversedBy' => 'galleryItems', |
||
| 248 | 'joinColumns' => [ |
||
| 249 | [ |
||
| 250 | 'name' => 'gallery_id', |
||
| 251 | 'referencedColumnName' => 'id', |
||
| 252 | 'onDelete' => 'CASCADE', |
||
| 253 | ], |
||
| 254 | ], |
||
| 255 | 'orphanRemoval' => false, |
||
| 256 | ]); |
||
| 257 | |||
| 258 | $collector->addAssociation($config['class']['gallery_item'], 'mapManyToOne', [ |
||
| 259 | 'fieldName' => 'media', |
||
| 260 | 'targetEntity' => $config['class']['media'], |
||
| 261 | 'cascade' => [ |
||
| 262 | 'persist', |
||
| 263 | ], |
||
| 264 | 'mappedBy' => null, |
||
| 265 | 'inversedBy' => 'galleryItems', |
||
| 266 | 'joinColumns' => [ |
||
| 267 | [ |
||
| 268 | 'name' => 'media_id', |
||
| 269 | 'referencedColumnName' => 'id', |
||
| 270 | 'onDelete' => 'CASCADE', |
||
| 271 | ], |
||
| 272 | ], |
||
| 273 | 'orphanRemoval' => false, |
||
| 274 | ]); |
||
| 275 | |||
| 276 | $collector->addAssociation($config['class']['gallery'], 'mapOneToMany', [ |
||
| 277 | 'fieldName' => 'galleryItems', |
||
| 278 | 'targetEntity' => $config['class']['gallery_item'], |
||
| 279 | 'cascade' => [ |
||
| 280 | 'persist', |
||
| 281 | ], |
||
| 282 | 'mappedBy' => 'gallery', |
||
| 283 | 'orphanRemoval' => true, |
||
| 284 | 'orderBy' => [ |
||
| 285 | 'position' => 'ASC', |
||
| 286 | ], |
||
| 287 | ]); |
||
| 288 | |||
| 289 | if ($this->isClassificationEnabled($config)) { |
||
| 290 | $collector->addAssociation($config['class']['media'], 'mapManyToOne', [ |
||
| 291 | 'fieldName' => 'category', |
||
| 292 | 'targetEntity' => $config['class']['category'], |
||
| 293 | 'cascade' => [ |
||
| 294 | 'persist', |
||
| 295 | ], |
||
| 296 | 'mappedBy' => null, |
||
| 297 | 'inversedBy' => null, |
||
| 298 | 'joinColumns' => [ |
||
| 299 | [ |
||
| 300 | 'name' => 'category_id', |
||
| 301 | 'referencedColumnName' => 'id', |
||
| 302 | 'onDelete' => 'SET NULL', |
||
| 303 | ], |
||
| 304 | ], |
||
| 305 | 'orphanRemoval' => false, |
||
| 306 | ]); |
||
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Inject CDN dependency to default provider. |
||
| 312 | */ |
||
| 313 | public function configureCdnAdapter(ContainerBuilder $container, array $config): void |
||
| 314 | { |
||
| 315 | // add the default configuration for the server cdn |
||
| 316 | if ($container->hasDefinition('sonata.media.cdn.server') && isset($config['cdn']['server'])) { |
||
| 317 | $container->getDefinition('sonata.media.cdn.server') |
||
| 318 | ->replaceArgument(0, $config['cdn']['server']['path']) |
||
| 319 | ; |
||
| 320 | } else { |
||
| 321 | $container->removeDefinition('sonata.media.cdn.server'); |
||
| 322 | } |
||
| 323 | |||
| 324 | if ($container->hasDefinition('sonata.media.cdn.panther') && isset($config['cdn']['panther'])) { |
||
| 325 | $container->getDefinition('sonata.media.cdn.panther') |
||
| 326 | ->replaceArgument(0, $config['cdn']['panther']['path']) |
||
| 327 | ->replaceArgument(1, $config['cdn']['panther']['username']) |
||
| 328 | ->replaceArgument(2, $config['cdn']['panther']['password']) |
||
| 329 | ->replaceArgument(3, $config['cdn']['panther']['site_id']) |
||
| 330 | ; |
||
| 331 | } else { |
||
| 332 | $container->removeDefinition('sonata.media.cdn.panther'); |
||
| 333 | } |
||
| 334 | |||
| 335 | if ($container->hasDefinition('sonata.media.cdn.cloudfront') && isset($config['cdn']['cloudfront'])) { |
||
| 336 | $container->getDefinition('sonata.media.cdn.cloudfront') |
||
| 337 | ->replaceArgument(0, $config['cdn']['cloudfront']['path']) |
||
| 338 | ->replaceArgument(1, $config['cdn']['cloudfront']['key']) |
||
| 339 | ->replaceArgument(2, $config['cdn']['cloudfront']['secret']) |
||
| 340 | ->replaceArgument(3, $config['cdn']['cloudfront']['distribution_id']) |
||
| 341 | ; |
||
| 342 | } else { |
||
| 343 | $container->removeDefinition('sonata.media.cdn.cloudfront'); |
||
| 344 | } |
||
| 345 | |||
| 346 | if ($container->hasDefinition('sonata.media.cdn.fallback') && isset($config['cdn']['fallback'])) { |
||
| 347 | $container->getDefinition('sonata.media.cdn.fallback') |
||
| 348 | ->replaceArgument(0, new Reference($config['cdn']['fallback']['master'])) |
||
| 349 | ->replaceArgument(1, new Reference($config['cdn']['fallback']['fallback'])) |
||
| 350 | ; |
||
| 351 | } else { |
||
| 352 | $container->removeDefinition('sonata.media.cdn.fallback'); |
||
| 353 | } |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Inject filesystem dependency to default provider. |
||
| 358 | */ |
||
| 359 | public function configureFilesystemAdapter(ContainerBuilder $container, array $config): void |
||
| 360 | { |
||
| 361 | // add the default configuration for the local filesystem |
||
| 362 | if ($container->hasDefinition('sonata.media.adapter.filesystem.local') && isset($config['filesystem']['local'])) { |
||
| 363 | $container->getDefinition('sonata.media.adapter.filesystem.local') |
||
| 364 | ->addArgument($config['filesystem']['local']['directory']) |
||
| 365 | ->addArgument($config['filesystem']['local']['create']) |
||
| 366 | ; |
||
| 367 | } else { |
||
| 368 | $container->removeDefinition('sonata.media.adapter.filesystem.local'); |
||
| 369 | } |
||
| 370 | |||
| 371 | // add the default configuration for the FTP filesystem |
||
| 372 | if ($container->hasDefinition('sonata.media.adapter.filesystem.ftp') && isset($config['filesystem']['ftp'])) { |
||
| 373 | $container->getDefinition('sonata.media.adapter.filesystem.ftp') |
||
| 374 | ->addArgument($config['filesystem']['ftp']['directory']) |
||
| 375 | ->addArgument($config['filesystem']['ftp']['host']) |
||
| 376 | ->addArgument([ |
||
| 377 | 'port' => $config['filesystem']['ftp']['port'], |
||
| 378 | 'username' => $config['filesystem']['ftp']['username'], |
||
| 379 | 'password' => $config['filesystem']['ftp']['password'], |
||
| 380 | 'passive' => $config['filesystem']['ftp']['passive'], |
||
| 381 | 'create' => $config['filesystem']['ftp']['create'], |
||
| 382 | 'mode' => $config['filesystem']['ftp']['mode'], |
||
| 383 | ]) |
||
| 384 | ; |
||
| 385 | } else { |
||
| 386 | $container->removeDefinition('sonata.media.adapter.filesystem.ftp'); |
||
| 387 | $container->removeDefinition('sonata.media.filesystem.ftp'); |
||
| 388 | } |
||
| 389 | |||
| 390 | // add the default configuration for the S3 filesystem |
||
| 391 | if ($container->hasDefinition('sonata.media.adapter.filesystem.s3') && isset($config['filesystem']['s3'])) { |
||
| 392 | $container->getDefinition('sonata.media.adapter.filesystem.s3') |
||
| 393 | ->replaceArgument(0, new Reference('sonata.media.adapter.service.s3')) |
||
| 394 | ->replaceArgument(1, $config['filesystem']['s3']['bucket']) |
||
| 395 | ->replaceArgument(2, ['create' => $config['filesystem']['s3']['create'], 'region' => $config['filesystem']['s3']['region'], 'directory' => $config['filesystem']['s3']['directory'], 'ACL' => $config['filesystem']['s3']['acl']]) |
||
| 396 | ; |
||
| 397 | |||
| 398 | $container->getDefinition('sonata.media.metadata.amazon') |
||
| 399 | ->replaceArgument(0, [ |
||
| 400 | 'acl' => $config['filesystem']['s3']['acl'], |
||
| 401 | 'storage' => $config['filesystem']['s3']['storage'], |
||
| 402 | 'encryption' => $config['filesystem']['s3']['encryption'], |
||
| 403 | 'meta' => $config['filesystem']['s3']['meta'], |
||
| 404 | 'cache_control' => $config['filesystem']['s3']['cache_control'], |
||
| 405 | ]) |
||
| 406 | ; |
||
| 407 | |||
| 408 | if (3 === $config['filesystem']['s3']['sdk_version']) { |
||
| 409 | $arguments = [ |
||
| 410 | 'region' => $config['filesystem']['s3']['region'], |
||
| 411 | 'version' => $config['filesystem']['s3']['version'], |
||
| 412 | ]; |
||
| 413 | |||
| 414 | if (isset($config['filesystem']['s3']['endpoint'])) { |
||
| 415 | $arguments['endpoint'] = $config['filesystem']['s3']['endpoint']; |
||
| 416 | } |
||
| 417 | |||
| 418 | if (isset($config['filesystem']['s3']['secretKey'], $config['filesystem']['s3']['accessKey'])) { |
||
| 419 | $arguments['credentials'] = [ |
||
| 420 | 'secret' => $config['filesystem']['s3']['secretKey'], |
||
| 421 | 'key' => $config['filesystem']['s3']['accessKey'], |
||
| 422 | ]; |
||
| 423 | } |
||
| 424 | |||
| 425 | $container->getDefinition('sonata.media.adapter.service.s3') |
||
| 426 | ->replaceArgument(0, $arguments) |
||
| 427 | ; |
||
| 428 | } else { |
||
| 429 | $container->getDefinition('sonata.media.adapter.service.s3') |
||
| 430 | ->replaceArgument(0, [ |
||
| 431 | 'secret' => $config['filesystem']['s3']['secretKey'], |
||
| 432 | 'key' => $config['filesystem']['s3']['accessKey'], |
||
| 433 | ]) |
||
| 434 | ; |
||
| 435 | } |
||
| 436 | } else { |
||
| 437 | $container->removeDefinition('sonata.media.adapter.filesystem.s3'); |
||
| 438 | $container->removeDefinition('sonata.media.filesystem.s3'); |
||
| 439 | } |
||
| 440 | |||
| 441 | if ($container->hasDefinition('sonata.media.adapter.filesystem.replicate') && isset($config['filesystem']['replicate'])) { |
||
| 442 | $container->getDefinition('sonata.media.adapter.filesystem.replicate') |
||
| 443 | ->replaceArgument(0, new Reference($config['filesystem']['replicate']['master'])) |
||
| 444 | ->replaceArgument(1, new Reference($config['filesystem']['replicate']['slave'])) |
||
| 445 | ; |
||
| 446 | } else { |
||
| 447 | $container->removeDefinition('sonata.media.adapter.filesystem.replicate'); |
||
| 448 | $container->removeDefinition('sonata.media.filesystem.replicate'); |
||
| 449 | } |
||
| 450 | |||
| 451 | if ($container->hasDefinition('sonata.media.adapter.filesystem.mogilefs') && isset($config['filesystem']['mogilefs'])) { |
||
| 452 | $container->getDefinition('sonata.media.adapter.filesystem.mogilefs') |
||
| 453 | ->replaceArgument(0, $config['filesystem']['mogilefs']['domain']) |
||
| 454 | ->replaceArgument(1, $config['filesystem']['mogilefs']['hosts']) |
||
| 455 | ; |
||
| 456 | } else { |
||
| 457 | $container->removeDefinition('sonata.media.adapter.filesystem.mogilefs'); |
||
| 458 | $container->removeDefinition('sonata.media.filesystem.mogilefs'); |
||
| 459 | } |
||
| 460 | |||
| 461 | if ($container->hasDefinition('sonata.media.adapter.filesystem.opencloud') && |
||
| 462 | (isset($config['filesystem']['openstack']) || isset($config['filesystem']['rackspace']))) { |
||
| 463 | if (isset($config['filesystem']['openstack'])) { |
||
| 464 | $container->setParameter('sonata.media.adapter.filesystem.opencloud.class', 'OpenCloud\OpenStack'); |
||
| 465 | $settings = 'openstack'; |
||
| 466 | } else { |
||
| 467 | $container->setParameter('sonata.media.adapter.filesystem.opencloud.class', 'OpenCloud\Rackspace'); |
||
| 468 | $settings = 'rackspace'; |
||
| 469 | } |
||
| 470 | $container->getDefinition('sonata.media.adapter.filesystem.opencloud.connection') |
||
| 471 | ->replaceArgument(0, $config['filesystem'][$settings]['url']) |
||
| 472 | ->replaceArgument(1, $config['filesystem'][$settings]['secret']) |
||
| 473 | ; |
||
| 474 | $container->getDefinition('sonata.media.adapter.filesystem.opencloud') |
||
| 475 | ->replaceArgument(1, $config['filesystem'][$settings]['containerName']) |
||
| 476 | ->replaceArgument(2, $config['filesystem'][$settings]['create_container']); |
||
| 477 | $container->getDefinition('sonata.media.adapter.filesystem.opencloud.objectstore') |
||
| 478 | ->replaceArgument(1, $config['filesystem'][$settings]['region']); |
||
| 479 | } else { |
||
| 480 | $container->removeDefinition('sonata.media.adapter.filesystem.opencloud'); |
||
| 481 | $container->removeDefinition('sonata.media.adapter.filesystem.opencloud.connection'); |
||
| 482 | $container->removeDefinition('sonata.media.adapter.filesystem.opencloud.objectstore'); |
||
| 483 | $container->removeDefinition('sonata.media.filesystem.opencloud'); |
||
| 484 | } |
||
| 485 | } |
||
| 486 | |||
| 487 | public function configureExtra(ContainerBuilder $container, array $config): void |
||
| 488 | { |
||
| 489 | if ($config['pixlr']['enabled']) { |
||
| 490 | $container->getDefinition('sonata.media.extra.pixlr') |
||
| 491 | ->replaceArgument(0, $config['pixlr']['referrer']) |
||
| 492 | ->replaceArgument(1, $config['pixlr']['secret']) |
||
| 493 | ; |
||
| 494 | } else { |
||
| 495 | $container->removeDefinition('sonata.media.extra.pixlr'); |
||
| 496 | } |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Allow an extension to prepend the extension configurations. |
||
| 501 | */ |
||
| 502 | public function prepend(ContainerBuilder $container): void |
||
| 503 | { |
||
| 504 | $bundles = $container->getParameter('kernel.bundles'); |
||
| 505 | |||
| 506 | // Store SonataAdminBundle configuration for later use |
||
| 507 | if (isset($bundles['SonataAdminBundle'])) { |
||
| 508 | $this->bundleConfigs['SonataAdminBundle'] = current($container->getExtensionConfig('sonata_admin')); |
||
| 509 | } |
||
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Checks if the classification of media is enabled. |
||
| 514 | */ |
||
| 515 | private function isClassificationEnabled(array $config): bool |
||
| 516 | { |
||
| 517 | return interface_exists(CategoryInterface::class) |
||
| 518 | && !$config['force_disable_category']; |
||
| 519 | } |
||
| 520 | |||
| 521 | private function configureAdapters(ContainerBuilder $container, array $config): void |
||
| 522 | { |
||
| 523 | foreach (['gd', 'imagick', 'gmagick'] as $adapter) { |
||
| 524 | if ($container->hasParameter('sonata.media.adapter.image.'.$adapter.'.class')) { |
||
| 525 | $container->register( |
||
| 526 | 'sonata.media.adapter.image.'.$adapter, |
||
| 527 | $container->getParameter('sonata.media.adapter.image.'.$adapter.'.class') |
||
| 528 | ); |
||
| 529 | } |
||
| 530 | } |
||
| 531 | $container->setAlias('sonata.media.adapter.image.default', $config['adapters']['default']); |
||
| 532 | } |
||
| 533 | |||
| 534 | private function configureResizers(ContainerBuilder $container, array $config): void |
||
| 535 | { |
||
| 536 | if ($container->hasParameter('sonata.media.resizer.simple.class')) { |
||
| 537 | $class = $container->getParameter('sonata.media.resizer.simple.class'); |
||
| 538 | $definition = new Definition($class, [ |
||
| 539 | new Reference('sonata.media.adapter.image.default'), |
||
| 540 | '%sonata.media.resizer.simple.adapter.mode%', |
||
| 541 | new Reference('sonata.media.metadata.proxy'), |
||
| 542 | ]); |
||
| 543 | $definition->addTag('sonata.media.resizer'); |
||
| 544 | $container->setDefinition('sonata.media.resizer.simple', $definition); |
||
| 545 | } |
||
| 546 | |||
| 547 | if ($container->hasParameter('sonata.media.resizer.square.class')) { |
||
| 548 | $class = $container->getParameter('sonata.media.resizer.square.class'); |
||
| 549 | $definition = new Definition($class, [ |
||
| 550 | new Reference('sonata.media.adapter.image.default'), |
||
| 551 | '%sonata.media.resizer.square.adapter.mode%', |
||
| 552 | new Reference('sonata.media.metadata.proxy'), |
||
| 553 | ]); |
||
| 554 | $definition->addTag('sonata.media.resizer'); |
||
| 555 | $container->setDefinition('sonata.media.resizer.square', $definition); |
||
| 556 | } |
||
| 557 | |||
| 558 | $container->setAlias('sonata.media.resizer.default', $config['resizers']['default']); |
||
| 559 | } |
||
| 560 | |||
| 561 | private function registerSonataDoctrineMapping(array $config): void |
||
| 562 | { |
||
| 563 | $collector = DoctrineCollector::getInstance(); |
||
| 564 | |||
| 565 | $collector->addAssociation( |
||
| 566 | $config['class']['media'], |
||
| 567 | 'mapOneToMany', |
||
| 568 | OptionsBuilder::createOneToMany('galleryItems', $config['class']['gallery_item']) |
||
| 569 | ->cascade(['persist']) |
||
| 570 | ->mappedBy('media') |
||
| 571 | ); |
||
| 572 | |||
| 573 | $collector->addAssociation( |
||
| 574 | $config['class']['gallery_item'], |
||
| 575 | 'mapManyToOne', |
||
| 576 | OptionsBuilder::createManyToOne('gallery', $config['class']['gallery']) |
||
| 577 | ->cascade(['persist']) |
||
| 578 | ->inversedBy('galleryItems') |
||
| 579 | ->addJoin([ |
||
| 580 | 'name' => 'gallery_id', |
||
| 581 | 'referencedColumnName' => 'id', |
||
| 582 | 'onDelete' => 'CASCADE', |
||
| 583 | ]) |
||
| 584 | ); |
||
| 585 | |||
| 586 | $collector->addAssociation( |
||
| 587 | $config['class']['gallery_item'], |
||
| 588 | 'mapManyToOne', |
||
| 589 | OptionsBuilder::createManyToOne('media', $config['class']['media']) |
||
| 590 | ->cascade(['persist']) |
||
| 591 | ->inversedBy('galleryItems') |
||
| 592 | ->addJoin([ |
||
| 593 | 'name' => 'media_id', |
||
| 594 | 'referencedColumnName' => 'id', |
||
| 595 | 'onDelete' => 'CASCADE', |
||
| 596 | ]) |
||
| 597 | ); |
||
| 598 | |||
| 599 | $collector->addAssociation( |
||
| 600 | $config['class']['gallery'], |
||
| 601 | 'mapOneToMany', |
||
| 602 | OptionsBuilder::createOneToMany('galleryItems', $config['class']['gallery_item']) |
||
| 603 | ->cascade(['persist']) |
||
| 604 | ->mappedBy('gallery') |
||
| 605 | ->orphanRemoval() |
||
| 606 | ->addOrder('position', 'ASC') |
||
| 607 | ); |
||
| 608 | |||
| 609 | if ($this->isClassificationEnabled($config)) { |
||
| 610 | $collector->addAssociation( |
||
| 611 | $config['class']['media'], |
||
| 612 | 'mapManyToOne', |
||
| 613 | OptionsBuilder::createManyToOne('category', $config['class']['category']) |
||
| 614 | ->cascade(['persist']) |
||
| 615 | ->addJoin([ |
||
| 616 | 'name' => 'category_id', |
||
| 617 | 'referencedColumnName' => 'id', |
||
| 618 | 'onDelete' => 'SET NULL', |
||
| 619 | ]) |
||
| 620 | ); |
||
| 621 | } |
||
| 622 | } |
||
| 623 | } |
||
| 624 |
If you suppress an error, we recommend checking for the error condition explicitly: