Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b56a1ac

Browse filesBrowse files
author
dylan
committed
Changes to allow for multiple mappings
Useful when a api is split into a number of contexts
1 parent 00c03db commit b56a1ac
Copy full SHA for b56a1ac

2 files changed

+49-20Lines changed: 49 additions & 20 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/DependencyInjection/Configuration.php‎

Copy file name to clipboardExpand all lines: src/DependencyInjection/Configuration.php
+5-4Lines changed: 5 additions & 4 deletions
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
}
Collapse file

‎src/DependencyInjection/NilPortuguesSymfonyJsonApiExtension.php‎

Copy file name to clipboardExpand all lines: src/DependencyInjection/NilPortuguesSymfonyJsonApiExtension.php
+44-16Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,54 @@ 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 = [];
43-
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'];
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);
44+
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+
54+
if (0 === strpos($mapping, '@')) {
55+
$name = substr($mapping, 1, strpos($mapping, '/') - 1);
56+
57+
$dir = $this->resolveBundle($container, $name);
58+
$mapping = str_replace('@'.$name, $dir, $mapping);
4959
}
5060

51-
$definition = new Definition();
52-
$definition->setClass('NilPortugues\Api\Mapping\Mapper');
53-
$args = array($loadedMappings);
54-
$definition->setArguments($args);
55-
$definition->setLazy(true);
61+
if (true === \file_exists($mapping)) {
62+
$finder = new Finder();
63+
$finder->files()->in($mapping);
64+
foreach ($finder as $file) {
65+
/* @var \Symfony\Component\Finder\SplFileInfo $file */
66+
$mapping = \file_get_contents($file->getPathname());
67+
$mapping = Yaml::parse($mapping);
68+
$loadedMappings[] = $mapping['mapping'];
69+
}
70+
}
71+
}
72+
73+
return [$loadedMappings];
74+
}
5675

57-
$container->setDefinition('nil_portugues.api.mapping.mapper', $definition);
76+
private function resolveBundle(ContainerBuilder $container, $name)
77+
{
78+
$bundles = $container->getParameter('kernel.bundles');
79+
80+
if (!isset($bundles[$name])) {
81+
return null;
5882
}
83+
84+
$class = $bundles[$name];
85+
$refClass = new \ReflectionClass($class);
86+
return dirname($refClass->getFileName());
5987
}
6088

6189
/**

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.