This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Generator.php
953 lines (810 loc) · 32.2 KB
/
Generator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
<?php
namespace Rygilles\OpenApiGenerator\Generators;
use ErrorException;
use Illuminate\Console\Command;
use Illuminate\Foundation\Http\FormRequest;
use phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\DocBlockFactory;
use ReflectionClass;
use Rygilles\OpenApiGenerator\OpenApi\Document;
use Rygilles\OpenApiGenerator\OpenApi\Example;
use Rygilles\OpenApiGenerator\OpenApi\MediaType;
use Rygilles\OpenApiGenerator\OpenApi\OpenAPI;
use Rygilles\OpenApiGenerator\OpenApi\Operation;
use Rygilles\OpenApiGenerator\OpenApi\Parameter;
use Rygilles\OpenApiGenerator\OpenApi\PathItem;
use Rygilles\OpenApiGenerator\OpenApi\Reference;
use Rygilles\OpenApiGenerator\OpenApi\RequestBody;
use Rygilles\OpenApiGenerator\OpenApi\Response;
use Rygilles\OpenApiGenerator\OpenApi\Schema;
abstract class Generator
{
/**
* The parent command object
*
* @var Command
*/
protected $parentCommand = null;
/**
* Return defined parent command object
*
* @return Command|null
*/
public function getParentCommand()
{
return $this->parentCommand;
}
/**
* DocBlockFactory for doc block analysis
*
* @var DocBlockFactory
*/
protected $docBlockFactory;
/**
* Schemas data array with location as key
*
* @var string[]
*/
protected $identifiedSchemas = [];
/**
* Root document object pf the OpenAPI document
*
* @var OpenAPI
*/
protected $openAPI;
/**
* Profile provided from Laravel configuration file 'openapischemas.php'
*
* @var mixed[]
*/
protected $profile;
/**
* Create a new command instance.
*
* @param DocBlockFactory $docBlockFactory
* @param Command $parentCommand
*/
public function __construct(DocBlockFactory $docBlockFactory, $profile, Command $parentCommand)
{
$this->docBlockFactory = $docBlockFactory;
$this->parentCommand = $parentCommand;
$this->profile = $profile;
$this->openAPI = new OpenAPI();
}
/**
* Apply profile Open API bindings provided from configuration file
*/
public function applyProfileOpenApiBindings()
{
$this->openAPI->info = $this->profile['openapi_bindings']['info'];
if (isset($this->profile['openapi_bindings']['security'])) {
$this->openAPI->security = $this->profile['openapi_bindings']['security'];
}
if (isset($this->profile['openapi_bindings']['servers'])) {
$this->openAPI->servers = $this->profile['openapi_bindings']['servers'];
}
if (isset($this->profile['openapi_bindings']['components'])) {
$this->openAPI->components = $this->profile['openapi_bindings']['components'];
}
}
/**
* Generate and return the Open API JSON schema
*/
public function generateJSON()
{
// __toString() -> JSON
return $this->openAPI;
}
/**
* Process routes and create path item objects on the root Open API document.
*
* @param mixed $routes
*/
public function processRoutes($routes)
{
if (is_null($this->openAPI->paths)) {
$this->openAPI->paths = [];
}
foreach ($routes as $route) {
$this->processRoute($route);
}
}
/**
* Process a route
*
* @param \Illuminate\Routing\Route $route
*/
protected function processRoute($route)
{
$routeAction = $route->getAction();
$this->getParentCommand()->info('Processing route [' . implode('/', $route->methods()) . '] ' . $routeAction['uri']);
$routeControllerDocBlock = $this->getRouteControllerDocBlock($route);
$routeControllerOperationTags = $this->getDocBlockOperationTags($routeControllerDocBlock);
foreach ($route->methods() as $httpMethod) {
// Ignore HEAD
if (strtolower($httpMethod) == 'head') {
continue;
}
if (isset($this->openAPI->paths[$routeAction['uri']])) {
$pathItem = $this->openAPI->paths[$routeAction['uri']];
} else {
$pathItem = $this->openAPI->paths[$routeAction['uri']] = new PathItem();
if (!is_null($routeControllerDocBlock)) {
$pathItem->summary = $routeControllerDocBlock->getSummary();
if (empty($pathItem->summary)) {
$pathItem->summary = null;
}
$routeControllerDescriptionDocBlock = $routeControllerDocBlock->getDescription();
$pathItem->description = $routeControllerDescriptionDocBlock->render();
if (empty($pathItem->description)) {
$pathItem->description = null;
}
}
}
$operation = new Operation();
$operationTags = $routeControllerOperationTags;
$routeMethodDocBlock = $this->getRouteMethodDocBlock($route);
$extraParameterRefTags = [];
if (!is_null($routeMethodDocBlock)) {
$operation->summary = $routeMethodDocBlock->getSummary();
if (empty($operation->summary)) {
$operation->summary = null;
}
$routeMethodDescriptionDocBlock = $routeMethodDocBlock->getDescription();
$operation->description = $routeMethodDescriptionDocBlock->render();
if (empty($operation->description)) {
$operation->description = null;
}
$routeMethodOperationTags = $this->getDocBlockOperationTags($routeMethodDocBlock);
$operationTags = array_merge($routeControllerOperationTags, $routeMethodOperationTags);
$extraParameterRefTags = $this->getDocBlockExtraParameterRefTags($routeMethodDocBlock);
$operation->operationId = $this->getDocBlockOperationId($routeMethodDocBlock);
}
if (is_null($operation->operationId)) {
switch (strtolower($httpMethod)) {
case 'patch':
case 'put':
$operation->operationId = strtolower($httpMethod) . ucfirst(array_last(explode('.', $routeAction['as'])));
break;
default :
$operation->operationId = array_last(explode('.', $routeAction['as']));
}
}
if (count($operationTags) > 0) {
$operation->tags = $operationTags;
}
// Try
$routePathParameters = $this->getRoutePathParameters($route);
foreach ($routePathParameters as $parameterName => $parameterData) {
$parameterDescription = isset($parameterData['docDescription']) ? $parameterData['docDescription'] : '';
if (trim($parameterDescription) == '') {
$parameterDescription = null;
}
$parameter = new Parameter([
'name' => $parameterName,
'in' => 'path',
'description' => $parameterDescription,
'required' => true,
]);
if (isset($parameterData['type'])) {
$parameter->schema = new Schema([
'type' => $parameterData['type']
]);
}
if (isset($parameterData['docType'])) {
$parameter->schema = new Schema([
'type' => $parameterData['docType']
]);
}
$operation->parameters[] = $parameter;
}
// Try to get route validation rules
$routeValidationRules = $this->getRouteValidationRules($route);
if (count($routeValidationRules) > 0) {
switch (strtolower($httpMethod)) {
case 'get':
//case 'head':
foreach ($routeValidationRules as $parameterName => $rulesString) {
$rules = explode('|', $rulesString);
$parameter = new Parameter([
'name' => $parameterName,
'in' => 'query',
'description' => null, /* @todo Grab description from phpdoc custom tag ? */
'required' => in_array('required', $rules),
]);
$parameter->schema = $this->getPropertyValidationRulesSchema(
$rules,
'Validation rules for parameter "' . $parameterName . '"',
true
);
if (is_null($parameter->schema->format)) {
// ignore this parameter if no type found
$this->getParentCommand()->warn(' Ignore parameter "' . $parameterName . '"');
continue;
}
// Add if not already added from the "path" parameters
$find = false;
if (is_null($operation->parameters)) {
$operation->parameters = [];
}
/** @var Parameter $p */
foreach ($operation->parameters as $p) {
if ($p->name == $parameter->name) {
$find = true;
}
}
if (!$find) {
$operation->parameters[] = $parameter;
}
}
break;
case 'put':
case 'patch':
case 'post':
$requestBody = new RequestBody();
$mediaType = new MediaType();
$mediaType->schema = new Schema();
$mediaType->schema->properties = [];
// Change to multi-part if file in parameters ("string" type and "binary" format)
$contentType = 'application/json';
foreach ($routeValidationRules as $parameterName => $rulesString) {
$rules = explode('|', $rulesString);
$schema = $this->getPropertyValidationRulesSchema(
$rules,
'Validation rules for parameter "' . $parameterName . '"',
true
);
// ignore this property if no type found
if (is_null($schema->type)) {
$this->getParentCommand()->warn(' Ignore parameter "' . $parameterName . '"');
continue;
}
// Change to multi-part if file in parameters ("string" type and "binary" format)
if ($schema->type == 'string' && $schema->format == 'binary') {
$contentType = 'multipart/form-data';
// Change mediaType schema type
$mediaType->schema->type = 'object';
}
$mediaType->schema->properties[$parameterName] = $schema;
}
$mediaType->schema->required = $this->getValidationRulesSchemaRequired($routeValidationRules);
$requestBody->content[$contentType] = $mediaType;
$operation->requestBody = $requestBody;
$operation->responses = [];
break;
}
}
// Append extra parameter refs
foreach ($extraParameterRefTags as $extraParameterRefTag) {
$parameterRef = new Reference([
'ref' => $extraParameterRefTag
]);
if (is_null($operation->parameters)) {
$operation->parameters = [];
}
$operation->parameters[] = $parameterRef;
}
// First response
$response = new Response();
$responseMediaType = null;
$apiResponseSchemaRefTags = $routeMethodDocBlock->getTagsByName('OpenApiResponseSchemaRef');
if (count($apiResponseSchemaRefTags) > 0) {
if (is_null($responseMediaType)) {
$responseMediaType = new MediaType();
}
$apiResponseSchemaRefTag = $apiResponseSchemaRefTags[0];
$apiResponseSchemaRef = trim(str_replace('@OpenApiResponseSchemaRef', '', $apiResponseSchemaRefTag->render()));
$responseMediaType->schema = new Reference([
'ref' => $apiResponseSchemaRef
]);
}
$response->description = '';
$apiResponseDescriptionTags = $routeMethodDocBlock->getTagsByName('OpenApiResponseDescription');
if (count($apiResponseDescriptionTags) > 0) {
$apiResponseDescriptionTag = $apiResponseDescriptionTags[0];
$apiResponseDescription = trim(str_replace('@OpenApiResponseDescription', '', $apiResponseDescriptionTag->render()));
$response->description = $apiResponseDescription;
}
$apiDocsNoCallTags = $routeMethodDocBlock->getTagsByName('ApiDocsNoCall');
if (count($apiDocsNoCallTags) == 0) {
$exampleResponse = $this->getRouteCallExampleResponse($route, $httpMethod);
if (!is_null($exampleResponse)) {
if (is_null($responseMediaType)) {
$responseMediaType = new MediaType();
}
$exampleSchemaId = $this->getRouteExampleResponseId($route);
// Create Example schema in components
if (is_null($this->openAPI->components)) {
$this->openAPI->components = new Components();
}
$this->openAPI->components->examples[$exampleSchemaId] = new Example([
'value' => json_decode($exampleResponse, true)
]);
$responseMediaType->example = new Reference([
'ref' => '#/components/examples/' . $exampleSchemaId
]);
}
}
if (!is_null($responseMediaType)) {
$response->content['application/json'] = $responseMediaType;
}
// Excepted HTTP code specified ? "OpenApiResponseExceptedHTTPCode" tag
$apiResponseExceptedHTTPCode = null;
$apiResponseExceptedHTTPCodeTags = $routeMethodDocBlock->getTagsByName('OpenApiResponseExceptedHTTPCode');
if (count($apiResponseExceptedHTTPCodeTags) > 0) {
$apiResponseExceptedHTTPCodeTag = $apiResponseExceptedHTTPCodeTags[0];
$apiResponseExceptedHTTPCode = trim(str_replace('@OpenApiResponseExceptedHTTPCode', '', $apiResponseExceptedHTTPCodeTag->render()));
}
if (!is_null($apiResponseExceptedHTTPCode)) {
$operation->responses[$apiResponseExceptedHTTPCode] = $response;
} else {
switch (strtolower($httpMethod)) {
case 'get':
//case 'head':
$operation->responses['200'] = $response;
break;
case 'put':
case 'patch':
$operation->responses['200'] = $response;
break;
case 'post':
$operation->responses['201'] = $response;
break;
case 'delete':
$operation->responses['204'] = $response;
break;
}
}
// Default response (Errors)
$defaultResponse = new Response();
$defaultResponseMediaType = null;
$apiDefaultResponseSchemaRefTags = $routeMethodDocBlock->getTagsByName('OpenApiDefaultResponseSchemaRef');
if (count($apiDefaultResponseSchemaRefTags) > 0) {
if (is_null($defaultResponseMediaType)) {
$defaultResponseMediaType = new MediaType();
}
$apiDefaultResponseSchemaRefTag = $apiDefaultResponseSchemaRefTags[0];
$apiDefaultResponseSchemaRef = trim(str_replace('@OpenApiDefaultResponseSchemaRef', '', $apiDefaultResponseSchemaRefTag->render()));
$defaultResponseMediaType->schema = new Reference([
'ref' => $apiDefaultResponseSchemaRef
]);
}
$defaultResponse->description = '';
$apiDefaultResponseDescriptionTags = $routeMethodDocBlock->getTagsByName('OpenApiDefaultResponseDescription');
if (count($apiDefaultResponseDescriptionTags) > 0) {
$apiDefaultResponseDescriptionTag = $apiResponseDescriptionTags[0];
$apiDefaultResponseDescription = trim(str_replace('@OpenApiDefaultResponseDescription', '', $apiDefaultResponseDescriptionTag->render()));
$defaultResponse->description = $apiDefaultResponseDescription;
}
if (!is_null($defaultResponseMediaType)) {
$defaultResponse->content['application/json'] = $defaultResponseMediaType;
}
$operation->responses['default'] = $defaultResponse;
$pathItem->{strtolower($httpMethod)} = $operation;
}
}
/**
* Return the OpenAPI Operation Id from a doc block or null if not found.
*
* @param DocBlock $docBlock
* @return string|null
*/
protected function getDocBlockOperationId($docBlock)
{
$apiOperationIdTags = $docBlock->getTagsByName('OpenApiOperationId');
if (count($apiOperationIdTags) > 0) {
$apiOperationIdTag = $apiOperationIdTags[0];
$apiOperationId = trim(str_replace('@OpenApiOperationId', '',$apiOperationIdTag->render()));
return $apiOperationId;
}
return null;
}
/**
* Return the OpenAPI Operation Tags from a doc block.
*
* @param DocBlock $docBlock
* @return string[]
*/
protected function getDocBlockOperationTags($docBlock)
{
$apiOperationTagTags = $docBlock->getTagsByName('OpenApiOperationTag');
foreach ($apiOperationTagTags as $apiOperationTagTag) {
$apiOperationTag = trim(str_replace('@OpenApiOperationTag', '',$apiOperationTagTag->render()));
if (strstr($apiOperationTag, '[')) {
$tags = str_replace(['[', ']'], '', $apiOperationTag);
$tags = explode(',', $tags);
$tags = array_map('trim', $tags);
return $tags;
}
return [trim($apiOperationTag)];
}
return [];
}
/**
* Return the route example response Id to use for $ref in the schema/compoenents
*
* @param \Illuminate\Routing\Route $route
* @return stirng
*/
protected function getRouteExampleResponseId($route)
{
$routeAction = $route->getAction();
$alias = studly_case(str_replace('.', '_', $routeAction['as']));
return $alias . 'ExampleResponse';
}
/**
* Return the OpenAPI Extra Parameter Ref Tags from a doc block.
*
* @param DocBlock $docBlock
* @return string[]
*/
protected function getDocBlockExtraParameterRefTags($docBlock)
{
$results = [];
$apiExtraParameterRefTags = $docBlock->getTagsByName('OpenApiExtraParameterRef');
foreach ($apiExtraParameterRefTags as $apiExtraParameterRefTag) {
$apiExtraParameterRef = trim(str_replace('@OpenApiExtraParameterRef', '', $apiExtraParameterRefTag->render()));
$results[] = $apiExtraParameterRef;
}
return $results;
}
/**
* Get schema required array from validation rules or null if no required fields
*
* @param mixed[] $validationRules
* @return string[]|null
*/
protected function getValidationRulesSchemaRequired($validationRules)
{
$required = [];
foreach ($validationRules as $propertyName => $rulesString) {
$propertyRules = explode('|', $rulesString);
// Parse property rules to get format/schema
foreach ($propertyRules as $rule) {
$explodedRule = explode(':', $rule);
$ruleName = $explodedRule[0];
if ($ruleName == 'required') {
// Check if type is handled
$schema = $this->getPropertyValidationRulesSchema(
$propertyRules,
'Validation rules for property "' . $propertyName . '"'
);
if (!is_null($schema->type)) {
$required[] = $propertyName;
}
}
}
}
return count($required) > 0 ? $required : null;
}
/**
* Return a new schema based on validation rules for a property.
*
* @param string[] $propertyRules Array of property rules
* @param string $errorContext Information on context if an error occurs
* @param boolean $logCommandError
* @return Schema
* @throws ErrorException
*/
protected function getPropertyValidationRulesSchema($propertyRules, $errorContext, $logCommandError = false)
{
$schema = new Schema();
// Parse property rules to get format/schema
foreach ($propertyRules as $rule) {
$explodedRule = explode(':', $rule);
$ruleName = $explodedRule[0];
if (count($explodedRule) > 1) {
$ruleParams = explode(',', $explodedRule[1]);
}
switch ($ruleName) {
/* Primitive types */
case 'integer':
$schema->type = 'integer';
break;
case 'boolean':
$schema->type = 'boolean';
break;
case 'string':
$schema->type = 'string';
break;
/* Advanced types */
case 'uuid':
$schema->type = 'string';
// @fixme Use of custom "format" or must we defined a "pattern" ? Better case should be defining the custom "type" in "components"...
$schema->format = 'uuid';
//$schema->pattern = '^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$';
//$schema->description = 'UUID';
break;
case 'min':
if (count($explodedRule) == 0) {
throw new ErrorException('Laravel framework "min" rule parameter is missing.' . "\n" . $errorContext);
}
if ($schema->type == 'string') {
$schema->minLength = (int)$ruleParams[0];
} elseif ($schema->type == 'integer') {
$schema->minimum = (int)$ruleParams[0];
}
break;
case 'max':
if (count($explodedRule) == 0) {
throw new ErrorException('Laravel framework "min" rule parameter is missing.' . "\n" . $errorContext);
}
if ($schema->type == 'string') {
$schema->maxLength = (int)$ruleParams[0];
} elseif ($schema->type == 'integer') {
$schema->maximum = (int)$ruleParams[0];
}
break;
case 'in':
if (is_null($schema->type)) {
// Assuming it's a string
$schema->type = 'string';
}
$schema->enum = $ruleParams;
break;
case 'date':
$schema->type = 'string';
// Not really "date" or "date-time" format
$schema->description = 'Must be a valid date according to the strtotime PHP function.';
break;
case 'email':
$schema->type = 'string';
// @fixme Use of custom "format" or must we defined a "pattern" ? Better case should be defining the custom "type" in "components"...
$schema->format = 'email';
//$schema->pattern = '/^[a-zA-Z0-9.!#$%&\'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/';
break;
case 'password':
case 'strength':
$schema->type = 'string';
// @fixme Use of custom "format" or must we defined a "pattern" ? Better case should be defining the custom "type" in "components"...
$schema->format = 'password';
//$schema->pattern = '/(?=.*\d)(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/';
break;
case 'url':
$schema->type = 'string';
// @fixme Use of custom "format" or must we defined a "pattern" ? Better case should be defining the custom "type" in "components"...
$schema->format = 'url';
/*
// From Laravel "validateUrl" pattern :
$schema->pattern = '~^
((aaa|aaas|about|acap|acct|acr|adiumxtra|afp|afs|aim|apt|attachment|aw|barion|beshare|bitcoin|blob|bolo|callto|cap|chrome|chrome-extension|cid|coap|coaps|com-eventbrite-attendee|content|crid|cvs|data|dav|dict|dlna-playcontainer|dlna-playsingle|dns|dntp|dtn|dvb|ed2k|example|facetime|fax|feed|feedready|file|filesystem|finger|fish|ftp|geo|gg|git|gizmoproject|go|gopher|gtalk|h323|ham|hcp|http|https|iax|icap|icon|im|imap|info|iotdisco|ipn|ipp|ipps|irc|irc6|ircs|iris|iris.beep|iris.lwz|iris.xpc|iris.xpcs|itms|jabber|jar|jms|keyparc|lastfm|ldap|ldaps|magnet|mailserver|mailto|maps|market|message|mid|mms|modem|ms-help|ms-settings|ms-settings-airplanemode|ms-settings-bluetooth|ms-settings-camera|ms-settings-cellular|ms-settings-cloudstorage|ms-settings-emailandaccounts|ms-settings-language|ms-settings-location|ms-settings-lock|ms-settings-nfctransactions|ms-settings-notifications|ms-settings-power|ms-settings-privacy|ms-settings-proximity|ms-settings-screenrotation|ms-settings-wifi|ms-settings-workplace|msnim|msrp|msrps|mtqp|mumble|mupdate|mvn|news|nfs|ni|nih|nntp|notes|oid|opaquelocktoken|pack|palm|paparazzi|pkcs11|platform|pop|pres|prospero|proxy|psyc|query|redis|rediss|reload|res|resource|rmi|rsync|rtmfp|rtmp|rtsp|rtsps|rtspu|secondlife|service|session|sftp|sgn|shttp|sieve|sip|sips|skype|smb|sms|smtp|snews|snmp|soap.beep|soap.beeps|soldat|spotify|ssh|steam|stun|stuns|submit|svn|tag|teamspeak|tel|teliaeid|telnet|tftp|things|thismessage|tip|tn3270|turn|turns|tv|udp|unreal|urn|ut2004|vemmi|ventrilo|videotex|view-source|wais|webcal|ws|wss|wtai|wyciwyg|xcon|xcon-userid|xfire|xmlrpc\.beep|xmlrpc.beeps|xmpp|xri|ymsgr|z39\.50|z39\.50r|z39\.50s)):// # protocol
(([\pL\pN-]+:)?([\pL\pN-]+)@)? # basic auth
(
([\pL\pN\pS-\.])+(\.?([\pL]|xn\-\-[\pL\pN-]+)+\.?) # a domain name
| # or
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} # an IP address
| # or
\[
(?:(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){6})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:::(?:(?:(?:[0-9a-f]{1,4})):){5})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){4})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,1}(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){3})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,2}(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){2})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,3}(?:(?:[0-9a-f]{1,4})))?::(?:(?:[0-9a-f]{1,4})):)(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,4}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,5}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,6}(?:(?:[0-9a-f]{1,4})))?::))))
\] # an IPv6 address
)
(:[0-9]+)? # a port (optional)
(/?|/\S+|\?\S*|\#\S*) # a /, nothing, a / with something, a query or a fragment
$~ixu';
*/
break;
case 'image':
case 'file':
$schema->type = 'string';
$schema->format = 'binary';
break;
}
}
if ($logCommandError && is_null($schema->type)) {
$this->getParentCommand()->error($errorContext . "\n" . ' Unknown parameter type, rules : "' . implode(',', $propertyRules) . '"');
}
return $schema;
}
/**
* Try to get route method path parameters
*
* @param \Illuminate\Routing\Route $route
* @return mixed[]
*/
protected function getRoutePathParameters($route)
{
$parameters = [];
$routeAction = $route->getAction();
list($class, $method) = explode('@', $routeAction['uses']);
$reflection = new ReflectionClass($class);
$reflectionMethod = $reflection->getMethod($method);
foreach ($reflectionMethod->getParameters() as $parameter) {
$reflectionType = $parameter->getType();
// Only scalar types or undefined types
if (is_null($reflectionType) || $reflectionType->isBuiltin()) {
$parameters[$parameter->getName()] = [
'name' => $parameter->getName(),
];
if (!is_null($reflectionType) && $reflectionType->isBuiltin()) {
$parameters[$parameter->getName()]['type'] = (string)$parameter->getType();
}
}
}
if (count($parameters) > 0) {
// Inject the related phpdoc if exists.
$comment = $reflectionMethod->getDocComment();
if ($comment) {
$docBlock = $this->docBlockFactory->create($comment);
foreach ($docBlock->getTagsByName('param') as $tag) {
foreach ($parameters as $parameterName => $parameter) {
if ($tag->getVariableName() == $parameterName) {
$docDescription = $tag->getDescription()->render();
/** @var \ReflectionType $reflectionType */
$reflectionType = $tag->getType();
$parameters[$parameterName]['docType'] = (string)$reflectionType;
$parameters[$parameterName]['docDescription'] = $docDescription;
}
}
}
}
}
return $parameters;
}
/**
* Try to get route validation rules if an instance of class/sub class of FormRequest is injected in the route method.
*
* @param \Illuminate\Routing\Route $route
* @return string[]
*/
protected function getRouteValidationRules($route)
{
$routeAction = $route->getAction();
list($class, $method) = explode('@', $routeAction['uses']);
$reflection = new ReflectionClass($class);
$reflectionMethod = $reflection->getMethod($method);
foreach ($reflectionMethod->getParameters() as $parameter) {
$parameterType = $parameter->getClass();
if (! is_null($parameterType) && class_exists($parameterType->name)) {
$className = $parameterType->name;
if (is_subclass_of($className, FormRequest::class)) {
$parameterReflection = new $className;
if (method_exists($parameterReflection, 'validator')) {
return $parameterReflection->validator()->getRules();
} else {
return $parameterReflection->rules();
}
}
}
}
return [];
}
/**
* Return route model/resource name or null if nothing found.
*
* @param string $route
* @return string|null
*/
protected function getRouteResource($route)
{
if (!config('openapischemas.resource_tag')) {
$this->parentCommand->error('The "resource_tag" value in your apischemas.php configuration file must defined.');
exit();
}
$routeAction = $route->getAction();
list($class, $method) = explode('@', $routeAction['uses']);
$reflection = new ReflectionClass($class);
$comment = $reflection->getDocComment();
if ($comment) {
$docBlock = $this->docBlockFactory->create($comment);
foreach ($docBlock->getTags() as $tag) {
if ($tag->getName() === config('openapischemas.resource_tag')) {
return $tag->getDescription()->render();
}
}
}
return null;
}
/**
* Return route controller class doc block if comments are provided else return null.
*
* @param \Illuminate\Routing\Route $route
* @return DocBlock|null
*/
protected function getRouteControllerDocBlock($route)
{
$routeAction = $route->getAction();
list($class, $method) = explode('@', $routeAction['uses']);
$reflectionClass = new ReflectionClass($class);
$comment = $reflectionClass->getDocComment();
if ($comment) {
return($this->docBlockFactory->create($comment));
}
return null;
}
/**
* Return route method doc block if comments are provided else return null.
*
* @param \Illuminate\Routing\Route $route
* @return DocBlock|null
*/
protected function getRouteMethodDocBlock($route)
{
$routeAction = $route->getAction();
list($class, $method) = explode('@', $routeAction['uses']);
$reflectionClass = new ReflectionClass($class);
$reflectionMethod = $reflectionClass->getMethod($method);
$comment = $reflectionMethod->getDocComment();
if ($comment) {
return($this->docBlockFactory->create($comment));
}
return null;
}
/**
* Call the specified route to get an example response
*
* @param \Illuminate\Routing\Route $route
* @param string $method
* @return string
*/
protected function getRouteCallExampleResponse($route, $method)
{
$url = $this->getRouteUrlWithBindings($route);
$response = $this->callRoute($method, $url, []);
if (is_null($response)) {
return null;
}
if (is_object($response)) {
$response = $response->content();
}
return $response;
}
/**
* Return the route url to call with bindings from configuration files.
*
* @param $route
* @return string
*/
protected function getRouteUrlWithBindings($route)
{
$urlRouteBindings = [];
$urlInjectedBindings = [];
$routeAction = $route->getAction();
if (isset($routeAction['as'])) {
if (isset($this->profile['api_calls_bindings'])) {
foreach ($this->profile['api_calls_bindings'] as $apiCallsBinding) {
$routesAliases = $apiCallsBinding['routes_aliases'];
foreach ($routesAliases as $routeAlias) {
if ($routeAlias == $routeAction['as']) {
foreach ($apiCallsBinding['bindings'] as $binding) {
if ($binding['in'] == 'query-route') {
$urlRouteBindings[$binding['name']] = $binding['value'];
} else if ($binding['in'] == 'query-injected') {
$urlInjectedBindings[$binding['name']] = $binding['value'];
}
}
}
}
}
}
}
$url = $this->getRouteUri($route);
foreach ($urlRouteBindings as $name => $value) {
$url = str_replace('{'.$name.'}', $value, $url);
}
if (count($urlInjectedBindings) > 0) {
$firstName = array_keys($urlInjectedBindings)[0];
$firstValue = array_shift($urlInjectedBindings);
$url .= '?' . $firstName . '=' . $firstValue;
foreach ($urlInjectedBindings as $name => $value) {
$url .= '&' . $name . '=' . $value;
}
}
return $url;
}
/**
* Return the URI of a route
*
* @param \Illuminate\Routing\Route $route
* @return string
*/
abstract protected function getRouteUri($route);
/**
* Call a route
*
* @param string $method HTTP method to use
* @param string $url
* @param mixed[] $parameters
* @return \Illuminate\Http\Response|null
*/
abstract protected function callRoute($method, $url, $parameters);
}