Skip to content

Commit 61adb77

Browse files
NGSTACK-1017 move doctrine config to yaml file
1 parent 52a2153 commit 61adb77

3 files changed

Lines changed: 34 additions & 15 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"symfony/console": "^7.3 || ^8.0",
3434
"symfony/security-core": "^7.3 || ^8.0",
3535
"symfony/security-http": "^7.3 || ^8.0",
36+
"symfony/yaml": "^7.3 || ^8.0",
3637
"api-platform/symfony": "^4.2",
3738
"api-platform/doctrine-orm": "^4.2",
3839
"lexik/jwt-authentication-bundle": "^3.1",

config/doctrine.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
doctrine:
2+
orm:
3+
mappings:
4+
NetgenApiPlatformExtras:
5+
type: xml
6+
is_bundle: false
7+
dir: '%kernel.project_dir%/vendor/netgen/api-platform-extras/config/doctrine'
8+
prefix: 'Netgen\ApiPlatformExtras\Entity'

src/DependencyInjection/NetgenApiPlatformExtrasExtension.php

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@
44

55
namespace Netgen\ApiPlatformExtras\DependencyInjection;
66

7+
use RuntimeException;
78
use Symfony\Component\Config\Definition\ConfigurationInterface;
9+
use Symfony\Component\Config\Resource\FileResource;
810
use Symfony\Component\DependencyInjection\ContainerBuilder;
911
use Symfony\Component\DependencyInjection\Extension\Extension;
1012
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
13+
use Symfony\Component\Yaml\Exception\ParseException;
14+
use Symfony\Component\Yaml\Yaml;
1115

1216
use function dirname;
1317
use function in_array;
1418
use function is_array;
19+
use function is_file;
20+
use function is_readable;
21+
use function sprintf;
1522

1623
final class NetgenApiPlatformExtrasExtension extends Extension implements PrependExtensionInterface
1724
{
@@ -37,21 +44,24 @@ public function prepend(ContainerBuilder $container): void
3744
return;
3845
}
3946

40-
$container->prependExtensionConfig(
41-
'doctrine',
42-
[
43-
'orm' => [
44-
'mappings' => [
45-
'NetgenApiPlatformExtras' => [
46-
'type' => 'xml',
47-
'is_bundle' => false,
48-
'dir' => dirname(__DIR__, 2) . '/config/doctrine',
49-
'prefix' => 'Netgen\ApiPlatformExtras\Entity',
50-
],
51-
],
52-
],
53-
],
54-
);
47+
$configFile = dirname(__DIR__, 2) . '/config/doctrine.yaml';
48+
49+
if (!is_file($configFile) || !is_readable($configFile)) {
50+
return;
51+
}
52+
53+
try {
54+
$config = Yaml::parseFile($configFile);
55+
} catch (ParseException $e) {
56+
throw new RuntimeException(sprintf('Could not parse YAML file "%s": %s', $configFile, $e->getMessage()), 0, $e);
57+
}
58+
59+
if (!is_array($config)) {
60+
return;
61+
}
62+
63+
$container->addResource(new FileResource($configFile));
64+
$container->prependExtensionConfig('doctrine', $config['doctrine']);
5565
}
5666

5767
/**

0 commit comments

Comments
 (0)