From 32c5f08bd2ae742a9d8b153d5b63c4151399607f Mon Sep 17 00:00:00 2001 From: Peter Ragheb Date: Fri, 15 Mar 2024 21:13:43 +0200 Subject: [PATCH] Refactor classes instantiation for easier custom bindings (#822) * Refactor classes instantiation for easier custom binding * Make class properties protected --------- Co-authored-by: Peter Ragheb --- src/Commands/GenerateDocumentation.php | 2 +- src/ScribeServiceProvider.php | 2 +- src/Tools/Utils.php | 2 +- src/Writing/Writer.php | 10 +++++----- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Commands/GenerateDocumentation.php b/src/Commands/GenerateDocumentation.php index bddc5e33..127edd62 100644 --- a/src/Commands/GenerateDocumentation.php +++ b/src/Commands/GenerateDocumentation.php @@ -63,7 +63,7 @@ public function handle(RouteMatcherInterface $routeMatcher, GroupedEndpointsFact $this->writeExampleCustomEndpoint(); } - $writer = new Writer($this->docConfig, $this->paths); + $writer = app(Writer::class, ['config' => $this->docConfig, 'paths' => $this->paths]); $writer->writeDocs($groupedEndpoints); $this->upgradeConfigFileIfNeeded(); diff --git a/src/ScribeServiceProvider.php b/src/ScribeServiceProvider.php index 3b1d404c..0c5fce5f 100644 --- a/src/ScribeServiceProvider.php +++ b/src/ScribeServiceProvider.php @@ -120,7 +120,7 @@ protected function registerCommands(): void public function loadCustomTranslationLayer(): void { $this->app->extend('translation.loader', function ($defaultFileLoader) { - return new CustomTranslationsLoader($defaultFileLoader); + return app(CustomTranslationsLoader::class, ['loader' => $defaultFileLoader]); }); $this->app->forgetInstance('translator'); self::$customTranslationLayerLoaded = true; diff --git a/src/Tools/Utils.php b/src/Tools/Utils.php index d16eaf8a..e7db3e15 100644 --- a/src/Tools/Utils.php +++ b/src/Tools/Utils.php @@ -364,7 +364,7 @@ public static function trans(string $key, array $replace = []) { // We only load our custom translation layer if we really need it if (!ScribeServiceProvider::$customTranslationLayerLoaded) { - (new ScribeServiceProvider(app()))->loadCustomTranslationLayer(); + app(ScribeServiceProvider::class, ['app' => app()])->loadCustomTranslationLayer(); } $translation = trans($key, $replace); diff --git a/src/Writing/Writer.php b/src/Writing/Writer.php index f0ce56d7..d9fa1602 100644 --- a/src/Writing/Writer.php +++ b/src/Writing/Writer.php @@ -13,12 +13,12 @@ class Writer { - private bool $isStatic; - private bool $isExternal; + protected bool $isStatic; + protected bool $isExternal; - private ?string $staticTypeOutputPath; + protected ?string $staticTypeOutputPath; - private ?string $laravelTypeOutputPath; + protected ?string $laravelTypeOutputPath; protected array $generatedFiles = [ 'postman' => null, 'openapi' => null, @@ -31,7 +31,7 @@ class Writer ], ]; - private string $laravelAssetsPath; + protected string $laravelAssetsPath; public function __construct(protected DocumentationConfig $config, public PathConfig $paths) {