-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
214 additions
and
2 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,84 @@ | ||
<?php | ||
|
||
namespace Bigz\HalapiBundle\EventListener; | ||
|
||
use App\Transformer\EntityTransformer; | ||
use League\Fractal\Resource\Collection; | ||
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
use League\Fractal\Manager; | ||
use League\Fractal\Resource\Item; | ||
use League\Fractal\Serializer\JsonApiSerializer; | ||
|
||
/** | ||
* Class ResponseListener | ||
* Exeprimental... waiting for some feedbacks. | ||
* @package App\EventListener | ||
*/ | ||
class ResponseListener | ||
{ | ||
/** | ||
* @param FilterResponseEvent $event | ||
*/ | ||
public function onKernelResponse(FilterResponseEvent $event) | ||
{ | ||
if (200 !== $event->getResponse()->getStatusCode() | ||
|| 'application/vnd.api+json' !== $event->getRequest()->headers->get('Content-Type') | ||
) { | ||
return; | ||
} | ||
|
||
$content = json_decode($event->getResponse()->getContent(), 1); | ||
|
||
$fractal = new Manager(); | ||
$includes = []; | ||
$inclusion = $event->getRequest()->get('include'); | ||
|
||
if ($inclusion) { | ||
$fractal->parseIncludes($inclusion); | ||
$includes = explode(',', $inclusion); | ||
} | ||
|
||
$fractal->setSerializer(new JsonApiSerializer('')); | ||
$resource = $this->getResource($content, $includes, $this->guessType($event->getRequest())); | ||
$data = $fractal->createData($resource)->toArray(); | ||
|
||
if ($resource instanceof Collection) { | ||
$data['links'] = $content['_links']; | ||
} | ||
|
||
$event->getResponse()->setContent(json_encode($data)); | ||
} | ||
|
||
/** | ||
* @param $content | ||
* @param $includes | ||
* @param $type | ||
* @return Collection|Item | ||
*/ | ||
private function getResource($content, $includes, $type) | ||
{ | ||
if (isset($content['id'])) { | ||
return new Item($content, new EntityTransformer($includes), $type); | ||
} | ||
|
||
return new Collection($content['_embedded'], new EntityTransformer($includes), $type); | ||
} | ||
|
||
/** | ||
* Guess an entityType from the route path. | ||
* Doesn't seem very reliable... (subresources & stuff like that) | ||
* | ||
* @param Request $request | ||
* @return bool|string | ||
*/ | ||
private function guessType(Request $request) | ||
{ | ||
$path = $request->getPathInfo(); | ||
|
||
$enPosition = strpos($path,'/', 1); | ||
|
||
return substr($path, 1, $enPosition ? $enPosition - 2 : strlen($path) - 2); | ||
} | ||
} |
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,57 @@ | ||
<?php | ||
|
||
namespace Bigz\HalapiBundle\Transformer; | ||
|
||
use League\Fractal\TransformerAbstract; | ||
|
||
class EntityTransformer extends TransformerAbstract | ||
{ | ||
public function __construct($includes) | ||
{ | ||
$this->availableIncludes = $includes; | ||
} | ||
|
||
/** | ||
* Turn this item object into a generic array | ||
* | ||
* @return array | ||
*/ | ||
public function transform($toFormat) | ||
{ | ||
$data = $toFormat; | ||
|
||
if (isset($data['_links'])) { | ||
$data['links'] = []; | ||
|
||
if (isset($data['_links']['self'])) { | ||
$data['links']['self'] = $data['_links']['self']; | ||
} | ||
if (isset($data['_links']['next'])) { | ||
$data['links']['next'] = $data['_links']['next']; | ||
} | ||
|
||
unset($data['_links']); | ||
} | ||
|
||
unset($data['_embedded']); | ||
return $data; | ||
} | ||
|
||
public function __call($name, $arguments) | ||
{ | ||
if (strpos($name, 'include') === 0) { | ||
$shortName = strtolower(substr($name, 7, strlen($name) - 7)); | ||
$relation = $arguments[0]['_embedded'][$shortName]; | ||
if (isset($relation['id'])) { | ||
return $this->item($relation, new EntityTransformer($this->availableIncludes), $shortName); | ||
} | ||
|
||
return $this->collection( | ||
$relation, | ||
new EntityTransformer($this->availableIncludes), | ||
substr($shortName, 0, strlen($shortName) - 1) // singular | ||
); | ||
|
||
} | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.