From 6d60950fd578dab07b6fd15fb5390953481b46e8 Mon Sep 17 00:00:00 2001 From: Jacob Dreesen Date: Mon, 27 Jun 2022 20:01:49 +0200 Subject: [PATCH] Support modern bundle structure --- .../JMSSerializerExtension.php | 22 ++++++++----------- .../DependencyInjection/ConfigurationTest.php | 9 +++++++- .../CustomHandlerPassTest.php | 2 +- .../DependencyInjection/DoctrinePassTest.php | 1 + .../EventSubscribersAndListenersPassTest.php | 2 +- ...mErrorHandlerTranslationDomainPassTest.php | 1 + .../JMSSerializerExtensionTest.php | 1 + .../NamingStrategyTest.php | 1 + .../TwigExtensionPassTest.php | 6 +++++ 9 files changed, 29 insertions(+), 16 deletions(-) diff --git a/DependencyInjection/JMSSerializerExtension.php b/DependencyInjection/JMSSerializerExtension.php index 146f5fde..f5401d32 100644 --- a/DependencyInjection/JMSSerializerExtension.php +++ b/DependencyInjection/JMSSerializerExtension.php @@ -234,7 +234,7 @@ private function loadInternal(array $config, ScopedContainer $container, array $ $container->removeDefinition('jms_serializer.cache.cache_warmer'); } - $directories = $this->detectMetadataDirectories($config['metadata'], $bundles); + $directories = $this->detectMetadataDirectories($config['metadata'], $container->getParameter('kernel.bundles_metadata')); $container ->getDefinition('jms_serializer.metadata.file_locator') @@ -401,16 +401,13 @@ private function setVisitorOptions(array $config, ScopedContainer $container): v } } - private function detectMetadataDirectories($metadata, $bundles): array + private function detectMetadataDirectories(array $metadata, array $bundlesMetadata): array { $directories = []; if ($metadata['auto_detection']) { - foreach ($bundles as $name => $class) { - $ref = new \ReflectionClass($class); - - $dir = dirname($ref->getFileName()) . '/Resources/config/serializer'; - if (file_exists($dir)) { - $directories[$ref->getNamespaceName()] = $dir; + foreach ($bundlesMetadata as $bundle) { + if (is_dir($dir = $bundle['path'] . '/Resources/config/serializer') || is_dir($dir = $bundle['path'] . '/config/serializer')) { + $directories[$bundle['namespace']] = $dir; } } } @@ -419,15 +416,14 @@ private function detectMetadataDirectories($metadata, $bundles): array $directory['path'] = rtrim(str_replace('\\', '/', $directory['path']), '/'); if ('@' === $directory['path'][0]) { - $pathParts = explode('/', $directory['path']); + $pathParts = explode('/', $directory['path'], 2); $bundleName = substr($pathParts[0], 1); - if (!isset($bundles[$bundleName])) { - throw new RuntimeException(sprintf('The bundle "%s" has not been registered with AppKernel. Available bundles: %s', $bundleName, implode(', ', array_keys($bundles)))); + if (!isset($bundlesMetadata[$bundleName])) { + throw new RuntimeException(sprintf('The bundle "%s" has not been registered with AppKernel. Available bundles: %s', $bundleName, implode(', ', array_keys($bundlesMetadata)))); } - $ref = new \ReflectionClass($bundles[$bundleName]); - $directory['path'] = dirname($ref->getFileName()) . substr($directory['path'], strlen('@' . $bundleName)); + $directory['path'] = $bundlesMetadata[$bundleName]['path'] . substr($directory['path'], strlen('@' . $bundleName)); } $dir = rtrim($directory['path'], '\\/'); diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index 0a0dadcc..3f2e634d 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -16,12 +16,19 @@ class ConfigurationTest extends TestCase { private function getContainer(array $configs = []) { + $bundles = ['JMSSerializerBundle' => 'JMS\SerializerBundle\JMSSerializerBundle']; $container = new ContainerBuilder(); $container->set('annotation_reader', new AnnotationReader()); $container->setParameter('kernel.debug', true); $container->setParameter('kernel.cache_dir', sys_get_temp_dir() . '/serializer'); - $container->setParameter('kernel.bundles', ['JMSSerializerBundle' => 'JMS\SerializerBundle\JMSSerializerBundle']); + $container->setParameter('kernel.bundles', $bundles); + $container->setParameter('kernel.bundles_metadata', array_map(static function (string $class): array { + return [ + 'path' => (new $class)->getPath(), + 'namespace' => (new \ReflectionClass($class))->getNamespaceName(), + ]; + }, $bundles)); $bundle = new JMSSerializerBundle(); diff --git a/Tests/DependencyInjection/CustomHandlerPassTest.php b/Tests/DependencyInjection/CustomHandlerPassTest.php index 3d8b74f0..6310a05a 100644 --- a/Tests/DependencyInjection/CustomHandlerPassTest.php +++ b/Tests/DependencyInjection/CustomHandlerPassTest.php @@ -27,7 +27,7 @@ private function getContainer(array $configs = []) $container->setParameter('kernel.debug', true); $container->setParameter('kernel.cache_dir', sys_get_temp_dir() . '/serializer'); $container->setParameter('kernel.bundles', []); - $container->setParameter('kernel.bundles', []); + $container->setParameter('kernel.bundles_metadata', []); $loader->load(['jms_serializer' => $configs], $container); diff --git a/Tests/DependencyInjection/DoctrinePassTest.php b/Tests/DependencyInjection/DoctrinePassTest.php index 94aec0d6..5066ec9f 100644 --- a/Tests/DependencyInjection/DoctrinePassTest.php +++ b/Tests/DependencyInjection/DoctrinePassTest.php @@ -25,6 +25,7 @@ private function getContainer(array $configs = []) $container->setParameter('kernel.debug', true); $container->setParameter('kernel.cache_dir', sys_get_temp_dir() . '/serializer'); $container->setParameter('kernel.bundles', []); + $container->setParameter('kernel.bundles_metadata', []); $pass = new DoctrinePass(); $container->addCompilerPass($pass); diff --git a/Tests/DependencyInjection/EventSubscribersAndListenersPassTest.php b/Tests/DependencyInjection/EventSubscribersAndListenersPassTest.php index 8696b081..aefcc418 100644 --- a/Tests/DependencyInjection/EventSubscribersAndListenersPassTest.php +++ b/Tests/DependencyInjection/EventSubscribersAndListenersPassTest.php @@ -27,7 +27,7 @@ private function getContainer(array $configs = []) $container->setParameter('kernel.debug', true); $container->setParameter('kernel.cache_dir', sys_get_temp_dir() . '/serializer'); $container->setParameter('kernel.bundles', []); - $container->setParameter('kernel.bundles', []); + $container->setParameter('kernel.bundles_metadata', []); $loader->load(['jms_serializer' => $configs], $container); diff --git a/Tests/DependencyInjection/FormErrorHandlerTranslationDomainPassTest.php b/Tests/DependencyInjection/FormErrorHandlerTranslationDomainPassTest.php index c2d2c69b..1bee76cf 100644 --- a/Tests/DependencyInjection/FormErrorHandlerTranslationDomainPassTest.php +++ b/Tests/DependencyInjection/FormErrorHandlerTranslationDomainPassTest.php @@ -24,6 +24,7 @@ private function getContainer(array $configs = []) $container->setParameter('kernel.debug', true); $container->setParameter('kernel.cache_dir', sys_get_temp_dir() . '/serializer'); $container->setParameter('kernel.bundles', []); + $container->setParameter('kernel.bundles_metadata', []); $loader->load(['jms_serializer' => $configs], $container); diff --git a/Tests/DependencyInjection/JMSSerializerExtensionTest.php b/Tests/DependencyInjection/JMSSerializerExtensionTest.php index 96819f3c..c980520d 100644 --- a/Tests/DependencyInjection/JMSSerializerExtensionTest.php +++ b/Tests/DependencyInjection/JMSSerializerExtensionTest.php @@ -793,6 +793,7 @@ private function getContainerForConfigLoad(array $configs, ?callable $configurat $container->setParameter('kernel.debug', true); $container->setParameter('kernel.cache_dir', sys_get_temp_dir() . '/serializer'); $container->setParameter('kernel.bundles', []); + $container->setParameter('kernel.bundles_metadata', []); $container->setParameter('foo', 'bar'); $container->set('annotation_reader', new AnnotationReader()); $container->setDefinition('doctrine', new Definition(Registry::class)); diff --git a/Tests/DependencyInjection/NamingStrategyTest.php b/Tests/DependencyInjection/NamingStrategyTest.php index 483aa823..6a3dc159 100644 --- a/Tests/DependencyInjection/NamingStrategyTest.php +++ b/Tests/DependencyInjection/NamingStrategyTest.php @@ -25,6 +25,7 @@ private function getContainer(array $configs = []) $container->setParameter('kernel.debug', true); $container->setParameter('kernel.cache_dir', sys_get_temp_dir() . '/serializer'); $container->setParameter('kernel.bundles', []); + $container->setParameter('kernel.bundles_metadata', []); $loader->load(['jms_serializer' => $configs], $container); diff --git a/Tests/DependencyInjection/TwigExtensionPassTest.php b/Tests/DependencyInjection/TwigExtensionPassTest.php index e280093b..98f3cf0c 100644 --- a/Tests/DependencyInjection/TwigExtensionPassTest.php +++ b/Tests/DependencyInjection/TwigExtensionPassTest.php @@ -23,6 +23,12 @@ private function getContainer(array $bundles = ['TwigBundle' => TwigBundle::clas $container->setParameter('kernel.debug', true); $container->setParameter('kernel.cache_dir', sys_get_temp_dir() . '/serializer'); $container->setParameter('kernel.bundles', $bundles); + $container->setParameter('kernel.bundles_metadata', array_map(static function (string $class): array { + return [ + 'path' => (new $class)->getPath(), + 'namespace' => (new \ReflectionClass($class))->getNamespaceName(), + ]; + }, $bundles)); $loader->load([[]], $container);