Skip to content

Commit c1b11d2

Browse files
committed
封装方法
1 parent 2d96e25 commit c1b11d2

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

src/Command/Install.php

+5-21
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Symfony\Component\Console\Output\OutputInterface;
1212
use Symfony\Component\Process\Exception\ProcessFailedException;
1313
use Symfony\Component\Process\Process;
14+
use function process;
1415

1516
class Install extends Command
1617
{
@@ -51,29 +52,13 @@ protected function checkOs()
5152
{
5253
if (file_exists('/usr/bin/yum')) {
5354
$pm = 'yum';
54-
$process = new Process('command -v lsb_release >/dev/null 2>&1 || { yum -y install redhat-lsb-core; }');//todo 给予当前用户
55-
try {
56-
$process->mustRun();
57-
} catch (\Exception $e) {
58-
throw new RuntimeException($process->getErrorOutput());
59-
}
55+
process('command -v lsb_release >/dev/null 2>&1 || { yum -y install redhat-lsb-core; }');
6056
} else if (file_exists('/usr/bin/apt-get')) {
6157
$pm = 'apt-get';
62-
$process = new Process('command -v lsb_release >/dev/null 2>&1 || { yum -y install redhat-lsb-core; }');//todo 给予当前用户
63-
try {
64-
$process->mustRun();
65-
} catch (\Exception $e) {
66-
throw new RuntimeException($process->getErrorOutput());
67-
}
58+
process('command -v lsb_release >/dev/null 2>&1 || { yum -y install redhat-lsb-core; }');
6859
}
60+
process('command -v lsb_release >/dev/null 2>&1 || { echo >&2 "Fail to use lsb_release!"; exit 1; }');
6961
//
70-
$process = new Process('command -v lsb_release >/dev/null 2>&1 || { echo >&2 "Fail to use lsb_release!"; exit 1; }');
71-
try {
72-
$process->mustRun();
73-
} catch (\Exception $e) {
74-
throw new RuntimeException($process->getErrorOutput());
75-
}
76-
7762

7863
// if (file_exists('/etc/redhat-release')) {
7964
// //RedHat,Centos
@@ -101,8 +86,7 @@ protected function checkCmakeOrInstall(OutputInterface $output)
10186
//检测系统
10287
//如果是ubuntu,直接apt-get
10388
//如果是centos则安装编译安装cmake
104-
$process = new Process('sudo apt-get install -y cmake');
105-
$process->mustRun();
89+
process('sudo apt-get install -y cmake');
10690
}
10791

10892
}

src/helpers.php

+19
Original file line numberDiff line numberDiff line change
@@ -1 +1,20 @@
11
<?php declare(strict_types=1);
2+
3+
use Symfony\Component\Process\Process;
4+
use Symfony\Component\Console\Exception\RuntimeException;
5+
6+
7+
if (!function_exists('process')) {
8+
/**
9+
* @param $command
10+
*/
11+
function process($command)
12+
{
13+
$process = new Process($command);
14+
try {
15+
$process->mustRun();
16+
} catch (\Exception $e) {
17+
throw new RuntimeException($process->getErrorOutput());
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)