1
|
|
|
<?php |
2
|
|
|
namespace SmoothPhp\LaravelAdapter\Console; |
3
|
|
|
|
4
|
|
|
use Illuminate\Config\Repository; |
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class EventStoreBranchSwap |
9
|
|
|
* @package SmoothPhp\LaravelAdapter\Console |
10
|
|
|
* @author Simon Bennett <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
final class EventStoreBranchSwap extends Command |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The name and signature of the console command. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $signature = 'smoothphp:swapbranch'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The console command description. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $description = 'Command description'; |
28
|
|
|
|
29
|
|
|
/** @var Repository */ |
30
|
|
|
private $config; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Create a new command instance. |
34
|
|
|
* |
35
|
|
|
* @param Repository $config |
36
|
|
|
*/ |
37
|
|
|
public function __construct(Repository $config) |
38
|
|
|
{ |
39
|
|
|
parent::__construct(); |
40
|
|
|
$this->config = $config; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Execute the console command. |
45
|
|
|
* |
46
|
|
|
* @return mixed |
47
|
|
|
*/ |
48
|
|
|
public function handle() |
49
|
|
|
{ |
50
|
|
|
// Get current branch name |
51
|
|
|
|
52
|
|
|
//swap the confection |
53
|
|
|
// fouse rebuild |
54
|
|
|
|
55
|
|
|
//save config |
56
|
|
|
$branch = $this->getGitBranch(); |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
$this->replaceEnvConfig($branch); |
60
|
|
|
|
61
|
|
|
if (!\Schema::connection($this->config->get('cqrses.eventstore_connection'))->hasTable($branch)) { |
62
|
|
|
$this->call('smoothphp:buildeventstore', ['--force' => true]); |
63
|
|
|
} |
64
|
|
|
$this->call('smoothphp:rebuild'); |
65
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function getGitBranch() |
69
|
|
|
{ |
70
|
|
|
$shellOutput = []; |
71
|
|
|
exec('git branch | ' . "grep ' * '", $shellOutput); |
72
|
|
|
foreach ($shellOutput as $line) { |
|
|
|
|
73
|
|
|
if (strpos($line, '* ') !== false) { |
74
|
|
|
return trim(strtolower(str_replace(['* ', '/'], ['', '-'], $line))); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return null; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param $branch |
83
|
|
|
*/ |
84
|
|
|
protected function replaceEnvConfig($branch) |
85
|
|
|
{ |
86
|
|
|
$envFilePath = base_path() . '/.env'; |
87
|
|
|
|
88
|
|
|
$rebuildFunction = function ($data) use ($branch) { |
89
|
|
|
if (stristr($data, 'DB_TABLE_EVENTSTORE')) { |
90
|
|
|
return "DB_TABLE_EVENTSTORE={$branch}\n"; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $data; |
94
|
|
|
}; |
95
|
|
|
|
96
|
|
|
$contentArray = array_map($rebuildFunction,file($envFilePath)); |
97
|
|
|
|
98
|
|
|
file_put_contents($envFilePath, implode('',$contentArray)); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.