Skip to content

Commit

Permalink
Merge pull request #16 from ash-jc-allen/v2.1.0
Browse files Browse the repository at this point in the history
v2.1.0
  • Loading branch information
ash-jc-allen authored Oct 5, 2020
2 parents d649563 + c1dd06a commit 09fd0c2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

**v2.1.0 (released 2020-10-05):**
- Added a timeOut option for the Artisan and external commands. [#14](https://github.com/ash-jc-allen/laravel-executor/pull/14)

**v2.0.0 (released 2020-09-16):**
- Added the functionality to run interactive commands. [#9](https://github.com/ash-jc-allen/laravel-executor/pull/9) [#10](https://github.com/ash-jc-allen/laravel-executor/pull/10)
- Added support for Laravel 8 and Guzzle 7. [#12](https://github.com/ash-jc-allen/laravel-executor/pull/12)
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,13 @@ class AppUpdate extends Executor
}
```

Note: In some cases, you may want to run a command that requires your input. For example, you might have a command that
In some cases, you may want to run a command that requires your input. For example, you might have a command that
creates a new user in the database and need you to input some details. In this case, you can pass ``` true ``` as the second
parameter to the ``` ->runArtisan() ``` method to specify that it is an interactive command.

To determine the process timeout for the command, you can also pass a time in seconds as the third parameter to the
``` ->runArtisan() ``` method.

#### Adding a Command
To run a command (that can't be run with Artisan) via your Executor class, you can add the ``` runExternal() ``` method to your Executor's ``` run() ```
method. For example, the code below shows how you could set the Executor to run the built-in Composer ``` composer install ```
Expand All @@ -128,10 +131,14 @@ class AppUpdate extends Executor
}
```

Note: In some cases, you may want to run a command that requires your input. For example, you might have a command that
In some cases, you may want to run a command that requires your input. For example, you might have a command that
creates a new user in the database and need you to input some details. In this case, you can pass ``` true ``` as the second
parameter to the ``` ->runExternal() ``` method to specify that it is an interactive command.

To determine the process timeout for the command, you can also pass a time in seconds as the third parameter to the
``` ->runExternal() ``` method.


#### Adding a Closure
Sometimes you might want to run some code that doesn't necessarily fit into an existing command. In this case, you can add a closure
to your Executor instead. The example below shows how to pass a simple closure to your Executor class:
Expand Down
21 changes: 13 additions & 8 deletions src/Classes/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,18 @@ abstract public function run(): self;
* should be executed.
*
* @param string $command
* @param int $timeOut
* @param bool $isInteractive
* @return $this
* @throws ExecutorException
*/
public function runArtisan(string $command, bool $isInteractive = false): self
public function runArtisan(string $command, bool $isInteractive = false, int $timeOut = 60): self
{
$this->validateCommand($command, $isInteractive);

$command = 'php artisan '.$command;

$this->runCommand($command, $isInteractive);
$this->runCommand($command, $isInteractive, $timeOut);

return $this;
}
Expand All @@ -82,15 +83,16 @@ public function runArtisan(string $command, bool $isInteractive = false): self
* items that should be executed.
*
* @param string $command
* @param int $timeOut
* @param bool $isInteractive
* @return $this
* @throws ExecutorException
*/
public function runExternal(string $command, bool $isInteractive = false): self
public function runExternal(string $command, bool $isInteractive = false, int $timeOut = 60): self
{
$this->validateCommand($command, $isInteractive);

$this->runCommand($command, $isInteractive);
$this->runCommand($command, $isInteractive, $timeOut);

return $this;
}
Expand Down Expand Up @@ -134,10 +136,11 @@ public function ping(string $url, array $headers = []): self
/**
* Handle the running of a console command.
*
* @param string $commandToRun
* @param bool $isInteractive
* @param string $commandToRun
* @param int $timeOut
* @param bool $isInteractive
*/
private function runCommand(string $commandToRun, bool $isInteractive = false): void
private function runCommand(string $commandToRun, bool $isInteractive = false, int $timeOut = 60): void
{
if ($isInteractive) {
$this->runInteractiveCommand($commandToRun);
Expand All @@ -147,6 +150,8 @@ private function runCommand(string $commandToRun, bool $isInteractive = false):

$process = new Process(explode(' ', $commandToRun));

$process->setTimeout($timeOut);

$process->setWorkingDirectory(base_path());

$process->run(function ($type, $buffer) {
Expand All @@ -163,7 +168,7 @@ private function runCommand(string $commandToRun, bool $isInteractive = false):
/**
* Handle the running of an interactive console command.
*
* @param string $commandToRun
* @param string $commandToRun
*/
private function runInteractiveCommand(string $commandToRun): void
{
Expand Down

0 comments on commit 09fd0c2

Please sign in to comment.