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
/**
21
 * Composer Task
22
 *
23
 * Run composer straight from phing
24
 *
25
 * @author  nuno costa <nuno@francodacosta.com>
26
 * @license MIT
27
 * @package phing.tasks.ext
28
 */
29
class ComposerTask extends Task
30
{
31
    /**
32
     * Path to php interpreter
33
     *
34
     * @var string
35
     */
36
    private $php = '';
37

38
    /**
39
     * Composer command to execute
40
     *
41
     * @var string
42
     */
43
    private $command = null;
44

45
    /**
46
     * Commandline object
47
     *
48
     * @var Commandline
49
     */
50
    private $commandLine = null;
51

52
    /**
53
     * Path to Composer application
54
     *
55
     * @var string
56
     */
57
    private $composer = 'composer.phar';
58

59
    /**
60
     * Constructor.
61
     */
62 1
    public function __construct()
63
    {
64 1
        parent::__construct();
65 1
        $this->commandLine = new Commandline();
66
    }
67

68
    /**
69
     * Initialize the interpreter with the Phing property php.interpreter.
70
     */
71 0
    public function init()
72
    {
73 0
        $this->setPhp($this->project->getProperty('php.interpreter'));
74
    }
75

76
    /**
77
     * Sets the path to php executable.
78
     *
79
     * @param string $php
80
     */
81 1
    public function setPhp($php)
82
    {
83 1
        $this->php = $php;
84
    }
85

86
    /**
87
     * Gets the path to php executable.
88
     *
89
     * @return string
90
     */
91 1
    public function getPhp()
92
    {
93 1
        return $this->php;
94
    }
95

96
    /**
97
     * Sets the Composer command to execute.
98
     *
99
     * @param string $command
100
     */
101 1
    public function setCommand($command)
102
    {
103 1
        $this->command = $command;
104
    }
105

106
    /**
107
     * Return the Composer command to execute.
108
     *
109
     * @return String
110
     */
111 1
    public function getCommand()
112
    {
113 1
        return $this->command;
114
    }
115

116
    /**
117
     * Sets the path to Composer application.
118
     *
119
     * @param string $console
120
     */
121 1
    public function setComposer($console)
122
    {
123 1
        $this->composer = $console;
124
    }
125

126
    /**
127
     * Returns the path to Composer application.
128
     *
129
     * If the filepath is non existent, try to find it on the system.
130
     *
131
     * @return string
132
     * @throws IOException
133
     */
134 1
    public function getComposer()
135
    {
136 1
        $composerFile = new SplFileInfo($this->composer);
137 1
        if (false === $composerFile->isFile()) {
138 1
            $message = sprintf('Composer binary not found at "%s"', $composerFile);
139 1
            $this->log($message, Project::MSG_WARN);
140 1
            $composerLocation = FileSystem::getFileSystem()->which('composer');
141 1
            if (!empty($composerLocation)) {
142 1
                $message = sprintf('Composer binary found at "%s", updating location', $composerLocation[0]);
143 1
                $this->log($message, Project::MSG_INFO);
144 1
                $this->setComposer($composerLocation);
145
            }
146
        }
147 1
        return $this->composer;
148
    }
149

150
    /**
151
     * Creates a nested arg task.
152
     *
153
     * @return CommandlineArgument
154
     */
155

156 1
    public function createArg()
157
    {
158 1
        return $this->commandLine->createArgument();
159
    }
160

161
    /**
162
     * Prepares the command string to be executed.
163
     *
164
     * @return string
165
     * @throws IOException
166
     */
167 1
    private function prepareCommandLine()
168
    {
169 1
        $this->commandLine->setExecutable($this->getPhp());
170 1
        $command = $this->getCommand();
171 1
        if (empty($command)) {
172 0
            throw new BuildException('"command" attribute is required');
173
        }
174
        //We are un-shifting arguments to the beginning of the command line because arguments should be at the end
175 1
        $this->commandLine->createArgument(true)->setValue($command);
176 1
        $this->commandLine->createArgument(true)->setValue($this->getComposer());
177 1
        $commandLine = (string) $this->commandLine;
178
        //Creating new Commandline instance. It allows to handle subsequent calls correctly
179 1
        $this->commandLine = new Commandline();
180

181 1
        return $commandLine;
182
    }
183

184
    /**
185
     * Executes the Composer task.
186
     *
187
     * @throws IOException
188
     */
189 0
    public function main()
190
    {
191 0
        $commandLine = $this->prepareCommandLine();
192 0
        $this->log("Executing " . $commandLine);
193 0
        passthru($commandLine, $returnCode);
194

195 0
        if ($returnCode > 0) {
196 0
            throw new BuildException("Composer execution failed");
197
        }
198
    }
199
}

Read our documentation on viewing source code .

Loading