Skip to content

Commit

Permalink
Merge 0aa2e45 into 593927a
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkaOnLine committed Mar 31, 2022
2 parents 593927a + 0aa2e45 commit ce834e0
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 72 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/test-config.yml
Expand Up @@ -71,10 +71,12 @@ jobs:

- name: Publish code coverage
env:
CC_TEST_REPORTER_ID: d5b1b36d663604a8900fb8a586625a94eae6cdb6f6d7372aec8f066c174b4a5c
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_PROJECT_TOKEN }}
run: |
cp ${{github.workspace}}/tests/storage/logs/test-reports/clover.xml ${{github.workspace}}/clover.xml
./cc-test-reporter after-build -t clover --exit-code 0
bash <(curl -Ls https://coverage.codacy.com/get.sh) report -r ${{github.workspace}}/clover.xml
if: matrix.php == '8.1' && matrix.l5-swagger-flags == 'latest'

- name: Publish coveralls report
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Expand Up @@ -60,7 +60,7 @@ WORKDIR /app/l5-swagger-app

RUN /usr/local/bin/php -dxdebug.mode=off /usr/local/bin/composer config repositories.l5-swagger path '../'

RUN /usr/local/bin/php -dxdebug.mode=off /usr/local/bin/composer require 'DarkaOnLine/l5-swagger:dev-master'
RUN /usr/local/bin/php -dxdebug.mode=off /usr/local/bin/composer require 'darkaonline/l5-swagger:dev-master'

RUN ln -s /app/tests/storage/annotations/ app/annotations

Expand Down
24 changes: 16 additions & 8 deletions src/Generator.php
Expand Up @@ -3,8 +3,8 @@
namespace L5Swagger;

use Exception;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\File;
use L5Swagger\Exceptions\L5SwaggerException;
use OpenApi\Annotations\OpenApi;
use OpenApi\Annotations\Server;
Expand Down Expand Up @@ -84,6 +84,11 @@ class Generator
*/
protected $scanOptions;

/**
* @var ?Filesystem
*/
protected $fileSystem;

/**
* Generator constructor.
*
Expand All @@ -98,7 +103,8 @@ public function __construct(
array $constants,
bool $yamlCopyRequired,
SecurityDefinitions $security,
array $scanOptions
array $scanOptions,
?Filesystem $filesystem = null
) {
$this->annotationsDir = $paths['annotations'];
$this->docDir = $paths['docs'];
Expand All @@ -110,6 +116,8 @@ public function __construct(
$this->yamlCopyRequired = $yamlCopyRequired;
$this->security = $security;
$this->scanOptions = $scanOptions;

$this->fileSystem = $filesystem ?? new Filesystem();
}

/**
Expand All @@ -134,15 +142,15 @@ public function generateDocs(): void
*/
protected function prepareDirectory(): self
{
if (File::exists($this->docDir) && ! File::isWritable($this->docDir)) {
if ($this->fileSystem->exists($this->docDir) && ! $this->fileSystem->isWritable($this->docDir)) {
throw new L5SwaggerException('Documentation storage directory is not writable');
}

if (! File::exists($this->docDir)) {
File::makeDirectory($this->docDir);
if (! $this->fileSystem->exists($this->docDir)) {
$this->fileSystem->makeDirectory($this->docDir);
}

if (! File::exists($this->docDir)) {
if (! $this->fileSystem->exists($this->docDir)) {
throw new L5SwaggerException('Documentation storage directory could not be created');
}

Expand Down Expand Up @@ -293,13 +301,13 @@ protected function makeYamlCopy(): void
{
if ($this->yamlCopyRequired) {
$yamlDocs = (new YamlDumper(2))->dump(
json_decode(file_get_contents($this->docsFile), true),
json_decode($this->fileSystem->get($this->docsFile), true),
20,
0,
Yaml::DUMP_OBJECT_AS_MAP ^ Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE
);

file_put_contents(
$this->fileSystem->put(
$this->yamlDocsFile,
$yamlDocs
);
Expand Down
4 changes: 3 additions & 1 deletion src/Http/Controllers/SwaggerAssetController.php
Expand Up @@ -2,6 +2,7 @@

namespace L5Swagger\Http\Controllers;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller as BaseController;
Expand All @@ -11,13 +12,14 @@ class SwaggerAssetController extends BaseController
{
public function index(Request $request, $asset)
{
$fileSystem = new Filesystem();
$documentation = $request->offsetGet('documentation');

try {
$path = swagger_ui_dist_path($documentation, $asset);

return (new Response(
file_get_contents($path),
$fileSystem->get($path),
200,
[
'Content-Type' => (pathinfo($asset))['extension'] == 'css'
Expand Down
17 changes: 11 additions & 6 deletions src/Http/Controllers/SwaggerController.php
Expand Up @@ -2,10 +2,11 @@

namespace L5Swagger\Http\Controllers;

use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Request as RequestFacade;
use Illuminate\Support\Facades\Response as ResponseFacade;
Expand All @@ -28,20 +29,22 @@ public function __construct(GeneratorFactory $generatorFactory)
* Dump api-docs content endpoint. Supports dumping a json, or yaml file.
*
* @param Request $request
* @param string $file
* @param ?string $file
* @return Response
*
* @throws L5SwaggerException
* @throws FileNotFoundException
*/
public function docs(Request $request, string $file = null)
{
$fileSystem = new Filesystem();
$documentation = $request->offsetGet('documentation');
$config = $request->offsetGet('config');

$targetFile = $config['paths']['docs_json'] ?? 'api-docs.json';
$yaml = false;

if (! is_null($file)) {
if ($file !== null) {
$targetFile = $file;
$parts = explode('.', $file);

Expand Down Expand Up @@ -72,11 +75,11 @@ public function docs(Request $request, string $file = null)
}
}

if (! file_exists($filePath)) {
if (! $fileSystem->exists($filePath)) {
abort(404, sprintf('Unable to locate documentation file at: "%s"', $filePath));
}

$content = File::get($filePath);
$content = $fileSystem->get($filePath);

if ($yaml) {
return ResponseFacade::make($content, 200, [
Expand Down Expand Up @@ -133,12 +136,14 @@ public function api(Request $request)
* @return string
*
* @throws L5SwaggerException
* @throws FileNotFoundException
*/
public function oauth2Callback(Request $request)
{
$fileSystem = new Filesystem();
$documentation = $request->offsetGet('documentation');

return File::get(swagger_ui_dist_path($documentation, 'oauth2-redirect.html'));
return $fileSystem->get(swagger_ui_dist_path($documentation, 'oauth2-redirect.html'));
}

/**
Expand Down
28 changes: 19 additions & 9 deletions src/SecurityDefinitions.php
Expand Up @@ -2,6 +2,8 @@

namespace L5Swagger;

use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;

class SecurityDefinitions
Expand Down Expand Up @@ -32,11 +34,15 @@ public function __construct(array $securitySchemesConfig = [], array $securityCo
* Reads in the l5-swagger configuration and appends security settings to documentation.
*
* @param string $filename The path to the generated json documentation
*
* @throws FileNotFoundException
*/
public function generate($filename)
public function generate(string $filename): void
{
$fileSystem = new Filesystem();

$documentation = collect(
json_decode(file_get_contents($filename))
json_decode($fileSystem->get($filename))
);

if (is_array($this->securitySchemesConfig) && ! empty($this->securitySchemesConfig)) {
Expand All @@ -47,7 +53,7 @@ public function generate($filename)
$documentation = $this->injectSecurity($documentation, $this->securityConfig);
}

file_put_contents(
$fileSystem->put(
$filename,
$documentation->toJson(JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
);
Expand All @@ -60,7 +66,7 @@ public function generate($filename)
* @param array $config The securityScheme settings from l5-swagger
* @return Collection
*/
protected function injectSecuritySchemes(Collection $documentation, array $config)
protected function injectSecuritySchemes(Collection $documentation, array $config): Collection
{
$components = collect();
if ($documentation->has('components')) {
Expand All @@ -73,7 +79,7 @@ protected function injectSecuritySchemes(Collection $documentation, array $confi
}

foreach ($config as $key => $cfg) {
$securitySchemes->offsetSet($key, self::arrayToObject($cfg));
$securitySchemes->offsetSet($key, $this->arrayToObject($cfg));
}

$components->offsetSet('securitySchemes', $securitySchemes);
Expand All @@ -90,18 +96,22 @@ protected function injectSecuritySchemes(Collection $documentation, array $confi
* @param array $config The security settings from l5-swagger
* @return Collection
*/
protected function injectSecurity(Collection $documentation, array $config)
protected function injectSecurity(Collection $documentation, array $config): Collection
{
$security = collect();
if ($documentation->has('security')) {
$security = collect($documentation->get('security'));
}

foreach ($config as $key => $cfg) {
$security->offsetSet($key, self::arrayToObject($cfg));
if (! empty($cfg)) {
$security->put($key, $this->arrayToObject($cfg));
}
}

$documentation->offsetSet('security', $security);
if ($security->count()) {
$documentation->offsetSet('security', $security);
}

return $documentation;
}
Expand All @@ -112,7 +122,7 @@ protected function injectSecurity(Collection $documentation, array $config)
* @param $array
* @return object
*/
public static function arrayToObject($array)
protected function arrayToObject($array)
{
return json_decode(json_encode($array));
}
Expand Down
10 changes: 7 additions & 3 deletions tests/ConsoleTest.php
Expand Up @@ -2,6 +2,7 @@

namespace Tests;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Artisan;
use L5Swagger\Exceptions\L5SwaggerException;

Expand All @@ -17,13 +18,15 @@ class ConsoleTest extends TestCase
*/
public function canGenerate(string $artisanCommand): void
{
$fileSystem = new Filesystem();

$this->setAnnotationsPath();

Artisan::call($artisanCommand);

$this->assertFileExists($this->jsonDocsFile());

$fileContent = file_get_contents($this->jsonDocsFile());
$fileContent = $fileSystem->get($this->jsonDocsFile());

$this->assertJson($fileContent);
$this->assertStringContainsString('L5 Swagger', $fileContent);
Expand All @@ -49,11 +52,12 @@ public function provideGenerateCommands(): iterable
*/
public function canPublish(): void
{
$fileSystem = new Filesystem();
Artisan::call('vendor:publish', ['--provider' => 'L5Swagger\L5SwaggerServiceProvider']);

$config = $this->configFactory->documentationConfig();

$this->assertTrue(file_exists(config_path('l5-swagger.php')));
$this->assertTrue(file_exists($config['paths']['views'].'/index.blade.php'));
$this->assertTrue($fileSystem->exists(config_path('l5-swagger.php')));
$this->assertTrue($fileSystem->exists($config['paths']['views'].'/index.blade.php'));
}
}

0 comments on commit ce834e0

Please sign in to comment.