summaryrefslogtreecommitdiffstats
path: root/vendor/gipfl/process/src/ProcessList.php
blob: a3f7c30327a1644f8afaae7ab5ec5977ba50b984 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php

namespace gipfl\Process;

/**
 * @method ChildProcess current
 */
use Evenement\EventEmitterInterface;
use Evenement\EventEmitterTrait;
use InvalidArgumentException;
use React\ChildProcess\Process as ChildProcess;
use SplObjectStorage;

class ProcessList extends SplObjectStorage implements EventEmitterInterface
{
    const ON_ATTACHED = 'attached';
    const ON_DETACHED = 'detached';

    use EventEmitterTrait;

    #[\ReturnTypeWillChange]
    public function attach($object, $info = null)
    {
        if (! $object instanceof ChildProcess || $info !== null) {
            throw new InvalidArgumentException(sprintf(
                'Can attach only %s instances',
                ChildProcess::class
            ));
        }
        $object->on('exit', function () use ($object) {
            $this->detach($object);
        });

        parent::attach($object, $info);
        $this->emit(self::ON_ATTACHED, [$object]);
    }

    #[\ReturnTypeWillChange]
    public function detach($object)
    {
        parent::detach($object);
        $this->emit(self::ON_DETACHED, [$object]);
    }
}