Skip to content

Commit

Permalink
Support modern bundle structure
Browse files Browse the repository at this point in the history
  • Loading branch information
jdreesen committed Jan 6, 2023
1 parent 4b7f6a6 commit 6d60950
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 16 deletions.
22 changes: 9 additions & 13 deletions DependencyInjection/JMSSerializerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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;
}
}
}
Expand All @@ -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'], '\\/');
Expand Down
9 changes: 8 additions & 1 deletion Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion Tests/DependencyInjection/CustomHandlerPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions Tests/DependencyInjection/DoctrinePassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions Tests/DependencyInjection/JMSSerializerExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
1 change: 1 addition & 0 deletions Tests/DependencyInjection/NamingStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 6 additions & 0 deletions Tests/DependencyInjection/TwigExtensionPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 6d60950

Please sign in to comment.