Skip to content

Commit b89907e

Browse files
committed
Merge branch 'master' into 158-ability-to-generate-documentation-for-invokable-controllers
2 parents 7e336ea + 47a7888 commit b89907e

23 files changed

+2156
-896
lines changed

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
"require": {
1818
"php": "^8.3",
1919
"laravel/framework": ">=11.34",
20-
"phpunit/phpunit": "^11.4",
20+
"phpunit/phpunit": ">=11.4",
2121
"ext-json": "*"
2222
},
2323
"require-dev": {
24-
"orchestra/testbench": "^9.3",
24+
"orchestra/testbench": ">=9.3",
2525
"php-coveralls/php-coveralls": "^2.7",
26-
"php-mock/php-mock-phpunit": "^2.10"
26+
"php-mock/php-mock-phpunit": ">=2.10",
27+
"ronasit/laravel-helpers": "^3.4"
2728
},
2829
"autoload": {
2930
"psr-4": {

composer.lock

Lines changed: 1598 additions & 729 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/auto-doc.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
'title' => env('APP_NAME', 'Name of Your Application'),
4848
'termsOfService' => '',
4949
'contact' => [
50-
'email' => null
50+
'email' => null,
5151
],
5252
'license' => [
5353
'name' => '',
@@ -184,5 +184,32 @@
184184
'development',
185185
],
186186

187-
'config_version' => '2.8',
187+
/*
188+
|--------------------------------------------------------------------------
189+
| Paratests
190+
|--------------------------------------------------------------------------
191+
|
192+
| The config for parallel tests execution setup
193+
*/
194+
'paratests' => [
195+
'tmp_file_lock' => [
196+
/*
197+
|--------------------------------------------------------------------------
198+
| Maximum attempts count, int
199+
|--------------------------------------------------------------------------
200+
| The maximum number of attempts to append data to a temporary documentation file
201+
*/
202+
'max_retries' => 20,
203+
204+
/*
205+
|--------------------------------------------------------------------------
206+
| Wait time between attempts, microseconds
207+
|--------------------------------------------------------------------------
208+
| The waiting time between attempts to write to the temporary documentation file while the file is locked
209+
*/
210+
'wait_time' => 500,
211+
],
212+
],
213+
214+
'config_version' => '2.9',
188215
];

readme.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,13 @@ passing PHPUnit tests.
4343
> ],
4444
> ```
4545
46-
2. Run `php artisan vendor:publish --provider=RonasIT\\AutoDoc\\AutoDocServiceProvider`
47-
1. Add `\RonasIT\AutoDoc\Http\Middleware\AutoDocMiddleware::class` middleware to the global HTTP middleware list `bootstrap\app.php`:
46+
2. Run
47+
48+
```
49+
php artisan vendor:publish --provider=RonasIT\\AutoDoc\\AutoDocServiceProvider
50+
```
51+
52+
3. Add `\RonasIT\AutoDoc\Http\Middleware\AutoDocMiddleware::class` middleware to the global HTTP middleware list `bootstrap\app.php`:
4853
4954
```php
5055
return Application::configure(basePath: dirname(__DIR__))
@@ -57,7 +62,8 @@ passing PHPUnit tests.
5762
```
5863

5964
4. Add `\RonasIT\AutoDoc\Traits\AutoDocTestCaseTrait` trait to `tests/TestCase.php`
60-
1. Configure documentation saving using one of the next ways:
65+
66+
5. Configure documentation saving using one of the next ways:
6167
- Add `SwaggerExtension` to the `<extensions>` block of your `phpunit.xml`.
6268
**Please note that this way will be removed after updating**
6369
**PHPUnit up to 10 version (https://github.com/sebastianbergmann/phpunit/issues/4676)**
@@ -232,4 +238,3 @@ can be found in the [Contributing guide](CONTRIBUTING.md).
232238
## License
233239

234240
Laravel Swagger plugin is open-sourced software licensed under the [MIT license](LICENSE).
235-

src/Contracts/SwaggerDriverContract.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,28 @@
55
interface SwaggerDriverContract
66
{
77
/**
8-
* Save temporary data
8+
* Add current process tmp data to the temp documentation file
9+
*
10+
* @param callable $appendDataCallback with 1 array argument, containing the current temp file content as array
11+
*/
12+
public function appendProcessDataToTmpFile(callable $appendDataCallback): void;
13+
14+
/**
15+
* Get temporary data from the temp file shared between all PHPUnit processes
16+
*/
17+
public function getTmpData(): ?array;
18+
19+
/**
20+
* Save current process temporary data
921
*
1022
* @param array $data
1123
*/
12-
public function saveTmpData($data);
24+
public function saveProcessTmpData(array $data): void;
1325

1426
/**
15-
* Get temporary data
27+
* Get current process temporary data
1628
*/
17-
public function getTmpData();
29+
public function getProcessTmpData(): ?array;
1830

1931
/**
2032
* Save production data

src/Drivers/BaseDriver.php

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

33
namespace RonasIT\AutoDoc\Drivers;
44

5+
use Illuminate\Support\Facades\ParallelTesting;
56
use RonasIT\AutoDoc\Contracts\SwaggerDriverContract;
7+
use RonasIT\AutoDoc\Support\Mutex;
68

79
abstract class BaseDriver implements SwaggerDriverContract
810
{
911
protected string $tempFilePath;
12+
protected string $processTempFilePath;
13+
protected Mutex $mutex;
1014

1115
public function __construct()
1216
{
1317
$this->tempFilePath = storage_path('temp_documentation.json');
18+
19+
$this->processTempFilePath = ($token = ParallelTesting::token())
20+
? storage_path("temp_documentation_{$token}.json")
21+
: $this->tempFilePath;
22+
23+
$this->mutex = new Mutex(
24+
maxRetries: config('auto-doc.paratests.tmp_file_lock.max_retries'),
25+
waitTime: config('auto-doc.paratests.tmp_file_lock.wait_time'),
26+
);
1427
}
1528

16-
public function saveTmpData($data): void
29+
public function saveProcessTmpData(array $data): void
1730
{
18-
file_put_contents($this->tempFilePath, json_encode($data));
31+
file_put_contents($this->processTempFilePath, json_encode($data));
1932
}
2033

21-
public function getTmpData(): ?array
34+
public function getProcessTmpData(): ?array
2235
{
23-
if (file_exists($this->tempFilePath)) {
24-
$content = file_get_contents($this->tempFilePath);
36+
if (file_exists($this->processTempFilePath)) {
37+
$content = file_get_contents($this->processTempFilePath);
2538

2639
return json_decode($content, true);
2740
}
2841

2942
return null;
3043
}
3144

32-
protected function clearTmpData(): void
45+
public function appendProcessDataToTmpFile(callable $appendDataCallback): void
46+
{
47+
$this->mutex->writeFileWithLock($this->tempFilePath, function (string $tempFileContent) use ($appendDataCallback) {
48+
$resultDocContent = $appendDataCallback(json_decode($tempFileContent, true));
49+
50+
return json_encode($resultDocContent);
51+
});
52+
}
53+
54+
public function getTmpData(): ?array
3355
{
3456
if (file_exists($this->tempFilePath)) {
35-
unlink($this->tempFilePath);
57+
$data = $this->mutex->readFileWithLock($this->tempFilePath);
58+
59+
return json_decode($data, true);
60+
}
61+
62+
return null;
63+
}
64+
65+
protected function clearProcessTmpData(): void
66+
{
67+
if (file_exists($this->processTempFilePath)) {
68+
unlink($this->processTempFilePath);
3669
}
3770
}
3871
}

src/Drivers/LocalDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function saveData(): void
2424
{
2525
file_put_contents($this->prodFilePath, json_encode($this->getTmpData()));
2626

27-
$this->clearTmpData();
27+
$this->clearProcessTmpData();
2828
}
2929

3030
public function getDocumentation(): array

src/Drivers/RemoteDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function saveData(): void
2828
'Content-Type: application/json',
2929
]);
3030

31-
$this->clearTmpData();
31+
$this->clearProcessTmpData();
3232
}
3333

3434
public function getDocumentation(): array

src/Drivers/StorageDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function saveData(): void
2828
{
2929
$this->disk->put($this->prodFilePath, json_encode($this->getTmpData()));
3030

31-
$this->clearTmpData();
31+
$this->clearProcessTmpData();
3232
}
3333

3434
public function getDocumentation(): array

src/Services/SwaggerService.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
use Illuminate\Http\Request;
77
use Illuminate\Http\Testing\File;
88
use Illuminate\Support\Arr;
9+
use Illuminate\Support\Facades\ParallelTesting;
910
use Illuminate\Support\Facades\URL;
1011
use Illuminate\Support\Str;
1112
use ReflectionClass;
13+
use RonasIT\AutoDoc\Contracts\SwaggerDriverContract;
1214
use RonasIT\AutoDoc\Exceptions\DocFileNotExistsException;
1315
use RonasIT\AutoDoc\Exceptions\EmptyContactEmailException;
1416
use RonasIT\AutoDoc\Exceptions\EmptyDocFileException;
@@ -18,7 +20,6 @@
1820
use RonasIT\AutoDoc\Exceptions\SwaggerDriverClassNotFoundException;
1921
use RonasIT\AutoDoc\Exceptions\UnsupportedDocumentationViewerException;
2022
use RonasIT\AutoDoc\Exceptions\WrongSecurityConfigException;
21-
use RonasIT\AutoDoc\Contracts\SwaggerDriverContract;
2223
use RonasIT\AutoDoc\Traits\GetDependenciesTrait;
2324
use RonasIT\AutoDoc\Validators\SwaggerSpecValidator;
2425
use Symfony\Component\HttpFoundation\Response;
@@ -75,12 +76,12 @@ public function __construct(Container $container)
7576

7677
$this->security = $this->config['security'];
7778

78-
$this->data = $this->driver->getTmpData();
79+
$this->data = $this->driver->getProcessTmpData();
7980

8081
if (empty($this->data)) {
8182
$this->data = $this->generateEmptyData();
8283

83-
$this->driver->saveTmpData($this->data);
84+
$this->driver->saveProcessTmpData($this->data);
8485
}
8586
}
8687
}
@@ -188,7 +189,7 @@ public function addData(Request $request, $response)
188189
$this->parseRequest();
189190
$this->parseResponse($response);
190191

191-
$this->driver->saveTmpData($this->data);
192+
$this->driver->saveProcessTmpData($this->data);
192193
}
193194

194195
protected function prepareItem()
@@ -799,6 +800,18 @@ protected function saveTempData()
799800

800801
public function saveProductionData()
801802
{
803+
if (ParallelTesting::token()) {
804+
$this->driver->appendProcessDataToTmpFile(function (array $sharedTmpData) {
805+
$resultDocContent = (empty($sharedTmpData))
806+
? $this->generateEmptyData()
807+
: $sharedTmpData;
808+
809+
$this->mergeOpenAPIDocs($resultDocContent, $this->data);
810+
811+
return $resultDocContent;
812+
});
813+
}
814+
802815
$this->driver->saveData();
803816
}
804817

@@ -875,6 +888,8 @@ protected function getClassAnnotations($class): array
875888

876889
$annotations = $reflection->getDocComment();
877890

891+
$annotations = Str::of($annotations)->remove("\r");
892+
878893
$blocks = explode("\n", $annotations);
879894

880895
$result = [];

0 commit comments

Comments
 (0)