瀏覽代碼

Convert some more logging to Clara

shalvah 5 年之前
父節點
當前提交
d966b8397d

+ 16 - 12
src/Extracting/Strategies/Responses/ResponseCalls.php

@@ -3,6 +3,8 @@
 namespace Knuckles\Scribe\Extracting\Strategies\Responses;
 
 use Dingo\Api\Dispatcher;
+use Exception;
+use Illuminate\Contracts\Http\Kernel;
 use Illuminate\Http\Request;
 use Illuminate\Http\Response;
 use Illuminate\Routing\Route;
@@ -11,6 +13,8 @@ use Knuckles\Scribe\Extracting\ParamHelpers;
 use Knuckles\Scribe\Extracting\Strategies\Strategy;
 use Knuckles\Scribe\Tools\Flags;
 use Knuckles\Scribe\Tools\Utils;
+use ReflectionClass;
+use ReflectionFunctionAbstract;
 
 /**
  * Make a call to the route and retrieve its response.
@@ -21,14 +25,14 @@ class ResponseCalls extends Strategy
 
     /**
      * @param Route $route
-     * @param \ReflectionClass $controller
-     * @param \ReflectionFunctionAbstract $method
+     * @param ReflectionClass $controller
+     * @param ReflectionFunctionAbstract $method
      * @param array $routeRules
      * @param array $context
      *
      * @return array|null
      */
-    public function __invoke(Route $route, \ReflectionClass $controller, \ReflectionFunctionAbstract $method, array $routeRules, array $context = [])
+    public function __invoke(Route $route, ReflectionClass $controller, ReflectionFunctionAbstract $method, array $routeRules, array $context = [])
     {
         $rulesToApply = $routeRules['response_calls'] ?? [];
         if (! $this->shouldMakeApiCall($route, $rulesToApply, $context)) {
@@ -51,12 +55,12 @@ class ResponseCalls extends Strategy
                     'content' => $response->getContent(),
                 ],
             ];
-        } catch (\Exception $e) {
-            echo 'Exception thrown during response call for [' . implode(',', $route->methods) . "] {$route->uri}.\n";
+        } catch (Exception $e) {
+            clara('knuckleswtf/scribe')->warn('Exception thrown during response call for [' . implode(',', $route->methods) . "] {$route->uri}.");
             if (Flags::$shouldBeVerbose) {
                 Utils::dumpException($e);
             } else {
-                echo "Run this again with the --verbose flag to see the exception.\n";
+                clara('knuckleswtf/scribe')->warn("Run this again with the --verbose flag to see the exception.");
             }
             $response = null;
         } finally {
@@ -150,7 +154,7 @@ class ResponseCalls extends Strategy
     {
         try {
             app('db')->beginTransaction();
-        } catch (\Exception $e) {
+        } catch (Exception $e) {
         }
     }
 
@@ -161,7 +165,7 @@ class ResponseCalls extends Strategy
     {
         try {
             app('db')->rollBack();
-        } catch (\Exception $e) {
+        } catch (Exception $e) {
         }
     }
 
@@ -277,7 +281,7 @@ class ResponseCalls extends Strategy
     /**
      * @param Request $request
      *
-     * @throws \Exception
+     * @throws Exception
      *
      * @return \Illuminate\Http\JsonResponse|mixed|\Symfony\Component\HttpFoundation\Response
      */
@@ -295,15 +299,15 @@ class ResponseCalls extends Strategy
     /**
      * @param Request $request
      *
-     * @throws \Exception
+     * @throws Exception
      *
      * @return \Symfony\Component\HttpFoundation\Response
      */
     protected function callLaravelRoute(Request $request): \Symfony\Component\HttpFoundation\Response
     {
         // Confirm we're running in Laravel, not Lumen
-        if (app()->bound(\Illuminate\Contracts\Http\Kernel::class)) {
-            $kernel = app(\Illuminate\Contracts\Http\Kernel::class);
+        if (app()->bound(Kernel::class)) {
+            $kernel = app(Kernel::class);
             $response = $kernel->handle($request);
             $kernel->terminate($request, $response);
         } else {

+ 11 - 11
src/Extracting/Strategies/Responses/UseApiResourceTags.php

@@ -32,11 +32,11 @@ class UseApiResourceTags extends Strategy
      * @param array $rulesToApply
      * @param array $context
      *
-     * @throws \Exception
-     *
      * @return array|null
+     *@throws Exception
+     *
      */
-    public function __invoke(Route $route, \ReflectionClass $controller, \ReflectionFunctionAbstract $method, array $rulesToApply, array $context = [])
+    public function __invoke(Route $route, ReflectionClass $controller, ReflectionFunctionAbstract $method, array $rulesToApply, array $context = [])
     {
         $docBlocks = RouteDocBlocker::getDocBlocksFromRoute($route);
         /** @var DocBlock $methodDocBlock */
@@ -65,7 +65,7 @@ class UseApiResourceTags extends Strategy
 
             try {
                 $resource = new $apiResourceClass($modelInstance);
-            } catch (\Exception $e) {
+            } catch (Exception $e) {
                 // If it is a ResourceCollection class, it might throw an error
                 // when trying to instantiate with something other than a collection
                 $resource = new $apiResourceClass(collect([$modelInstance]));
@@ -89,12 +89,12 @@ class UseApiResourceTags extends Strategy
                     'content' => $response->getContent(),
                 ],
             ];
-        } catch (\Exception $e) {
-            echo 'Exception thrown when fetching Eloquent API resource response for [' . implode(',', $route->methods) . "] {$route->uri}.\n";
+        } catch (Exception $e) {
+            clara('knuckleswtf/scribe')->warn('Exception thrown when fetching Eloquent API resource response for [' . implode(',', $route->methods) . "] {$route->uri}.");
             if (Flags::$shouldBeVerbose) {
                 Utils::dumpException($e);
             } else {
-                echo "Run this again with the --verbose flag to see the exception.\n";
+                clara('knuckleswtf/scribe')->warn("Run this again with the --verbose flag to see the exception.");
             }
 
             return null;
@@ -151,9 +151,9 @@ class UseApiResourceTags extends Strategy
             $type = ltrim($type, '\\');
 
             return factory($type)->make();
-        } catch (\Exception $e) {
+        } catch (Exception $e) {
             if (Flags::$shouldBeVerbose) {
-                echo "Eloquent model factory failed to instantiate {$type}; trying to fetch from database.\n";
+                clara('knuckleswtf/scribe')->warn("Eloquent model factory failed to instantiate {$type}; trying to fetch from database.");
             }
 
             $instance = new $type();
@@ -164,10 +164,10 @@ class UseApiResourceTags extends Strategy
                     if ($firstInstance) {
                         return $firstInstance;
                     }
-                } catch (\Exception $e) {
+                } catch (Exception $e) {
                     // okay, we'll stick with `new`
                     if (Flags::$shouldBeVerbose) {
-                        echo "Failed to fetch first {$type} from database; using `new` to instantiate.\n";
+                        clara('knuckleswtf/scribe')->warn("Failed to fetch first {$type} from database; using `new` to instantiate.");
                     }
                 }
             }

+ 4 - 4
src/Extracting/Strategies/Responses/UseTransformerTags.php

@@ -85,11 +85,11 @@ class UseTransformerTags extends Strategy
                 ],
             ];
         } catch (Exception $e) {
-            echo 'Exception thrown when fetching transformer response for [' . implode(',', $route->methods) . "] {$route->uri}.\n";
+            clara('knuckleswtf/scribe')->warn('Exception thrown when fetching transformer response for [' . implode(',', $route->methods) . "] {$route->uri}.");
             if (Flags::$shouldBeVerbose) {
                 Utils::dumpException($e);
             } else {
-                echo "Run this again with the --verbose flag to see the exception.\n";
+                clara('knuckleswtf/scribe')->warn("Run this again with the --verbose flag to see the exception.");
             }
 
             return null;
@@ -160,7 +160,7 @@ class UseTransformerTags extends Strategy
             return factory($type)->make();
         } catch (Exception $e) {
             if (Flags::$shouldBeVerbose) {
-                echo "Eloquent model factory failed to instantiate {$type}; trying to fetch from database.\n";
+                clara('knuckleswtf/scribe')->warn("Eloquent model factory failed to instantiate {$type}; trying to fetch from database.");
             }
 
             $instance = new $type();
@@ -174,7 +174,7 @@ class UseTransformerTags extends Strategy
                 } catch (Exception $e) {
                     // okay, we'll stick with `new`
                     if (Flags::$shouldBeVerbose) {
-                        echo "Failed to fetch first {$type} from database; using `new` to instantiate.\n";
+                        clara('knuckleswtf/scribe')->warn("Failed to fetch first {$type} from database; using `new` to instantiate.");
                     }
                 }
             }

+ 5 - 3
src/Tools/Utils.php

@@ -2,6 +2,8 @@
 
 namespace Knuckles\Scribe\Tools;
 
+use Closure;
+use Exception;
 use Illuminate\Routing\Route;
 use League\Flysystem\Adapter\Local;
 use League\Flysystem\Filesystem;
@@ -83,7 +85,7 @@ class Utils
         return $uri;
     }
 
-    public static function dumpException(\Exception $e)
+    public static function dumpException(Exception $e)
     {
         if (class_exists(\NunoMaduro\Collision\Handler::class)) {
             $output = new ConsoleOutput(OutputInterface::VERBOSITY_VERBOSE);
@@ -93,7 +95,7 @@ class Utils
             $handler->handle();
         } else {
             dump($e);
-            echo "You can get better exception output by installing the library \nunomaduro/collision (PHP 7.1+ only).\n";
+            clara('knuckleswtf/scribe')->info("You can get better exception output by installing the library nunomaduro/collision.");
         }
     }
 
@@ -203,7 +205,7 @@ class Utils
     {
         [$class, $method] = $routeControllerAndMethod;
 
-        if ($class instanceof \Closure) {
+        if ($class instanceof Closure) {
             return new ReflectionFunction($class);
         }