Passed
Push — main ( 49e508...46fa35 )
by Michiel
06:19
created

SymfonyConsoleTask::getCommand()   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
eloc 1
dl 0
loc 3
ccs 2
cts 2
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\Task\Optional\SymfonyConsoleArg;
23
use Phing\Exception\BuildException;
24
use Phing\Project;
25
use Phing\Task;
26
use Phing\Type\Commandline;
27
28
/**
29
 * Symfony Console Task
30
 *
31
 * @author  nuno costa <[email protected]>
32
 * @license GPL
33
 * @package phing.tasks.ext.symfony
34
 */
35
class SymfonyConsoleTask extends Task
36
{
37
38
    /**
39
     *
40
     * @var SymfonyConsoleArg[] a collection of Arg objects
41
     */
42
    private $args = [];
43
44
    /**
45
     *
46
     * @var string the Symfony console command to execute
47
     */
48
    private $command = null;
49
50
    /**
51
     *
52
     * @var string path to symfony console application
53
     */
54
    private $console = 'bin/console';
55
56
    /**
57
     *
58
     * @var string property to be set
59
     */
60
    private $propertyName = null;
61
62
    /**
63
     * Whether to check the return code.
64
     *
65
     * @var boolean
66
     */
67
    private $checkreturn = false;
68
69
    /**
70
     * Is the symfony cli debug mode set? (true by default)
71
     *
72
     * @var boolean
73
     */
74
    private $debug = true;
75
76
    /**
77
     * @var bool $silent
78
     */
79
    private $silent = false;
80
81
    /**
82
     * sets the symfony console command to execute
83
     *
84
     * @param string $command
85
     */
86 4
    public function setCommand($command)
87
    {
88 4
        $this->command = $command;
89 4
    }
90
91
    /**
92
     * return the symfony console command to execute
93
     *
94
     * @return String
95
     */
96 1
    public function getCommand()
97
    {
98 1
        return $this->command;
99
    }
100
101
    /**
102
     * sets the path to symfony console application
103
     *
104
     * @param string $console
105
     */
106 4
    public function setConsole($console)
107
    {
108 4
        $this->console = $console;
109 4
    }
110
111
    /**
112
     * returns the path to symfony console application
113
     *
114
     * @return string
115
     */
116 1
    public function getConsole()
117
    {
118 1
        return $this->console;
119
    }
120
121
    /**
122
     * Set the name of the property to store the application output in
123
     *
124
     * @param  $property
125
     * @return void
126
     */
127
    public function setPropertyName($property)
128
    {
129
        $this->propertyName = $property;
130
    }
131
132
    /**
133
     * Whether to check the return code.
134
     *
135
     * @param boolean $checkreturn If the return code shall be checked
136
     *
137
     * @return void
138
     */
139
    public function setCheckreturn(bool $checkreturn)
140
    {
141
        $this->checkreturn = $checkreturn;
142
    }
143
144
    /**
145
     * Whether to set the symfony cli debug mode
146
     *
147
     * @param boolean $debug If the symfony cli debug mode is set
148
     *
149
     * @return void
150
     */
151 3
    public function setDebug(bool $debug)
152
    {
153 3
        $this->debug = $debug;
154 3
    }
155
156
    /**
157
     * Get if the symfony cli debug mode is set
158
     *
159
     * @return boolean
160
     */
161 1
    public function getDebug()
162
    {
163 1
        return $this->debug;
164
    }
165
166 1
    public function setSilent(bool $flag)
167
    {
168 1
        $this->silent = $flag;
169 1
    }
170
171 1
    public function getSilent()
172
    {
173 1
        return $this->silent;
174
    }
175
176
    /**
177
     * appends an arg tag to the arguments stack
178
     *
179
     * @return SymfonyConsoleArg Argument object
180
     */
181
182 5
    public function createArg()
183
    {
184 5
        $num = array_push($this->args, new SymfonyConsoleArg());
185
186 5
        return $this->args[$num - 1];
187
    }
188
189
    /**
190
     * return the argumments passed to this task
191
     *
192
     * @return array of Arg()
193
     */
194 1
    public function getArgs()
195
    {
196 1
        return $this->args;
197
    }
198
199
    /**
200
     * Check if the no-debug option was added via args
201
     *
202
     * @return boolean
203
     */
204 2
    private function isNoDebugArgPresent()
205
    {
206 2
        foreach ($this->args as $arg) {
207 2
            if ($arg->getName() == "no-debug") {
208 1
                return true;
209
            }
210
        }
211
212 1
        return false;
213
    }
214
215
    /**
216
     * Gets the command string to be executed
217
     *
218
     * @return string
219
     */
220 3
    public function getCmdString()
221
    {
222
        // Add no-debug arg if it isn't already present
223 3
        if (!$this->debug && !$this->isNoDebugArgPresent()) {
224 1
            $this->createArg()->setName("no-debug");
225
        }
226
        $cmd = [
227 3
            Commandline::quoteArgument($this->console),
228 3
            $this->command,
229 3
            implode(' ', $this->args)
230
        ];
231 3
        $cmd = implode(' ', $cmd);
232
233 3
        return $cmd;
234
    }
235
236
    /**
237
     * executes the synfony console application
238
     */
239
    public function main()
240
    {
241
        $cmd = $this->getCmdString();
242
243
        $this->silent ?: $this->log("executing $cmd");
244
        $return = null;
245
        $output = [];
246
        exec($cmd, $output, $return);
247
248
        $lines = implode("\r\n", $output);
249
250
        $this->silent ?: $this->log($lines, Project::MSG_INFO);
251
252
        if ($this->propertyName != null) {
253
            $this->project->setProperty($this->propertyName, $lines);
254
        }
255
256
        if ($return != 0 && $this->checkreturn) {
257
            $this->log('Task exited with code: ' . $return, Project::MSG_ERR);
258
            throw new BuildException("SymfonyConsole execution failed");
259
        }
260
    }
261
}
262