Skip to content

Commit c988145

Browse files
update to laravel 11 (#21)
1 parent d4fe117 commit c988145

File tree

2 files changed

+18
-42
lines changed

2 files changed

+18
-42
lines changed

src/Definitions/DefinitionGenerator.php

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php
22

33
namespace Mezatsong\SwaggerDocs\Definitions;
44

@@ -75,7 +75,7 @@ function generateSchemas(): array {
7575

7676
if ($obj instanceof Model) { //check to make sure it is a model
7777
$reflection = new ReflectionClass($obj);
78-
78+
7979
// $with = $reflection->getProperty('with');
8080
// $with->setAccessible(true);
8181

@@ -86,16 +86,15 @@ function generateSchemas(): array {
8686
->filter(
8787
fn($method) => !empty($method->getReturnType()) &&
8888
str_contains(
89-
$method->getReturnType(),
89+
$method->getReturnType(),
9090
\Illuminate\Database\Eloquent\Relations::class
9191
)
9292
)
9393
->pluck('name')
9494
->all();
9595

9696
$table = $obj->getTable();
97-
$list = Schema::connection($obj->getConnectionName())->getColumnListing($table);
98-
$list = array_diff($list, $obj->getHidden());
97+
$columns = Schema::getColumns($table);
9998

10099
$properties = [];
101100
$required = [];
@@ -110,39 +109,18 @@ function generateSchemas(): array {
110109
$table = $prefix . $table;
111110
}
112111

113-
foreach ($list as $item) {
114-
115-
/**
116-
* @var \Doctrine\DBAL\Schema\Column
117-
*/
118-
$column = $conn->getDoctrineColumn($table, $item);
119-
120-
$data = $this->convertDBalTypeToSwaggerType(
121-
Type::getTypeRegistry()->lookupName($column->getType())
122-
);
123-
124-
if ($data['type'] == 'string' && ($len = $column->getLength())) {
125-
$data['description'] .= "($len)";
126-
}
127-
128-
$description = $column->getComment();
112+
foreach ($columns as $column) {
113+
$description = $column['comment'];
129114
if (!is_null($description)) {
130-
$data['description'] .= ": $description";
115+
$column['description'] .= ": $description";
131116
}
132117

133-
$default = $column->getDefault();
134-
if (!is_null($default)) {
135-
$data['default'] = $default;
136-
}
137-
138-
$data['nullable'] = ! $column->getNotnull();
118+
$this->addExampleKey($column);
139119

140-
$this->addExampleKey($data);
120+
$properties[$column['name']] = $column;
141121

142-
$properties[$item] = $data;
143-
144-
if ($column->getNotnull()) {
145-
$required[] = $item;
122+
if (!$column['nullable']) {
123+
$required[] = $column['name'];
146124
}
147125
}
148126

@@ -152,7 +130,7 @@ function generateSchemas(): array {
152130
'type' => 'object',
153131
'$ref' => '#/components/schemas/' . last(explode('\\', $relatedClass)),
154132
];
155-
133+
156134
$resultsClass = get_class((object) ($obj->{$relationName}()->getResults()));
157135

158136
if (str_contains($resultsClass, \Illuminate\Database\Eloquent\Collection::class)) {
@@ -164,7 +142,7 @@ function generateSchemas(): array {
164142
$properties[$relationName] = $refObject;
165143
}
166144
}
167-
145+
168146
// $required = array_merge($required, $with->getValue($obj));
169147

170148
foreach ($appends->getValue($obj) as $item) {
@@ -204,7 +182,7 @@ function generateSchemas(): array {
204182
'type' => 'object',
205183
'properties' => (object) $properties,
206184
];
207-
185+
208186
if ( ! empty($required)) {
209187
$definition['required'] = $required;
210188
}
@@ -295,7 +273,7 @@ private function convertDBalTypeToSwaggerType(string $type): array {
295273
case 'bigserial':
296274
case 'bigint':
297275
$property = [
298-
'type' => 'integer',
276+
'type' => 'integer',
299277
'format' => 'int64'
300278
];
301279
break;

src/SwaggerServiceProvider.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
<?php
1+
<?php
22

33
namespace Mezatsong\SwaggerDocs;
44

55
use Illuminate\Support\Arr;
66
use Illuminate\Support\Facades\DB;
77
use Illuminate\Support\Facades\Log;
8+
use Illuminate\Support\Facades\Schema;
89
use Illuminate\Support\Facades\Validator;
910
use Illuminate\Support\ServiceProvider;
1011
use Mezatsong\SwaggerDocs\Commands\GenerateSwaggerDocumentation;
@@ -52,10 +53,7 @@ public function boot(): void {
5253
$this->loadValidationRules();
5354

5455
try {
55-
DB::connection()
56-
->getDoctrineConnection()
57-
->getDatabasePlatform()
58-
->registerDoctrineTypeMapping('enum', 'string');
56+
Schema::registerEnumMapping('enum', 'string');
5957
} catch (\Exception $e) {
6058
Log::error('[Mezatsong\SwaggerDocs] Could not register enum type as string because of connexion error.');
6159
}

0 commit comments

Comments
 (0)