Skip to content

Commit

Permalink
Allowing multiple paths for the serializers to be
Browse files Browse the repository at this point in the history
  • Loading branch information
Nil Portugués Calderó committed Mar 21, 2017
1 parent c68a735 commit 8f9fb78
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 22 deletions.
9 changes: 5 additions & 4 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ public function getConfigTreeBuilder()

$treeBuilder
->root('nilportugues_hal_json')
->children()
->scalarNode('mappings')->isRequired()->cannotBeEmpty()->defaultValue(self::DEFAULT_PATH)->end()
->end()
;
->children()
->arrayNode('mappings')->prototype('scalar')
->isRequired()
->cannotBeEmpty()
->end();

return $treeBuilder;
}
Expand Down
52 changes: 37 additions & 15 deletions src/DependencyInjection/NilPortuguesSymfonyHalJsonExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,48 @@ public function load(array $configs, ContainerBuilder $container)
*/
private function setMappings(ContainerBuilder $container, $config)
{
if (true === \file_exists($config['mappings'])) {
$finder = new Finder();
$finder->files()->in($config['mappings']);
$loadedMappings = [];
$definition = new Definition();
$definition->setClass('NilPortugues\Api\Mapping\Mapper');
$args = $this->resolveMappings($container, $config['mappings']);
$definition->setArguments($args);
$definition->setLazy(true);
$container->setDefinition('nil_portugues.api.mapping.mapper', $definition);
}

foreach ($finder as $file) {
/* @var \Symfony\Component\Finder\SplFileInfo $file */
$mapping = \file_get_contents($file->getPathname());
$mapping = Yaml::parse($mapping);
$loadedMappings[] = $mapping['mapping'];
private function resolveMappings(ContainerBuilder $container, $mappings)
{
$loadedMappings = [];
foreach ($mappings as $mapping) {
if (0 === strpos($mapping, '@')) {
$name = substr($mapping, 1, strpos($mapping, '/') - 1);
$dir = $this->resolveBundle($container, $name);
$mapping = str_replace('@'.$name, $dir, $mapping);
}
if (true === \file_exists($mapping)) {
$finder = new Finder();
$finder->files()->in($mapping);
foreach ($finder as $file) {
/* @var \Symfony\Component\Finder\SplFileInfo $file */
$mapping = \file_get_contents($file->getPathname());
$mapping = Yaml::parse($mapping);
$loadedMappings[] = $mapping['mapping'];
}
}
}

$definition = new Definition();
$definition->setClass('NilPortugues\Api\Mapping\Mapper');
$args = array($loadedMappings);
$definition->setArguments($args);
$definition->setLazy(true);
return [$loadedMappings];
}

$container->setDefinition('nil_portugues.api.mapping.mapper', $definition);
private function resolveBundle(ContainerBuilder $container, $name)
{
$bundles = $container->getParameter('kernel.bundles');
if (!isset($bundles[$name])) {
return;
}
$class = $bundles[$name];
$refClass = new \ReflectionClass($class);

return dirname($refClass->getFileName());
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/Serializer/HalJsonSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class HalJsonSerializer extends DeepCopySerializer
{
/**
* @param JsonTransformer $transformer
* @param Router $router
* @param Router $router
*/
public function __construct(JsonTransformer $transformer, Router $router)
{
Expand All @@ -36,7 +36,7 @@ public function __construct(JsonTransformer $transformer, Router $router)

/**
* @param JsonTransformer $transformer
* @param Router $router
* @param Router $router
*/
private function mapUrls(JsonTransformer $transformer, Router $router)
{
Expand All @@ -46,7 +46,6 @@ private function mapUrls(JsonTransformer $transformer, Router $router)
$mappings = $reflectionProperty->getValue($transformer);

foreach ($mappings as $key => &$mapping) {

if ($key === HalPagination::class) {
continue;
}
Expand Down

0 comments on commit 8f9fb78

Please sign in to comment.