Completed
Pull Request — master (#34)
by Bernhard
11:06
created

PuliRunner   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 64.29%

Importance

Changes 8
Bugs 5 Features 0
Metric Value
wmc 17
c 8
b 5
f 0
lcom 1
cbo 5
dl 0
loc 111
ccs 27
cts 42
cp 0.6429
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 32 6
A getPuliCommand() 0 4 1
A run() 0 22 3
C find() 0 22 7
1
<?php
2
3
/*
4
 * This file is part of the puli/composer-plugin package.
5
 *
6
 * (c) Bernhard Schussek <[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
12
namespace Puli\ComposerPlugin;
13
14
use RuntimeException;
15
use Symfony\Component\Process\ExecutableFinder;
16
use Symfony\Component\Process\PhpExecutableFinder;
17
use Symfony\Component\Process\Process;
18
use Symfony\Component\Process\ProcessUtils;
19
20
/**
21
 * Executes the "puli" command.
22
 *
23
 * @since  1.0
24
 *
25
 * @author Bernhard Schussek <[email protected]>
26
 */
27
class PuliRunner
28
{
29
    /**
30
     * @var string
31
     */
32
    private $puli;
33
34
    /**
35
     * Creates a new runner.
36
     *
37
     * @param string|null $binDir The path to Composer's "bin-dir".
38
     */
39 3
    public function __construct($binDir = null)
40
    {
41 3
        $phpFinder = new PhpExecutableFinder();
42
43 3
        if (!($php = $phpFinder->find())) {
44
            throw new RuntimeException('The "php" command could not be found.');
45
        }
46
47 3
        $finder = new ExecutableFinder();
48
49
        // Search:
50
        // 1. in the current working directory
51
        // 2. in Composer's "bin-dir"
52
        // 3. in the system path
53 3
        $searchPath = array_merge(array(getcwd()), (array) $binDir);
54
55
        // Search "puli.phar" in the PATH and the current directory
56 3
        if (!($puli = $this->find('puli.phar', $searchPath, $finder))) {
57
            // Search "puli" in the PATH and Composer's "bin-dir"
58 3
            if (!($puli = $this->find('puli', $searchPath, $finder))) {
59
                throw new RuntimeException('The "puli"/"puli.phar" command could not be found.');
60
            }
61 3
        }
62
63 3
        $content = file_get_contents($puli, null, null, -1, 18);
64
65 3
        if ($content === '#!/usr/bin/env php' || 0 === strpos($content, '<?php')) {
66 2
            $this->puli = escapeshellcmd($php).' '.ProcessUtils::escapeArgument($puli);
67 2
        } else {
68 1
            $this->puli = escapeshellcmd($puli);
69
        }
70 3
    }
71
72
    /**
73
     * Returns the command used to execute Puli.
74
     *
75
     * @return string The "puli" command.
76
     */
77 3
    public function getPuliCommand()
78
    {
79 3
        return $this->puli;
80
    }
81
82
    /**
83
     * Runs a Puli command.
84
     *
85
     * @param string   $command The Puli command to run.
86
     * @param string[] $args    Arguments to quote and insert into the command.
87
     *                          For each key "key" in this array, the placeholder
88
     *                          "%key%" should be present in the command string.
89
     *
90
     * @return string The lines of the output.
91
     */
92
    public function run($command, array $args = array())
93
    {
94
        $replacements = array();
95
96
        foreach ($args as $key => $arg) {
97
            $replacements['%'.$key.'%'] = ProcessUtils::escapeArgument($arg);
98
        }
99
100
        // Disable colorization so that we can process the output
101
        // Enable exception traces by using the "-vv" switch
102
        $fullCommand = sprintf('%s %s --no-ansi -vv', $this->puli, strtr($command, $replacements));
103
104
        $process = new Process($fullCommand);
105
        $process->run();
106
107
        if (!$process->isSuccessful()) {
108
            throw PuliRunnerException::forProcess($process);
109
        }
110
111
        // Normalize line endings across systems
112
        return str_replace("\r\n", "\n", $process->getOutput());
113
    }
114
115 3
    private function find($name, array $dirs, ExecutableFinder $finder)
116
    {
117 3
        $suffixes = array('');
118
119 3
        if ('\\' === DIRECTORY_SEPARATOR) {
120
            $suffixes[] = '.bat';
121
        }
122
123
        // The finder first looks in the system directories and then in the
124
        // user-defined ones. We want to check the user-defined ones first.
125 3
        foreach ($dirs as $dir) {
126 3
            foreach ($suffixes as $suffix) {
127 3
                $file = $dir.DIRECTORY_SEPARATOR.$name.$suffix;
128
129 3
                if (is_file($file) && ('\\' === DIRECTORY_SEPARATOR || is_executable($file))) {
130 3
                    return $file;
131
                }
132 3
            }
133 3
        }
134
135 3
        return $finder->find($name);
136
    }
137
}
138