partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit 2d7a112110119901abec0eada43dd5bc6488eec9
parent ebbfedb153bbdb09ec4dbef0244a5a21f67f2365
Author: Felicitus <felicitus@felicitus.org>
Date:   Tue, 26 May 2015 18:02:24 +0200

This bundle extends the NelmioApiDocBundle so that it can read annotations of the parent class. That way, PartKeepr can
have one superclass with commonly used parameters (like start and limit for pagination) without the need of re-defining
common parameters for each child class.

The current limitation is that it currently only works for direct descendants and not for the whole class hierarchy.

Diffstat:
Asrc/PartKeepr/RESTBundle/Request/ParamReader.php | 25+++++++++++++++++++++++++
Asrc/PartKeepr/RESTBundle/Resources/config/request.xml | 29+++++++++++++++++++++++++++++
2 files changed, 54 insertions(+), 0 deletions(-)

diff --git a/src/PartKeepr/RESTBundle/Request/ParamReader.php b/src/PartKeepr/RESTBundle/Request/ParamReader.php @@ -0,0 +1,24 @@ +<?php +namespace PartKeepr\RESTBundle\Request; + +use FOS\RestBundle\Request\ParamReader as FOSRestParamReader; + +class ParamReader extends FOSRestParamReader +{ + public function getParamsFromMethod(\ReflectionMethod $method) + { + $parentParams = array(); + $params = parent::getParamsFromMethod($method); + + // This loads the annotations of the parent method + $declaringClass = $method->getDeclaringClass(); + $parentClass = $declaringClass->getParentClass(); + + if ($parentClass && $parentClass->hasMethod($method->getShortName())) { + $parentMethod = $parentClass->getMethod($method->getShortName()); + $parentParams = parent::getParamsFromMethod($parentMethod); + } + + return array_merge($params, $parentParams); + } +}+ \ No newline at end of file diff --git a/src/PartKeepr/RESTBundle/Resources/config/request.xml b/src/PartKeepr/RESTBundle/Resources/config/request.xml @@ -0,0 +1,29 @@ +<?xml version="1.0" ?> + +<container xmlns="http://symfony.com/schema/dic/services" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> + + <parameters> + + <parameter key="fos_rest.request.param_fetcher.class">FOS\RestBundle\Request\ParamFetcher</parameter> + <parameter key="partkeepr.request.param_fetcher.reader.class">PartKeepr\RESTBundle\Request\ParamReader</parameter> + + </parameters> + + <services> + + <service id="fos_rest.request.param_fetcher" class="%fos_rest.request.param_fetcher.class%" scope="request"> + <argument type="service" id="partkeepr.request.param_fetcher.reader"/> + <argument type="service" id="request"/> + <argument type="service" id="fos_rest.violation_formatter"/> + <argument type="service" id="validator" on-invalid="null"/> + </service> + + <service id="partkeepr.request.param_fetcher.reader" class="%partkeepr.request.param_fetcher.reader.class%"> + <argument type="service" id="annotation_reader"/> + </service> + + </services> + +</container>