Passed
Push — master ( b5ca42...8fe651 )
by Siad
05:05
created

ComposerTask::setComposer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
use Phing\Exception\BuildException;
21
use Phing\Io\FileSystem;
22
use Phing\Io\IOException;
23
use Phing\Project;
24
use Phing\Task;
25
use Phing\Type\Commandline;
26
use Phing\Type\CommandlineArgument;
27
28
/**
29
 * Composer Task
30
 *
31
 * Run composer straight from phing
32
 *
33
 * @author  nuno costa <[email protected]>
34
 * @license MIT
35
 * @package phing.tasks.ext
36
 */
37
class ComposerTask extends Task
38
{
39
    /**
40
     * Path to php interpreter
41
     *
42
     * @var string
43
     */
44
    private $php = '';
45
46
    /**
47
     * Composer command to execute
48
     *
49
     * @var string
50
     */
51
    private $command = null;
52
53
    /**
54
     * Commandline object
55
     *
56
     * @var Commandline
57
     */
58
    private $commandLine = null;
59
60
    /**
61
     * Path to Composer application
62
     *
63
     * @var string
64
     */
65
    private $composer = 'composer.phar';
66
67
    /**
68
     * Constructor.
69
     */
70 1
    public function __construct()
71
    {
72 1
        parent::__construct();
73 1
        $this->commandLine = new Commandline();
74 1
    }
75
76
    /**
77
     * Initialize the interpreter with the Phing property php.interpreter.
78
     */
79
    public function init()
80
    {
81
        $this->setPhp($this->project->getProperty('php.interpreter'));
82
    }
83
84
    /**
85
     * Sets the path to php executable.
86
     *
87
     * @param string $php
88
     */
89 2
    public function setPhp($php)
90
    {
91 2
        $this->php = $php;
92 2
    }
93
94
    /**
95
     * Gets the path to php executable.
96
     *
97
     * @return string
98
     */
99 2
    public function getPhp()
100
    {
101 2
        return $this->php;
102
    }
103
104
    /**
105
     * Sets the Composer command to execute.
106
     *
107
     * @param string $command
108
     */
109 2
    public function setCommand($command)
110
    {
111 2
        $this->command = $command;
112 2
    }
113
114
    /**
115
     * Return the Composer command to execute.
116
     *
117
     * @return String
118
     */
119 2
    public function getCommand()
120
    {
121 2
        return $this->command;
122
    }
123
124
    /**
125
     * Sets the path to Composer application.
126
     *
127
     * @param string $console
128
     */
129 2
    public function setComposer($console)
130
    {
131 2
        $this->composer = $console;
132 2
    }
133
134
    /**
135
     * Returns the path to Composer application.
136
     *
137
     * If the filepath is non existent, try to find it on the system.
138
     *
139
     * @return string
140
     * @throws IOException
141
     */
142 3
    public function getComposer()
143
    {
144 3
        $composerFile = new SplFileInfo($this->composer);
145 3
        if (false === $composerFile->isFile()) {
146 3
            $message = sprintf('Composer binary not found at "%s"', $composerFile);
147 3
            $this->log($message, Project::MSG_WARN);
148 3
            $composerLocation = FileSystem::getFileSystem()->which('composer');
149 3
            if (!empty($composerLocation)) {
150 2
                $message = sprintf('Composer binary found at "%s", updating location', $composerLocation[0]);
151 2
                $this->log($message, Project::MSG_INFO);
152 2
                $this->setComposer($composerLocation);
153
            }
154
        }
155 3
        return $this->composer;
156
    }
157
158
    /**
159
     * Creates a nested arg task.
160
     *
161
     * @return CommandlineArgument
162
     */
163
164 2
    public function createArg()
165
    {
166 2
        return $this->commandLine->createArgument();
167
    }
168
169
    /**
170
     * Prepares the command string to be executed.
171
     *
172
     * @return string
173
     * @throws IOException
174
     */
175 1
    private function prepareCommandLine()
176
    {
177 1
        $this->commandLine->setExecutable($this->getPhp());
178 1
        $command = $this->getCommand();
179 1
        if (empty($command)) {
180
            throw new BuildException('"command" attribute is required');
181
        }
182
        //We are un-shifting arguments to the beginning of the command line because arguments should be at the end
183 1
        $this->commandLine->createArgument(true)->setValue($command);
184 1
        $this->commandLine->createArgument(true)->setValue($this->getComposer());
185 1
        $commandLine = (string) $this->commandLine;
186
        //Creating new Commandline instance. It allows to handle subsequent calls correctly
187 1
        $this->commandLine = new Commandline();
188
189 1
        return $commandLine;
190
    }
191
192
    /**
193
     * Executes the Composer task.
194
     *
195
     * @throws IOException
196
     */
197
    public function main()
198
    {
199
        $commandLine = $this->prepareCommandLine();
200
        $this->log("Executing " . $commandLine);
201
        passthru($commandLine, $returnCode);
202
203
        if ($returnCode > 0) {
204
            throw new BuildException("Composer execution failed");
205
        }
206
    }
207
}
208