Skip to content

Commit c3c5251

Browse files
committed
Async
1 parent b17b64c commit c3c5251

File tree

3 files changed

+32
-57
lines changed

3 files changed

+32
-57
lines changed

src/Async.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,28 @@
1111
class Async
1212
{
1313
/** @var TaskInterface[] */
14-
private array $pendingQueue = [];
14+
protected array $pendingQueue = [];
1515

1616
/** @var TaskInterface[] */
17-
private array $progressQueue = [];
17+
protected array $progressQueue = [];
1818

1919
/** @var TaskInterface[] */
20-
private array $finishedQueue = [];
20+
protected array $finishedQueue = [];
2121

2222
/** @var TaskInterface[] */
23-
private array $failedQueue = [];
23+
protected array $failedQueue = [];
2424

2525
/** @var TaskFactoryInterface */
26-
private TaskFactoryInterface $taskFactory;
26+
protected TaskFactoryInterface $taskFactory;
2727

2828
/** @var Encoder */
29-
private Encoder $encoder;
29+
protected Encoder $encoder;
3030

3131
/** @var mixed[] */
32-
private array $results = [];
32+
protected array $results = [];
3333

3434
/** @var int */
35-
private int $sleep;
35+
protected int $sleep;
3636

3737
/**
3838
* Async constructor.
@@ -44,7 +44,7 @@ class Async
4444
public function __construct(
4545
?TaskFactoryInterface $taskFactory = null,
4646
?Encoder $encoder = null,
47-
?int $sleep = 50
47+
?int $sleep = 5000
4848
) {
4949
$this->taskFactory = $taskFactory ?? new TaskFactory();
5050
$this->encoder = $encoder ?? new Encoder();
@@ -53,7 +53,7 @@ public function __construct(
5353
$this->listener();
5454
}
5555

56-
private function listener(): void
56+
protected function listener(): void
5757
{
5858
pcntl_async_signals(true);
5959
pcntl_signal(SIGCHLD, function ($signo, $status) {
@@ -79,7 +79,7 @@ private function listener(): void
7979
* @param TaskInterface $task
8080
* @return TaskInterface
8181
*/
82-
private function finished(TaskInterface $task): TaskInterface
82+
protected function finished(TaskInterface $task): TaskInterface
8383
{
8484
unset($this->progressQueue[$task->pid()]);
8585

@@ -92,7 +92,7 @@ private function finished(TaskInterface $task): TaskInterface
9292
return $task;
9393
}
9494

95-
private function notify(): void
95+
protected function notify(): void
9696
{
9797
$process = array_shift($this->pendingQueue);
9898

@@ -107,7 +107,7 @@ private function notify(): void
107107
* @param TaskInterface $task
108108
* @return TaskInterface
109109
*/
110-
private function progress(TaskInterface $task): TaskInterface
110+
protected function progress(TaskInterface $task): TaskInterface
111111
{
112112
$task->start();
113113

@@ -122,7 +122,7 @@ private function progress(TaskInterface $task): TaskInterface
122122
* @param TaskInterface $task
123123
* @return TaskInterface
124124
*/
125-
private function failed(TaskInterface $task): TaskInterface
125+
protected function failed(TaskInterface $task): TaskInterface
126126
{
127127
unset($this->progressQueue[$task->pid()]);
128128

@@ -148,7 +148,7 @@ public function add(callable $process): TaskInterface
148148
* @param TaskInterface $task
149149
* @return TaskInterface
150150
*/
151-
private function pending(TaskInterface $task): TaskInterface
151+
protected function pending(TaskInterface $task): TaskInterface
152152
{
153153
$this->pendingQueue[$task->id()] = $task;
154154

src/Task.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function output(): mixed
103103
* @param callable $callback
104104
* @param array $arguments
105105
*/
106-
private function call(callable $callback, array $arguments): void
106+
protected function call(callable $callback, array $arguments): void
107107
{
108108
call_user_func_array($callback, $arguments);
109109
}

src/TaskFactory.php

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -30,50 +30,25 @@ class TaskFactory implements TaskFactoryInterface
3030
*/
3131
public function __construct()
3232
{
33-
$this->findAutoload()
34-
->findScript();
33+
$this->autoloader = $this->find('vendor/autoload.php');
34+
$this->script = $this->find('bin/child');
3535
}
3636

3737
/**
38-
* @return $this
38+
* @param string $file
39+
* @return string|null
3940
*/
40-
private function findScript(): self
41+
protected function find(string $file): ?string
4142
{
42-
if (!$this->script) {
43-
$paths = array_filter([
44-
__DIR__ . '/../../../../bin/child',
45-
__DIR__ . '/../../../bin/child',
46-
__DIR__ . '/../../bin/child',
47-
__DIR__ . '/../bin/child',
48-
__DIR__ . '/bin/child',
49-
50-
], static function (string $path) {
51-
return file_exists($path);
52-
});
53-
$this->script = reset($paths);
54-
}
55-
56-
return $this;
57-
}
58-
59-
/**
60-
* @return $this
61-
*/
62-
private function findAutoload(): self
63-
{
64-
if (!$this->autoloader) {
65-
$paths = array_filter([
66-
__DIR__ . '/../../../../vendor/autoload.php',
67-
__DIR__ . '/../../../vendor/autoload.php',
68-
__DIR__ . '/../../vendor/autoload.php',
69-
__DIR__ . '/../vendor/autoload.php',
70-
], static function (string $path) {
71-
return file_exists($path);
72-
});
73-
$this->autoloader = reset($paths);
74-
}
75-
76-
return $this;
43+
$paths = array_filter([
44+
__DIR__ . '/../../../../'.$file,
45+
__DIR__ . '/../../../'.$file,
46+
__DIR__ . '/../../'.$file,
47+
__DIR__ . '/../'.$file,
48+
], static function (string $path) {
49+
return file_exists($path);
50+
});
51+
return reset($paths);
7752
}
7853

7954
/**
@@ -97,15 +72,15 @@ public function createTask(
9772
/**
9873
* @return string
9974
*/
100-
private function id(): string
75+
protected function id(): string
10176
{
10277
return (++$this->index) . $this->pid();
10378
}
10479

10580
/**
10681
* @return string
10782
*/
108-
private function pid(): string
83+
protected function pid(): string
10984
{
11085
$this->pid = $this->pid ?? getmypid();
11186

0 commit comments

Comments
 (0)