|
| 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\ResourceRest; |
| 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 payload aliases |
| 22 | + * |
| 23 | + * @author Daniel Leech <[email protected]> |
| 24 | + */ |
| 25 | +class PayloadAliasRegistry |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var array |
| 29 | + */ |
| 30 | + private $aliasesByRepository; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var RepositoryRegistryInterface |
| 34 | + */ |
| 35 | + private $repositoryRegistry; |
| 36 | + |
| 37 | + /** |
| 38 | + * @param RepositoryRegistryInterface $repositoryRegistry |
| 39 | + * @param array $aliases |
| 40 | + */ |
| 41 | + public function __construct( |
| 42 | + RepositoryRegistryInterface $repositoryRegistry, |
| 43 | + array $aliases = array() |
| 44 | + ) |
| 45 | + { |
| 46 | + $this->repositoryRegistry = $repositoryRegistry; |
| 47 | + |
| 48 | + foreach ($aliases as $alias => $config) { |
| 49 | + if (!isset($this->aliasesByRepository[$config['repository']])) { |
| 50 | + $this->aliasesByRepository[$config['repository']] = array(); |
| 51 | + } |
| 52 | + |
| 53 | + $this->aliasesByRepository[$config['repository']][$config['type']] = $alias; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Return the alias for the given PHPCR resource |
| 59 | + * |
| 60 | + * @param Resource $resource |
| 61 | + * |
| 62 | + * @return string |
| 63 | + */ |
| 64 | + public function getPayloadAlias(Resource $resource) |
| 65 | + { |
| 66 | + $repositoryType = $this->repositoryRegistry->getRepositoryType( |
| 67 | + $resource->getRepository() |
| 68 | + ); |
| 69 | + |
| 70 | + $type = $resource->getPayloadType(); |
| 71 | + |
| 72 | + if (null === $type) { |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + if (!isset($this->aliasesByRepository[$repositoryType])) { |
| 77 | + throw new \RuntimeException(sprintf( |
| 78 | + 'No repositories registered with alias "%s". I know about "%s"', |
| 79 | + $repositoryType, |
| 80 | + implode('", "', array_keys($this->aliasesByRepository)) |
| 81 | + )); |
| 82 | + } |
| 83 | + |
| 84 | + if (!isset($this->aliasesByRepository[$repositoryType][$type])) { |
| 85 | + return $type; |
| 86 | + } |
| 87 | + |
| 88 | + return $this->aliasesByRepository[$repositoryType][$type]; |
| 89 | + } |
| 90 | +} |
0 commit comments