Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a RelationUrlGenerator, with a nice Controller trait :)
- Loading branch information
1 parent
9f0ca96
commit 2ca67f8
Showing
10 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace FSC\HateoasBundle\Controller; | ||
|
||
trait HateoasTrait | ||
{ | ||
protected function generateRelationUrl($object, $rel) | ||
{ | ||
return $this->container->get('fsc_hateoas.routing.relation_url_generator')->generateUrl($object, $rel); | ||
} | ||
|
||
protected function generateSelfUrl($object) | ||
{ | ||
return $this->generateRelationUrl($object, 'self'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace FSC\HateoasBundle\Routing; | ||
|
||
use FSC\HateoasBundle\Metadata\MetadataFactoryInterface; | ||
use FSC\HateoasBundle\Factory\ParametersFactoryInterface; | ||
|
||
class RelationUrlGenerator | ||
{ | ||
protected $urlGenerator; | ||
protected $metadataFactory; | ||
protected $parametersFactory; | ||
|
||
public function __construct(UrlGenerator $urlGenerator, MetadataFactoryInterface $metadataFactory, | ||
ParametersFactoryInterface $parametersFactory) | ||
{ | ||
$this->urlGenerator = $urlGenerator; | ||
$this->metadataFactory = $metadataFactory; | ||
$this->parametersFactory = $parametersFactory; | ||
} | ||
|
||
public function generateUrl($object, $rel) | ||
{ | ||
$metadata = $this->metadataFactory->getMetadata($object); | ||
$relationMetadata = $metadata->getRelation($rel); | ||
|
||
if (null === $relationMetadata) { | ||
throw new \RuntimeException(sprintf('Relation "%s" doesn\'t exist.', $rel)); | ||
} | ||
|
||
return $this->urlGenerator->generate( | ||
$relationMetadata->getRoute(), | ||
$this->parametersFactory->createParameters($object, $relationMetadata->getParams()) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
looks like you forgot to add the trait ..