Skip to content

[WIP] ReferenceType Configuration #351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions Api/IriConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Dunglas\ApiBundle\Model\DataProviderInterface;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Routing\Exception\ExceptionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;

/**
Expand Down Expand Up @@ -50,19 +51,26 @@ class IriConverter implements IriConverterInterface
*/
private $classMetadataFactory;

/**
* @var bool|string The type of reference to be generated (one of the {@see UrlGeneratorInterface} constants)
*/
private $referenceType;

public function __construct(
ResourceCollectionInterface $resourceCollection,
DataProviderInterface $dataProvider,
ClassMetadataFactoryInterface $classMetadataFactory,
RouterInterface $router,
PropertyAccessorInterface $propertyAccessor
PropertyAccessorInterface $propertyAccessor,
$referenceType = UrlGeneratorInterface::ABSOLUTE_PATH
) {
$this->resourceCollection = $resourceCollection;
$this->dataProvider = $dataProvider;
$this->classMetadataFactory = $classMetadataFactory;
$this->router = $router;
$this->propertyAccessor = $propertyAccessor;
$this->routeCache = new \SplObjectStorage();
$this->referenceType = $referenceType;
}

/**
Expand Down Expand Up @@ -94,8 +102,12 @@ public function getItemFromIri($iri, $fetchData = false)
/**
* {@inheritdoc}
*/
public function getIriFromItem($item, $referenceType = RouterInterface::ABSOLUTE_PATH)
public function getIriFromItem($item, $referenceType = null)
{
if ($referenceType === null) {
$referenceType = $this->referenceType;
}

if ($resource = $this->resourceCollection->getResourceForEntity($item)) {
$identifierName = $this->getIdentifierNameFromResource($resource);

Expand All @@ -112,8 +124,12 @@ public function getIriFromItem($item, $referenceType = RouterInterface::ABSOLUTE
/**
* {@inheritdoc}
*/
public function getIriFromResource(ResourceInterface $resource, $referenceType = RouterInterface::ABSOLUTE_PATH)
public function getIriFromResource(ResourceInterface $resource, $referenceType = null)
{
if ($referenceType === null) {
$referenceType = $this->referenceType;
}

try {
return $this->router->generate($this->getRouteName($resource, 'collection'), [], $referenceType);
} catch (ExceptionInterface $e) {
Expand Down
6 changes: 6 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public function getConfigTreeBuilder()
->children()
->scalarNode('title')->cannotBeEmpty()->isRequired()->info('The title of the API.')->end()
->scalarNode('description')->cannotBeEmpty()->isRequired()->info('The description of the API.')->end()
->enumNode('reference_type')
->values(['absolute_url', 'absolute_path', 'relative_path', 'network_path'])
->cannotBeEmpty()
->defaultValue('absolute_path')
->info('The type how context URIs are generated')
->end()
->arrayNode('supported_formats')
->defaultValue(['jsonld'])
->cannotBeEmpty()
Expand Down
23 changes: 23 additions & 0 deletions DependencyInjection/DunglasApiExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
* The extension of this bundle.
Expand Down Expand Up @@ -52,6 +53,7 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('api.title', $config['title']);
$container->setParameter('api.description', $config['description']);
$container->setParameter('api.supported_formats', $config['supported_formats']);
$container->setParameter('api.reference_type', $this->getContextReferenceType($config['reference_type']));
$container->setParameter('api.collection.filter_name.order', $config['collection']['filter_name']['order']);
$container->setParameter('api.collection.order', $config['collection']['order']);
$container->setParameter('api.collection.pagination.enabled', $config['collection']['pagination']['enabled']);
Expand Down Expand Up @@ -90,6 +92,27 @@ public function load(array $configs, ContainerBuilder $container)
}
}

/**
* Translates the string reference types to the constant values of the UrlGeneratorInterface.
*
* @param $contextReferenceType string One of absolute_url, absolute_path, relative_path or network_path
* @return bool|string
*/
private function getContextReferenceType($contextReferenceType) {
switch ($contextReferenceType) {
case 'absolute_url':
return UrlGeneratorInterface::ABSOLUTE_URL;
case 'absolute_path':
return UrlGeneratorInterface::ABSOLUTE_PATH;
case 'relative_path':
return UrlGeneratorInterface::RELATIVE_PATH;
case 'network_path':
return UrlGeneratorInterface::NETWORK_PATH;
default:
break;
}
}

/**
* Enables JSON-LD and Hydra support.
*
Expand Down
2 changes: 1 addition & 1 deletion Doctrine/EventListener/ManagerViewListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace Dunglas\ApiBundle\Doctrine\EventListener;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\ObjectManager;
use Dunglas\ApiBundle\Api\ResourceInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
Expand Down
2 changes: 1 addition & 1 deletion Doctrine/Orm/Extension/EagerLoadingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace Dunglas\ApiBundle\Doctrine\Orm\Extension;

use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\QueryBuilder;
use Dunglas\ApiBundle\Api\ResourceInterface;
use Dunglas\ApiBundle\Doctrine\Orm\QueryCollectionExtensionInterface;
use Dunglas\ApiBundle\Doctrine\Orm\QueryItemExtensionInterface;
Expand Down
2 changes: 1 addition & 1 deletion FosUser/EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace Dunglas\ApiBundle\FosUser;

use FOS\UserBundle\Model\UserManagerInterface;
use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Model\UserManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;

Expand Down
2 changes: 1 addition & 1 deletion Hydra/Serializer/CollectionNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function supportsNormalization($data, $format = null)
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = array())
public function normalize($object, $format = null, array $context = [])
{
$resource = $this->resourceResolver->guessResource($object, $context);

Expand Down
1 change: 1 addition & 0 deletions Hydra/Serializer/ConstraintViolationListNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function __construct(RouterInterface $router)
{
$this->router = $router;
}

/**
* {@inheritdoc}
*/
Expand Down
2 changes: 1 addition & 1 deletion Hydra/Serializer/ErrorNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct(RouterInterface $router, $debug)
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = array())
public function normalize($object, $format = null, array $context = [])
{
if ($object instanceof \Exception || $object instanceof FlattenException) {
$message = $object->getMessage();
Expand Down
11 changes: 9 additions & 2 deletions JsonLd/ContextBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,21 @@ class ContextBuilder
*/
private $resourceCollection;

/**
* @var bool|string The type of reference to be generated (one of the {@see UrlGeneratorInterface} constants)
*/
private $referenceType;

public function __construct(
RouterInterface $router,
EventDispatcherInterface $eventDispatcher,
ResourceCollectionInterface $resourceCollection
ResourceCollectionInterface $resourceCollection,
$referenceType
) {
$this->router = $router;
$this->eventDispatcher = $eventDispatcher;
$this->resourceCollection = $resourceCollection;
$this->referenceType = $referenceType;
}

/**
Expand Down Expand Up @@ -114,7 +121,7 @@ public function getContext(ResourceInterface $resource = null)
*/
public function getContextUri(ResourceInterface $resource)
{
return $this->router->generate('api_jsonld_context', ['shortName' => $resource->getShortName()]);
return $this->router->generate('api_jsonld_context', ['shortName' => $resource->getShortName()], $this->referenceType);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions JsonLd/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
use Dunglas\ApiBundle\Api\IriConverterInterface;
use Dunglas\ApiBundle\Api\ResourceCollectionInterface;
use Dunglas\ApiBundle\Api\ResourceInterface;
use Dunglas\ApiBundle\Api\ResourceResolver;
use Dunglas\ApiBundle\Exception\InvalidArgumentException;
use Dunglas\ApiBundle\Exception\RuntimeException;
use Dunglas\ApiBundle\Api\ResourceResolver;
use Dunglas\ApiBundle\JsonLd\ContextBuilder;
use Dunglas\ApiBundle\Mapping\AttributeMetadataInterface;
use Dunglas\ApiBundle\Mapping\ClassMetadataInterface;
use Dunglas\ApiBundle\Mapping\Factory\ClassMetadataFactoryInterface;
use Dunglas\ApiBundle\Mapping\AttributeMetadataInterface;
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Serializer\Exception\CircularReferenceException;
Expand Down
2 changes: 1 addition & 1 deletion Mapping/Loader/DoctrineIdentifierLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
namespace Dunglas\ApiBundle\Mapping\Loader;

use Doctrine\Common\Persistence\ManagerRegistry;
use Dunglas\ApiBundle\Mapping\Factory\AttributeMetadataFactoryInterface;
use Dunglas\ApiBundle\Mapping\ClassMetadataInterface;
use Dunglas\ApiBundle\Mapping\Factory\AttributeMetadataFactoryInterface;

/**
* Doctrine identifier loader.
Expand Down
2 changes: 1 addition & 1 deletion Mapping/Loader/ReflectionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

namespace Dunglas\ApiBundle\Mapping\Loader;

use Dunglas\ApiBundle\Mapping\Factory\AttributeMetadataFactoryInterface;
use Dunglas\ApiBundle\Mapping\AttributeMetadataInterface;
use Dunglas\ApiBundle\Mapping\ClassMetadataInterface;
use Dunglas\ApiBundle\Mapping\Factory\AttributeMetadataFactoryInterface;

/**
* Uses serialization groups or alternatively reflection to populate attributes.
Expand Down
2 changes: 1 addition & 1 deletion Mapping/Loader/SerializerMetadataLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

namespace Dunglas\ApiBundle\Mapping\Loader;

use Dunglas\ApiBundle\Mapping\Factory\AttributeMetadataFactoryInterface;
use Dunglas\ApiBundle\Mapping\AttributeMetadataInterface;
use Dunglas\ApiBundle\Mapping\ClassMetadataInterface;
use Dunglas\ApiBundle\Mapping\Factory\AttributeMetadataFactoryInterface;
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface as SerializerAttributeMetadataInterface;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;

Expand Down
1 change: 1 addition & 0 deletions Resources/config/api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<argument type="service" id="api.mapping.class_metadata_factory" />
<argument type="service" id="api.router" />
<argument type="service" id="api.property_accessor" />
<argument key="referenceType">%api.reference_type%</argument>
</service>

<service id="api.format_negotiator" class="Negotiation\FormatNegotiator" public="false" />
Expand Down
1 change: 1 addition & 0 deletions Resources/config/jsonld.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<argument type="service" id="router" />
<argument type="service" id="event_dispatcher" />
<argument type="service" id="api.resource_collection" />
<argument key="referenceType">%api.reference_type%</argument>
</service>

<!-- Serializer -->
Expand Down
4 changes: 2 additions & 2 deletions Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function getRouteCollection()
return $this->router->getRouteCollection();
}

/*
/**
* {@inheritdoc}
*/
public function match($pathInfo)
Expand All @@ -78,7 +78,7 @@ public function match($pathInfo)
}
}

/*
/**
* {@inheritdoc}
*/
public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
Expand Down
2 changes: 1 addition & 1 deletion Tests/Behat/TestBundle/Entity/CustomNormalizedDummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function getAlias()
return $this->alias;
}

/**n
/**
* @param string $alias
*/
public function setAlias($alias)
Expand Down
2 changes: 1 addition & 1 deletion Tests/Behat/TestBundle/Entity/Dummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class Dummy
*/
public $relatedDummies;
/**
* @var Array serialize data.
* @var array serialize data.
*
* @ORM\Column(type="json_array", nullable=true)
*/
Expand Down
2 changes: 1 addition & 1 deletion Tests/Behat/TestBundle/Entity/RelatedDummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace Dunglas\ApiBundle\Tests\Behat\TestBundle\Entity;

use Dunglas\ApiBundle\Annotation\Iri;
use Doctrine\ORM\Mapping as ORM;
use Dunglas\ApiBundle\Annotation\Iri;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;

Expand Down
1 change: 1 addition & 0 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase
private static $defaultConfig = [
'title' => 'title',
'description' => 'description',
'reference_type' => 'absolute_path',
'supported_formats' => ['jsonld'],
'cache' => false,
'enable_fos_user' => false,
Expand Down
1 change: 1 addition & 0 deletions Tests/Doctrine/Orm/Extension/PaginationExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\QueryBuilder;
use Dunglas\ApiBundle\Api\Resource;
use Dunglas\ApiBundle\Api\ResourceInterface;
use Dunglas\ApiBundle\Doctrine\Orm\Extension\PaginationExtension;
use Prophecy\Argument;
Expand Down
1 change: 1 addition & 0 deletions Tests/Fixtures/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Query
public function getFirstResult()
{
}

public function getMaxResults()
{
}
Expand Down
1 change: 0 additions & 1 deletion Tests/Routing/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Prophecy\Argument;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Router as SymfonyRouter;

/**
* @author Kévin Dunglas <[email protected]>
Expand Down