Skip to content

Commit

Permalink
Nette 3
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar authored and Milan Felix Šulc committed Jul 6, 2019
1 parent 2454780 commit 10956a7
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 53 deletions.
4 changes: 2 additions & 2 deletions .docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ and add [Swagger UI](https://petstore.swagger.io) as [Tracy](https://github.com/
- [Definitions - define schema](#definitions)
- [Config](#config)
- [External Files](#external-files)
- [OpenApi Annotations](#openapi-annotations-experimental)
- [OpenApi Annotations](#openapi-annotations)
- [Core Annotations](#core-annotations)
- [Entity Annotations](#entity-annotations)
- [Custom - write custom definition](#custom-definition)
Expand Down Expand Up @@ -98,7 +98,7 @@ api:

Supported types are `neon`, `yaml` and `json`.

#### OpenApi-Annotations **(Experimental)**
#### OpenApi-Annotations

This definition comes from core, but use only `OpenApi` annotation.

Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ and add [Swagger UI](https://petstore.swagger.io) as [Tracy](https://github.com/

## Version

| State | Version | Branch | PHP | Composer |
|-------------|--------------|----------|----------|-------------------------------------------------|
| development | `^0.6.0` | `master` | `>= 7.1` | `minimum-stability: dev`, `prefer-stable: true` |
| stable | `^0.5.0` | `master` | `>= 7.1` | |
| stable | `^0.3.0` | `master` | `>= 5.6` | |
| State | Version | Branch | Nette | PHP |
|-------------|---------|----------|-------|---------|
| dev | `^0.7` | `master` | 3.0+ | `^7.2` |
| stable | `^0.6` | `master` | 3.0+ | `^7.2` |
| stable | `^0.5` | `master` | 2.4 | `>=7.1` |
| stable | `^0.3` | `master` | 2.4 | `>=5.6` |

![](https://github.com/apitte/openapi/blob/master/.docs/assets/panel.png "Tracy Panel")

Expand Down
12 changes: 4 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
"require": {
"php": "^7.2",
"apitte/core": "^0.6.0",
"nette/utils": "~2.5.3",
"tracy/tracy": "~2.4.14 || ~2.5.0 || ~2.6.0"
"nette/utils": "~3.0.1",
"tracy/tracy": "~2.6.0"
},
"suggest": {
"symfony/yaml": "Allows yaml definition"
},
"require-dev": {
"nette/di": "~2.4.12",
"contributte/di": "^0.4.0",
"ninjify/nunjuck": "^0.2.0",
"ninjify/qa": "^0.8.0",
"phpstan/extension-installer": "^1.0",
Expand All @@ -40,10 +40,6 @@
"phpstan/phpstan-strict-rules": "^0.11",
"symfony/yaml": "^4.2"
},
"conflict": {
"nette/di": "<2.4.12",
"symfony/yaml": "<4.2.0"
},
"autoload": {
"psr-4": {
"Apitte\\OpenApi\\": "src"
Expand All @@ -56,7 +52,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "0.6.x-dev"
"dev-master": "0.7.x-dev"
}
}
}
89 changes: 52 additions & 37 deletions src/DI/OpenApiPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace Apitte\OpenApi\DI;

use Apitte\Core\DI\Plugin\AbstractPlugin;
use Apitte\Core\DI\Plugin\PluginCompiler;
use Apitte\Core\DI\Plugin\Plugin;
use Apitte\Core\Exception\Logical\InvalidArgumentException;
use Apitte\OpenApi\SchemaBuilder;
use Apitte\OpenApi\SchemaDefinition\ArrayDefinition;
Expand All @@ -15,32 +14,40 @@
use Apitte\OpenApi\SchemaDefinition\NeonDefinition;
use Apitte\OpenApi\SchemaDefinition\YamlDefinition;
use Apitte\OpenApi\Tracy\SwaggerUIPanel;
use Contributte\DI\Helper\ExtensionDefinitionsHelper;
use Nette\DI\Definitions\Definition;
use Nette\DI\Definitions\Statement;
use Nette\PhpGenerator\ClassType;
use Nette\Schema\Expect;
use Nette\Schema\Schema;
use Nette\Utils\Strings;
use stdClass;

class OpenApiPlugin extends AbstractPlugin
/**
* @property-read stdClass $config
*/
class OpenApiPlugin extends Plugin
{

public const PLUGIN_NAME = 'openapi';

/** @var mixed[] */
protected $defaults = [
'definitions' => null,
'definition' => [],
'files' => [],
'swaggerUi' => [
'url' => null,
'expansion' => SwaggerUIPanel::EXPANSION_LIST,
'filter' => true,
'title' => 'SwaggerUi',
'panel' => true,
],
];

public function __construct(PluginCompiler $compiler)
public static function getName(): string
{
parent::__construct($compiler);
$this->name = self::PLUGIN_NAME;
return 'openapi';
}

protected function getConfigSchema(): Schema
{
return Expect::structure([
'definitions' => Expect::arrayOf(Expect::type('string|array|' . Statement::class)),
'definition' => Expect::array(),
'files' => Expect::arrayOf('string'),
'swaggerUi' => Expect::structure([
'url' => Expect::string()->nullable(),
'expansion' => Expect::anyOf(...SwaggerUIPanel::EXPANSIONS)->default(SwaggerUIPanel::EXPANSION_LIST),
'filter' => Expect::bool(true),
'title' => Expect::string('SwaggerUi'),
'panel' => Expect::bool(true),
]),
]);
}

/**
Expand All @@ -50,7 +57,8 @@ public function loadPluginConfiguration(): void
{
$builder = $this->getContainerBuilder();
$global = $this->compiler->getExtension()->getConfig();
$config = $this->getConfig();
$config = $this->config;
$definitionHelper = new ExtensionDefinitionsHelper($this->compiler->getExtension()->getCompiler());

$builder->addDefinition($this->prefix('entityAdapter'))
->setFactory(EntityAdapter::class);
Expand All @@ -64,12 +72,12 @@ public function loadPluginConfiguration(): void
$schemaBuilder = $builder->addDefinition($this->prefix('schemaBuilder'))
->setFactory(SchemaBuilder::class);

if ($config['definitions'] === null) {
if ($config->definitions === []) {
$schemaBuilder
->addSetup('addDefinition', [new BaseDefinition()])
->addSetup('addDefinition', [$entityDefinition])
->addSetup('addDefinition', [$coreDefinition]);
foreach ($config['files'] as $file) {
foreach ($config->files as $file) {
if (Strings::endsWith($file, '.neon')) {
$schemaBuilder->addSetup('addDefinition', [new NeonDefinition($file)]);
} elseif (Strings::endsWith($file, '.yaml') || Strings::endsWith($file, '.yml')) {
Expand All @@ -84,38 +92,45 @@ public function loadPluginConfiguration(): void
}
}

$schemaBuilder->addSetup('addDefinition', [new ArrayDefinition($config['definition'])]);
$schemaBuilder->addSetup('addDefinition', [new ArrayDefinition($config->definition)]);
} else {
foreach ($config['definitions'] as $customDefinition) {
$schemaBuilder->addSetup('addDefinition', [$customDefinition]);
foreach ($config->definitions as $definitionName => $definitionConfig) {
$definitionPrefix = $this->prefix('definition.' . $definitionName);
$definition = $definitionHelper->getDefinitionFromConfig($definitionConfig, $definitionPrefix);

if ($definition instanceof Definition) {
$definition->setAutowired(false);
}

$schemaBuilder->addSetup('addDefinition', [$definition]);
}
}

if ($global['debug'] !== true) {
if (!$global->debug) {
return;
}

if ($config['swaggerUi']['panel']) {
if ($config->swaggerUi->panel) {
$builder->addDefinition($this->prefix('swaggerUi.panel'))
->setFactory(SwaggerUIPanel::class)
->addSetup('setUrl', [$config['swaggerUi']['url']])
->addSetup('setExpansion', [$config['swaggerUi']['expansion']])
->addSetup('setFilter', [$config['swaggerUi']['filter']])
->addSetup('setTitle', [$config['swaggerUi']['title']])
->addSetup('setUrl', [$config->swaggerUi->url])
->addSetup('setExpansion', [$config->swaggerUi->expansion])
->addSetup('setFilter', [$config->swaggerUi->filter])
->addSetup('setTitle', [$config->swaggerUi->title])
->setAutowired(false);
}
}

public function afterPluginCompile(ClassType $class): void
{
$global = $this->compiler->getExtension()->getConfig();
if ($global['debug'] !== true) {
if (!$global->debug) {
return;
}
$config = $this->getConfig();
$config = $this->config;

$initialize = $class->getMethod('initialize');
if ($config['swaggerUi']['panel']) {
if ($config->swaggerUi->panel) {
$initialize->addBody('$this->getService(?)->addPanel($this->getService(?));', [
'tracy.bar',
$this->prefix('swaggerUi.panel'),
Expand Down
2 changes: 1 addition & 1 deletion src/SchemaDefinition/JsonDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function load(): array
if ($content === false) {
throw new InvalidStateException('Cant read file ' . $this->file);
}
$decode = Json::decode($content, true);
$decode = Json::decode($content, Json::FORCE_ARRAY);
if ($decode === false || $decode === null) {
return [];
}
Expand Down
6 changes: 6 additions & 0 deletions src/Tracy/SwaggerUIPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ class SwaggerUIPanel implements IBarPanel
EXPANSION_LIST = 'list',
EXPANSION_NONE = 'none';

public const EXPANSIONS = [
self::EXPANSION_FULL,
self::EXPANSION_LIST,
self::EXPANSION_NONE,
];

/** @var string|null */
private $url;

Expand Down

0 comments on commit 10956a7

Please sign in to comment.