1. Go to this page and download the library: Download darwinnatha/laravel-process library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
darwinnatha / laravel-process example snippets
// app/Processes/AbstractProcess.php
abstract class AbstractProcess
{
public array $tasks = [];
public function handle(mixed $input): mixed
{
// Your custom logging
Log::info('Process started', ['process' => static::class]);
// Your custom validation
$this->validateInput($input);
// Your custom transaction handling
DB::beginTransaction();
try {
$result = Pipeline::send($input)
->through($this->getMiddleware()) // Your middleware
->through($this->tasks)
->thenReturn();
DB::commit();
// Your custom success handling
$this->onSuccess($result);
return $result;
} catch (Throwable $e) {
DB::rollBack();
// Your custom error handling
$this->onError($e);
throw $e;
}
}
// Your custom methods
protected function validateInput(mixed $input): void { /* ... */ }
protected function getMiddleware(): array { return []; }
protected function onSuccess(mixed $result): void { /* ... */ }
protected function onError(Throwable $e): void { /* ... */ }
}
namespace App\Processes\User;
use App\Processes\AbstractProcess; // Your custom base class
class CreateUserProcess extends AbstractProcess
{
public array $tasks = [
Tasks\ValidateUserData::class,
Tasks\SaveUserToDatabase::class,
Tasks\SendWelcomeEmail::class,
];
}
final class ValidateUserData implements TaskInterface
{
public function __invoke(mixed $input, callable $next): mixed
{
// Handle any input type
$data = match(true) {
$input instanceof Request => $input->validated(),
is_array($input) => $input,
is_object($input) => (array) $input,
default => throw new InvalidArgumentException('Unsupported input')
};
// Your validation logic
if (!$this->isValid($data)) {
throw new ValidationException('Invalid data');
}
return $next($input);
}
}
$process = new CreateUserProcess();
// With an array
$result = $process->handle(['name' => 'John', 'email' => '[email protected]']);
// With a Request
$result = $process->handle($request);
// With a DTO
$result = $process->handle($userDto);
// With any custom object
$result = $process->handle($customObject);
use DarwinNatha\Process\Traits\FormatsApiResponse;
final class CreateUserTask implements TaskInterface
{
use FormatsApiResponse; // Optional!
public function __invoke(mixed $input, callable $next): mixed
{
try {
$user = User::create($this->extractData($input));
return $this->created($user, 'User created successfully');
} catch (Exception $e) {
return $this->error('User creation failed', ['error' => $e->getMessage()]);
}
}
}
public function handle(mixed $input): mixed
{
$cacheKey = $this->getCacheKey($input);
if ($cacheKey && Cache::has($cacheKey)) {
return Cache::get($cacheKey);
}
$result = $this->executeProcess($input);
if ($cacheKey) {
Cache::put($cacheKey, $result, $this->getCacheTtl());
}
return $result;
}