Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit 913e236

Browse files
committed
Merge pull request #8 from symfony-cmf/resource_alias
Resource aliases
2 parents ec01f8d + 6a338b5 commit 913e236

29 files changed

+909
-142
lines changed

CmfResourceRestBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313

1414
use Symfony\Component\HttpKernel\Bundle\Bundle;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Cmf\Bundle\ResourceRestBundle\DependencyInjection\Compiler\EnhancerPass;
1617

1718
class CmfResourceRestBundle extends Bundle
1819
{
1920
public function build(ContainerBuilder $container)
2021
{
22+
$container->addCompilerPass(new EnhancerPass());
2123
parent::build($container);
2224
}
2325
}

DependencyInjection/CmfResourceRestExtension.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
1717
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
1818
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
19+
use Symfony\Component\Config\Definition\Processor;
1920

2021
class CmfResourceRestExtension extends Extension implements PrependExtensionInterface
2122
{
@@ -46,8 +47,52 @@ public function prepend(ContainerBuilder $container)
4647
*/
4748
public function load(array $configs, ContainerBuilder $container)
4849
{
50+
$processor = new Processor();
4951
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
52+
$configuration = new Configuration();
53+
$config = $processor->processConfiguration($configuration, $configs);
54+
5055
$loader->load('serializer.xml');
5156
$loader->load('resource-rest.xml');
57+
$loader->load('enhancer.xml');
58+
59+
$this->configurePayloadAliasRegistry($container, $config['payload_alias_map']);
60+
$this->configureEnhancerMap($container, $config['enhancer_map']);
61+
}
62+
63+
public function getNamespace()
64+
{
65+
return 'http://cmf.symfony.com/schema/dic/' . $this->getAlias();
66+
}
67+
68+
private function configurePayloadAliasRegistry(ContainerBuilder $container, $aliasMap)
69+
{
70+
$registry = $container->getDefinition('cmf_resource_rest.registry.payload_alias');
71+
$registry->replaceArgument(1, $aliasMap);
72+
}
73+
74+
private function configureEnhancerMap(ContainerBuilder $container, $enhancerMap)
75+
{
76+
$enhancerMap = $this->normalizeEnhancerMap($enhancerMap);
77+
$registry = $container->getDefinition('cmf_resource_rest.registry.enhancer');
78+
$registry->replaceArgument(1, $enhancerMap);
79+
}
80+
81+
private function normalizeEnhancerMap($enhancerMap)
82+
{
83+
// normalize enhancer map
84+
$normalized = array();
85+
foreach ($enhancerMap as $enhancerMapping) {
86+
$repository = $enhancerMapping['repository'];
87+
$enhancer = $enhancerMapping['enhancer'];
88+
89+
if (!isset($normalized[$repository])) {
90+
$normalized[$repository] = array();
91+
}
92+
93+
$normalized[$repository][] = $enhancer;
94+
}
95+
96+
return $normalized;
5297
}
5398
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2014 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Cmf\Bundle\ResourceRestBundle\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\ContainerBuilder;
15+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16+
use Symfony\Component\DependencyInjection\Reference;
17+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
18+
19+
/**
20+
* @author Daniel Leech <[email protected]>
21+
*/
22+
class EnhancerPass implements CompilerPassInterface
23+
{
24+
public function process(ContainerBuilder $container)
25+
{
26+
if (!$container->has('cmf_resource_rest.registry.enhancer')) {
27+
return;
28+
}
29+
30+
$taggedIds = $container->findTaggedServiceIds('cmf_resource_rest.enhancer');
31+
32+
$repositoryMap = array();
33+
$aliasMap = array();
34+
foreach ($taggedIds as $id => $attributes) {
35+
if (!isset($attributes[0]['alias'])) {
36+
throw new InvalidArgumentException(sprintf(
37+
'Resource enhancer "%s" has no "alias" attribute in its tag',
38+
$id
39+
));
40+
}
41+
42+
$name = $attributes[0]['alias'];
43+
44+
if (isset($aliasMap[$name])) {
45+
throw new InvalidArgumentException(sprintf(
46+
'Enhancer with name "%s" (id: "%s") has already been registered',
47+
$name,
48+
$id
49+
));
50+
}
51+
52+
$aliasMap[$name] = $id;
53+
}
54+
55+
$registryDef = $container->getDefinition('cmf_resource_rest.registry.enhancer');
56+
$registryDef->replaceArgument(2, $aliasMap);
57+
}
58+
}

DependencyInjection/Configuration.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2014 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Cmf\Bundle\ResourceRestBundle\DependencyInjection;
13+
14+
use Symfony\Component\Config\Definition\ConfigurationInterface;
15+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
16+
17+
class Configuration implements ConfigurationInterface
18+
{
19+
/**
20+
* Returns the config tree builder.
21+
*
22+
* @return TreeBuilder
23+
*/
24+
public function getConfigTreeBuilder()
25+
{
26+
$treeBuilder = new TreeBuilder();
27+
$treeBuilder->root('cmf_resource_rest')
28+
->fixXmlConfig('payload_alias', 'payload_alias_map')
29+
->fixXmlConfig('enhance', 'enhancer_map')
30+
->children()
31+
->arrayNode('payload_alias_map')
32+
->useAttributeAsKey('name')
33+
->prototype('array')
34+
->children()
35+
->scalarNode('repository')->end()
36+
->scalarNode('type')->end()
37+
->end()
38+
->end()
39+
->end()
40+
->arrayNode('enhancer_map')
41+
->prototype('array')
42+
->children()
43+
->scalarNode('repository')->isRequired()->end()
44+
->scalarNode('enhancer')->isRequired()->end()
45+
->end()
46+
->end()
47+
->end()
48+
->end();
49+
50+
return $treeBuilder;
51+
}
52+
}

Enhancer/EnhancerInterface.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2014 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Cmf\Bundle\ResourceRestBundle\Enhancer;
13+
14+
use JMS\Serializer\Context;
15+
use Puli\Repository\Api\Resource\Resource;
16+
17+
/**
18+
* Enhancer classes enhance the REST response for resources
19+
*
20+
* @author Daniel Leech <[email protected]>
21+
*/
22+
interface EnhancerInterface
23+
{
24+
/**
25+
* Enhance the given serialization context.
26+
*
27+
* For example:
28+
*
29+
* $context->addData('foobar', 'Some value');
30+
*
31+
* @param Context Serialization context
32+
* @param Resource The resource being serialized
33+
*/
34+
public function enhance(Context $context, Resource $resource);
35+
}

Enhancer/PayloadEnhancer.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2014 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Cmf\Bundle\ResourceRestBundle\Enhancer;
13+
14+
use JMS\Serializer\Context;
15+
use Puli\Repository\Api\Resource\Resource;
16+
17+
/**
18+
* Serialize the payload
19+
*
20+
* @author Daniel Leech <[email protected]>
21+
*/
22+
class PayloadEnhancer implements EnhancerInterface
23+
{
24+
/**
25+
* {@inheritDoc}
26+
*/
27+
public function enhance(Context $context, Resource $resource)
28+
{
29+
$visitor = $context->getVisitor();
30+
$visitor->addData('payload', $context->accept($resource->getPayload()));
31+
}
32+
}

Registry/EnhancerRegistry.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2014 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Cmf\Bundle\ResourceRestBundle\Registry;
13+
14+
use Symfony\Cmf\Component\Resource\RepositoryFactoryInterface;
15+
use Symfony\Component\DependencyInjection\ContainerInterface;
16+
use Symfony\Cmf\Component\Resource\RepositoryRegistryInterface;
17+
use Puli\Repository\Api\Resource\Resource;
18+
use Puli\Repository\Api\ResourceRepository;
19+
20+
/**
21+
* Registry for resource enhancers
22+
*
23+
* @author Daniel Leech <[email protected]>
24+
*/
25+
class EnhancerRegistry
26+
{
27+
/**
28+
* @var array
29+
*/
30+
private $aliasMap = array();
31+
32+
/**
33+
* @var array
34+
*/
35+
private $enhancerMap = array();
36+
37+
/**
38+
* @var ContainerInterface
39+
*/
40+
private $container;
41+
42+
/**
43+
* @param ContainerInterface $container The service container
44+
* @param array $enhancerMap Map of enhancer aliases to repository names
45+
* @param array $aliasMap Serice ID map for enhancer aliases
46+
*/
47+
public function __construct(
48+
ContainerInterface $container,
49+
$enhancerMap = array(),
50+
$aliasMap = array()
51+
)
52+
{
53+
$this->container = $container;
54+
$this->enhancerMap = $enhancerMap;
55+
$this->aliasMap = $aliasMap;
56+
}
57+
58+
/**
59+
* Return all of the enhancers which are reigstered against
60+
* the repository with the given alias.
61+
*
62+
* @param string $repositoryAlias
63+
* @return EnhancerInterface[]
64+
*/
65+
public function getEnhancers($repositoryAlias)
66+
{
67+
if (!isset($this->enhancerMap[$repositoryAlias])) {
68+
return array();
69+
}
70+
71+
$aliases = $this->enhancerMap[$repositoryAlias];
72+
73+
foreach ($aliases as $alias) {
74+
if (!isset($this->aliasMap[$alias])) {
75+
throw new \InvalidArgumentException(sprintf(
76+
'Unknown enhancer alias "%s". Known aliases: "%s"',
77+
implode('", "', array_keys($this->aliasMap))
78+
));
79+
}
80+
81+
$enhancer = $this->container->get(
82+
$this->aliasMap[$alias]
83+
);
84+
$enhancers[] = $enhancer;
85+
}
86+
87+
return $enhancers;
88+
}
89+
}

0 commit comments

Comments
 (0)