PHP code example of teksite / handler

1. Go to this page and download the library: Download teksite/handler 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/ */

    

teksite / handler example snippets


'providers' => [
// Other Service Providers
Teksite\Handler\HandlerServiceProvider::class,
],

return ServiceWrapper::make()->do(function () {
// your code
})->ifFailed(function () {
// in case your code failed
})->run();

// Disable error handling wrapper
ServiceWrapper::make(withHandler: false)->do(fn() => $this->someMethod())->run();

// Disable database transaction (enabled by default)
ServiceWrapper::make(hasTransaction: false)->do(fn() => $this->someMethod())->run();

// Disable ServiceResult wrapping (enabled by default)
ServiceWrapper::make(wrapServiceResult: false)->do(fn() => $this->someMethod())->run();

public function run(bool $dispatchSuccessEvent = false, bool $dispatchFailureEvent = false): mixed

return ServiceWrapper::make()
->do(function () {
// Your business logic
return User::create($request->validated());
})
->ifFailed(function () {
// Fallback logic
return ['error' => 'User creation failed'];
})
->run(
dispatchSuccessEvent: true,  // Dispatches success event on completion
dispatchFailureEvent: false  // Disables failure event dispatch
);


return [
// ... other configurations

    'success_event_class' => "Teksite\\Handler\\Events\\OnSuccessEvent",
    'failure_event_class' => "Teksite\\Handler\\Events\\OnFailureEvent",
];

$result = ServiceWrapper::make()->do(fn() => Post::find(1))->run();

if ($result->isSuccess()) {
$post = $result->getData();
} else {
$error = $result->getData(); // Error information
}


$rawResult = ServiceWrapper::make(wrapServiceResult: false)->do(fn() => Post::find(1))->run();

use Teksite\Handler\Facade\FetchData;
use Teksite\Handler\Services\ServiceWrapper;

public function get(mixed $fetchData = [])
{
return ServiceWrapper::make()
->do(function () use ($fetchData) {
return FetchData::get(Post::class, ['title'], ...$fetchData);
})
->ifFailed(function () {
// Handle failure
})
->run(dispatchSuccessEvent: true);
}

use Teksite\Handler\Facade\FetchData;

// Without ServiceWrapper
$posts = FetchData::get(Post::class, ['title']);

$posts = FetchData::only(['id', 'title'])
->with(['user'])
->search(['title', 'category'])
->orderBy('title')
->sort('asc')
->perPage(20)
->limitPagination(100)
->get(Post::class);

// Clear all fluent configuration on a shared instance before reusing it
FetchData::reset();

FetchData::get(
string|Model|Builder|Relation|Closure $model,  // Model class, model instance, query builder, relation, or a closure returning a Builder
string|array|null $searchColumns = null,        // Search columns with operators
array|string|null $only = null,                 // Columns to select (defaults to all)
null|int|false $perPage = null,                 // Items per page (pagination)
null|false|int $limitPagination = null,         // Maximum items per page limit
array $with = [],                               // Relations to eager load
array $withCount = []                           // Relations to count
): \Illuminate\Support\Collection|\Illuminate\Pagination\LengthAwarePaginator;


$searchColumns = [
['column' => 'title', 'operator' => 'LIKE'],
['column' => 'category', 'operator' => '='],
'status' // Simple column search (default 'LIKE')
];

use Teksite\Handler\Services\ResponderServices;

// Success response
$response = ResponderServices::success('Well done!', ['post' => $post], 201);

// Failure response
$response = ResponderServices::failed('Something went wrong', ['auth' => 'forbidden'], 500);

// Redirect as HTTP response
$response->go();

// Return as JSON response (for APIs and AJAX)
$response->reply();


namespace App\Events;

use Illuminate\Foundation\Events\Dispatchable;

class UserCreationSucceeded
{
use Dispatchable;

    public function __construct(public $user)
    {
        //
    }
}


'success_event_class' => \App\Events\UserCreationSucceeded::class,

ServiceWrapper::make()
->do(fn() => User::create($data))
->run(dispatchSuccessEvent: true);

// Dispatch only success event
$wrapper->run(dispatchSuccessEvent: true, dispatchFailureEvent: false);

// Dispatch only failure event (default behavior)
$wrapper->run(dispatchSuccessEvent: false, dispatchFailureEvent: true);

// Dispatch both
$wrapper->run(dispatchSuccessEvent: true, dispatchFailureEvent: true);

// Dispatch none
$wrapper->run(dispatchSuccessEvent: false, dispatchFailureEvent: false);

bash
php artisan vendor:publish --provider="Teksite\Handler\ServiceProvider"