Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 22 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
# cakephp-swagger

[![Build Status](https://img.shields.io/travis/alt3/cakephp-swagger/master.svg?style=flat-square)](https://travis-ci.org/alt3/cakephp-swagger)
[![StyleCI Status](https://styleci.io/repos/45741948/shield)](https://styleci.io/repos/45741948)
[![Coverage Status](https://img.shields.io/codecov/c/github/alt3/cakephp-swagger/master.svg?style=flat-square)](https://codecov.io/github/alt3/cakephp-swagger)
[![Total Downloads](https://img.shields.io/packagist/dt/alt3/cakephp-swagger.svg?style=flat-square)](https://packagist.org/packages/alt3/cakephp-swagger)
[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](LICENSE.txt)

CakePHP 4.x plugin that adds auto-generated Swagger 2.0 documentation to your projects using swagger-php and swagger-ui.
CakePHP 5.x plugin that adds auto-generated Swagger 2.0 documentation to your projects using swagger-php and swagger-ui.

## Requirements

* CakePHP 4.0+
* CakePHP 5.0+
* Some [swagger-php](https://github.com/zircote/swagger-php) annotation knowledge

## Installation

Install the plugin using composer:

```bash
composer require alt3/cakephp-swagger
composer require cstaf/cakephp-swagger
```

## Enabling
## Enabling

Enable the plugin in the `bootstrap()` method found in `src/Application.php`:

```php
public function bootstrap()
{
parent::bootstrap();
$this->addPlugin('Alt3/Swagger');
$this->addPlugin('Cstaf/Swagger');
}
```

> Also make sure that AssetMiddleware is loaded inside `Application.php` or all Swagger page assets will 404.

## Installation check

After enabling the plugin, browsing to `http://your.app/alt3/swagger` should now produce the
After enabling the plugin, browsing to `http://your.app/swagger` should now produce the
[Swagger-UI](http://swagger.io/swagger-ui/) interface:

![Default UI index](/docs/images/ui-index-default.png)
Expand All @@ -53,7 +48,7 @@ use Cake\Core\Configure;
return [
'Swagger' => [
'ui' => [
'title' => 'ALT3 Swagger',
'title' => 'Swagger',
'validator' => true,
'api_selector' => true,
'route' => '/swagger/',
Expand Down Expand Up @@ -94,12 +89,10 @@ Use the `ui` section to customize the following Swagger-UI options:
- `title`: sets the Swagger-UI page title, defaults to `cakephp-swagger`
- `validator`: show/hide the validator image, defaults to `true`
- `api_selector`: show/hide the api selector form fields, defaults to `true`
- `route`: expose the UI using a custom route, defaults to `/alt3/swagger/`
- `schemes`: array used to specify third field
[used to generate the BASE URL](https://github.com/alt3/cakephp-swagger/issues/6)
(`host` is fetched realtime, `basePath` is also fetched realtime if not
[defined via annotations](https://github.com/alt3/cakephp-swagger/issues/29)),
defaults to `null`
- `route`: expose the UI using a custom route, defaults to `/swagger/`
- `schemes`: array used to specify third field
(`host` is fetched realtime, `basePath` is also fetched realtime if not),
defaults to `null`

> Please note that the UI will auto-load the first document found in the library.

Expand All @@ -108,8 +101,8 @@ defaults to `null`
Use the `docs` section to customize the following options:

- `crawl`: enable to crawl-generate new documents instead of
serving from filesystem, defaults to `true`
- `route`: expose the documents using a custom route, defaults to `/alt3/swagger/docs/`
serving from filesystem, defaults to `true`
- `route`: expose the documents using a custom route, defaults to `/swagger/docs/`
- `cors`: specify CORS headers to send with the json responses, defaults to `null`

### Library section
Expand All @@ -118,16 +111,16 @@ Use the `library` section to specify one or multiple swagger documents so:

- swagger-php will know which files and folders to parse for annotations
- swagger-php can produce the swagger json
- this plugin can expose the json at `http://your.app/alt3/swagger/docs/:id`
(so it can be used by the UI)
- this plugin can expose the json at `http://your.app/swagger/docs/:id`
(so it can be used by the UI)

The following library example would result in:

- swagger-php scanning all files and folders defined in `include`
- swagger-php ignoring all files and folders defined in `exclude`
- two endpoints serving json swagger documents:
- `http://your.app/alt3/swagger/docs/api`
- `http://your.app/alt3/swagger/docs/editor`
- `http://your.app/swagger/docs/api`
- `http://your.app/swagger/docs/editor`

```php
'library' => [
Expand All @@ -147,7 +140,7 @@ The following library example would result in:
]
```

It would also make `http://your.app/alt3/swagger/docs` produce a json list
It would also make `http://your.app/swagger/docs` produce a json list
with links to all available documents similar to the example below.

```json
Expand All @@ -156,11 +149,11 @@ with links to all available documents similar to the example below.
"data": [
{
"document": "api",
"link": "http://your.app/alt3/swagger/docs/api"
"link": "http://your.app/swagger/docs/api"
},
{
"document": "editor",
"link": "http://your.app/alt3/swagger/docs/editor"
"link": "http://your.app/swagger/docs/editor"
}
]
}
Expand All @@ -178,7 +171,7 @@ bin/cake swagger makedocs <host>
```

> The host argument (e.g. your.app.com) is required, should not include
protocols and is used to set the `host` property inside your swagger documents.
> protocols and is used to set the `host` property inside your swagger documents.

## Quickstart Annotation Example

Expand Down Expand Up @@ -261,11 +254,3 @@ Which should result in:
- [The Swagger Specification](https://github.com/swagger-api/swagger-spec)
- [PHP Annotation Examples](https://github.com/zircote/swagger-php/tree/master/Examples)
- [Swagger Document Checklist](http://apievangelist.com/2015/06/15/my-minimum-viable-definition-for-a-complete-swagger-api-definition/)

## Contribute

Before submitting a PR make sure:

- [PHPUnit](http://book.cakephp.org/4.0/en/development/testing.html#running-tests)
and [CakePHP Code Sniffer](https://github.com/cakephp/cakephp-codesniffer) tests pass
- [Coveralls Code Coverage ](https://coveralls.io/github/alt3/cakephp-swagger) remains at 100%
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
{
"name": "alt3/cakephp-swagger",
"description": "Instant Swagger documentation for your CakePHP 4.x APIs",
"name": "cstaf/cakephp-swagger",
"description": "Instant Swagger documentation for your CakePHP 5.x APIs",
"type": "cakephp-plugin",
"license": "MIT",
"require": {
"php": ">=7.0",
"cakephp/cakephp": "^4.0",
"cakephp/cakephp": "^5.0",
"zircote/swagger-php": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^7.0"
},
"support": {
"issues": "https://github.com/alt3/cakephp-swagger/issues",
"source": "https://github.com/alt3/cakephp-swagger"
"issues": "https://github.com/joaopatrocinio/cakephp-swagger/issues",
"source": "https://github.com/joaopatrocinio/cakephp-swagger"
},
"autoload": {
"psr-4": {
"Alt3\\Swagger\\": "src"
"Cstaf\\Swagger\\": "src"
},
"files": [
"aliases.php"
]
},
"autoload-dev": {
"psr-4": {
"Alt3\\Swagger\\Test\\": "tests"
"Cstaf\\Swagger\\Test\\": "tests"
}
}
}
28 changes: 14 additions & 14 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,49 @@
/*
* Connect routes using configuration file, otherwise use defaults:
*
* - UI, defaults to /alt3/swagger
* - docs, defaults to /alt3/swagger/docs
* - per library document, defaults to /alt3/swagger/docs/:id
* - UI, defaults to /swagger
* - docs, defaults to /swagger/docs
* - per library document, defaults to /swagger/docs/:id
*/
Router::plugin('Alt3/Swagger', [
$routes->plugin('Cstaf/Swagger', [
'path' => '/',
], function (\Cake\Routing\RouteBuilder $routes) {

// UI route
if (Configure::read('Swagger.ui.route')) {
$routes->connect(
Configure::read('Swagger.ui.route'),
['plugin' => 'Alt3/Swagger', 'controller' => 'Ui', 'action' => 'index']
['plugin' => 'Cstaf/Swagger', 'controller' => 'Ui', 'action' => 'index']
);
} else {
$routes->connect(
'/alt3/swagger/',
['plugin' => 'Alt3/Swagger', 'controller' => 'Ui', 'action' => 'index']
'/cstaf/swagger/',
['plugin' => 'Cstaf/Swagger', 'controller' => 'Ui', 'action' => 'index']
);
}

// Documents route
if (Configure::read('Swagger.docs.route')) {
$routes->connect(
Configure::read('Swagger.docs.route'),
['plugin' => 'Alt3/Swagger', 'controller' => 'Docs', 'action' => 'index']
['plugin' => 'Cstaf/Swagger', 'controller' => 'Docs', 'action' => 'index']
);

$routes->connect(
Configure::read('Swagger.docs.route') . ':id',
['plugin' => 'Alt3/Swagger', 'controller' => 'Docs', 'action' => 'index']
Configure::read('Swagger.docs.route') . '{id}',
['plugin' => 'Cstaf/Swagger', 'controller' => 'Docs', 'action' => 'index']
)
->setPatterns(['id' => '\w+'])
->setPass(['id']);
} else {
$routes->connect(
'/alt3/swagger/docs',
['plugin' => 'Alt3/Swagger', 'controller' => 'Docs', 'action' => 'index']
'/cstaf/swagger/docs',
['plugin' => 'Cstaf/Swagger', 'controller' => 'Docs', 'action' => 'index']
);

$routes->connect(
'/alt3/swagger/docs/:id',
['plugin' => 'Alt3/Swagger', 'controller' => 'Docs', 'action' => 'index']
'/cstaf/swagger/docs/{id}',
['plugin' => 'Cstaf/Swagger', 'controller' => 'Docs', 'action' => 'index']
)
->setPatterns(['id' => '\w+'])
->setPass(['id']);
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/AppController.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);

namespace Alt3\Swagger\Controller;
namespace Cstaf\Swagger\Controller;

use Cake\Controller\Controller as BaseController;
use Cake\Core\Configure;
Expand Down
6 changes: 3 additions & 3 deletions src/Controller/DocsController.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
declare(strict_types=1);

namespace Alt3\Swagger\Controller;
namespace Cstaf\Swagger\Controller;

use Alt3\Swagger\Lib\SwaggerTools;
use Cstaf\Swagger\Lib\SwaggerTools;
use Cake\Routing\Router;

class DocsController extends AppController
Expand Down Expand Up @@ -65,7 +65,7 @@ protected function getJsonDocumentList()
static::$apiResponseBody['data'][] = [
'document' => $document,
'link' => Router::url([
'plugin' => 'Alt3/Swagger',
'plugin' => 'Cstaf/Swagger',
'controller' => 'Docs',
'action' => 'index',
$document,
Expand Down
4 changes: 2 additions & 2 deletions src/Controller/UiController.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);

namespace Alt3\Swagger\Controller;
namespace Cstaf\Swagger\Controller;

use Cake\Routing\Router;

Expand Down Expand Up @@ -36,7 +36,7 @@ public function getDefaultDocumentUrl()
$defaultDocument = key($this->config['library']);

return Router::url([
'plugin' => 'Alt3/Swagger',
'plugin' => 'Cstaf/Swagger',
'controller' => 'Docs',
'action' => 'index',
$defaultDocument,
Expand Down
8 changes: 3 additions & 5 deletions src/Lib/SwaggerTools.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php
declare(strict_types=1);

namespace Alt3\Swagger\Lib;
namespace Cstaf\Swagger\Lib;

use Cake\Core\Configure;
use Cake\Filesystem\File;
use Cake\Http\Exception\InternalErrorException;
use Cake\Http\Exception\NotFoundException;

Expand Down Expand Up @@ -32,9 +31,8 @@ public static function getSwaggerDocument($id, $host)
if (!file_exists($filePath)) {
throw new NotFoundException("Swagger json document was not found on filesystem: $filePath");
}
$fh = new File($filePath);

return $fh->read();

return file_get_contents($filePath);
}

// otherwise crawl-generate a fresh document
Expand Down
4 changes: 2 additions & 2 deletions src/Plugin.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php
declare(strict_types=1);

namespace Alt3\Swagger;
namespace Cstaf\Swagger;

use Cake\Core\BasePlugin;

/**
* Class Plugin
* @package Alt3\Swagger
* @package Cstaf\Swagger
*/
class Plugin extends BasePlugin
{
Expand Down
6 changes: 3 additions & 3 deletions src/Shell/SwaggerShell.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
declare(strict_types=1);

namespace Alt3\Swagger\Shell;
namespace Cstaf\Swagger\Shell;

use Alt3\Swagger\Controller\AppController;
use Alt3\Swagger\Lib\SwaggerTools;
use Cstaf\Swagger\Controller\AppController;
use Cstaf\Swagger\Lib\SwaggerTools;
use Cake\Console\ConsoleOptionParser;
use Cake\Console\Shell;
use Cake\Core\Configure;
Expand Down
Loading