Skip to content

Commit c1e96f1

Browse files
committed
Merge pull request #14 from dylan-qton/master
Added ability to configure multiple mapping sources
2 parents 00c03db + a46a7ff commit c1e96f1

File tree

3 files changed

+50
-20
lines changed

3 files changed

+50
-20
lines changed

Diff for: README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ It can be also be customized and placed elsewhere by editing the `app/config/con
7272
# app/config/config.yml
7373

7474
nilportugues_json_api:
75-
mappings: "%kernel.root_dir%/config/serializer/"
75+
mappings:
76+
- "%kernel.root_dir%/config/serializer/"
77+
- @AppBundle/Product/config/Mappings
7678

7779
```
7880

Diff for: src/DependencyInjection/Configuration.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ public function getConfigTreeBuilder()
2222

2323
$treeBuilder
2424
->root('nilportugues_json_api')
25-
->children()
26-
->scalarNode('mappings')->isRequired()->cannotBeEmpty()->defaultValue(self::DEFAULT_PATH)->end()
27-
->end()
28-
;
25+
->children()
26+
->arrayNode('mappings')->prototype('scalar')
27+
->isRequired()
28+
->cannotBeEmpty()
29+
->end();
2930

3031
return $treeBuilder;
3132
}

Diff for: src/DependencyInjection/NilPortuguesSymfonyJsonApiExtension.php

+42-15
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,53 @@ public function load(array $configs, ContainerBuilder $container)
3636
*/
3737
private function setMappings(ContainerBuilder $container, $config)
3838
{
39-
if (true === \file_exists($config['mappings'])) {
40-
$finder = new Finder();
41-
$finder->files()->in($config['mappings']);
42-
$loadedMappings = [];
39+
$definition = new Definition();
40+
$definition->setClass('NilPortugues\Api\Mapping\Mapper');
41+
$args = $this->resolveMappings($container, $config['mappings']);
42+
$definition->setArguments($args);
43+
$definition->setLazy(true);
4344

44-
foreach ($finder as $file) {
45-
/* @var \Symfony\Component\Finder\SplFileInfo $file */
46-
$mapping = \file_get_contents($file->getPathname());
47-
$mapping = Yaml::parse($mapping);
48-
$loadedMappings[] = $mapping['mapping'];
45+
$container->setDefinition('nil_portugues.api.mapping.mapper', $definition);
46+
}
47+
48+
private function resolveMappings(ContainerBuilder $container, $mappings)
49+
{
50+
$loadedMappings = [];
51+
52+
foreach ($mappings as $mapping) {
53+
if (0 === strpos($mapping, '@')) {
54+
$name = substr($mapping, 1, strpos($mapping, '/') - 1);
55+
56+
$dir = $this->resolveBundle($container, $name);
57+
$mapping = str_replace('@'.$name, $dir, $mapping);
58+
}
59+
60+
if (true === \file_exists($mapping)) {
61+
$finder = new Finder();
62+
$finder->files()->in($mapping);
63+
foreach ($finder as $file) {
64+
/* @var \Symfony\Component\Finder\SplFileInfo $file */
65+
$mapping = \file_get_contents($file->getPathname());
66+
$mapping = Yaml::parse($mapping);
67+
$loadedMappings[] = $mapping['mapping'];
68+
}
4969
}
70+
}
71+
72+
return [$loadedMappings];
73+
}
5074

51-
$definition = new Definition();
52-
$definition->setClass('NilPortugues\Api\Mapping\Mapper');
53-
$args = array($loadedMappings);
54-
$definition->setArguments($args);
55-
$definition->setLazy(true);
75+
private function resolveBundle(ContainerBuilder $container, $name)
76+
{
77+
$bundles = $container->getParameter('kernel.bundles');
5678

57-
$container->setDefinition('nil_portugues.api.mapping.mapper', $definition);
79+
if (!isset($bundles[$name])) {
80+
return null;
5881
}
82+
83+
$class = $bundles[$name];
84+
$refClass = new \ReflectionClass($class);
85+
return dirname($refClass->getFileName());
5986
}
6087

6188
/**

0 commit comments

Comments
 (0)