1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* AppserverIo\RestApi\Servlets\AnnotatedServlet |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/appserver-io/restapi |
18
|
|
|
* @link http://www.appserver.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\RestApi\Servlets; |
22
|
|
|
|
23
|
|
|
use Symfony\Component\Finder\Finder; |
24
|
|
|
use AppserverIo\Psr\HttpMessage\Protocol; |
25
|
|
|
use AppserverIo\Psr\Servlet\Http\HttpServlet; |
26
|
|
|
use AppserverIo\Psr\Servlet\ServletConfigInterface; |
27
|
|
|
use AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface; |
28
|
|
|
use AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Annotated servlet that renders an OpenApi 2.0 compatible swagger JSON configuration file. |
32
|
|
|
* |
33
|
|
|
* @author Tim Wagner <[email protected]> |
34
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
35
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
36
|
|
|
* @link https://github.com/appserver-io/restapi |
37
|
|
|
* @link http://www.appserver.io |
38
|
|
|
*/ |
39
|
|
|
class SwaggerServlet extends HttpServlet |
40
|
|
|
{ |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The content of the swagger JSON file. |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $swagger; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Initializes the servlet with the passed configuration. |
51
|
|
|
* |
52
|
|
|
* @param \AppserverIo\Psr\Servlet\ServletConfigInterface $servletConfig The configuration to initialize the servlet with |
53
|
|
|
* |
54
|
|
|
* @throws \AppserverIo\Psr\Servlet\ServletException Is thrown if the configuration has errors |
55
|
|
|
* @return void |
56
|
|
|
* @see \AppserverIo\Psr\Servlet\GenericServlet::init() |
57
|
|
|
*/ |
58
|
|
|
public function init(ServletConfigInterface $servletConfig) |
59
|
|
|
{ |
60
|
|
|
|
61
|
|
|
// call parent method |
62
|
|
|
parent::init($servletConfig); |
63
|
|
|
|
64
|
|
|
// initialize the finder for a fine granular |
65
|
|
|
$finder = new Finder(); |
66
|
|
|
$finder->in(sprintf('%s/*/classes', $servletConfig->getWebappPath())); |
67
|
|
|
$finder->files('*.php'); |
|
|
|
|
68
|
|
|
|
69
|
|
|
// generate the content of the Swagger JSON file |
70
|
|
|
$this->swagger = \Swagger\scan($finder); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Handles a HTTP GET request. |
75
|
|
|
* |
76
|
|
|
* @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface $servletRequest The request instance |
77
|
|
|
* @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The response instance |
78
|
|
|
* |
79
|
|
|
* @return void |
80
|
|
|
* @throws \AppserverIo\Psr\Servlet\ServletException Is thrown if the request method is not implemented |
81
|
|
|
* @see \AppserverIo\Psr\Servlet\Http\HttpServlet::doGet() |
82
|
|
|
*/ |
83
|
|
|
public function doGet(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse) |
84
|
|
|
{ |
85
|
|
|
$servletResponse->addHeader(Protocol::HEADER_CONTENT_TYPE, 'application/json'); |
86
|
|
|
$servletResponse->appendBodyStream($this->swagger); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.