Skip to content

Instantly share code, notes, and snippets.

@AhmedHelalAhmed
Last active September 24, 2021 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AhmedHelalAhmed/526445de2a7c7326910c41d9711603a6 to your computer and use it in GitHub Desktop.
Save AhmedHelalAhmed/526445de2a7c7326910c41d9711603a6 to your computer and use it in GitHub Desktop.
DumpService
<?php
use App\Models\Customer;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
/**
* Class DumpService
* @author Ahmed Helal Ahmed
*/
class DumpService
{
private bool $isLogFileEnabled = false;
private bool $isDataBaseTransactionEnabled = false;
private bool $isQueryLogEnabled = false;
private string $url = "http://127.0.0.1:8990";
public function execute()
{
$this->dumpStartOfScript();
if ($this->isDataBaseTransactionEnabled) {
DB::beginTransaction();
}
if ($this->isQueryLogEnabled) {
DB::enableQueryLog();
}
if ($this->isLogFileEnabled) {
file_put_contents(storage_path('logs/laravel.log'), "\n\n\n");
Log::channel('single')->debug('START');
}
echo "\n\n";
$this->handle();
echo "\n\n";
if ($this->isQueryLogEnabled) {
$queries = collect(DB::getQueryLog())->map(function (array $queryDetails) {
$bindings = collect($queryDetails['bindings'])
->map(function ($param) {
if (is_numeric($param)) {
return $param;
} else {
return "'${param}'";
}
});
return Str::replaceArray('?', $bindings->toArray(), $queryDetails['query']);
});
if ($queries->isNotEmpty()) {
dump("========================( QUERIES )===============================");
echo "\n\n";
$queries->each(function ($query, $index) {
dump(($index + 1) . "- $query");
echo "\n";
});
}
echo "\n\n";
}
if ($this->isLogFileEnabled) {
Log::channel('single')->debug('END');
}
$this->dieEndOfScript();
if ($this->isDataBaseTransactionEnabled) {
DB::commit();
}
}
private function dumpStartOfScript()
{
dump("=========================( START )===============================");
echo "\n\n";
}
/**
* Write you code here
*/
private function handle()
{
// =======================================================> START
Customer::query()->whereIn('id', [1])->first();
dump([
'start' => 'Hello world',
'User' => \App\Models\Customer::first(),
'end' => 'Good bye!'
]);
// =======================================================> END
}
private function dieEndOfScript()
{
dd("=========================( END )===============================");
}
}
<?php
require('DumpService.php');
(new DumpService())->execute();
@AhmedHelalAhmed
Copy link
Author

php artisan tinker laravel-tinker/test.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment