Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
V3 (#8)
Browse files Browse the repository at this point in the history
* Initial refactoring
* Add some integration tests
* Add travis integration
* Bump PHP to 5.6
* Add travis-ci badge
  • Loading branch information
DerManoMann authored Sep 6, 2017
1 parent b920688 commit 75cbd69
Show file tree
Hide file tree
Showing 20 changed files with 583 additions and 109 deletions.
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: php

php:
- 5.6
- 7
- 7.1
- 7.2

sudo: false

install:
- travis_retry composer install --no-interaction --prefer-dist

after_script:
- php vendor/bin/coveralls -v
78 changes: 69 additions & 9 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Bridge to generate swagger documentation from Silex Annotations
===============================================================

[![Build Status](https://travis-ci.org/DerManoMann/silex2swagger.png)](https://travis-ci.org/DerManoMann/silex2swagger)

## Introduction
[Silex Annotations](https://github.com/danadesrosiers/silex-annotation-provider) are an easy way to configure
routes in Silex.
Expand All @@ -9,8 +11,9 @@ With this bridge, in combination with [Swagger-PHP](https://github.com/zircote/s
Typically the Swagger annotations are added on top of existing Silex annotations to complement/complete the definitions.


## Example
````
## Mixing Silex and Swagger annotations

````php
<?php

namespace mycode;
Expand Down Expand Up @@ -46,23 +49,75 @@ class Controller

````

## Attaching Swagger properties to a Silex ````Request````
Silex2Swagger provides a custom ````Request```` annotation that allows to attach any supported
Swagger property to a request. All that is required is to use a custom implementation of the
Silex ````Request```` annotation.

````php
<?php

namespace mycode;

use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Swagger\Annotations as SWG;
use DDesrosiers\SilexAnnotations\Annotations as SLX;
use Radebatz\Silex2Swagger\Swagger\Annotations as S2S;

class Controller
{

/**
* Update.
*
* @SLX\Route(
* @S2S\Request(method="POST", uri="/login",
* @S2S\SwaggerProperty(name="consumes", value={"application/x-www-form-urlencoded"})
* ),
*
* @SWG\Parameter(
* name="email",
* in="formData",
* description="Email address",
* required=true,
* type="string"
* ),
* @SWG\Parameter(
* name="password",
* in="formData",
* description="Password",
* required=true,
* type="string"
* )
* )
*/
public function update(Application $app, Request $request)
{
...
}

````

## Generating Swagger
### Using the CL
````
./bin/silex2swagger silex2swagger:build --path=[src] --file=swagger.json
````

### Using (Simple) Code
```php
### Using (simple) cCode
````php
<?php

require 'vendor/autoload.php';

use Radebatz\Silex\Swagger\SilexSwaggerAnalysis;
use Silex\Application;
use Radebatz\Silex2Swagger\Swagger\S2SAnalysis;
use Radebatz\Silex2Swagger\Swagger\S2SConverter;

$swagger = \Swagger\scan('./src', ['analysis' => new Silex2SwaggerAnalysis([], null, new Silex2SwaggerConverter(new Application()))]);
echo $swagger
```
$swagger = \Swagger\scan('./src', ['analysis' => new S2SAnalysis([], null, new S2SConverter(new Application()))]);
echo $swagger;
````

For a more complete example have a look at the included Symfony Console command.

Expand All @@ -71,7 +126,7 @@ For a more complete example have a look at the included Symfony Console command.
* All annotation classes need to be in the class path (visible by the auto loader).
* In order to accurately merge/group annotations it is necessary to use the `@SLX\Route`
Example:
````
````php
/**
* @SLX\Route(
* @SLX\Request(method="GET", uri="/foo"),
Expand Down Expand Up @@ -105,3 +160,8 @@ For a more complete example have a look at the included Symfony Console command.

### v2.0.2
* Fix double slash being created using ````@Controller```` without a prefix

### v3.0.0
* Introduce unique namespace and cleanup
* Add custom Swagger-PHP Request annotation that supports all swagger properties
* Bump PHP requirements to PHP 5.6
2 changes: 1 addition & 1 deletion bin/silex2swagger
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ set_time_limit(0);
use Symfony\Component\Console\Application;

$application = new Application();
$application->add(new \Radebatz\Console\Command\Silex2SwaggerCommand());
$application->add(new \Radebatz\Silex2Swagger\Console\Command\Silex2SwaggerCommand());
$application->run();
17 changes: 8 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,32 @@
"source": "https://github.com/DerManoMann/silex2swagger.git"
},
"require": {
"php": ">=5.5.9",
"php": ">=5.6",
"ddesrosiers/silex-annotation-provider": "~2.0",
"zircote/swagger-php": "~2.0",
"psr/log": "^1.0"
},
"require-dev": {
"symfony/console": "~2.8|3.0.0",
"phpunit/phpunit": "~5.0",
"satooshi/php-coveralls": "dev-master"
"satooshi/php-coveralls": "^1.0"
},
"autoload": {
"psr-4": {
"Radebatz\\Silex\\Swagger\\": "src/Silex/Swagger",
"Radebatz\\Console\\Command\\": "src/Console/Command"
"Radebatz\\Silex2Swagger\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Radebatz\\Silex\\Swagger\\Tests\\": "tests"
"Radebatz\\Silex2Swagger\\Tests\\": "tests"
}
},
"config": {
"bin-dir": "bin"
},
"bin": [
"bin/silex2swagger"
],
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
"dev-master": "3.0.x-dev"
}
}
}
22 changes: 22 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Silex 2 Swagger Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>
</phpunit>
67 changes: 20 additions & 47 deletions src/Console/Command/Silex2SwaggerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,18 @@
* file that was distributed with this source code.
*/

namespace Radebatz\Console\Command;
namespace Radebatz\Silex2Swagger\Console\Command;

use Psr\Log\AbstractLogger;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Silex\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Swagger\Logger;
use Radebatz\Silex\Swagger\Silex2SwaggerAnalysis;
use Radebatz\Silex\Swagger\Silex2SwaggerConverter;

/**
* Simple Psr logger wrapper around the Swagger logger.
*/
class PsrLogger extends AbstractLogger {
protected $logger;

/**
*/
public function __construct(Logger $logger)
{
$this->logger = $logger;
}

/**
* {@inheritDoc}
*/
public function log($level, $message, array $context = [])
{
if (in_array($level, [LogLevel::NOTICE, LogLevel::INFO])) {
$this->logger->notice($message);
} else {
$this->logger->warning($message);
}
}
}
use Radebatz\Silex2Swagger\Swagger\PsrLogger;
use Radebatz\Silex2Swagger\Swagger\S2SAnalysis;
use Radebatz\Silex2Swagger\Swagger\S2SConverter;

/**
* Silex 2 Swagger command.
Expand All @@ -60,19 +33,18 @@ class Silex2SwaggerCommand extends Command
protected function configure()
{
$this
->setName('silex2swagger:build')
->setDescription('Build swagger.json')
->addOption('file', null, InputOption::VALUE_REQUIRED, 'Output file; if empty stdout will be used.', null)
->addOption('path', null, InputOption::VALUE_REQUIRED, 'Source path.', './src')
->addOption('namespace', null, InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Additional annotation namespaces to process.', [])
->addOption('auto-response', null, InputOption::VALUE_NONE, 'Create default response if none set.')
->addOption('auto-description', null, InputOption::VALUE_NONE, 'Create default operation description based on method and path if none set.')
->addOption('auto-summary', null, InputOption::VALUE_NONE, 'Create default operation summary based on method and path if none set.')
->setHelp(<<<EOT
->setName('silex2swagger:build')
->setDescription('Build swagger.json')
->addOption('file', null, InputOption::VALUE_REQUIRED, 'Output file; if empty stdout will be used.', null)
->addOption('path', null, InputOption::VALUE_REQUIRED, 'Source path.', './src')
->addOption('namespace', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Additional annotation namespaces to process.', [])
->addOption('auto-response', null, InputOption::VALUE_NONE, 'Create default response if none set.')
->addOption('auto-description', null, InputOption::VALUE_NONE, 'Create default operation description based on method and path if none set.')
->addOption('auto-summary', null, InputOption::VALUE_NONE, 'Create default operation summary based on method and path if none set.')
->setHelp(<<<EOT
Build swagger.json.
EOT
)
;
);
}

/**
Expand All @@ -96,10 +68,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
return;
}

if ($entry instanceof Exception) {
if ($entry instanceof \Exception) {
$entry = $entry->getMessage();
}
foreach ((array) $entry as $message) {
foreach ((array)$entry as $message) {
$output->writeln(sprintf('%s: %s', $type, $message));
}
};
Expand All @@ -109,7 +81,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$swagger = \Swagger\scan(
$path,
array_merge(
['analysis' => new Silex2SwaggerAnalysis([], null, $this->getConverter($logger, $options), $namespaces)],
['analysis' => new S2SAnalysis([], null, $this->getConverter($logger, $options), $namespaces)],
$this->scanOptions()
)
);
Expand All @@ -134,7 +106,8 @@ protected function getSilexApp()
*
* @return array
*/
protected function scanOptions() {
protected function scanOptions()
{
return [];
}

Expand All @@ -153,6 +126,6 @@ protected function converterOptions()
*/
protected function getConverter(LoggerInterface $logger = null, array $options = [])
{
return new Silex2SwaggerConverter($this->getSilexApp(), array_merge($options, $this->converterOptions()), $logger);
return new S2SConverter($this->getSilexApp(), array_merge($options, $this->converterOptions()), $logger);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Radebatz\Silex\Swagger\Annotations;
namespace Radebatz\Silex2Swagger\Swagger\Annotations;

use Swagger\Annotations\AbstractAnnotation;

Expand Down
26 changes: 26 additions & 0 deletions src/Swagger/Annotations/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the silex2swagger library.
*
* (c) Martin Rademacher <mano@radebatz.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Radebatz\Silex2Swagger\Swagger\Annotations;

use DDesrosiers\SilexAnnotations\Annotations as SLX;

/**
* A Silex <code>Request</code> annotation with support for arbitrary swagger properties.
*
* @Annotation
*/
class Request extends SLX\Request
{

/** @var \Radebatz\Silex2Swagger\Swagger\Annotations\SwaggerProperty[] */
public $swaggerProperties = [];
}
27 changes: 27 additions & 0 deletions src/Swagger/Annotations/SwaggerProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the silex2swagger library.
*
* (c) Martin Rademacher <mano@radebatz.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Radebatz\Silex2Swagger\Swagger\Annotations;

/**
* A custom Swagger property
*
* @Annotation
*/
class SwaggerProperty
{

/** @var string */
public $name = null;

/** @var mixed */
public $value = null;
}
Loading

0 comments on commit 75cbd69

Please sign in to comment.