4
4
5
5
use Exception ;
6
6
use Illuminate \Database \Eloquent \Model ;
7
+ use Illuminate \Database \Eloquent \Model as IlluminateModel ;
7
8
use Illuminate \Routing \Route ;
8
9
use Illuminate \Support \Arr ;
9
10
use League \Fractal \Manager ;
@@ -34,7 +35,7 @@ class UseTransformerTags extends Strategy
34
35
*
35
36
* @return array|null
36
37
*/
37
- public function __invoke (Route $ route , \ ReflectionClass $ controller , \ ReflectionMethod $ method , array $ rulesToApply , array $ context = [])
38
+ public function __invoke (Route $ route , ReflectionClass $ controller , ReflectionMethod $ method , array $ rulesToApply , array $ context = [])
38
39
{
39
40
$ docBlocks = RouteDocBlocker::getDocBlocksFromRoute ($ route );
40
41
/** @var DocBlock $methodDocBlock */
@@ -47,6 +48,7 @@ public function __invoke(Route $route, \ReflectionClass $controller, \Reflection
47
48
* Get a response from the transformer tags.
48
49
*
49
50
* @param array $tags
51
+ * @param Route $route
50
52
*
51
53
* @return array|null
52
54
*/
@@ -57,7 +59,7 @@ protected function getTransformerResponse(array $tags, Route $route)
57
59
return null ;
58
60
}
59
61
60
- list ( $ statusCode , $ transformer) = $ this ->getStatusCodeAndTransformerClass ($ transformerTag );
62
+ [ $ statusCode , $ transformer] = $ this ->getStatusCodeAndTransformerClass ($ transformerTag );
61
63
$ model = $ this ->getClassToBeTransformed ($ tags , (new ReflectionClass ($ transformer ))->getMethod ('transform ' ));
62
64
$ modelInstance = $ this ->instantiateTransformerModel ($ model );
63
65
@@ -81,7 +83,7 @@ protected function getTransformerResponse(array $tags, Route $route)
81
83
'content ' => $ response ->getContent (),
82
84
],
83
85
];
84
- } catch (\ Exception $ e ) {
86
+ } catch (Exception $ e ) {
85
87
echo 'Exception thrown when fetching transformer response for [ ' .implode (', ' , $ route ->methods )."] {$ route ->uri }. \n" ;
86
88
if (Flags::$ shouldBeVerbose ) {
87
89
Utils::dumpException ($ e );
@@ -112,6 +114,8 @@ private function getStatusCodeAndTransformerClass($tag): array
112
114
* @param array $tags
113
115
* @param ReflectionMethod $transformerMethod
114
116
*
117
+ * @throws Exception
118
+ *
115
119
* @return string
116
120
*/
117
121
private function getClassToBeTransformed (array $ tags , ReflectionMethod $ transformerMethod ): string
@@ -125,9 +129,9 @@ private function getClassToBeTransformed(array $tags, ReflectionMethod $transfor
125
129
$ type = $ modelTag ->getContent ();
126
130
} else {
127
131
$ parameter = Arr::first ($ transformerMethod ->getParameters ());
128
- if ($ parameter ->hasType () && ! $ parameter ->getType ()->isBuiltin () && class_exists (( string ) $ parameter ->getType ())) {
132
+ if ($ parameter ->hasType () && ! $ parameter ->getType ()->isBuiltin () && class_exists ($ parameter ->getType ()-> getName ())) {
129
133
// Ladies and gentlemen, we have a type!
130
- $ type = ( string ) $ parameter ->getType ();
134
+ $ type = $ parameter ->getType ()-> getName ();
131
135
}
132
136
}
133
137
@@ -153,20 +157,20 @@ protected function instantiateTransformerModel(string $type)
153
157
$ type = ltrim ($ type , '\\' );
154
158
155
159
return factory ($ type )->make ();
156
- } catch (\ Exception $ e ) {
160
+ } catch (Exception $ e ) {
157
161
if (Flags::$ shouldBeVerbose ) {
158
162
echo "Eloquent model factory failed to instantiate {$ type }; trying to fetch from database. \n" ;
159
163
}
160
164
161
165
$ instance = new $ type ;
162
- if ($ instance instanceof \ Illuminate \ Database \ Eloquent \Model ) {
166
+ if ($ instance instanceof IlluminateModel ) {
163
167
try {
164
168
// we can't use a factory but can try to get one from the database
165
169
$ firstInstance = $ type ::first ();
166
170
if ($ firstInstance ) {
167
171
return $ firstInstance ;
168
172
}
169
- } catch (\ Exception $ e ) {
173
+ } catch (Exception $ e ) {
170
174
// okay, we'll stick with `new`
171
175
if (Flags::$ shouldBeVerbose ) {
172
176
echo "Failed to fetch first {$ type } from database; using `new` to instantiate. \n" ;
0 commit comments