Skip to content

Instantly share code, notes, and snippets.

@htuscher
Created July 27, 2015 12:00
Show Gist options
  • Select an option

  • Save htuscher/f2ad358846660f95c9ee to your computer and use it in GitHub Desktop.

Select an option

Save htuscher/f2ad358846660f95c9ee to your computer and use it in GitHub Desktop.
<?php
namespace Vendor\Package\Servlets\MyServlet;
use ...;
/**
* @Route(name="stylecardsearch", urlPattern={"/foo/bar.do", "/foo/bar.do*"}, initParams={})
*/
class MyServlet extends HttpServlet {
/**
* @param HttpServletRequestInterface $servletRequest
* @param HttpServletResponseInterface $servletResponse
* @return mixed
* @throws \Exception
* @ResponseFormat(groups="myGroup1, myGroup2")
*/
public function doGet(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse) {
<?php
/**
* @Aspect
*/
class ResponseAspect {
/**
* @Pointcut("call(\Vendor\Package\Servlets\*\*Servlet->do*())")
*/
public function allServletDoMethods() {
}
/**
* @Around("pointcut(allServletDoMethods())")
* @param MethodInvocationInterface $methodInvocation
* @return mixed
*/
public function formatHandlingAdvice(MethodInvocationInterface $methodInvocation) {
$methodName = $methodInvocation->getName();
$servletClassReflection = new ReflectionClass($methodInvocation->getStructureName(), [], [ResponseFormat::ANNOTATION => ResponseFormat::__getClass()]);
$reflectionMethod = $servletClassReflection->getMethod($methodName);
$reflectionAnnotation = $reflectionMethod->getAnnotation('ResponseFormat');
$responseGroups = $reflectionAnnotation->newInstance()->getGroups();
}
<?php
namespace Vendor\Package\Annotations;
use AppserverIo\Lang\Reflection\ReflectionClass;
use AppserverIo\Lang\Reflection\ReflectionAnnotation;
/**
* Annotation implementation representing a @ResponseFormat annotation on a property.
*/
class ResponseFormat extends ReflectionAnnotation {
/**
* The annotation for method, a bean has to be injected.
*
* @var string
*/
const ANNOTATION = 'ResponseFormat';
/**
* This method returns the class name as
* a string.
*
* @return string
*/
public static function __getClass()
{
return __CLASS__;
}
/**
* Returns the groups of the return format.
*
* @return array|null
*/
public function getGroups() {
$groups = $this->getValue('groups');
if (!is_null($groups)) {
$groups = array_map('trim',explode(',', $groups));
}
return $groups;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment