DumpService
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 )==============================="); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require('DumpService.php'); | |
(new DumpService())->execute(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
php artisan tinker laravel-tinker/test.php