Skip to content

Commit 1386d25

Browse files
authored
Add support for returnArray and latest flags to the Show command (#6)
1 parent 9eb0c1b commit 1386d25

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/Commands/BaseCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,14 @@ protected function runComposerCommand(): array
168168
$return = [
169169
'code' => $code,
170170
'output' => preg_split('/(\n|\r\n)/', trim($output->fetch())),
171+
'arguments' => $arguments,
171172
];
172173
} catch (\Exception $e) {
173174
$return = [
174175
'code' => 1,
175176
'output' => preg_split('/(\n|\r\n)/', $e->getMessage()),
176177
'exception' => $e,
178+
'arguments' => $arguments,
177179
];
178180
}
179181

src/Commands/Show.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@ class Show extends BaseCommand
2424
* @param ShowMode $mode Mode to run the command against
2525
* @param string|null $package Individual package to search
2626
* @param boolean $noDev Exclude dev dependencies from search
27+
* @param boolean $latest Include the latest key (might only be present when returnArray = true)
28+
* @param boolean $returnArray Return the results as an array.
2729
* @return void
2830
*/
2931
final public function __construct(
3032
Composer $composer,
3133
public ShowMode $mode = ShowMode::INSTALLED,
3234
public ?string $package = null,
33-
public bool $noDev = false
35+
public bool $noDev = false,
36+
public bool $latest = false,
37+
public bool $returnArray = false,
3438
) {
3539
parent::__construct($composer);
3640
}
@@ -56,6 +60,13 @@ public function execute()
5660
}
5761

5862
$results = json_decode(implode(PHP_EOL, $output['output']), true);
63+
64+
if ($this->returnArray) {
65+
return $this->package
66+
? $results ?? []
67+
: $results['installed'] ?? [];
68+
}
69+
5970
$packages = [];
6071

6172
if (is_null($this->package) && $this->mode->isCollectible()) {
@@ -236,6 +247,10 @@ protected function arguments(): array
236247
$arguments['--no-dev'] = true;
237248
}
238249

250+
if ($this->latest) {
251+
$arguments['--latest'] = true;
252+
}
253+
239254
$arguments['--format'] = 'json';
240255

241256
return $arguments;

0 commit comments

Comments
 (0)