Skip to content
Permalink
Browse files

Nette 3

  • Loading branch information...
mabar authored and f3l1x committed Jun 13, 2019
1 parent 2454780 commit 10956a74cbba3b5d6169c4db1b9cfe052d8a0d01
Showing with 71 additions and 53 deletions.
  1. +2 −2 .docs/README.md
  2. +6 −5 README.md
  3. +4 −8 composer.json
  4. +52 −37 src/DI/OpenApiPlugin.php
  5. +1 −1 src/SchemaDefinition/JsonDefinition.php
  6. +6 −0 src/Tracy/SwaggerUIPanel.php
@@ -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)
@@ -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.

@@ -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")

@@ -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",
@@ -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"
@@ -56,7 +52,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "0.6.x-dev"
"dev-master": "0.7.x-dev"
}
}
}
@@ -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;
@@ -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),
]),
]);
}
/**
@@ -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);
@@ -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')) {
@@ -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'),
@@ -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 [];
}
@@ -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;

0 comments on commit 10956a7

Please sign in to comment.
You can’t perform that action at this time.