Skip to content

Commit f06792c

Browse files
committed
merge the php-utils to the lib
1 parent 1247c03 commit f06792c

18 files changed

+1486
-319
lines changed

.php_cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
$header = <<<'EOF'
4+
This file is part of toolkit/sys-utils.
5+
6+
@author https://github.com/inhere
7+
@link https://github.com/php-toolkit/sys-utils
8+
@license MIT
9+
EOF;
10+
11+
return PhpCsFixer\Config::create()
12+
->setRiskyAllowed(true)
13+
->setRules([
14+
'@PSR2' => true,
15+
'array_syntax' => [
16+
'syntax' => 'short'
17+
],
18+
'list_syntax' => [
19+
'syntax' => 'short'
20+
],
21+
'class_attributes_separation' => true,
22+
'declare_strict_types' => true,
23+
'global_namespace_import' => [
24+
'import_constants' => true,
25+
'import_functions' => true,
26+
],
27+
'header_comment' => [
28+
'comment_type' => 'PHPDoc',
29+
'header' => $header,
30+
'separate' => 'bottom'
31+
],
32+
'no_unused_imports' => true,
33+
'single_quote' => true,
34+
'standardize_not_equals' => true,
35+
])
36+
->setFinder(
37+
PhpCsFixer\Finder::create()
38+
// ->exclude('test')
39+
->exclude('runtime')
40+
->exclude('vendor')
41+
->in(__DIR__)
42+
)
43+
->setUsingCache(false);

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
}
1818
],
1919
"require": {
20-
"php": ">7.1.0"
20+
"php": ">7.1.0",
21+
"toolkit/stdlib": "^1.0"
2122
},
2223
"autoload": {
2324
"psr-4": {

example/proc-editor.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of toolkit/sys-utils.
4+
*
5+
* @author https://github.com/inhere
6+
* @link https://github.com/php-toolkit/sys-utils
7+
* @license MIT
8+
*/
9+
10+
require dirname(__DIR__) . '/test/bootstrap.php';
11+
12+
$editor = 'vim';
13+
\Toolkit\Sys\Proc\ProcWrapper::runEditor($editor, 'test.txt');

example/proc0.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of toolkit/sys-utils.
4+
*
5+
* @author https://github.com/inhere
6+
* @link https://github.com/php-toolkit/sys-utils
7+
* @license MIT
8+
*/
9+
10+
require dirname(__DIR__) . '/test/bootstrap.php';

example/property_exists.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of toolkit/sys-utils.
4+
*
5+
* @author https://github.com/inhere
6+
* @link https://github.com/php-toolkit/sys-utils
7+
* @license MIT
8+
*/
9+
10+
class Some
11+
{
12+
public $prop0;
13+
14+
private $prop1;
15+
16+
protected $prop2;
17+
18+
/**
19+
* @return mixed
20+
*/
21+
public function getProp1()
22+
{
23+
return $this->prop1;
24+
}
25+
}
26+
27+
28+
echo "use class:\n";
29+
30+
echo 'public: ' . (property_exists(Some::class, 'prop0') ? 'Y' : 'N') . PHP_EOL;
31+
echo 'private: ' . (property_exists(Some::class, 'prop1') ? 'Y' : 'N') . PHP_EOL;
32+
echo 'protected: ' . (property_exists(Some::class, 'prop2') ? 'Y' : 'N') . PHP_EOL;
33+
echo "use object:\n";
34+
35+
$object = new Some();
36+
37+
echo 'public: ' . (property_exists($object, 'prop0') ? 'Y' : 'N') . PHP_EOL;
38+
echo 'private: ' . (property_exists($object, 'prop1') ? 'Y' : 'N') . PHP_EOL;
39+
echo 'protected: ' . (property_exists($object, 'prop2') ? 'Y' : 'N') . PHP_EOL;

src/Exec.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of toolkit/sys-utils.
4+
*
5+
* @author https://github.com/inhere
6+
* @link https://github.com/php-toolkit/sys-utils
7+
* @license MIT
8+
*/
9+
10+
namespace Toolkit\Sys;
11+
12+
use RuntimeException;
13+
use Toolkit\Sys\Proc\ProcWrapper;
14+
use function chdir;
15+
use function implode;
16+
use function ob_get_clean;
17+
use function ob_start;
18+
use function pclose;
19+
use function popen;
20+
use function system;
21+
use function exec;
22+
23+
/**
24+
* Class Exec
25+
*
26+
* @package Toolkit\Sys
27+
*/
28+
class Exec
29+
{
30+
/**
31+
* @param string $command
32+
* @param string $workDir
33+
* @param bool $outAsString
34+
*
35+
* @return array
36+
*/
37+
public static function exec(string $command, string $workDir = '', bool $outAsString = true): array
38+
{
39+
if ($workDir) {
40+
chdir($workDir);
41+
}
42+
43+
exec($command, $output, $status);
44+
45+
return [$status, $outAsString ? implode("\n", $output) : $output];
46+
}
47+
48+
/**
49+
* @param string $command
50+
* @param string $workDir
51+
*
52+
* @return array
53+
*/
54+
public static function system(string $command, string $workDir = ''): array
55+
{
56+
if ($workDir) {
57+
chdir($workDir);
58+
}
59+
60+
ob_start();
61+
system($command, $status);
62+
$output = ob_get_clean();
63+
64+
return [$status, $output];
65+
}
66+
67+
/**
68+
* @param string $command
69+
* @param string $workDir
70+
*
71+
* @return string|null
72+
*/
73+
public static function shellExec(string $command, string $workDir = ''): ?string
74+
{
75+
if ($workDir) {
76+
chdir($workDir);
77+
}
78+
79+
return shell_exec($command);
80+
}
81+
82+
/**
83+
* run a command in background
84+
*
85+
* @param string $cmd
86+
*/
87+
public static function bgExec(string $cmd): void
88+
{
89+
self::inBackground($cmd);
90+
}
91+
92+
/**
93+
* run a command in background
94+
*
95+
* @param string $cmd
96+
*/
97+
public static function inBackground(string $cmd): void
98+
{
99+
if (SysEnv::isWindows()) {
100+
pclose(popen('start /B ' . $cmd, 'r'));
101+
} else {
102+
exec($cmd . ' > /dev/null &');
103+
}
104+
}
105+
106+
/**
107+
* run a command. it is support windows
108+
*
109+
* @param string $command
110+
* @param string $cwd
111+
*
112+
* @return array [$code, $output, $error]
113+
* @throws RuntimeException
114+
*/
115+
public static function run(string $command, string $cwd = ''): array
116+
{
117+
return ProcWrapper::runCmd($command, $cwd);
118+
}
119+
}

src/PhpEnv.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of toolkit/sys-utils.
4+
*
5+
* @author https://github.com/inhere
6+
* @link https://github.com/php-toolkit/sys-utils
7+
* @license MIT
8+
*/
9+
10+
namespace Toolkit\Sys;
11+
12+
use Toolkit\Sys\Traits\PhpEnvTrait;
13+
use Toolkit\Stdlib\Helper\PhpHelper;
14+
15+
/**
16+
* Class Php - alias of the class PhpHelper
17+
*
18+
* @package Toolkit\Sys
19+
*/
20+
class PhpEnv
21+
{
22+
use PhpEnvTrait;
23+
}

src/Proc/ProcFunc.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of toolkit/sys-utils.
4+
*
5+
* @author https://github.com/inhere
6+
* @link https://github.com/php-toolkit/sys-utils
7+
* @license MIT
8+
*/
9+
10+
namespace Toolkit\Sys\Proc;
11+
12+
use RuntimeException;
13+
use function proc_close;
14+
use function proc_get_status;
15+
use function proc_open;
16+
use function proc_terminate;
17+
18+
/**
19+
* Class ProcFunc
20+
*
21+
* @package Toolkit\Sys\Proc
22+
*/
23+
class ProcFunc
24+
{
25+
/**
26+
* @param string $cmd
27+
* @param array $descriptorSpec
28+
* @param array $pipes
29+
* @param string $workDir
30+
* @param array $env
31+
* @param array $otherOptions
32+
*
33+
* @return resource
34+
* @see https://www.php.net/manual/en/function.proc-open.php
35+
*/
36+
public static function open(
37+
string $cmd,
38+
array $descriptorSpec,
39+
array &$pipes,
40+
string $workDir = '',
41+
array $env = [],
42+
array $otherOptions = []
43+
) {
44+
$process = proc_open($cmd, $descriptorSpec, $pipes, $workDir, $env, $otherOptions);
45+
46+
if (!is_resource($process)) {
47+
throw new RuntimeException("Can't open resource with proc_open.");
48+
}
49+
50+
return $process;
51+
}
52+
53+
/**
54+
* Get information about a process opened by `proc_open`
55+
*
56+
* @param resource $process
57+
*
58+
* @return array
59+
* @see https://www.php.net/manual/en/function.proc-get-status.php
60+
*/
61+
public static function getStatus($process): array
62+
{
63+
return (array)proc_get_status($process);
64+
}
65+
66+
/**
67+
* Close a process opened by `proc_open` and return the exit code of that process
68+
*
69+
* @param resource $process
70+
*
71+
* @return int
72+
* @see https://www.php.net/manual/en/function.proc-close.php
73+
*/
74+
public static function close($process): int
75+
{
76+
return proc_close($process);
77+
}
78+
79+
/**
80+
* Kills a process opened by `proc_open`
81+
*
82+
* @param resource $process
83+
*
84+
* @return bool
85+
* @see https://www.php.net/manual/en/function.proc-terminate.php
86+
*/
87+
public static function terminate($process): bool
88+
{
89+
return proc_terminate($process);
90+
}
91+
}

0 commit comments

Comments
 (0)