PHPackages                             jascha030/cli - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. jascha030/cli

ActiveLibrary[CLI &amp; Console](/categories/cli)

jascha030/cli
=============

Service class for executing shell commands using symfony/process, heavily inspired by the CommandLine class in laravel/valet.

1.2.0(3mo ago)047↓100%MITPHPPHP &gt;=8.3CI passing

Since Feb 28Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/jascha030/cli)[ Packagist](https://packagist.org/packages/jascha030/cli)[ RSS](/packages/jascha030-cli/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (4)Versions (6)Used By (0)

PHP CLI service
===============

[](#php-cli-service)

Interface and implementation of a CLI Service class for executing shell commands with php, using `symfony/process`and `symfony/console`, **heavily** inspired by [the CommandLine class](https://github.com/laravel/valet/blob/master/cli/Valet/CommandLine.php) from `laravel/valet`.

Getting Started
---------------

[](#getting-started)

### Requirements

[](#requirements)

- PHP `^8.0`
- Composer `*` (but preferred `2.2` or later)

### Installation

[](#installation)

```
composer require jascha030/cli
```

Usage
-----

[](#usage)

The package contents consist mostly of one simple service class implementing an interface. Next to that there are also some derivatives of these classes and interfaces.

### ShellInterface

[](#shellinterface)

The main interface is the `Jascha030\CLI\Shell\ShellInterface`, which requires a class to implement four methods.

```
interface ShellInterface
{
    /**
     * Run a shell command.
     */
    public function run(string $command, ?string $cwd = null, ?callable $onError = null): string;

    /**
     * Run a shell command with sudo user capabilities.
     */
    public function runAsUser(string $command, ?string $cwd = null, ?callable $onError = null): string;

    /**
     * Run a shell command without writing output to the STDOUT or php.
     */
    public function quietly(string $command, ?string $cwd = null, ?callable $onError = null): void;

    /**
     * Run a shell command with sudo user capabilities,  without writing output to the STDOUT or php.
     */
    public function quietlyAsUser(string $command, ?string $cwd = null, ?callable $onError = null): void;
}
```

### Default Implementation

[](#default-implementation)

This package also provides class as most-basic implementation of the `Jascha030\CLI\Shell\ShellInterface`, The `Jascha030\CLI\Shell\Shell` class. This class implements all four methods.

All four methods use the `Shell::runCommand` method, which is also directly available.

```
public function runCommand(string $command, ?string $cwd = null, ?callable $onError = null): string;
```

**Below is a simple example of basic usage of the class**

```
