Skip to content

Commit eea510d

Browse files
committed
👔 up: add more helper method for FS and FTB, add ci on php8.3
1 parent f5d0a8f commit eea510d

File tree

5 files changed

+97
-49
lines changed

5 files changed

+97
-49
lines changed

.github/workflows/php.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
strategy:
1111
fail-fast: true
1212
matrix:
13-
php: [8.1, 8.2] #
13+
php: [8.1, 8.2, 8.3] #
1414
os: [ubuntu-latest, macOS-latest] # windows-latest,
1515
# include:
1616
# - os: 'ubuntu-latest'

phpunit.xml

+12-22
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
3-
<phpunit backupGlobals="false"
4-
backupStaticAttributes="false"
5-
bootstrap="test/bootstrap.php"
6-
colors="false"
7-
convertErrorsToExceptions="true"
8-
convertNoticesToExceptions="true"
9-
convertWarningsToExceptions="true"
10-
stopOnFailure="false"
11-
12-
>
13-
<testsuites>
14-
<testsuite name="Php Library Test Suite">
15-
<directory>test</directory>
16-
</testsuite>
17-
</testsuites>
18-
19-
<filter>
20-
<whitelist>
21-
<directory suffix=".php">src</directory>
22-
</whitelist>
23-
</filter>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="test/bootstrap.php" colors="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
3+
<coverage/>
4+
<testsuites>
5+
<testsuite name="Php Library Test Suite">
6+
<directory>test</directory>
7+
</testsuite>
8+
</testsuites>
9+
<source>
10+
<include>
11+
<directory suffix=".php">src</directory>
12+
</include>
13+
</source>
2414
</phpunit>

src/Extra/FileTreeBuilder.php

+48-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function copy(string $srcFile, string $dstFile, ?callable $afterFn = null
153153
* @param array $options = [
154154
* 'include' => [], // limit copy files or dirs
155155
* 'exclude' => [], // exclude files or dirs
156-
* 'renderOn' => ['*.java', ], // patterns to render
156+
* 'renderOn' => ['*.java', ], // patterns to render file
157157
* 'afterFn' => function(string $newFile) {},
158158
* ]
159159
*
@@ -450,6 +450,18 @@ public function renderFile(string $tplFile, array $tplVars = []): static
450450
return $this->tplFile($tplFile, '', $tplVars);
451451
}
452452

453+
/**
454+
* Render template vars in the give files, will update file contents to rendered.
455+
*
456+
* @param string ...$tplFiles
457+
*
458+
* @return $this
459+
*/
460+
public function renderFiles(string ...$tplFiles): static
461+
{
462+
return $this->tplFiles($tplFiles);
463+
}
464+
453465
/**
454466
* Create files from template files
455467
*
@@ -640,6 +652,7 @@ protected function renderPathVars(string $path): string
640652
'workdir' => $this->workdir,
641653
]);
642654

655+
$vars['projectDir'] = $this->baseDir;
643656
return Str::renderVars($path, $vars, '{%s}', true);
644657
}
645658

@@ -756,6 +769,40 @@ public function setTplVars(array $tplVars): self
756769
return $this;
757770
}
758771

772+
/**
773+
* @param array $tplVars
774+
*
775+
* @return $this
776+
*/
777+
public function addTplVars(array $tplVars): self
778+
{
779+
$this->tplVars = array_merge($this->tplVars, $tplVars);
780+
return $this;
781+
}
782+
783+
/**
784+
* @param string $key
785+
* @param mixed $value
786+
*
787+
* @return $this
788+
*/
789+
public function setTplVar(string $key, mixed $value): self
790+
{
791+
$this->tplVars[$key] = $value;
792+
return $this;
793+
}
794+
795+
/**
796+
* @param string $key
797+
* @param mixed|null $default
798+
*
799+
* @return mixed
800+
*/
801+
public function getTplVar(string $key, mixed $default = null): mixed
802+
{
803+
return $this->tplVars[$key] ?? $default;
804+
}
805+
759806
/**
760807
* @param callable(string $newFile): void $afterCopy
761808
*

src/FileSystem.php

+12-12
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,7 @@ public static function isExclude(string $path, array $patterns): bool
106106
if (!$patterns) {
107107
return false;
108108
}
109-
110-
foreach ($patterns as $pattern) {
111-
if ($pattern === '*' || $pattern === '**/*') {
112-
return true;
113-
}
114-
115-
if (fnmatch($pattern, $path)) {
116-
return true;
117-
}
118-
}
119-
return false;
109+
return self::isMatch($path, $patterns);
120110
}
121111

122112
/**
@@ -130,13 +120,23 @@ public static function isInclude(string $path, array $patterns): bool
130120
if (!$patterns) {
131121
return true;
132122
}
123+
return self::isMatch($path, $patterns);
124+
}
133125

126+
/**
127+
* @param string $path
128+
* @param array $patterns
129+
*
130+
* @return bool
131+
*/
132+
public static function isMatch(string $path, array $patterns): bool
133+
{
134134
foreach ($patterns as $pattern) {
135135
if ($pattern === '*' || $pattern === '**/*') {
136136
return true;
137137
}
138138

139-
if (fnmatch($pattern, $path)) {
139+
if ($pattern === $path || fnmatch($pattern, $path)) {
140140
return true;
141141
}
142142
}

test/FsTest.php

+24-13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPUnit\Framework\TestCase;
66
use Toolkit\FsUtil\FS;
7+
use Toolkit\Stdlib\OS;
78
use function strlen;
89
use function vdump;
910

@@ -37,32 +38,42 @@ public function testBasicFsMethods(): void
3738
{
3839
// join
3940
$this->assertEquals('/ab', FS::join('/ab'));
40-
$this->assertEquals('/ab/d', FS::join('/ab', '', 'd'));
41-
$this->assertEquals('/ab/d/e', FS::join('/ab', 'd', 'e'));
4241
$this->assertEquals('/ab', FS::join('/ab', '.'));
4342
$this->assertEquals('/ab', FS::join('/ab', './'));
44-
$this->assertEquals('/ab/cd', FS::join('/ab', './cd'));
43+
if (OS::isWindows()) {
44+
$this->assertEquals('/ab\\d', FS::join('/ab', '', 'd'));
45+
$this->assertEquals('/ab\\d\\e', FS::join('/ab', 'd', 'e'));
46+
$this->assertEquals('/ab\\cd', FS::join('/ab', './cd'));
47+
} else {
48+
$this->assertEquals('/ab/d', FS::join('/ab', 'd'));
49+
$this->assertEquals('/ab/d/e', FS::join('/ab', 'd', 'e'));
50+
$this->assertEquals('/ab/cd', FS::join('/ab', './cd'));
51+
}
52+
4553
}
4654

4755
public function testIsExclude_isInclude(): void
4856
{
49-
$this->assertTrue(FS::isInclude('./abc.php', []));
50-
$this->assertTrue(FS::isInclude('./abc.php', ['*']));
51-
$this->assertTrue(FS::isInclude('./abc.php', ['*.php']));
52-
$this->assertTrue(FS::isInclude('path/to/abc.php', ['*.php']));
53-
$this->assertFalse(FS::isInclude('./abc.php', ['*.xml']));
57+
$tests = [
58+
['./abc.php', '*', true],
59+
['./abc.php', '*.php', true],
60+
['./abc.php', '*.yml', false],
61+
['path/to/abc.php', '*.php', true],
62+
];
63+
foreach ($tests as $item) {
64+
$this->assertEquals($item[2], FS::isInclude($item[0], [$item[1]]));
65+
$this->assertEquals($item[2], FS::isExclude($item[0], [$item[1]]));
66+
$this->assertEquals($item[2], FS::isMatch($item[0], [$item[1]]));
67+
}
5468

69+
$this->assertTrue(FS::isInclude('./abc.php', []));
5570
$this->assertFalse(FS::isExclude('./abc.php', []));
56-
$this->assertTrue(FS::isExclude('./abc.php', ['*']));
57-
$this->assertTrue(FS::isExclude('./abc.php', ['*.php']));
58-
$this->assertTrue(FS::isExclude('path/to/abc.php', ['*.php']));
59-
$this->assertFalse(FS::isExclude('./abc.php', ['*.yml']));
6071
}
6172

6273
public function testRealpath(): void
6374
{
6475
$rPaths = [];
65-
$tests = [
76+
$tests = [
6677
'~',
6778
'~/.kite',
6879
];

0 commit comments

Comments
 (0)