|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mtolhuys\LaravelSchematics\Actions\Relation; |
|
4
|
|
|
|
|
5
|
|
|
use ReflectionClass; |
|
6
|
|
|
use Config; |
|
7
|
|
|
|
|
8
|
|
|
use Mtolhuys\LaravelSchematics\Actions\Relation\CreateRelationAction; |
|
9
|
|
|
|
|
10
|
|
|
class CreatePivotRelationsAction extends CreateRelationAction |
|
11
|
|
|
{ |
|
12
|
|
|
public function execute($request) |
|
13
|
|
|
{ |
|
14
|
|
|
$stub = __DIR__ . '/../../../resources/stubs/relation.stub'; |
|
15
|
|
|
|
|
16
|
|
|
$this->createRelation( |
|
17
|
|
|
$stub, |
|
18
|
|
|
$request['pivot']['source'], |
|
19
|
|
|
$request['pivot']['target'], |
|
20
|
|
|
" , '" . $request['pivot']['foreignKeys']['second'] . "' , '" . $request['pivot']['foreignKeys']['first'] . "'", |
|
21
|
|
|
$request['pivot']['methods']['first'], |
|
22
|
|
|
'BelongsToMany', |
|
23
|
|
|
Config::get('schematics.model.namespace') . $request['name'] |
|
24
|
|
|
); |
|
25
|
|
|
|
|
26
|
|
|
$this->createRelation( |
|
27
|
|
|
$stub, |
|
28
|
|
|
$request['pivot']['target'], |
|
29
|
|
|
$request['pivot']['source'], |
|
30
|
|
|
" , '" . $request['pivot']['foreignKeys']['first'] . "' , '" . $request['pivot']['foreignKeys']['second'] . "'", |
|
31
|
|
|
$request['pivot']['methods']['second'], |
|
32
|
|
|
'BelongsToMany', |
|
33
|
|
|
Config::get('schematics.model.namespace') . $request['name'] |
|
34
|
|
|
); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
//TODO: should be place in parent so it can also be used there |
|
38
|
|
|
private function createRelation($stub, $source, $target, $keys, $methodName, $type, $pivotModelName = null) |
|
39
|
|
|
{ |
|
40
|
|
|
$file = (new ReflectionClass($source))->getFileName(); |
|
41
|
|
|
$lines = file($file, FILE_IGNORE_NEW_LINES); |
|
42
|
|
|
$injectionLine = $this->endOfClass($file) - 1; |
|
43
|
|
|
$pivotTable = App($pivotModelName)->getTable(); |
|
|
|
|
|
|
44
|
|
|
$generateMethodParams = [ |
|
45
|
|
|
'type' => $type, |
|
46
|
|
|
'source' => $source, |
|
47
|
|
|
'target' => $target, |
|
48
|
|
|
'keys'=> $keys, |
|
49
|
|
|
'method' => ['name' => $methodName], |
|
50
|
|
|
'pivot' => $pivotModelName, |
|
51
|
|
|
]; |
|
52
|
|
|
$lines[$injectionLine] = PHP_EOL . $this->generateMethod($generateMethodParams, $stub) . '}'; |
|
53
|
|
|
file_put_contents($file, implode("\n", $lines)); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.