/
PublishConfigCommand.php
86 lines (72 loc) · 2.41 KB
/
PublishConfigCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace Sentry\Laravel;
use Illuminate\Console\Command;
class PublishConfigCommand extends Command
{
/**
* Laravel 5.0.x: The name and signature of the console command.
*
* @var string
*/
protected $name = 'sentry:publish';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sentry:publish';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Publishes the Sentry Config';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->info('[Sentry] Publishing config ...');
$this->call('vendor:publish', [
'--provider' => 'Sentry\Laravel\ServiceProvider'
]);
if ($this->confirm('Enable Performance Monitoring?', true)) {
$this->setEnvironmentValue(['SENTRY_TRACES_SAMPLE_RATE' => 1.0]);
$this->info('[Sentry] Added `SENTRY_TRACES_SAMPLE_RATE=1` to your .env file.');
$testCommandPrompt = 'Want to send a test Event & Transaction?';
$args = ['--transaction' => true];
} else {
$testCommandPrompt = 'Want to send a test Event?';
$args = [];
}
if ($this->confirm($testCommandPrompt, true)) {
$this->call('sentry:test', $args);
}
}
public function setEnvironmentValue(array $values)
{
$envFile = app()->environmentFilePath();
$str = file_get_contents($envFile);
if (count($values) > 0) {
foreach ($values as $envKey => $envValue) {
$str .= "\n"; // In case the searched variable is in the last line without \n
$keyPosition = strpos($str, "{$envKey}=");
$endOfLinePosition = strpos($str, "\n", $keyPosition);
$oldLine = substr($str, $keyPosition, $endOfLinePosition - $keyPosition);
// If key does not exist, add it
if (!$keyPosition || !$endOfLinePosition || !$oldLine) {
$str .= "{$envKey}={$envValue}\n";
} else {
$str = str_replace($oldLine, "{$envKey}={$envValue}", $str);
}
}
}
$str = substr($str, 0, -1);
if (!file_put_contents($envFile, $str)) {
return false;
}
return true;
}
}