1<?php
2
3/*
4 *  $Id: 4dc0098e06183c52f5b3809e596b63929860ac9a $
5 *
6 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
7 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
8 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
9 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
10 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
11 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
12 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
13 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
14 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
16 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17 *
18 * This software consists of voluntary contributions made by many individuals
19 * and is licensed under the LGPL. For more information please see
20 * <http://phing.info>.
21 */
22
23require_once "phing/Task.php";
24require_once "phing/types/Commandline.php";
25
26/**
27 * Composer Task
28 * Run composer straight from phing
29 *
30 * @author nuno costa <nuno@francodacosta.com>
31 * @license MIT
32 * @version $Id: 4dc0098e06183c52f5b3809e596b63929860ac9a $
33 * @package phing.tasks.ext
34 */
35class ComposerTask extends Task
36{
37    /**
38     * @var string the path to php interperter
39     */
40    private $php = 'php';
41
42    /**
43     *
44     * @var string the Composer command to execute
45     */
46    private $command = null;
47
48    /**
49     *
50     * @var Commandline
51     */
52    private $commandLine =null;
53    /**
54     *
55     * @var string path to Composer application
56     */
57    private $composer = 'composer.phar';
58
59    public function __construct()
60    {
61        $this->commandLine = new Commandline();
62    }
63
64    /**
65     * Sets the path to php executable.
66     *
67     * @param string $php
68     */
69    public function setPhp($php)
70    {
71        $this->php = $php;
72    }
73
74    /**
75     * gets the path to php executable.
76     *
77     * @return string
78     */
79    public function getPhp()
80    {
81        return $this->php;
82    }
83    /**
84     * sets the Composer command to execute
85     * @param string $command
86     */
87    public function setCommand($command)
88    {
89        $this->command = $command;
90    }
91
92    /**
93     * return the Composer command to execute
94     * @return String
95     */
96    public function getCommand()
97    {
98        return $this->command;
99    }
100
101    /**
102     * sets the path to Composer application
103     * @param string $console
104     */
105    public function setComposer($console)
106    {
107        $this->composer = $console;
108    }
109
110    /**
111     * returns the path to Composer application
112     * @return string
113     */
114    public function getComposer()
115    {
116        return $this->composer;
117    }
118
119    /**
120     * creates a nested arg task
121     *
122     * @return Arg Argument object
123     */
124
125    public function createArg()
126    {
127        return $this->commandLine->createArgument();
128    }
129
130    /**
131     * Prepares the command string to be executed
132     * @return string
133     */
134    private function prepareCommandLine()
135    {
136        $this->commandLine->setExecutable($this->getPhp());
137        //We are un-shifting arguments to the beginning of the command line because arguments should be at the end
138        $this->commandLine->createArgument(true)->setValue($this->getCommand());
139        $this->commandLine->createArgument(true)->setValue($this->getComposer());
140        $commandLine = strval($this->commandLine);
141        //Creating new Commandline instance. It allows to handle subsequent calls correctly
142        $this->commandLine = new Commandline();
143        return $commandLine;
144    }
145    /**
146     * executes the Composer task
147     */
148    public function main()
149    {
150        $commandLine = $this->prepareCommandLine();
151        $this->log("executing " . $commandLine);
152
153        $composerFile = new SplFileInfo($this->getComposer());
154        if (false === $composerFile->isFile()) {
155            throw new BuildException(sprintf('Composer binary not found, path is "%s"', $composerFile));
156        }
157
158        $return = 0;
159        passthru($commandLine, $return);
160
161        if ($return > 0) {
162            throw new BuildException("Composer execution failed");
163        }
164    }
165}
166