partkeepr

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

commit 7a89c25421eb78db0ae9fc3e999bde4b05374044
parent 4a5c5e2a00d77f2e9ad410cceac0c0419fea5dd3
Author: Felicitus <felicitus@felicitus.org>
Date:   Tue, 26 May 2015 18:00:07 +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/ApiDocBundle/DependencyInjection/PartKeeprApiDocExtension.php | 27+++++++++++++++++++++++++++
Asrc/PartKeepr/ApiDocBundle/Extractor/ApiDocExtractor.php | 42++++++++++++++++++++++++++++++++++++++++++
Asrc/PartKeepr/ApiDocBundle/PartKeeprApiDocBundle.php | 13+++++++++++++
Asrc/PartKeepr/ApiDocBundle/README.md | 9+++++++++
Asrc/PartKeepr/ApiDocBundle/Resources/config/services.xml | 20++++++++++++++++++++
5 files changed, 111 insertions(+), 0 deletions(-)

diff --git a/src/PartKeepr/ApiDocBundle/DependencyInjection/PartKeeprApiDocExtension.php b/src/PartKeepr/ApiDocBundle/DependencyInjection/PartKeeprApiDocExtension.php @@ -0,0 +1,27 @@ +<?php + + +namespace PartKeepr\ApiDocBundle\DependencyInjection; + + +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\Config\FileLocator; +use Symfony\Component\HttpKernel\DependencyInjection\Extension; +use Symfony\Component\DependencyInjection\Loader; + +/** + * This is the class that loads and manages your bundle configuration + * + * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} + */ +class PartKeeprApiDocExtension extends Extension +{ + /** + * {@inheritdoc} + */ + public function load(array $configs, ContainerBuilder $container) + { + $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); + $loader->load('services.xml'); + } +} diff --git a/src/PartKeepr/ApiDocBundle/Extractor/ApiDocExtractor.php b/src/PartKeepr/ApiDocBundle/Extractor/ApiDocExtractor.php @@ -0,0 +1,41 @@ +<?php +namespace PartKeepr\ApiDocBundle\Extractor; + + +use Symfony\Component\Routing\Route; +use Nelmio\ApiDocBundle\Annotation\ApiDoc; + +class ApiDocExtractor extends \Nelmio\ApiDocBundle\Extractor\ApiDocExtractor +{ + /** + * + * Parses annotations for a given method, and adds new information to the given ApiDoc + * annotation. Useful to extract information from the FOSRestBundle annotations. + * + * This method has been adjusted so that it can read annotations from the parent class. + * + * @param ApiDoc $annotation + * @param Route $route + * @param \ReflectionMethod $method + */ + protected function parseAnnotations(ApiDoc $annotation, Route $route, \ReflectionMethod $method) + { + $classAnnotations = $this->reader->getMethodAnnotations($method); + + $declaringClass = $method->getDeclaringClass(); + $parentClass = $declaringClass->getParentClass(); + + $parentAnnotations = array(); + + if ($parentClass && $parentClass->hasMethod($method->getShortName())) { + $parentMethod = $parentClass->getMethod($method->getShortName()); + $parentAnnotations = $this->reader->getMethodAnnotations($parentMethod); + } + + $allAnnotations = array_merge($classAnnotations, $parentAnnotations); + + foreach ($this->handlers as $handler) { + $handler->handle($annotation, $allAnnotations, $route, $method); + } + } +}+ \ No newline at end of file diff --git a/src/PartKeepr/ApiDocBundle/PartKeeprApiDocBundle.php b/src/PartKeepr/ApiDocBundle/PartKeeprApiDocBundle.php @@ -0,0 +1,13 @@ +<?php + +namespace PartKeepr\ApiDocBundle; + +use Symfony\Component\HttpKernel\Bundle\Bundle; + +class PartKeeprApiDocBundle extends Bundle +{ + public function getParent() + { + return 'NelmioApiDocBundle'; + } +} diff --git a/src/PartKeepr/ApiDocBundle/README.md b/src/PartKeepr/ApiDocBundle/README.md @@ -0,0 +1,8 @@ +PartKeeprApiDocBundle +===================== + +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.+ \ No newline at end of file diff --git a/src/PartKeepr/ApiDocBundle/Resources/config/services.xml b/src/PartKeepr/ApiDocBundle/Resources/config/services.xml @@ -0,0 +1,20 @@ +<?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="partkeepr_api_doc.extractor.api_doc_extractor.class">PartKeepr\ApiDocBundle\Extractor\ApiDocExtractor</parameter> + </parameters> + + <services> + <service id="nelmio_api_doc.extractor.api_doc_extractor" + class="%partkeepr_api_doc.extractor.api_doc_extractor.class%"> + <argument type="service" id="service_container"/> + <argument type="service" id="router"/> + <argument type="service" id="annotation_reader"/> + <argument type="service" id="nelmio_api_doc.doc_comment_extractor"/> + <argument type="collection"/> + </service> + </services> +</container>