Browse Source

Refactor classes instantiation for easier custom bindings (#822)

* Refactor classes instantiation for easier custom binding

* Make class properties protected

---------

Co-authored-by: Peter Ragheb <peteragheb@gmail.com>
Peter Ragheb 1 year ago
parent
commit
32c5f08bd2

+ 1 - 1
src/Commands/GenerateDocumentation.php

@@ -63,7 +63,7 @@ class GenerateDocumentation extends Command
             $this->writeExampleCustomEndpoint();
         }
 
-        $writer = new Writer($this->docConfig, $this->paths);
+        $writer = app(Writer::class, ['config' => $this->docConfig, 'paths' => $this->paths]);
         $writer->writeDocs($groupedEndpoints);
 
         $this->upgradeConfigFileIfNeeded();

+ 1 - 1
src/ScribeServiceProvider.php

@@ -120,7 +120,7 @@ class ScribeServiceProvider extends ServiceProvider
     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;

+ 1 - 1
src/Tools/Utils.php

@@ -364,7 +364,7 @@ class Utils
     {
         // 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);

+ 5 - 5
src/Writing/Writer.php

@@ -13,12 +13,12 @@ use Symfony\Component\Yaml\Yaml;
 
 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)
     {