This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Acacha\Llum\Console; |
||
4 | |||
5 | use Acacha\Llum\Exceptions\InvalidCommandException; |
||
6 | use Acacha\Llum\Traits\LaravelConfigFile; |
||
7 | use Illuminate\Config\Repository; |
||
8 | use Symfony\Component\Console\Command\Command; |
||
9 | use Symfony\Component\Console\Input\InputArgument; |
||
10 | use Symfony\Component\Console\Input\InputInterface; |
||
11 | use Symfony\Component\Console\Output\OutputInterface; |
||
12 | use Symfony\Component\Process\Process; |
||
13 | |||
14 | /** |
||
15 | * Class LlumCommand. |
||
16 | */ |
||
17 | abstract class LlumCommand extends Command |
||
18 | { |
||
19 | use LaravelConfigFile; |
||
20 | |||
21 | /** |
||
22 | * The output interface. |
||
23 | * |
||
24 | * @var OutputInterface |
||
25 | */ |
||
26 | protected $output; |
||
27 | |||
28 | /** |
||
29 | * Command name. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $commandName; |
||
34 | |||
35 | /** |
||
36 | * Command description. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $commandDescription; |
||
41 | |||
42 | /** |
||
43 | * Command argument. |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $argument; |
||
48 | |||
49 | /** |
||
50 | * Argument type. |
||
51 | * |
||
52 | * @var int |
||
53 | */ |
||
54 | protected $argumentType = InputArgument::REQUIRED; |
||
55 | |||
56 | /** |
||
57 | * Command argument description. |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $argumentDescription; |
||
62 | |||
63 | /** |
||
64 | * Method to execute. |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $method; |
||
69 | |||
70 | /** |
||
71 | * Laravel config file (config/app.php). |
||
72 | * |
||
73 | * @var string |
||
74 | */ |
||
75 | protected $laravel_config_file; |
||
76 | |||
77 | /** |
||
78 | * Laravel services file (config/services.php). |
||
79 | * |
||
80 | * @var string |
||
81 | */ |
||
82 | protected $laravel_services_file; |
||
83 | |||
84 | /** |
||
85 | * Path to config folder. |
||
86 | * |
||
87 | * @var string |
||
88 | */ |
||
89 | protected $configPath; |
||
90 | |||
91 | /** |
||
92 | * Config repository. |
||
93 | * |
||
94 | * @var Repository |
||
95 | */ |
||
96 | protected $config; |
||
97 | |||
98 | /** |
||
99 | * Install development version. |
||
100 | * |
||
101 | * @var bool |
||
102 | */ |
||
103 | protected $installDev = false; |
||
104 | |||
105 | /** |
||
106 | * LlumCommand constructor. |
||
107 | */ |
||
108 | public function __construct() |
||
109 | { |
||
110 | parent::__construct(); |
||
111 | $this->configPath = __DIR__.'/../config/'; |
||
112 | $this->laravel_config_file = getcwd().'/config/app.php'; |
||
113 | $this->laravel_services_file = getcwd().'/config/services.php'; |
||
114 | $this->config = $this->obtainConfig(); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Initialize command. |
||
119 | * |
||
120 | * @param InputInterface $input |
||
121 | * @param OutputInterface $output |
||
122 | */ |
||
123 | View Code Duplication | protected function initialize(InputInterface $input, OutputInterface $output) |
|
0 ignored issues
–
show
|
|||
124 | { |
||
125 | parent::initialize($input, $output); |
||
126 | if (($input->hasOption('dev'))) { |
||
127 | if (($input->getOption('dev'))) { |
||
128 | $this->installDev = true; |
||
129 | } |
||
130 | } |
||
131 | } |
||
132 | |||
133 | /* |
||
134 | * gets dev option |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | protected function getDevOption() |
||
139 | { |
||
140 | return $this->installDev ? ':dev-master' : ''; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Require composer package. |
||
145 | * |
||
146 | * @param $package |
||
147 | */ |
||
148 | private function requireComposerPackage($package) |
||
149 | { |
||
150 | $composer = $this->findComposer(); |
||
151 | $process = new Process($composer.' require '.$package.''.$this->getDevOption(), |
||
152 | null, null, null, null); |
||
153 | $this->output->writeln('<info>Running composer require '.$package.$this->getDevOption().'</info>'); |
||
154 | $process->run(function ($type, $line) { |
||
155 | $this->output->write($line); |
||
156 | }); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Get the composer command for the environment. |
||
161 | * |
||
162 | * @return string |
||
163 | */ |
||
164 | private function findComposer() |
||
165 | { |
||
166 | if (file_exists(getcwd().'/composer.phar')) { |
||
167 | return '"'.PHP_BINARY.'" composer.phar"'; |
||
168 | } |
||
169 | |||
170 | return 'composer'; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * get package from config. |
||
175 | * |
||
176 | * @param $name |
||
177 | * |
||
178 | * @return array |
||
179 | */ |
||
180 | private function getPackageFromConfig($name) |
||
181 | { |
||
182 | //Check if package name is a composer package name |
||
183 | if (str_contains($name, '/')) { |
||
184 | return $this->config->get($this->getPackageNameByComposerName($name)); |
||
185 | } |
||
186 | |||
187 | return $this->config->get($name); |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Installs laravel package form config/packages.php file. |
||
192 | * |
||
193 | * @param string $name |
||
194 | * |
||
195 | * @return int -1 if error occurred |
||
196 | */ |
||
197 | protected function package($name) |
||
198 | { |
||
199 | $package = $this->obtainPackage($name); |
||
200 | |||
201 | if ($package == -1) { |
||
202 | return; |
||
203 | } |
||
204 | |||
205 | list($name, $providers, $aliases, $after) = array_fill(0, 4, null); |
||
206 | extract($package, EXTR_IF_EXISTS); |
||
207 | |||
208 | $this->requireComposerPackage($name); |
||
209 | |||
210 | if ($this->setupLaravelConfigFile($providers, $aliases) == -1) { |
||
211 | return -1; |
||
212 | } |
||
213 | |||
214 | $this->executeScriptAfterPackageInstallation($after); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Obtain package. |
||
219 | * |
||
220 | * @param $name |
||
221 | * |
||
222 | * @return array|int |
||
223 | */ |
||
224 | private function obtainPackage($name) |
||
225 | { |
||
226 | $package = $this->getPackageFromConfig($name); |
||
227 | |||
228 | if ($package == null) { |
||
229 | $this->showPackageNotFoundError($name); |
||
230 | |||
231 | return -1; |
||
232 | } |
||
233 | |||
234 | return $package; |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Execute post package installation script. |
||
239 | * |
||
240 | * @param $after |
||
241 | */ |
||
242 | private function executeScriptAfterPackageInstallation($after) |
||
243 | { |
||
244 | if ($after != null) { |
||
245 | passthru($after); |
||
246 | } |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Get config repository. |
||
251 | * |
||
252 | * @return Repository |
||
253 | */ |
||
254 | protected function obtainConfig() |
||
255 | { |
||
256 | return new Repository(require $this->configPath.'packages.php'); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Get package name by composer package name. |
||
261 | * |
||
262 | * @param $composerPackageName |
||
263 | * |
||
264 | * @return string |
||
265 | */ |
||
266 | private function getPackageNameByComposerName($composerPackageName) |
||
267 | { |
||
268 | foreach ($this->config->all() as $key => $configItem) { |
||
269 | if ($configItem['name'] == $composerPackageName) { |
||
270 | return $key; |
||
271 | } |
||
272 | } |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Show package not found error. |
||
277 | * |
||
278 | * @param $name |
||
279 | */ |
||
280 | protected function showPackageNotFoundError($name) |
||
281 | { |
||
282 | $this->output->writeln('<error>Package '.$name.' not found in file '.$this->configPath.'packages.php</error>'); |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Configure the command options. |
||
287 | * |
||
288 | * @param ConsoleCommand $command |
||
289 | * |
||
290 | * @throws \Acacha\Llum\Exceptions\InvalidCommandException |
||
291 | */ |
||
292 | protected function configureCommand(ConsoleCommand $command) |
||
293 | { |
||
294 | $this->ignoreValidationErrors(); |
||
295 | |||
296 | $name = $command->name(); |
||
297 | $description = $command->description(); |
||
298 | |||
299 | if (!is_string($name) || !is_string($description)) { |
||
300 | throw new InvalidCommandException(); |
||
301 | } |
||
302 | |||
303 | $this->setName($name) |
||
304 | ->setDescription($description); |
||
305 | if ($command->argument() != null) { |
||
306 | $this->addArgument($command->argument()['name'], |
||
307 | $command->argument()['type'], |
||
308 | $command->argument()['description'] |
||
309 | ); |
||
310 | } |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Execute the command. |
||
315 | * |
||
316 | * @param InputInterface $input |
||
317 | * @param OutputInterface $output |
||
318 | * |
||
319 | * @return int|null|void |
||
320 | */ |
||
321 | protected function execute(InputInterface $input, OutputInterface $output) |
||
322 | { |
||
323 | $this->output = $output; |
||
324 | $method = $this->method; |
||
325 | $this->setHelperSet($this->getApplication()->getHelperSet()); |
||
326 | if ($this->argument != null) { |
||
327 | $argument = $input->getArgument($this->argument); |
||
328 | $this->$method($argument); |
||
329 | |||
330 | return; |
||
331 | } |
||
332 | |||
333 | $this->$method($input,$output); |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Configure the command options. |
||
338 | */ |
||
339 | protected function configure() |
||
340 | { |
||
341 | $command = new ConsoleCommand(); |
||
342 | |||
343 | $command->name($this->commandName) |
||
344 | ->description($this->commandDescription); |
||
345 | |||
346 | if ($this->argument != null) { |
||
347 | $command->argument([ |
||
348 | 'name' => $this->argument, |
||
349 | 'description' => $this->argumentDescription, |
||
350 | 'type' => $this->argumentType, |
||
351 | ]); |
||
352 | } |
||
353 | $this->configureCommand($command); |
||
354 | } |
||
355 | } |
||
356 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.