Client generartion
This commit is contained in:
parent
5e14218ad4
commit
f63220ff2a
4 changed files with 231 additions and 2 deletions
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
[submodule "clients/Api"]
|
||||
path = clients/Api
|
||||
url = git@gitlab.com:service-web-lab/dam-client.git
|
||||
225
app/Console/Commands/Api/GenerateApiClient.php
Normal file
225
app/Console/Commands/Api/GenerateApiClient.php
Normal file
|
|
@ -0,0 +1,225 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console\Commands\Api;
|
||||
|
||||
use App\Enums\Legacy\ApiError;
|
||||
use App\Enums\Legacy\ApiStatus;
|
||||
use App\Enums\MediaExtension;
|
||||
use App\Exceptions\Api\ApiErrorException;
|
||||
use App\Exceptions\Api\DamException;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class GenerateApiClient extends Command
|
||||
{
|
||||
private const TARGET = 'clients/Api/src';
|
||||
private const CLIENTNAMESPACE = 'LDC\\DAM\\Client';
|
||||
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'api:generate-client';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Generate the dam client in clients/Api folder';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$this->copyEnums();
|
||||
$this->copyExceptions();
|
||||
$this->copyRoutesList();
|
||||
$this->copyApiConfigInfo();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy config files
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function copyApiConfigInfo()
|
||||
{
|
||||
$this->info('Copy Api Config');
|
||||
$rootTarget = app_path('../' . self::TARGET);
|
||||
if (File::exists("$rootTarget/../apiConfig.readonly.json")) {
|
||||
File::delete("$rootTarget/../apiConfig.readonly.json");
|
||||
}
|
||||
File::put(
|
||||
"$rootTarget/../apiConfig.readonly.json",
|
||||
\json_encode(config('api'), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
|
||||
);
|
||||
$this->phpcbfFile("$rootTarget/../apiConfig.readonly.json");
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy routes into a json file
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function copyRoutesList()
|
||||
{
|
||||
$this->info('Copy Routes');
|
||||
$routes = [];
|
||||
/** @var \Illuminate\Routing\Route */
|
||||
foreach (Route::getRoutes() as $route) {
|
||||
if (\strpos($route->getPrefix(), 'api') !== false) {
|
||||
$routes[$route->getName()] = [
|
||||
'uri' => $route->uri(),
|
||||
'methods' => $route->methods()
|
||||
];
|
||||
}
|
||||
}
|
||||
$rootTarget = app_path('../' . self::TARGET);
|
||||
if (File::exists("$rootTarget/routes.json")) {
|
||||
File::delete("$rootTarget/routes.json");
|
||||
}
|
||||
File::put(
|
||||
"$rootTarget/routes.json",
|
||||
\json_encode($routes, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
|
||||
);
|
||||
$this->phpcbfFile("$rootTarget/routes.json");
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy Exeption classes
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function copyExceptions(): void
|
||||
{
|
||||
$this->info('Copy Exceptions');
|
||||
$rootTarget = app_path('../' . self::TARGET);
|
||||
$this->copyToFolder([
|
||||
DamException::class,
|
||||
ApiErrorException::class
|
||||
], 'Exceptions');
|
||||
$this->replaceInPhpFile(
|
||||
"$rootTarget/Exceptions/ApiErrorException.php",
|
||||
ApiError::class,
|
||||
self::CLIENTNAMESPACE . '\\Enums\\ApiError'
|
||||
);
|
||||
$this->replaceInPhpFile(
|
||||
"$rootTarget/Exceptions/DamException.php",
|
||||
DamException::class,
|
||||
self::CLIENTNAMESPACE . '\\Exceptions\\DamException'
|
||||
);
|
||||
$this->replaceInPhpFile(
|
||||
"$rootTarget/Exceptions/DamException.php",
|
||||
ApiErrorException::class,
|
||||
self::CLIENTNAMESPACE . '\\Exceptions\\ApiErrorException'
|
||||
);
|
||||
$this->phpcbfFile("$rootTarget/Exceptions/DamException.php");
|
||||
$this->phpcbfFile("$rootTarget/Exceptions/ApiErrorException.php");
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy enums classes
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function copyEnums(): void
|
||||
{
|
||||
$this->info('Copy Enums');
|
||||
$classes = [
|
||||
ApiError::class,
|
||||
ApiStatus::class
|
||||
];
|
||||
$this->copyToFolder($classes, 'Enums');
|
||||
$rootTarget = app_path('../' . self::TARGET);
|
||||
foreach ($classes as $class) {
|
||||
$classBasename = class_basename($class);
|
||||
$dest = "$rootTarget/Enums/$classBasename.php";
|
||||
$this->replaceInPhpFile($dest, 'BaseEnumRoutable', 'BaseEnum');
|
||||
$this->replaceInPhpFile($dest, '\\Legacy', '');
|
||||
$this->phpcbfFile($dest);
|
||||
}
|
||||
|
||||
$mediaExtensionEnum = "<?php\n\nnamespace LDC\DAM\Client\Enums;\n\nuse Kwaadpepper\Enum\BaseEnum;\n\n/**\n";
|
||||
foreach (MediaExtension::cases() as $strType) {
|
||||
$mediaExtensionEnum .= " * @method static self {$strType->name}()\n";
|
||||
}
|
||||
// phpcs:ignore Generic.Files.LineLength.TooLong
|
||||
$mediaExtensionEnum .= " *\n * @package DAMclient\n * @subpackage enums\n */\nclass MediaExtension extends BaseEnum\n{\n}\n";
|
||||
File::put("$rootTarget/Enums/MediaExtension.php", $mediaExtensionEnum);
|
||||
$this->phpcbfFile("$rootTarget/Enums/MediaExtensio.php");
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy Classes to specified folder
|
||||
*
|
||||
* @param array $classes
|
||||
* @param string $folder
|
||||
* @return void
|
||||
*/
|
||||
private function copyToFolder(array $classes, string $folder): void
|
||||
{
|
||||
$rootTarget = app_path('../' . self::TARGET);
|
||||
foreach ($classes as $class) {
|
||||
$classFileName = \str_replace("App\\$folder\\", '', sprintf('%s.php', $class));
|
||||
$classFileName = \str_replace('\\', '/', $classFileName);
|
||||
$classBasename = class_basename($class);
|
||||
$source = \app_path("$folder/$classFileName");
|
||||
$dest = "$rootTarget/$folder/$classBasename.php";
|
||||
if (File::exists($dest)) {
|
||||
File::delete($dest);
|
||||
}
|
||||
File::copy($source, $dest);
|
||||
$this->replaceInPhpFile($dest, 'namespace App', 'namespace ' . self::CLIENTNAMESPACE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces a specific string with another on a file
|
||||
*
|
||||
* @param string $source
|
||||
* @param string $search
|
||||
* @param string $replace
|
||||
* @return void
|
||||
*/
|
||||
private function replaceInPhpFile(string $source, string $search, string $replace): void
|
||||
{
|
||||
$fContent = File::get($source);
|
||||
|
||||
// Remove trailing \Api .
|
||||
$regex = '/namespace ([a-zA-Z0-9\\\\]+)\\\\Api;/m';
|
||||
$fContent = preg_replace($regex, 'namespace $1;', $fContent);
|
||||
|
||||
// Replace namespace top level.
|
||||
$pos = strpos($fContent, $search);
|
||||
if ($pos !== false) {
|
||||
File::put($source, \str_replace($search, $replace, $fContent));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Lint the file
|
||||
*
|
||||
* @param string $path
|
||||
* @return void
|
||||
* @throws \Symfony\Component\Process\Exception\RuntimeException If anything goes wrong.
|
||||
* @throws \Symfony\Component\Process\Exception\ProcessTimedOutException If anything goes wrong.
|
||||
* @throws \Symfony\Component\Process\Exception\ProcessSignaledException If anything goes wrong.
|
||||
* @throws \Symfony\Component\Process\Exception\LogicException If anything goes wrong.
|
||||
*/
|
||||
private function phpcbfFile(string $path): void
|
||||
{
|
||||
$process = new Process(['./vendor/bin/phpcbf', $path]);
|
||||
$process->run();
|
||||
}
|
||||
}
|
||||
|
|
@ -48,7 +48,8 @@
|
|||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Tests\\": "tests/"
|
||||
"Tests\\": "tests/",
|
||||
"LDC\\DAM\\Client\\": "clients/Api/src/"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
|
|
|
|||
2
composer.lock
generated
2
composer.lock
generated
|
|
@ -4,7 +4,7 @@
|
|||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "d3caf1a99a48f4de736a4a4f8aa14124",
|
||||
"content-hash": "1731504e63f41b434c0f6639604ae78b",
|
||||
"packages": [
|
||||
{
|
||||
"name": "adldap2/adldap2",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue