Skip to content

Commit

Permalink
Implement simple way to alter base uri resolving. (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
koemeet authored Feb 8, 2017
1 parent 51995f6 commit 5396e52
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 7 deletions.
3 changes: 2 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public function getConfigTreeBuilder()

$rootNode
->children()
->booleanNode('show_version_info')->defaultValue(true)->end()
->booleanNode('show_version_info')->defaultValue(true)->end()
->scalarNode('base_uri')->defaultValue('/api')->end()
->end()
;

Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/MangoJsonApiExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ public function load(array $configs, ContainerBuilder $container)
}

$container->setParameter('json_api.show_version_info', $config['show_version_info']);
$container->setParameter('json_api.base_uri', $config['base_uri']);
}
}
17 changes: 11 additions & 6 deletions EventListener/Serializer/JsonEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\ObjectEvent;
use JMS\Serializer\Naming\PropertyNamingStrategyInterface;
use JMS\Serializer\VisitorInterface;
use Mango\Bundle\JsonApiBundle\Configuration\Metadata\ClassMetadata;
use Mango\Bundle\JsonApiBundle\Configuration\Relationship;
use Mango\Bundle\JsonApiBundle\Resolver\BaseUri\BaseUriResolverInterface;
use Mango\Bundle\JsonApiBundle\Serializer\JsonApiSerializationVisitor;
use Metadata\MetadataFactoryInterface;
use Symfony\Component\HttpFoundation\RequestStack;
Expand Down Expand Up @@ -64,7 +64,10 @@ class JsonEventSubscriber implements EventSubscriberInterface
*/
protected $currentPath;

protected $baseUrl = '/api';
/**
* @var BaseUriResolverInterface
*/
protected $baseUriResolver;

/**
* @param MetadataFactoryInterface $jsonApiMetadataFactory
Expand All @@ -76,12 +79,14 @@ public function __construct(
MetadataFactoryInterface $jsonApiMetadataFactory,
MetadataFactoryInterface $jmsMetadataFactory,
PropertyNamingStrategyInterface $namingStrategy,
RequestStack $requestStack
RequestStack $requestStack,
BaseUriResolverInterface $baseUriResolver
) {
$this->jsonApiMetadataFactory = $jsonApiMetadataFactory;
$this->jmsMetadataFactory = $jmsMetadataFactory;
$this->namingStrategy = $namingStrategy;
$this->requestStack = $requestStack;
$this->baseUriResolver = $baseUriResolver;
}

/**
Expand Down Expand Up @@ -190,7 +195,7 @@ public function onPostSerialize(ObjectEvent $event)
// TODO: Improve link handling
if (true === $metadata->getResource()->getShowLinkSelf()) {
$visitor->addData('links', array(
'self' => $this->baseUrl.'/'.$metadata->getResource()
'self' => $this->baseUriResolver->getBaseUri().'/'.$metadata->getResource()
->getType($object).'/'.$this->getId($metadata, $object),
));
}
Expand All @@ -216,12 +221,12 @@ protected function processRelationshipLinks($primaryObject, Relationship $relati

// TODO: Improve this
if ($relationship->getShowLinkSelf()) {
$links['self'] = $this->baseUrl.'/'.$primaryMetadata->getResource()
$links['self'] = $this->baseUriResolver->getBaseUri().'/'.$primaryMetadata->getResource()
->getType($primaryObject).'/'.$primaryId.'/relationships/'.$relationshipPayloadKey;
}

if ($relationship->getShowLinkRelated()) {
$links['related'] = $this->baseUrl.'/'.$primaryMetadata->getResource()->getType($primaryObject).'/'.$primaryId.'/'.$relationshipPayloadKey;
$links['related'] = $this->baseUriResolver->getBaseUri().'/'.$primaryMetadata->getResource()->getType($primaryObject).'/'.$primaryId.'/'.$relationshipPayloadKey;
}

return $links;
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ Integration of JSON API with Symfony 2 (FOSRestBundle)
If you want to experiment with this implementation, you can just enable this bundle in your `AppKernel` and everything should work directly. Try to serialize some annotated php classes and check it out!

### Configuration reference
```yml
mango_json_api:
show_version_info: true # default
base_uri: /api # default
```
## Annotations
### @Resource
This will define your class as a JSON-API resource, and you can optionally set it's type name.
Expand Down
37 changes: 37 additions & 0 deletions Resolver/BaseUri/BaseUriResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* (c) Steffen Brem
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Mango\Bundle\JsonApiBundle\Resolver\BaseUri;

/**
* @author Steffen Brem <[email protected]>
*/
class BaseUriResolver implements BaseUriResolverInterface
{
/**
* @var string
*/
private $baseUri;

/**
* @param string $baseUri
*/
public function __construct($baseUri)
{
$this->baseUri = $baseUri;
}

/**
* @inheritDoc
*/
public function getBaseUri()
{
return $this->baseUri;
}
}
21 changes: 21 additions & 0 deletions Resolver/BaseUri/BaseUriResolverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* (c) Steffen Brem
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Mango\Bundle\JsonApiBundle\Resolver\BaseUri;

/**
* @author Steffen Brem <[email protected]>
*/
interface BaseUriResolverInterface
{
/**
* @return string
*/
public function getBaseUri();
}
5 changes: 5 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<argument type="service" id="jms_serializer.metadata_factory"/>
<argument type="service" id="jms_serializer.naming_strategy"/>
<argument type="service" id="request_stack"/>
<argument type="service" id="json_api.resolver.base_uri"/>
<tag name="jms_serializer.event_subscriber"/>
</service>

Expand All @@ -37,6 +38,10 @@
<tag name="jms_serializer.subscribing_handler"/>
</service>

<service id="json_api.resolver.base_uri" alias="json_api.resolver.base_uri.default"/>
<service id="json_api.resolver.base_uri.default" class="Mango\Bundle\JsonApiBundle\Resolver\BaseUri\BaseUriResolver">
<argument>%json_api.base_uri%</argument>
</service>
</services>

</container>

0 comments on commit 5396e52

Please sign in to comment.