Skip to content

Commit

Permalink
Merge pull request #43 from RonasIT/42-drivers-feature
Browse files Browse the repository at this point in the history
feat: implement drivers
  • Loading branch information
astorozhevsky authored May 3, 2022
2 parents 1509a35 + f15add8 commit 7ac392b
Show file tree
Hide file tree
Showing 15 changed files with 295 additions and 182 deletions.
69 changes: 43 additions & 26 deletions config/auto-doc.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

use RonasIT\Support\AutoDoc\Drivers\LocalDriver;
use RonasIT\Support\AutoDoc\Drivers\RemoteDriver;
use RonasIT\Support\AutoDoc\Drivers\StorageDriver;

return [

/*
Expand All @@ -9,7 +13,6 @@
|
| Route which will return documentation
*/

'route' => '/',

/*
Expand All @@ -19,7 +22,6 @@
|
| Information fields
*/

'info' => [

/*
Expand All @@ -29,7 +31,6 @@
|
| You can use your custom documentation view
*/

'description' => 'swagger-description',
'version' => '0.0.0',
'title' => 'Name of Your Application',
Expand All @@ -54,7 +55,6 @@
| Base path for API routes. If config is set, all routes which starts from
| this value will be grouped.
*/

'basePath' => '/',
'schemes' => [],
'definitions' => [],
Expand All @@ -67,7 +67,6 @@
| Library name, which used to secure the project.
| Available values: "jwt", "laravel", "null"
*/

'security' => '',
'defaults' => [

Expand All @@ -76,7 +75,6 @@
| Default descriptions of code statuses
|--------------------------------------------------------------------------
*/

'code-descriptions' => [
'200' => 'Operation successfully done',
'204' => 'Operation successfully done',
Expand All @@ -86,31 +84,50 @@

/*
|--------------------------------------------------------------------------
| Data Collector Class
| Driver
|--------------------------------------------------------------------------
|
| Class of data collector, which will collect and save documentation
| It can be your own data collector class which should be inherited from
| RonasIT\Support\AutoDoc\Interfaces\DataCollectorInterface interface,
| or our data collectors from next packages:
|
| ronasit/local-data-collector
| ronasit/laravel-remote-data-collector
|
| If config not set, will be using ronasit/local-data-collector
| The name of driver, which will collect and save documentation
| Feel free to use your own driver class which should be inherited from
| `RonasIT\Support\AutoDoc\Interfaces\SwaggerDriverInterface` interface,
| or one of our drivers from the `drivers` config:
*/
'driver' => 'local',

'data_collector' => \RonasIT\Support\AutoDoc\DataCollectors\LocalDataCollector::class,
'drivers' => [
'local' => [
'class' => LocalDriver::class,
'production_path' => storage_path('documentation.json')
],
'remote' => [
'class' => RemoteDriver::class,
'key' => 'project_name',
'url' => 'http://example.com'
],
'storage' => [
'class' => StorageDriver::class,

/*
|--------------------------------------------------------------------------
| Storage disk
|--------------------------------------------------------------------------
|
| One of the filesystems.disks config value
*/
'disk' => 'public',
'production_path' => 'documentation.json'
]
],

/*
|--------------------------------------------------------------------------
| Swagger documentation visibility environments list
|--------------------------------------------------------------------------
|
| The list of environments in which auto documentation will be displaying
*/
/*
|--------------------------------------------------------------------------
| Swagger documentation visibility environments list
|--------------------------------------------------------------------------
|
| The list of environments in which auto documentation will be displaying
*/
'display_environments' => [
'local',
'development',
],
'development'
]
];
5 changes: 0 additions & 5 deletions config/local-data-collector.php

This file was deleted.

5 changes: 0 additions & 5 deletions src/AutoDocServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ public function boot()
__DIR__ . '/../config/auto-doc.php' => config_path('auto-doc.php'),
], 'config');

$this->publishes([
__DIR__ . '/../config/local-data-collector.php' => config_path('local-data-collector.php'),
], 'config');

$this->publishes([
__DIR__ . '/Views/swagger-description.blade.php' => resource_path('views/swagger-description.blade.php'),
], 'view');
Expand All @@ -34,6 +30,5 @@ public function boot()

public function register()
{

}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<?php

namespace RonasIT\Support\AutoDoc\DataCollectors;
namespace RonasIT\Support\AutoDoc\Drivers;

use stdClass;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use RonasIT\Support\AutoDoc\Interfaces\DataCollectorInterface;
use RonasIT\Support\AutoDoc\Interfaces\SwaggerDriverInterface;
use RonasIT\Support\AutoDoc\Exceptions\MissedProductionFilePathException;

class LocalDataCollector implements DataCollectorInterface
class LocalDriver implements SwaggerDriverInterface
{
public $prodFilePath;
public $tempFilePath;

protected static $data;

public function __construct()
{
$this->prodFilePath = config('local-data-collector.production_path');
$this->prodFilePath = config('auto-doc.drivers.local.production_path');

if (empty($this->prodFilePath)) {
throw new MissedProductionFilePathException();
Expand All @@ -41,7 +41,7 @@ public function saveData()
self::$data = [];
}

public function getDocumentation()
public function getDocumentation(): stdClass
{
if (!file_exists($this->prodFilePath)) {
throw new FileNotFoundException();
Expand Down
62 changes: 62 additions & 0 deletions src/Drivers/RemoteDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace RonasIT\Support\AutoDoc\Drivers;

use stdClass;
use RonasIT\Support\AutoDoc\Interfaces\SwaggerDriverInterface;

class RemoteDriver implements SwaggerDriverInterface
{
protected $key;
protected $remoteUrl;

protected static $data;

public function __construct()
{
$this->key = config('auto-doc.drivers.remote.key');
$this->remoteUrl = config('auto-doc.drivers.remote.url');
}

public function saveTmpData($tempData)
{
self::$data = $tempData;
}

public function getTmpData()
{
return self::$data;
}

public function saveData()
{
$curl = curl_init();

curl_setopt($curl, CURLOPT_URL,$this->getUrl());
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($this->getTmpData()));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

curl_exec($curl);

curl_close($curl);

self::$data = [];
}

public function getDocumentation(): stdClass
{
$content = file_get_contents($this->getUrl());

if (empty($content)) {
throw new FileNotFoundException();
}

return json_decode($content);
}

protected function getUrl()
{
return "{$this->remoteUrl}/documentations/{$this->key}";
}
}
52 changes: 52 additions & 0 deletions src/Drivers/StorageDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace RonasIT\Support\AutoDoc\Drivers;

use stdClass;
use Illuminate\Support\Facades\Storage;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use RonasIT\Support\AutoDoc\Interfaces\SwaggerDriverInterface;

class StorageDriver implements SwaggerDriverInterface
{
protected $disk;
protected $filePath;

protected static $data;

public function __construct()
{
$this->disk = config('auto-doc.drivers.storage.disk');
$this->filePath = config('auto-doc.drivers.storage.production_path');
}

public function saveTmpData($tempData)
{
self::$data = $tempData;
}

public function getTmpData()
{
return self::$data;
}

public function saveData()
{
$content = json_encode(self::$data);

Storage::disk($this->disk)->put($this->filePath, $content);

self::$data = [];
}

public function getDocumentation(): stdClass
{
if (!Storage::disk($this->disk)->exists($this->filePath)) {
throw new FileNotFoundException();
}

$fileContent = Storage::disk($this->disk)->get($this->filePath);

return json_decode($fileContent);
}
}
17 changes: 0 additions & 17 deletions src/Exceptions/DataCollectorClassNotFoundException.php

This file was deleted.

14 changes: 0 additions & 14 deletions src/Exceptions/EmptyDataCollectorClassException.php

This file was deleted.

13 changes: 13 additions & 0 deletions src/Exceptions/InvalidDriverClassException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace RonasIT\Support\AutoDoc\Exceptions;

use Exception;

class InvalidDriverClassException extends Exception
{
public function __construct(string $driver)
{
parent::__construct("The driver '{$driver}' is not implements the SwaggerDriverInterface.");
}
}
6 changes: 3 additions & 3 deletions src/Exceptions/MissedProductionFilePathException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

class MissedProductionFilePathException extends Exception
{
public function __construct($message = 'Production file path missed in config',
$code = 0,
Exception $previous = null)
public function __construct($message = null, $code = 0, Exception $previous = null)
{
$message = $message ?? 'Production file path missed in config';

parent::__construct($message, $code, $previous);
}
}
17 changes: 17 additions & 0 deletions src/Exceptions/SwaggerDriverClassNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace RonasIT\Support\AutoDoc\Exceptions;

use Exception;

class SwaggerDriverClassNotFoundException extends Exception
{
public function __construct($className)
{
if (empty($message)) {
$message = "Driver class '{$className}' was not found. Please check configuration file.";
}

parent::__construct($message);
}
}
1 change: 0 additions & 1 deletion src/Exceptions/WrongSecurityConfigException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@

class WrongSecurityConfigException extends Exception
{

}
Loading

0 comments on commit 7ac392b

Please sign in to comment.