Skip to content

Commit f6432ff

Browse files
Merge pull request #8 from Syrian-Open-Source/v1.1.0
V1.1.0
2 parents af58d87 + 920178f commit f6432ff

File tree

10 files changed

+219
-31
lines changed

10 files changed

+219
-31
lines changed

.github/workflows/php-cs-fixer.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Check & fix styling
2+
3+
on: [push,pull_request]
4+
5+
jobs:
6+
php-cs-fixer:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- name: Checkout code
11+
uses: actions/checkout@v2
12+
with:
13+
ref: ${{ github.head_ref }}
14+
15+
- name: Run PHP Code Style Fixer
16+
uses: docker://oskarstark/php-cs-fixer-ga
17+
with:
18+
args: --config=.php-cs-fixer.dist.php --allow-risky=yes
19+
20+
- name: Commit changes
21+
uses: stefanzweifel/git-auto-commit-action@v4
22+
with:
23+
commit_message: Fix styling

.github/workflows/php.yml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: PHP Composer
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches: [main]
66
pull_request:
7-
branches: [ main ]
7+
branches: [main]
88

99
jobs:
1010
build:
@@ -27,9 +27,3 @@ jobs:
2727
${{ runner.os }}-php-
2828
- name: Install dependencies
2929
run: composer install --prefer-dist --no-progress
30-
31-
# Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit"
32-
# Docs: https://getcomposer.org/doc/articles/scripts.md
33-
34-
# - name: Run test suite
35-
# run: composer run-script test

.github/workflows/tests.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ jobs:
99
fail-fast: false
1010
matrix:
1111
os: [ubuntu-latest]
12-
php: [8.1, 8.0]
13-
laravel: [8.*]
12+
php: [7.4, 8.0]
13+
laravel: [8.*,7.*]
1414
dependency-version: [prefer-stable]
1515
include:
16-
- testbench: ^6.23
17-
laravel: 8.*
16+
- testbench: ^6.23
17+
laravel: 8.*
1818

19+
- testbench: 5.20
20+
laravel: 7.*
1921

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: "Update Changelog"
2+
3+
on:
4+
release:
5+
types: [released]
6+
7+
jobs:
8+
update:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v2
14+
with:
15+
ref: main
16+
17+
- name: Update Changelog
18+
uses: stefanzweifel/changelog-updater-action@v1
19+
with:
20+
latest-version: ${{ github.event.release.name }}
21+
release-notes: ${{ github.event.release.body }}
22+
23+
- name: Commit updated CHANGELOG
24+
uses: stefanzweifel/git-auto-commit-action@v4
25+
with:
26+
branch: main
27+
commit_message: Update CHANGELOG
28+
file_pattern: CHANGELOG.md

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ php artisan multi-process:install
2222
Features
2323
-----------
2424
- [Run process from commands](https://github.com/syrian-open-source/laravel-multi-process/blob/main/docs/commands.md)
25+
- [Run php codes](https://github.com/syrian-open-source/laravel-multi-process/blob/main/docs/php.md)
2526

2627

2728
Changelog

docs/commands.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,26 @@ Usage
33
* Define multiple process and execute them by start function.
44

55
```shell
6-
$proceses = \SOS\MultiProcess\Facades\MultiProcessFacade::setTasks(
6+
$process = \SOS\MultiProcess\Facades\MultiProcessFacade::setTasks(
77
"php artisan make:model modelName",
88
"php artisan make:model ControllerName",
99
// and you can define unlimited commands
1010
);
11-
$proceses->start();
11+
$process->start();
1212

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

1616
```shell
1717

18-
$proceses = \SOS\MultiProcess\Facades\MultiProcessFacade::setTasks(
18+
$process = \SOS\MultiProcess\Facades\MultiProcessFacade::setTasks(
1919
"php artisan make:model modelName",
2020
"php artisan make:model ControllerName",
2121
// and you can define unlimited commands
2222
);
2323

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

2727
```
2828
* Add options.
@@ -32,7 +32,7 @@ Usage
3232
// default options are in multi_process.php file.
3333
// you can change them from the file
3434
// or you can basicly added them from the setter function.
35-
$proceses = \SOS\MultiProcess\Facades\MultiProcessFacade::setTasks(
35+
$process = \SOS\MultiProcess\Facades\MultiProcessFacade::setTasks(
3636
"php artisan make:model modelName",
3737
"php artisan make:model ControllerName",
3838
// and you can define unlimited commands
@@ -41,10 +41,12 @@ Usage
4141
'ideTimeOut' => 60,
4242
'enableOutput' => true,
4343
'processTime' => 3,
44+
45+
// thorw exceprtion if any task was failed.
46+
'throwIfTaskNotSuccess' => false,
4447
);
4548

4649
// run or start your tasks.
47-
$proceses->start();
50+
$process->start();
4851

4952
```
50-

docs/php.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Usage
2+
--------
3+
* Define multiple process and execute them by start function,
4+
each task must be a callback function as you can see in the below example.
5+
6+
```shell
7+
$process = \SOS\MultiProcess\Facades\MultiProcessFacade::setTasks(
8+
function () {
9+
return \Illuminate\Support\Facades\DB::statement('delete from users where id = 5');
10+
}, function () {
11+
return \Illuminate\Support\Facades\DB::statement('delete from users where id = 6');
12+
}
13+
);
14+
$process->runPHP();
15+
```
16+
* Add options.
17+
18+
```shell
19+
20+
// default options are in multi_process.php file.
21+
// you can change them from the file
22+
// or you can basicly added them from the setter function.
23+
$process = \SOS\MultiProcess\Facades\MultiProcessFacade::setTasks(
24+
"php artisan make:model modelName",
25+
"php artisan make:model ControllerName",
26+
// and you can define unlimited commands
27+
)->setOptions([
28+
'timeOut' => 60,
29+
'ideTimeOut' => 60,
30+
'enableOutput' => true,
31+
'processTime' => 3,
32+
33+
// thorw exceprtion if any task was failed.
34+
'throwIfTaskNotSuccess' => false,
35+
);
36+
37+
// run or start your tasks.
38+
$process->start();
39+
40+
```
41+

src/Classes/MultiProcess.php

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
namespace SOS\MultiProcess\Classes;
55

66

7+
use Symfony\Component\Process\Exception\ProcessFailedException;
8+
use Symfony\Component\Process\PhpProcess;
79
use Symfony\Component\Process\Process;
810

911
/**
@@ -32,6 +34,7 @@ class MultiProcess
3234
'workingDirectory' => null,
3335
'enableOutput' => true,
3436
'processTime' => 3,
37+
'throwIfTaskNotSuccess' => false,
3538
];
3639
/**
3740
*
@@ -165,6 +168,22 @@ public function displayOutputMessage($type, $buffer)
165168
}
166169
}
167170

171+
/**
172+
* run the php codes
173+
*
174+
* @return \SOS\MultiProcess\Classes\MultiProcess
175+
* @throws \Exception
176+
* @author karam mustafa
177+
*/
178+
public function runPHP()
179+
{
180+
181+
$this->phpProcess($this->higherOrderRun());
182+
183+
$this->resolveNotRunningProcess($this->higherOrderRun());
184+
185+
return $this;
186+
}
168187

169188
/**
170189
* this function will execute run function in symfony component
@@ -175,19 +194,9 @@ public function displayOutputMessage($type, $buffer)
175194
*/
176195
public function run()
177196
{
178-
$callback = function (Process $process) {
179-
return $process->run(function ($type, $buffer) {
197+
$this->process($this->higherOrderRun());
180198

181-
// if we enable the output, then display this message depending on it type.
182-
if ($this->getOptions('enableOutput')) {
183-
$this->displayOutputMessage($type, $buffer);
184-
}
185-
});
186-
};
187-
188-
$this->process($callback);
189-
190-
$this->resolveNotRunningProcess($callback);
199+
$this->resolveNotRunningProcess($this->higherOrderRun());
191200

192201
return $this;
193202
}
@@ -213,6 +222,29 @@ public function start()
213222

214223
}
215224

225+
/**
226+
* run the php codes from tasks, and each task must be a callback function.
227+
*
228+
* @param $callback
229+
*
230+
* @author karam mustafa
231+
*/
232+
private function phpProcess($callback)
233+
{
234+
while ($task = $this->checkIfCanProcess()) {
235+
236+
$process = new PhpProcess("<?php {$task[$this->getCommandKey()]()} ?>");
237+
238+
// Add the process to the processing property
239+
$this->processing[] = $process;
240+
241+
$callback($process);
242+
243+
if (!$process->isSuccessful() && $this->getOptions('throwIfTaskNotSuccess')) {
244+
throw new ProcessFailedException($process);
245+
}
246+
}
247+
}
216248

217249
/**
218250
* this function will set the require config to a symfony process component.
@@ -239,6 +271,11 @@ private function process($callback)
239271
// this callback could be a start or run function in symfony component
240272
// or might be any callback that accept Process parameter as a dependency.
241273
$callback($process);
274+
275+
if (!$process->isSuccessful() && $this->getOptions('throwIfTaskNotSuccess')) {
276+
throw new ProcessFailedException($process);
277+
}
278+
242279
}
243280
}
244281

@@ -338,4 +375,23 @@ private function checkIfCanProcess()
338375
: false;
339376
}
340377

378+
/**
379+
* return a callback that execute run function inside process component.
380+
*
381+
* @return \Closure
382+
* @author karam mustafa
383+
*/
384+
private function higherOrderRun()
385+
{
386+
return function (Process $process) {
387+
return $process->run(function ($type, $buffer) {
388+
389+
// if we enable the output, then display this message depending on it type.
390+
if ($this->getOptions('enableOutput')) {
391+
$this->displayOutputMessage($type, $buffer);
392+
}
393+
});
394+
};
395+
}
396+
341397
}

src/Facades/MultiProcessFacade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* @method MultiProcess setTasks( ...$args)
1212
* @method MultiProcess start($callback)
1313
* @method MultiProcess run($callback)
14+
* @method MultiProcess runPHP($callback)
1415
* @method MultiProcess setOptions($options)
1516
*/
1617
class MultiProcessFacade extends Facade

tests/Feature/PHPTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace SOS\MultiProcess\Tests\Feature;
4+
5+
6+
use SOS\MultiProcess\Classes\MultiProcess;
7+
use SOS\MultiProcess\Tests\BaseTest;
8+
9+
class PHPTest extends BaseTest
10+
{
11+
12+
/**
13+
* test if the php codes are running successfully.
14+
*
15+
* @return void
16+
* @throws \Exception
17+
*
18+
*/
19+
public function test_if_php_code_run_successfully()
20+
{
21+
$completedState = 3;
22+
23+
$processor = (new MultiProcess())->setTasks(
24+
function () {
25+
echo 'process 1';
26+
},
27+
function () {
28+
echo 'process 2';
29+
},
30+
function () {
31+
echo 'process 3';
32+
}
33+
);
34+
35+
foreach ($processor->runPHP()->getTasks() as $task) {
36+
$this->assertEquals($task['state'], $completedState);
37+
}
38+
39+
}
40+
}

0 commit comments

Comments
 (0)