Skip to content

Commit d173e24

Browse files
committed
up: add new method for find bin file
1 parent b353979 commit d173e24

File tree

8 files changed

+109
-26
lines changed

8 files changed

+109
-26
lines changed

.github/workflows/php.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
strategy:
1616
fail-fast: true
1717
matrix:
18-
php: [7.3, 7.4] # 7.2,
18+
php: [7.2, 7.3, 7.4] #
1919
os: [ubuntu-latest, macOS-latest] # windows-latest,
2020

2121
steps:
@@ -49,4 +49,4 @@ jobs:
4949
# Docs: https://getcomposer.org/doc/articles/scripts.md
5050

5151
- name: Run test suite
52-
run: echo 'hello'
52+
run: phpunit -vv

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ composer.lock
77
*.log
88
*.pid
99
*.patch
10+
*.cache
1011
.DS_Store

phpunit.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="test/bootstrap.php" colors="false" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory suffix=".php">src</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="Php Library Test Suite">
10+
<directory>test/</directory>
11+
</testsuite>
12+
</testsuites>
13+
</phpunit>

phpunit.xml.dist

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/Sys.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
use function exec;
1616
use function function_exists;
1717
use function implode;
18+
use function is_file;
1819
use function ob_start;
1920
use function preg_match;
2021
use function preg_replace;
2122
use function shell_exec;
2223
use function system;
2324
use function trim;
25+
use const DIRECTORY_SEPARATOR;
2426

2527
/**
2628
* Class Sys
@@ -281,4 +283,43 @@ public static function getMemUsage(string $program)
281283

282284
return exec('ps aux | grep ' . $program . ' | grep -v grep | grep -v su | awk {"print $4"}');
283285
}
286+
287+
/**
288+
* find executable file by input
289+
*
290+
* Usage:
291+
*
292+
* ```php
293+
* $phpBin = Sys::findExecutable('php');
294+
* echo $phpBin; // "/usr/bin/php"
295+
* ```
296+
*
297+
* @param string $name
298+
* @param array $paths The dir paths for find bin file. if empty, will read from env $PATH
299+
*
300+
* @return string
301+
*/
302+
public static function findExecutable(string $name, array $paths = []): string
303+
{
304+
$paths = $paths ?: self::getEnvPaths();
305+
306+
foreach ($paths as $path) {
307+
$filename = $path . DIRECTORY_SEPARATOR . $name;
308+
if (is_file($filename)) {
309+
return $filename;
310+
}
311+
}
312+
return "";
313+
}
314+
315+
/**
316+
* @param string $name
317+
* @param array $paths
318+
*
319+
* @return bool
320+
*/
321+
public static function isExecutable(string $name, array $paths = []): bool
322+
{
323+
return self::findExecutable($name, $paths) !== "";
324+
}
284325
}

src/SysEnv.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Toolkit\Stdlib\OS;
1313
use function defined;
14+
use function explode;
1415
use function getenv;
1516
use function putenv;
1617

@@ -71,4 +72,18 @@ public static function setEnv(string $key, $value): bool
7172
{
7273
return putenv($key . '=' . $value);
7374
}
75+
76+
/**
77+
* @return array
78+
*/
79+
public static function getEnvPaths(): array
80+
{
81+
$pathStr = $_SERVER['PATH'] ?? '';
82+
if (!$pathStr) {
83+
return [];
84+
}
85+
86+
$sepChar = self::isWindows() ? ';' : ':';
87+
return explode($sepChar, $pathStr);
88+
}
7489
}

test/SysTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\SysTest;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Toolkit\Sys\Sys;
7+
use function vdump;
8+
9+
/**
10+
* Class SysTest
11+
*
12+
* @package Toolkit\SysTest
13+
*/
14+
class SysTest extends TestCase
15+
{
16+
public function testGetEnvPaths(): void
17+
{
18+
self::assertNotEmpty($paths = Sys::getEnvPaths());
19+
20+
vdump($paths);
21+
}
22+
23+
public function testFindExecutable(): void
24+
{
25+
self::assertTrue(Sys::isExecutable('php'));
26+
self::assertNotEmpty($path = Sys::findExecutable('php'));
27+
vdump($path);
28+
self::assertEmpty(Sys::findExecutable('php', ['not-exist-dir']));
29+
self::assertFalse(Sys::isExecutable('php', ['not-exist-dir']));
30+
}
31+
}

test/bootstrap.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,9 @@
2929
include $file;
3030
}
3131
});
32+
33+
if (is_file(dirname(__DIR__, 3) . '/autoload.php')) {
34+
require dirname(__DIR__, 3) . '/autoload.php';
35+
} elseif (is_file(dirname(__DIR__) . '/vendor/autoload.php')) {
36+
require dirname(__DIR__) . '/vendor/autoload.php';
37+
}

0 commit comments

Comments
 (0)