Skip to content

Commit bec87ea

Browse files
authored
Add files via upload
1 parent 0bdeae1 commit bec87ea

File tree

7 files changed

+242
-13
lines changed

7 files changed

+242
-13
lines changed

src/vennv/vapm/CoroutineGen.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ public static function launch(callable $callback): Generator
6969
return yield $callback;
7070
}
7171

72-
private static function schedule(ChildCoroutine $ChildCoroutine): void
72+
private static function schedule(ChildCoroutine $childCoroutine): void
7373
{
74-
self::$taskQueue?->enqueue($ChildCoroutine);
74+
self::$taskQueue?->enqueue($childCoroutine);
7575
}
7676

7777
private static function run(): void
@@ -81,18 +81,18 @@ private static function run(): void
8181
while (!self::$taskQueue->isEmpty())
8282
{
8383
/**
84-
* @var ChildCoroutine $ChildCoroutine
84+
* @var ChildCoroutine $childCoroutine
8585
*/
86-
$ChildCoroutine = self::$taskQueue->dequeue();
87-
$ChildCoroutine->run();
86+
$childCoroutine = self::$taskQueue->dequeue();
87+
$childCoroutine->run();
8888

89-
if ($ChildCoroutine->isFinished())
89+
if ($childCoroutine->isFinished())
9090
{
9191
//TODO: Remove from queue
9292
}
9393
else
9494
{
95-
self::schedule($ChildCoroutine);
95+
self::schedule($childCoroutine);
9696
}
9797
}
9898
}

src/vennv/vapm/Info.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
class Info
3030
{
3131

32-
public const VERSION = "1.6.7";
32+
public const VERSION = "1.7.0";
3333

3434
public const AUTHOR = "VennV";
3535

src/vennv/vapm/Thread.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
use ReflectionException;
3131
use Throwable;
3232

33-
abstract class Thread implements ThreadInterface
33+
abstract class Thread extends Threaded implements ThreadInterface
3434
{
3535

3636
abstract public function onRun(): void;
@@ -58,7 +58,7 @@ public function start(array $mode = DescriptorSpec::BASIC): void
5858
$pathAutoLoad
5959
);
6060

61-
$command = 'php -r "require_once \'' . $pathAutoLoad . '\'; include \'' . $class . '\'; $class = new ' . static::class . '(); $class->run();"';
61+
$command = 'php -r "require_once \'' . $pathAutoLoad . '\'; include \'' . $class . '\'; $class = new ' . static::class . '(); $class->onRun();"';
6262

6363
$process = proc_open(
6464
$command,
@@ -71,10 +71,24 @@ public function start(array $mode = DescriptorSpec::BASIC): void
7171
stream_set_blocking($pipes[1], false);
7272
stream_set_blocking($pipes[2], false);
7373

74-
fclose($pipes[0]);
74+
$data = json_encode(self::getShared());
75+
76+
if (is_string($data))
77+
{
78+
fwrite($pipes[0], $data);
79+
fclose($pipes[0]);
80+
}
7581

7682
while (proc_get_status($process)['running'])
7783
{
84+
$status = proc_get_status($process);
85+
86+
$this->setPid($status['pid']);
87+
$this->setExitCode($status['exitcode']);
88+
$this->setRunning($status['running']);
89+
$this->setSignaled($status['signaled']);
90+
$this->setStopped($status['stopped']);
91+
7892
FiberManager::wait();
7993
}
8094

src/vennv/vapm/Threaded.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
/*
4+
* Copyright (c) 2023 VennV
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
declare(strict_types = 1);
26+
27+
namespace vennv\vapm;
28+
29+
class Threaded
30+
{
31+
32+
private int $pid = -1;
33+
34+
private int $exitCode = -1;
35+
36+
private bool $isRunning = false;
37+
38+
private bool $signaled = false;
39+
40+
private bool $stopped = false;
41+
42+
/**
43+
* @var array<string, mixed>
44+
* @phpstan-var array<string, mixed>
45+
*/
46+
private static array $shared = [];
47+
48+
public function __construct()
49+
{}
50+
51+
public function getPid(): int
52+
{
53+
return $this->pid;
54+
}
55+
56+
public function setPid(int $pid): void
57+
{
58+
$this->pid = $pid;
59+
}
60+
61+
public function getExitCode(): int
62+
{
63+
return $this->exitCode;
64+
}
65+
66+
public function setExitCode(int $exitCode): void
67+
{
68+
$this->exitCode = $exitCode;
69+
}
70+
71+
public function isRunning(): bool
72+
{
73+
return $this->isRunning;
74+
}
75+
76+
public function setRunning(bool $isRunning): void
77+
{
78+
$this->isRunning = $isRunning;
79+
}
80+
81+
public function isSignaled(): bool
82+
{
83+
return $this->signaled;
84+
}
85+
86+
public function setSignaled(bool $signaled): void
87+
{
88+
$this->signaled = $signaled;
89+
}
90+
91+
public function isStopped(): bool
92+
{
93+
return $this->stopped;
94+
}
95+
96+
public function setStopped(bool $stopped): void
97+
{
98+
$this->stopped = $stopped;
99+
}
100+
101+
/**
102+
* @return array<string, mixed>
103+
* @phpstan-return array<string, mixed>
104+
*/
105+
public static function getShared(): array
106+
{
107+
return self::$shared;
108+
}
109+
110+
/**
111+
* @param array<string, mixed> $shared
112+
* @phpstan-param array<string, mixed> $shared
113+
*/
114+
public static function setShared(array $shared): void
115+
{
116+
self::$shared = $shared;
117+
}
118+
119+
public static function addShared(string $key, mixed $value): void
120+
{
121+
self::$shared[$key] = $value;
122+
}
123+
124+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/*
4+
* Copyright (c) 2023 VennV
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
declare(strict_types = 1);
26+
27+
namespace vennv\vapm;
28+
29+
interface ThreadedInterface
30+
{
31+
32+
public function getPid(): int;
33+
34+
public function setPid(int $pid): void;
35+
36+
public function getExitCode(): int;
37+
38+
public function setExitCode(int $exitCode): void;
39+
40+
public function isRunning(): bool;
41+
42+
public function setRunning(bool $isRunning): void;
43+
44+
public function isSignaled(): bool;
45+
46+
public function setSignaled(bool $signaled): void;
47+
48+
public function isStopped(): bool;
49+
50+
public function setStopped(bool $stopped): void;
51+
52+
/**
53+
* @return array<string, mixed>
54+
* @phpstan-return array<string, mixed>
55+
*/
56+
public static function getShared(): array;
57+
58+
/**
59+
* @param array<string, mixed> $shared
60+
* @phpstan-param array<string, mixed> $shared
61+
*/
62+
public static function setShared(array $shared): void;
63+
64+
public static function addShared(string $key, mixed $value): void;
65+
66+
}

src/vennv/vapm/Utils.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,37 @@
2626

2727
namespace vennv\vapm;
2828

29+
use Generator;
30+
use RecursiveDirectoryIterator;
31+
use RecursiveIteratorIterator;
32+
use SplFileInfo;
33+
use function preg_match;
34+
2935
final class Utils
3036
{
3137

32-
public static function milliSecsToSecs(float $milliSecs) : float
38+
public static function milliSecsToSecs(float $milliSecs): float
3339
{
3440
return $milliSecs / 1000;
3541
}
3642

43+
public static function getAllPHP(string $path): Generator
44+
{
45+
$dir = new RecursiveDirectoryIterator($path);
46+
$iterator = new RecursiveIteratorIterator($dir);
47+
48+
foreach ($iterator as $file)
49+
{
50+
if ($file instanceof SplFileInfo)
51+
{
52+
$fname = $file->getFilename();
53+
54+
if (preg_match('%\.php$%', $fname) === 1)
55+
{
56+
yield $file->getPathname();
57+
}
58+
}
59+
}
60+
}
61+
3762
}

virion.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: LibVapmPMMP
2-
version: 1.6.7
2+
version: 1.7.0
33
api: 5.0.0
44
php:
55
- 8.1

0 commit comments

Comments
 (0)