Skip to content

Commit

Permalink
Merge pull request #8 from Syrian-Open-Source/v1.1.0
Browse files Browse the repository at this point in the history
V1.1.0
  • Loading branch information
karam-mustafa authored Jan 21, 2022
2 parents af58d87 + 920178f commit f6432ff
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 31 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/php-cs-fixer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Check & fix styling

on: [push,pull_request]

jobs:
php-cs-fixer:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2
with:
ref: ${{ github.head_ref }}

- name: Run PHP Code Style Fixer
uses: docker://oskarstark/php-cs-fixer-ga
with:
args: --config=.php-cs-fixer.dist.php --allow-risky=yes

- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Fix styling
10 changes: 2 additions & 8 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: PHP Composer

on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]

jobs:
build:
Expand All @@ -27,9 +27,3 @@ jobs:
${{ runner.os }}-php-
- name: Install dependencies
run: composer install --prefer-dist --no-progress

# Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit"
# Docs: https://getcomposer.org/doc/articles/scripts.md

# - name: Run test suite
# run: composer run-script test
10 changes: 6 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
php: [8.1, 8.0]
laravel: [8.*]
php: [7.4, 8.0]
laravel: [8.*,7.*]
dependency-version: [prefer-stable]
include:
- testbench: ^6.23
laravel: 8.*
- testbench: ^6.23
laravel: 8.*

- testbench: 5.20
laravel: 7.*

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}

Expand Down
28 changes: 28 additions & 0 deletions .github/workflows/update-changelog.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: "Update Changelog"

on:
release:
types: [released]

jobs:
update:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2
with:
ref: main

- name: Update Changelog
uses: stefanzweifel/changelog-updater-action@v1
with:
latest-version: ${{ github.event.release.name }}
release-notes: ${{ github.event.release.body }}

- name: Commit updated CHANGELOG
uses: stefanzweifel/git-auto-commit-action@v4
with:
branch: main
commit_message: Update CHANGELOG
file_pattern: CHANGELOG.md
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ php artisan multi-process:install
Features
-----------
- [Run process from commands](https://github.com/syrian-open-source/laravel-multi-process/blob/main/docs/commands.md)
- [Run php codes](https://github.com/syrian-open-source/laravel-multi-process/blob/main/docs/php.md)


Changelog
Expand Down
16 changes: 9 additions & 7 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ Usage
* Define multiple process and execute them by start function.

```shell
$proceses = \SOS\MultiProcess\Facades\MultiProcessFacade::setTasks(
$process = \SOS\MultiProcess\Facades\MultiProcessFacade::setTasks(
"php artisan make:model modelName",
"php artisan make:model ControllerName",
// and you can define unlimited commands
);
$proceses->start();
$process->start();

```
* Define multiple process and execute them by run function.

```shell

$proceses = \SOS\MultiProcess\Facades\MultiProcessFacade::setTasks(
$process = \SOS\MultiProcess\Facades\MultiProcessFacade::setTasks(
"php artisan make:model modelName",
"php artisan make:model ControllerName",
// and you can define unlimited commands
);

// run function will allows you to get the output from the execution process.
$proceses->run();
$process->run();

```
* Add options.
Expand All @@ -32,7 +32,7 @@ Usage
// default options are in multi_process.php file.
// you can change them from the file
// or you can basicly added them from the setter function.
$proceses = \SOS\MultiProcess\Facades\MultiProcessFacade::setTasks(
$process = \SOS\MultiProcess\Facades\MultiProcessFacade::setTasks(
"php artisan make:model modelName",
"php artisan make:model ControllerName",
// and you can define unlimited commands
Expand All @@ -41,10 +41,12 @@ Usage
'ideTimeOut' => 60,
'enableOutput' => true,
'processTime' => 3,

// thorw exceprtion if any task was failed.
'throwIfTaskNotSuccess' => false,
);

// run or start your tasks.
$proceses->start();
$process->start();

```
41 changes: 41 additions & 0 deletions docs/php.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Usage
--------
* Define multiple process and execute them by start function,
each task must be a callback function as you can see in the below example.

```shell
$process = \SOS\MultiProcess\Facades\MultiProcessFacade::setTasks(
function () {
return \Illuminate\Support\Facades\DB::statement('delete from users where id = 5');
}, function () {
return \Illuminate\Support\Facades\DB::statement('delete from users where id = 6');
}
);
$process->runPHP();
```
* Add options.

```shell

// default options are in multi_process.php file.
// you can change them from the file
// or you can basicly added them from the setter function.
$process = \SOS\MultiProcess\Facades\MultiProcessFacade::setTasks(
"php artisan make:model modelName",
"php artisan make:model ControllerName",
// and you can define unlimited commands
)->setOptions([
'timeOut' => 60,
'ideTimeOut' => 60,
'enableOutput' => true,
'processTime' => 3,

// thorw exceprtion if any task was failed.
'throwIfTaskNotSuccess' => false,
);

// run or start your tasks.
$process->start();

```
80 changes: 68 additions & 12 deletions src/Classes/MultiProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
namespace SOS\MultiProcess\Classes;


use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\PhpProcess;
use Symfony\Component\Process\Process;

/**
Expand Down Expand Up @@ -32,6 +34,7 @@ class MultiProcess
'workingDirectory' => null,
'enableOutput' => true,
'processTime' => 3,
'throwIfTaskNotSuccess' => false,
];
/**
*
Expand Down Expand Up @@ -165,6 +168,22 @@ public function displayOutputMessage($type, $buffer)
}
}

/**
* run the php codes
*
* @return \SOS\MultiProcess\Classes\MultiProcess
* @throws \Exception
* @author karam mustafa
*/
public function runPHP()
{

$this->phpProcess($this->higherOrderRun());

$this->resolveNotRunningProcess($this->higherOrderRun());

return $this;
}

/**
* this function will execute run function in symfony component
Expand All @@ -175,19 +194,9 @@ public function displayOutputMessage($type, $buffer)
*/
public function run()
{
$callback = function (Process $process) {
return $process->run(function ($type, $buffer) {
$this->process($this->higherOrderRun());

// if we enable the output, then display this message depending on it type.
if ($this->getOptions('enableOutput')) {
$this->displayOutputMessage($type, $buffer);
}
});
};

$this->process($callback);

$this->resolveNotRunningProcess($callback);
$this->resolveNotRunningProcess($this->higherOrderRun());

return $this;
}
Expand All @@ -213,6 +222,29 @@ public function start()

}

/**
* run the php codes from tasks, and each task must be a callback function.
*
* @param $callback
*
* @author karam mustafa
*/
private function phpProcess($callback)
{
while ($task = $this->checkIfCanProcess()) {

$process = new PhpProcess("<?php {$task[$this->getCommandKey()]()} ?>");

// Add the process to the processing property
$this->processing[] = $process;

$callback($process);

if (!$process->isSuccessful() && $this->getOptions('throwIfTaskNotSuccess')) {
throw new ProcessFailedException($process);
}
}
}

/**
* this function will set the require config to a symfony process component.
Expand All @@ -239,6 +271,11 @@ private function process($callback)
// this callback could be a start or run function in symfony component
// or might be any callback that accept Process parameter as a dependency.
$callback($process);

if (!$process->isSuccessful() && $this->getOptions('throwIfTaskNotSuccess')) {
throw new ProcessFailedException($process);
}

}
}

Expand Down Expand Up @@ -338,4 +375,23 @@ private function checkIfCanProcess()
: false;
}

/**
* return a callback that execute run function inside process component.
*
* @return \Closure
* @author karam mustafa
*/
private function higherOrderRun()
{
return function (Process $process) {
return $process->run(function ($type, $buffer) {

// if we enable the output, then display this message depending on it type.
if ($this->getOptions('enableOutput')) {
$this->displayOutputMessage($type, $buffer);
}
});
};
}

}
1 change: 1 addition & 0 deletions src/Facades/MultiProcessFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* @method MultiProcess setTasks( ...$args)
* @method MultiProcess start($callback)
* @method MultiProcess run($callback)
* @method MultiProcess runPHP($callback)
* @method MultiProcess setOptions($options)
*/
class MultiProcessFacade extends Facade
Expand Down
40 changes: 40 additions & 0 deletions tests/Feature/PHPTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace SOS\MultiProcess\Tests\Feature;


use SOS\MultiProcess\Classes\MultiProcess;
use SOS\MultiProcess\Tests\BaseTest;

class PHPTest extends BaseTest
{

/**
* test if the php codes are running successfully.
*
* @return void
* @throws \Exception
*
*/
public function test_if_php_code_run_successfully()
{
$completedState = 3;

$processor = (new MultiProcess())->setTasks(
function () {
echo 'process 1';
},
function () {
echo 'process 2';
},
function () {
echo 'process 3';
}
);

foreach ($processor->runPHP()->getTasks() as $task) {
$this->assertEquals($task['state'], $completedState);
}

}
}

0 comments on commit f6432ff

Please sign in to comment.