Passed
Push — main ( b7649a...398f47 )
by Michiel
06:32
created

ComposerTask::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
namespace Phing\Task\Optional;
21
22
use Phing\Exception\BuildException;
23
use Phing\Io\FileSystem;
24
use Phing\Io\IOException;
25
use Phing\Project;
26
use Phing\Task;
27
use Phing\Type\Commandline;
28
use Phing\Type\CommandlineArgument;
29
use SplFileInfo;
30
31
/**
32
 * Composer Task
33
 *
34
 * Run composer straight from phing
35
 *
36
 * @author  nuno costa <[email protected]>
37
 * @license MIT
38
 * @package phing.tasks.ext
39
 */
40
class ComposerTask extends Task
41
{
42
    /**
43
     * Path to php interpreter
44
     *
45
     * @var string
46
     */
47
    private $php = '';
48
49
    /**
50
     * Composer command to execute
51
     *
52
     * @var string
53
     */
54
    private $command = null;
55
56
    /**
57
     * Commandline object
58
     *
59
     * @var Commandline
60
     */
61
    private $commandLine = null;
62
63
    /**
64
     * Path to Composer application
65
     *
66
     * @var string
67
     */
68
    private $composer = 'composer.phar';
69
70
    /**
71
     * Constructor.
72
     */
73 7
    public function __construct()
74
    {
75 7
        parent::__construct();
76 7
        $this->commandLine = new Commandline();
77 7
    }
78
79
    /**
80
     * Initialize the interpreter with the Phing property php.interpreter.
81
     */
82
    public function init()
83
    {
84
        $this->setPhp($this->project->getProperty('php.interpreter'));
85
    }
86
87
    /**
88
     * Sets the path to php executable.
89
     *
90
     * @param string $php
91
     */
92 2
    public function setPhp($php)
93
    {
94 2
        $this->php = $php;
95 2
    }
96
97
    /**
98
     * Gets the path to php executable.
99
     *
100
     * @return string
101
     */
102 2
    public function getPhp()
103
    {
104 2
        return $this->php;
105
    }
106
107
    /**
108
     * Sets the Composer command to execute.
109
     *
110
     * @param string $command
111
     */
112 2
    public function setCommand($command)
113
    {
114 2
        $this->command = $command;
115 2
    }
116
117
    /**
118
     * Return the Composer command to execute.
119
     *
120
     * @return String
121
     */
122 2
    public function getCommand()
123
    {
124 2
        return $this->command;
125
    }
126
127
    /**
128
     * Sets the path to Composer application.
129
     *
130
     * @param string $console
131
     */
132 3
    public function setComposer($console)
133
    {
134 3
        $this->composer = $console;
135 3
    }
136
137
    /**
138
     * Returns the path to Composer application.
139
     *
140
     * If the filepath is non existent, try to find it on the system.
141
     *
142
     * @return string
143
     * @throws IOException
144
     */
145 3
    public function getComposer()
146
    {
147 3
        $composerFile = new SplFileInfo($this->composer);
148 3
        if (false === $composerFile->isFile()) {
149 3
            $message = sprintf('Composer binary not found at "%s"', $composerFile);
150 3
            $this->log($message, Project::MSG_WARN);
151 3
            $composerLocation = FileSystem::getFileSystem()->which('composer');
152 3
            if (!empty($composerLocation)) {
153 2
                $message = sprintf('Composer binary found at "%s", updating location', $composerLocation[0]);
154 2
                $this->log($message, Project::MSG_INFO);
155 2
                $this->setComposer($composerLocation);
156
            }
157
        }
158 3
        return $this->composer;
159
    }
160
161
    /**
162
     * Creates a nested arg task.
163
     *
164
     * @return CommandlineArgument
165
     */
166
167 2
    public function createArg()
168
    {
169 2
        return $this->commandLine->createArgument();
170
    }
171
172
    /**
173
     * Prepares the command string to be executed.
174
     *
175
     * @return string
176
     * @throws IOException
177
     */
178 1
    private function prepareCommandLine()
179
    {
180 1
        $this->commandLine->setExecutable($this->getPhp());
181 1
        $command = $this->getCommand();
182 1
        if (empty($command)) {
183
            throw new BuildException('"command" attribute is required');
184
        }
185
        //We are un-shifting arguments to the beginning of the command line because arguments should be at the end
186 1
        $this->commandLine->createArgument(true)->setValue($command);
187 1
        $this->commandLine->createArgument(true)->setValue($this->getComposer());
188 1
        $commandLine = (string) $this->commandLine;
189
        //Creating new Commandline instance. It allows to handle subsequent calls correctly
190 1
        $this->commandLine = new Commandline();
191
192 1
        return $commandLine;
193
    }
194
195
    /**
196
     * Executes the Composer task.
197
     *
198
     * @throws IOException
199
     */
200
    public function main()
201
    {
202
        $commandLine = $this->prepareCommandLine();
203
        $this->log("Executing " . $commandLine);
204
        passthru($commandLine, $returnCode);
205
206
        if ($returnCode > 0) {
207
            throw new BuildException("Composer execution failed");
208
        }
209
    }
210
}
211