1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MedianetDev\PConnector\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
|
7
|
|
|
class ProfileFromUrl extends Command |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The name and signature of the console command. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $signature = 'pconnector:generate {url : URL api} {profile? : Name of profile}'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The console command description. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $description = 'Generate a PConnector profile'; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Execute the console command. |
27
|
|
|
* |
28
|
|
|
* @return bool|null |
29
|
|
|
*/ |
30
|
|
|
public function handle() |
31
|
|
|
{ |
32
|
|
|
$url = $this->argument('url'); |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
// Validate the Url |
36
|
|
|
if (filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED) === false) { |
37
|
|
|
return $this->error('Not a valid URL!'); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// Publish the config |
41
|
|
|
\Artisan::call('vendor:publish', ['--provider' => 'MedianetDev\PConnector\PConnectorServiceProvider', '--tag' => 'config']); |
42
|
|
|
|
43
|
|
|
// Extract profile from url |
44
|
|
|
$gateway = parse_url($url); |
|
|
|
|
45
|
|
|
|
46
|
|
|
$profiles = config('p-connector.profiles'); |
47
|
|
|
$profile_name = $this->argument('profile') ?? 'profile'; |
48
|
|
|
|
49
|
|
|
// Check existing profile name |
50
|
|
|
if (isset($profiles[$profile_name])) { |
51
|
|
|
return $this->error('Please check your profile name'); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Merge profiles |
55
|
|
|
$profiles = $profiles + |
56
|
|
|
[ |
57
|
|
|
$profile_name => [ |
58
|
|
|
'protocol' => $gateway['scheme'] ?? 'http', |
59
|
|
|
'host' => $gateway['host'] ?? 'foo.bar', |
60
|
|
|
'port' => $gateway['port'] ?? 80, |
61
|
|
|
'prefix' => $gateway['path'] ?? '/', |
62
|
|
|
], |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
// Add profile into config |
66
|
|
|
$path = config_path('p-connector.php'); |
67
|
|
|
config(['p-connector.profiles' => $profiles]); |
68
|
|
|
|
69
|
|
|
// Set new Config |
70
|
|
|
if (file_exists($path)) { |
71
|
|
|
file_put_contents($path, "<?php \n return \n {$this->var_export_format(config('p-connector'))};"); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->info('Profile added successfully.'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function var_export_format($var, $indent="") |
78
|
|
|
{ |
79
|
|
|
switch (gettype($var)) { |
80
|
|
|
case 'string': |
81
|
|
|
return '"'.addcslashes($var, "\\\$\"\r\n\t\v\f").'"'; |
82
|
|
|
case 'array': |
83
|
|
|
$indexed = array_keys($var) === range(0, count($var) - 1); |
84
|
|
|
$r = []; |
85
|
|
|
foreach ($var as $key => $value) { |
86
|
|
|
$r[] = "$indent " |
87
|
|
|
.($indexed ? '' : $this->var_export_format($key).' => ') |
88
|
|
|
.$this->var_export_format($value, "$indent "); |
89
|
|
|
} |
90
|
|
|
return "[\n".implode(",\n", $r)."\n".$indent.']'; |
91
|
|
|
case 'boolean': |
92
|
|
|
return $var ? 'TRUE' : 'FALSE'; |
93
|
|
|
case 'integer': case 'double': return $var; |
94
|
|
|
default: |
95
|
|
|
return var_export($var, true); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.