Skip to content

Commit

Permalink
feat: merge config
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Jun 13, 2020
1 parent a0c512e commit 0046388
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 30 deletions.
52 changes: 22 additions & 30 deletions src/EloquentDumperServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Recca0120\EloquentDumper;

use Closure;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\Arr;
Expand All @@ -16,18 +17,20 @@ class EloquentDumperServiceProvider extends ServiceProvider
*/
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/eloquent-dumper.php', 'eloquent-dumper');

$this->app->singleton(Dumper::class, function ($app) {
return new Dumper(
Arr::get($app['config'], 'eloquent-dumper.driver', Dumper::DEFAULT)
);
});

$closures = $this->getClosures();
$this->app->singleton(EloquentHelper::class, EloquentHelper::class);

Builder::macro($closures['sql']['name'], $closures['sql']['method']);
QueryBuilder::macro($closures['sql']['name'], $closures['sql']['method']);
Builder::macro($closures['dump']['name'], $closures['dump']['method']);
QueryBuilder::macro($closures['dump']['name'], $closures['dump']['method']);
Builder::macro('sql', self::sql());
QueryBuilder::macro('sql', self::sql());
Builder::macro('dumpSql', self::dump());
QueryBuilder::macro('dumpSql', self::dump());
}

/**
Expand All @@ -45,33 +48,22 @@ public function boot()
}

/**
* @return array[]
* @return Closure
*/
private function getClosures()
private static function sql()
{
$app = $this->app;

return [
'sql' => [
'name' => 'sql',
'method' => function () use ($app) {
return $app->make(Dumper::class)->dump($this->toSql(), $this->getBindings());
},
],
'dump' => [
'name' => 'dumpSql',
'method' => function () use ($app) {
$sql = $app->make(Dumper::class)->dump($this->toSql(), $this->getBindings());

if ($app->runningInConsole()) {
echo "\n".$sql."\n";

return;
}
return function () {
return app(EloquentHelper::class)->sql($this);
};
}

function_exists('dump') ? dump($sql) : var_dump($sql);
},
],
];
/**
* @return Closure
*/
private static function dump()
{
return function () {
return app(EloquentHelper::class)->dump($this);
};
}
}
62 changes: 62 additions & 0 deletions src/EloquentHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Recca0120\EloquentDumper;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Builder as QueryBuilder;

class EloquentHelper
{
/**
* @var Dumper
*/
private $dumper;
/**
* @var bool
*/
private $runningInConsole;
/**
* @var Application
*/
private $app;

/**
* EloquentHelper constructor.
* @param Dumper $dumper
* @param Application $app
*/
public function __construct(Dumper $dumper, Application $app)
{
$this->dumper = $dumper;
$this->app = $app;
}

/**
* @param QueryBuilder|Builder $query
* @return string
*/
public function sql($query)
{
return $this->dumper->dump($query->toSql(), $query->getBindings());
}

/**
* @param QueryBuilder|Builder $query
* @return Builder|QueryBuilder
*/
public function dump($query)
{
$sql = $this->sql($query);

if ($this->app->runningInConsole()) {
echo "\n".$sql."\n";

return $query;
}

function_exists('dump') ? dump($sql) : var_dump($sql);

return $query;
}
}

0 comments on commit 0046388

Please sign in to comment.