Skip to content
This repository was archived by the owner on Dec 9, 2025. It is now read-only.

Commit 10956a7

Browse files
mabarMilan Felix Šulc
authored andcommitted
Nette 3
1 parent 2454780 commit 10956a7

File tree

6 files changed

+71
-53
lines changed

6 files changed

+71
-53
lines changed

.docs/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ and add [Swagger UI](https://petstore.swagger.io) as [Tracy](https://github.com/
1111
- [Definitions - define schema](#definitions)
1212
- [Config](#config)
1313
- [External Files](#external-files)
14-
- [OpenApi Annotations](#openapi-annotations-experimental)
14+
- [OpenApi Annotations](#openapi-annotations)
1515
- [Core Annotations](#core-annotations)
1616
- [Entity Annotations](#entity-annotations)
1717
- [Custom - write custom definition](#custom-definition)
@@ -98,7 +98,7 @@ api:
9898

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

101-
#### OpenApi-Annotations **(Experimental)**
101+
#### OpenApi-Annotations
102102

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

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ and add [Swagger UI](https://petstore.swagger.io) as [Tracy](https://github.com/
2222

2323
## Version
2424

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

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

composer.json

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
"require": {
2424
"php": "^7.2",
2525
"apitte/core": "^0.6.0",
26-
"nette/utils": "~2.5.3",
27-
"tracy/tracy": "~2.4.14 || ~2.5.0 || ~2.6.0"
26+
"nette/utils": "~3.0.1",
27+
"tracy/tracy": "~2.6.0"
2828
},
2929
"suggest": {
3030
"symfony/yaml": "Allows yaml definition"
3131
},
3232
"require-dev": {
33-
"nette/di": "~2.4.12",
33+
"contributte/di": "^0.4.0",
3434
"ninjify/nunjuck": "^0.2.0",
3535
"ninjify/qa": "^0.8.0",
3636
"phpstan/extension-installer": "^1.0",
@@ -40,10 +40,6 @@
4040
"phpstan/phpstan-strict-rules": "^0.11",
4141
"symfony/yaml": "^4.2"
4242
},
43-
"conflict": {
44-
"nette/di": "<2.4.12",
45-
"symfony/yaml": "<4.2.0"
46-
},
4743
"autoload": {
4844
"psr-4": {
4945
"Apitte\\OpenApi\\": "src"
@@ -56,7 +52,7 @@
5652
},
5753
"extra": {
5854
"branch-alias": {
59-
"dev-master": "0.6.x-dev"
55+
"dev-master": "0.7.x-dev"
6056
}
6157
}
6258
}

src/DI/OpenApiPlugin.php

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
namespace Apitte\OpenApi\DI;
44

5-
use Apitte\Core\DI\Plugin\AbstractPlugin;
6-
use Apitte\Core\DI\Plugin\PluginCompiler;
5+
use Apitte\Core\DI\Plugin\Plugin;
76
use Apitte\Core\Exception\Logical\InvalidArgumentException;
87
use Apitte\OpenApi\SchemaBuilder;
98
use Apitte\OpenApi\SchemaDefinition\ArrayDefinition;
@@ -15,32 +14,40 @@
1514
use Apitte\OpenApi\SchemaDefinition\NeonDefinition;
1615
use Apitte\OpenApi\SchemaDefinition\YamlDefinition;
1716
use Apitte\OpenApi\Tracy\SwaggerUIPanel;
17+
use Contributte\DI\Helper\ExtensionDefinitionsHelper;
18+
use Nette\DI\Definitions\Definition;
19+
use Nette\DI\Definitions\Statement;
1820
use Nette\PhpGenerator\ClassType;
21+
use Nette\Schema\Expect;
22+
use Nette\Schema\Schema;
1923
use Nette\Utils\Strings;
24+
use stdClass;
2025

21-
class OpenApiPlugin extends AbstractPlugin
26+
/**
27+
* @property-read stdClass $config
28+
*/
29+
class OpenApiPlugin extends Plugin
2230
{
2331

24-
public const PLUGIN_NAME = 'openapi';
25-
26-
/** @var mixed[] */
27-
protected $defaults = [
28-
'definitions' => null,
29-
'definition' => [],
30-
'files' => [],
31-
'swaggerUi' => [
32-
'url' => null,
33-
'expansion' => SwaggerUIPanel::EXPANSION_LIST,
34-
'filter' => true,
35-
'title' => 'SwaggerUi',
36-
'panel' => true,
37-
],
38-
];
39-
40-
public function __construct(PluginCompiler $compiler)
32+
public static function getName(): string
4133
{
42-
parent::__construct($compiler);
43-
$this->name = self::PLUGIN_NAME;
34+
return 'openapi';
35+
}
36+
37+
protected function getConfigSchema(): Schema
38+
{
39+
return Expect::structure([
40+
'definitions' => Expect::arrayOf(Expect::type('string|array|' . Statement::class)),
41+
'definition' => Expect::array(),
42+
'files' => Expect::arrayOf('string'),
43+
'swaggerUi' => Expect::structure([
44+
'url' => Expect::string()->nullable(),
45+
'expansion' => Expect::anyOf(...SwaggerUIPanel::EXPANSIONS)->default(SwaggerUIPanel::EXPANSION_LIST),
46+
'filter' => Expect::bool(true),
47+
'title' => Expect::string('SwaggerUi'),
48+
'panel' => Expect::bool(true),
49+
]),
50+
]);
4451
}
4552

4653
/**
@@ -50,7 +57,8 @@ public function loadPluginConfiguration(): void
5057
{
5158
$builder = $this->getContainerBuilder();
5259
$global = $this->compiler->getExtension()->getConfig();
53-
$config = $this->getConfig();
60+
$config = $this->config;
61+
$definitionHelper = new ExtensionDefinitionsHelper($this->compiler->getExtension()->getCompiler());
5462

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

67-
if ($config['definitions'] === null) {
75+
if ($config->definitions === []) {
6876
$schemaBuilder
6977
->addSetup('addDefinition', [new BaseDefinition()])
7078
->addSetup('addDefinition', [$entityDefinition])
7179
->addSetup('addDefinition', [$coreDefinition]);
72-
foreach ($config['files'] as $file) {
80+
foreach ($config->files as $file) {
7381
if (Strings::endsWith($file, '.neon')) {
7482
$schemaBuilder->addSetup('addDefinition', [new NeonDefinition($file)]);
7583
} elseif (Strings::endsWith($file, '.yaml') || Strings::endsWith($file, '.yml')) {
@@ -84,38 +92,45 @@ public function loadPluginConfiguration(): void
8492
}
8593
}
8694

87-
$schemaBuilder->addSetup('addDefinition', [new ArrayDefinition($config['definition'])]);
95+
$schemaBuilder->addSetup('addDefinition', [new ArrayDefinition($config->definition)]);
8896
} else {
89-
foreach ($config['definitions'] as $customDefinition) {
90-
$schemaBuilder->addSetup('addDefinition', [$customDefinition]);
97+
foreach ($config->definitions as $definitionName => $definitionConfig) {
98+
$definitionPrefix = $this->prefix('definition.' . $definitionName);
99+
$definition = $definitionHelper->getDefinitionFromConfig($definitionConfig, $definitionPrefix);
100+
101+
if ($definition instanceof Definition) {
102+
$definition->setAutowired(false);
103+
}
104+
105+
$schemaBuilder->addSetup('addDefinition', [$definition]);
91106
}
92107
}
93108

94-
if ($global['debug'] !== true) {
109+
if (!$global->debug) {
95110
return;
96111
}
97112

98-
if ($config['swaggerUi']['panel']) {
113+
if ($config->swaggerUi->panel) {
99114
$builder->addDefinition($this->prefix('swaggerUi.panel'))
100115
->setFactory(SwaggerUIPanel::class)
101-
->addSetup('setUrl', [$config['swaggerUi']['url']])
102-
->addSetup('setExpansion', [$config['swaggerUi']['expansion']])
103-
->addSetup('setFilter', [$config['swaggerUi']['filter']])
104-
->addSetup('setTitle', [$config['swaggerUi']['title']])
116+
->addSetup('setUrl', [$config->swaggerUi->url])
117+
->addSetup('setExpansion', [$config->swaggerUi->expansion])
118+
->addSetup('setFilter', [$config->swaggerUi->filter])
119+
->addSetup('setTitle', [$config->swaggerUi->title])
105120
->setAutowired(false);
106121
}
107122
}
108123

109124
public function afterPluginCompile(ClassType $class): void
110125
{
111126
$global = $this->compiler->getExtension()->getConfig();
112-
if ($global['debug'] !== true) {
127+
if (!$global->debug) {
113128
return;
114129
}
115-
$config = $this->getConfig();
130+
$config = $this->config;
116131

117132
$initialize = $class->getMethod('initialize');
118-
if ($config['swaggerUi']['panel']) {
133+
if ($config->swaggerUi->panel) {
119134
$initialize->addBody('$this->getService(?)->addPanel($this->getService(?));', [
120135
'tracy.bar',
121136
$this->prefix('swaggerUi.panel'),

src/SchemaDefinition/JsonDefinition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function load(): array
2525
if ($content === false) {
2626
throw new InvalidStateException('Cant read file ' . $this->file);
2727
}
28-
$decode = Json::decode($content, true);
28+
$decode = Json::decode($content, Json::FORCE_ARRAY);
2929
if ($decode === false || $decode === null) {
3030
return [];
3131
}

src/Tracy/SwaggerUIPanel.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ class SwaggerUIPanel implements IBarPanel
1313
EXPANSION_LIST = 'list',
1414
EXPANSION_NONE = 'none';
1515

16+
public const EXPANSIONS = [
17+
self::EXPANSION_FULL,
18+
self::EXPANSION_LIST,
19+
self::EXPANSION_NONE,
20+
];
21+
1622
/** @var string|null */
1723
private $url;
1824

0 commit comments

Comments
 (0)