1
|
|
|
<?php namespace Pz\LaravelDoctrine\Rest; |
2
|
|
|
|
3
|
|
|
use App\JsonApiHydrator; |
|
|
|
|
4
|
|
|
use Doctrine\ORM\EntityManager; |
5
|
|
|
use Illuminate\Http\Response; |
6
|
|
|
use Illuminate\Routing\Controller; |
7
|
|
|
use League\Fractal\TransformerAbstract; |
8
|
|
|
|
9
|
|
|
use pmill\Doctrine\Hydrator\ArrayHydrator; |
10
|
|
|
use Pz\Doctrine\Rest\Action\DeleteAction; |
11
|
|
|
use Pz\Doctrine\Rest\Action\IndexAction; |
12
|
|
|
use Pz\Doctrine\Rest\Action\ShowAction; |
13
|
|
|
use Pz\Doctrine\Rest\Action\CreateAction; |
14
|
|
|
use Pz\Doctrine\Rest\Action\UpdateAction; |
15
|
|
|
|
16
|
|
|
use Pz\Doctrine\Rest\RestResponseFactory; |
17
|
|
|
use Pz\LaravelDoctrine\Rest\Request\CreateRestRequest; |
18
|
|
|
use Pz\LaravelDoctrine\Rest\Request\UpdateRestRequest; |
19
|
|
|
|
20
|
|
|
abstract class RestController extends Controller |
21
|
|
|
{ |
22
|
|
|
use IndexAction; |
23
|
|
|
use ShowAction; |
24
|
|
|
use DeleteAction; |
25
|
|
|
use CreateAction; |
26
|
|
|
use UpdateAction; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var EntityManager |
30
|
|
|
*/ |
31
|
|
|
protected $em; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var RestResponse|RestResponseFactory |
35
|
|
|
*/ |
36
|
|
|
protected $response; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return TransformerAbstract |
40
|
|
|
*/ |
41
|
|
|
abstract public function transformer(); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* UserController constructor. |
45
|
|
|
* |
46
|
|
|
* @param EntityManager $em |
47
|
|
|
* @param RestResponse $response |
48
|
|
|
*/ |
49
|
4 |
|
public function __construct(EntityManager $em, RestResponse $response) |
50
|
|
|
{ |
51
|
4 |
|
$this->em = $em; |
52
|
4 |
|
$this->response = $response; |
53
|
|
|
|
54
|
4 |
|
$this->middleware(function($request, \Closure $next) { |
55
|
|
|
try { |
56
|
|
|
/** @var Response $response */ |
57
|
4 |
|
$response = $next($request); |
58
|
|
|
|
59
|
4 |
|
if (isset($response->exception) && $response->exception instanceof \Exception) { |
60
|
4 |
|
return $this->response()->exception($response->exception); |
61
|
|
|
} |
62
|
|
|
|
63
|
4 |
|
} catch (\Exception $e) { |
64
|
4 |
|
return $this->response()->exception($e); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $response; |
68
|
4 |
|
}); |
69
|
4 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param CreateRestRequest $request |
73
|
|
|
* |
74
|
|
|
* @return object |
75
|
|
|
* @throws \Exception |
76
|
|
|
*/ |
77
|
|
|
public function createEntity($request) |
78
|
|
|
{ |
79
|
|
|
return $this->hydrator($request) |
80
|
|
|
->hydrate($this->repository()->getClassName(), $request->all()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param UpdateRestRequest $request |
85
|
|
|
* @param $entity |
86
|
|
|
* |
87
|
|
|
* @return object |
88
|
|
|
* @throws \Exception |
89
|
|
|
*/ |
90
|
|
|
public function updateEntity($request, $entity) |
91
|
|
|
{ |
92
|
|
|
return $this->hydrator($request) |
|
|
|
|
93
|
|
|
->hydrate($entity, $request->all()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return RestResponse |
98
|
|
|
*/ |
99
|
4 |
|
public function response() |
100
|
|
|
{ |
101
|
4 |
|
return $this->response->transformer($this->transformer()); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return ArrayHydrator |
106
|
|
|
*/ |
107
|
|
|
public function hydrator(RestRequest $request) |
108
|
|
|
{ |
109
|
|
|
if ($request->isJsonApi()) { |
110
|
|
|
return new JsonApiHydrator($this->em); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return new ArrayHydrator($this->em); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths