PhpProcessSpawner   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 85
rs 10
c 2
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A create() 0 4 1
A buildInternal() 0 19 4
A spawn() 0 7 1
1
<?php
2
3
/**
4
 * This file is part of tenside/core.
5
 *
6
 * (c) Christian Schiffler <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * This project is provided in good faith and hope to be usable by anyone.
12
 *
13
 * @package    tenside/core
14
 * @author     Christian Schiffler <[email protected]>
15
 * @copyright  2015 Christian Schiffler <[email protected]>
16
 * @license    https://github.com/tenside/core/blob/master/LICENSE MIT
17
 * @link       https://github.com/tenside/core
18
 * @filesource
19
 */
20
21
namespace Tenside\Core\Util;
22
23
use Symfony\Component\Process\Process;
24
use Tenside\Core\Config\TensideJsonConfig;
25
26
/**
27
 * This class implements a php process spawner.
28
 */
29
class PhpProcessSpawner
30
{
31
    /**
32
     * The configuration in use.
33
     *
34
     * @var TensideJsonConfig
35
     */
36
    private $config;
37
38
    /**
39
     * The home directory to run the process in.
40
     *
41
     * @var string
42
     */
43
    private $homePath;
44
45
    /**
46
     * Create a new instance.
47
     *
48
     * @param TensideJsonConfig $config   The configuration in use.
49
     *
50
     * @param string            $homePath The directory to use as home directory.
51
     */
52
    public function __construct(TensideJsonConfig $config, $homePath)
53
    {
54
        $this->config   = $config;
55
        $this->homePath = $homePath;
56
    }
57
58
    /**
59
     * Create a new instance.
60
     *
61
     * @param TensideJsonConfig $config   The configuration in use.
62
     *
63
     * @param string            $homePath The directory to use as home directory.
64
     *
65
     * @return PhpProcessSpawner
66
     */
67
    public static function create(TensideJsonConfig $config, $homePath)
68
    {
69
        return new static($config, $homePath);
70
    }
71
72
    /**
73
     * Create the process (automatically forcing to background if configured).
74
     *
75
     * @param array $arguments The additional arguments to add to the call.
76
     *
77
     * @return Process
78
     */
79
    public function spawn(array $arguments)
80
    {
81
        return $this
82
            ->buildInternal($arguments)
83
            ->setForceBackground($this->config->isForceToBackgroundEnabled())
84
            ->generate();
85
    }
86
87
    /**
88
     * Build the internal process builder.
89
     *
90
     * @param array $arguments The arguments.
91
     *
92
     * @return ProcessBuilder
93
     */
94
    private function buildInternal(array $arguments)
95
    {
96
        $builder = ProcessBuilder::create($this->config->getPhpCliBinary());
97
98
        if (null !== ($cliArguments = $this->config->getPhpCliArguments())) {
99
            $builder->addArguments($cliArguments);
100
        }
101
        $builder->addArguments($arguments);
102
103
        if (null !== ($environment = $this->config->getPhpCliEnvironment())) {
104
            foreach ($environment as $name => $value) {
105
                $builder->setEnv($name, $value);
106
            }
107
        }
108
        // MUST be kept last.
109
        $builder->setEnv('COMPOSER', $this->homePath . DIRECTORY_SEPARATOR . 'composer.json');
110
111
        return $builder;
112
    }
113
}
114