Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add methods for command output #17

Merged
merged 2 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 58 additions & 11 deletions src/ComposerReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function canWrite(): bool
if ($this->isProvidedAsJsonArray()) {
return false;
}

return is_file($this->file) && is_writable($this->file);
}

Expand Down Expand Up @@ -156,6 +157,62 @@ public function removeSection($section)
$this->_content = $content;
}

private ?string $_commandOutput = null;

private ?int $_commandStatus = null;

public function createCommand(string $command, ?string $folder = null): self
{
$this->_commandOutput = null;
$this->_commandStatus = null;

$olddir = null;
if ($folder === null) {
$folder = dirname($this->file);
$olddir = getcwd();
}

chdir($folder);

// Execute the command and capture its full output
$outputLines = [];
$status = null;
exec('composer ' . $command . ' 2>&1', $outputLines, $status);

// Store full output in case of failure
if ($status !== 0) {
// Failure: Keep all lines
$this->_commandOutput = implode("\n", $outputLines);
} else {
// Success: Keep only the last non-empty line
$filteredLines = array_filter($outputLines); // Remove empty lines
$this->_commandOutput = trim(end($filteredLines)); // Get last non-empty line
}

$this->_commandStatus = $status;

if ($olddir !== null) {
chdir($olddir);
}

return $this;
}

public function commandIsSuccessful(): bool
{
return $this->getCommandStatus() === 0;
}

public function getCommandOutput(): ?string
{
return trim($this->_commandOutput);
}

public function getCommandStatus(): ?int
{
return $this->_commandStatus;
}

/**
* Run a composer command in the given composer.json.
*
Expand All @@ -171,17 +228,7 @@ public function removeSection($section)
*/
public function runCommand($command)
{
$folder = dirname($this->file);
$olddir = getcwd();
chdir($folder);

ob_start();
$output = null;
$cmd = system('composer ' . $command, $output);
$output = ob_end_clean();
chdir($olddir);

return $cmd !== false;
return $this->createCommand($command)->commandIsSuccessful();
}

/**
Expand Down
17 changes: 17 additions & 0 deletions tests/ComposerReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,23 @@ public function testRunCommand()

$this->assertTrue($r);
}

public function testRunCommandOutput()
{
$reader = new ComposerReader($this->getWorkingComposerJson());

$this->assertTrue($reader->canRead());

$cmd = $reader->createCommand('dumpautoload');
$this->assertTrue($cmd->commandIsSuccessful());
$this->assertSame($cmd->getCommandOutput(), 'Generated autoload files');
$this->assertSame($cmd->getCommandStatus(), 0);

$cmdError = $reader->createCommand('dumpautoload --no-such-option');
$this->assertFalse($cmdError->commandIsSuccessful());
$this->assertSame($cmdError->getCommandStatus(), 1);
$this->assertStringContainsString('tion does not ex', $cmdError->getCommandOutput());
}

public function testRemove()
{
Expand Down