Skip to content

Commit

Permalink
Optimize register methods
Browse files Browse the repository at this point in the history
  • Loading branch information
guanguans committed Jul 11, 2020
1 parent 082a28e commit 78b5039
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 30 deletions.
15 changes: 15 additions & 0 deletions src/Exceptions/Exception.php
@@ -0,0 +1,15 @@
<?php

/*
* This file is part of the guanguans/laravel-raw-sql.
*
* (c) guanguans <ityaozm@gmail.com>
*
* This source file is subject to the MIT license that is bundled.
*/

namespace Guanguans\LaravelRawSql\Exceptions;

class Exception extends \Exception
{
}
15 changes: 15 additions & 0 deletions src/Exceptions/InvalidArgumentException.php
@@ -0,0 +1,15 @@
<?php

/*
* This file is part of the guanguans/laravel-raw-sql.
*
* (c) guanguans <ityaozm@gmail.com>
*
* This source file is subject to the MIT license that is bundled.
*/

namespace Guanguans\LaravelRawSql\Exceptions;

class InvalidArgumentException extends Exception
{
}
80 changes: 50 additions & 30 deletions src/ServiceProvider.php
Expand Up @@ -10,8 +10,10 @@

namespace Guanguans\LaravelRawSql;

use Closure;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Guanguans\LaravelRawSql\Exceptions\InvalidArgumentException;

class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
Expand All @@ -20,53 +22,71 @@ class ServiceProvider extends \Illuminate\Support\ServiceProvider
*/
public function boot()
{
$this->registerToRawSqlMacro();
$this->registerDumpRawSqlMacro();
$this->registerDdRawSqlMacro();
}
/**
* Register the `toRawSql` macro.
*/
$this->registerBuilderMacro('toRawSql', function ($macro) {
QueryBuilder::macro($macro, function () {
return array_reduce($this->getBindings(), function ($sql, $binding) {
return preg_replace('/\?/', is_numeric($binding) ? $binding : "'".$binding."'", $sql, 1);
}, $this->toSql());
});
});

/**
* Register the `toRawSql` macro.
*/
protected function registerToRawSqlMacro()
{
QueryBuilder::macro('toRawSql', function () {
return array_reduce($this->getBindings(), function ($sql, $binding) {
return preg_replace('/\?/', is_numeric($binding) ? $binding : "'".$binding."'", $sql, 1);
}, $this->toSql());
/**
* Register the `dumpRawSql` macro.
*/
$this->registerBuilderMacro('dumpRawSql', function ($macro) {
QueryBuilder::macro($macro, function () {
dump($this->toRawSql());
});
});

EloquentBuilder::macro('toRawSql', function () {
return ($this->getQuery()->toRawSql());
/**
* Register the `ddRawSql` macro.
*/
$this->registerBuilderMacro('ddRawSql', function ($macro) {
QueryBuilder::macro($macro, function () {
dd($this->toRawSql());
});
});
}

/**
* Register the `dumpRawSql` macro.
* @param $macro
* @param \Closure $closure
* @return bool
* @throws \Guanguans\LaravelRawSql\Exceptions\InvalidArgumentException
*/
protected function registerDumpRawSqlMacro()
protected function registerBuilderMacro($macro, Closure $closure)
{
QueryBuilder::macro('dumpRawSql', function () {
dump($this->toRawSql());
});
if (!is_string($macro)) {
throw new InvalidArgumentException('Macro name must be a string');
}

EloquentBuilder::macro('dumpRawSql', function () {
return ($this->getQuery()->dumpRawSql());
});
$closure($macro);

$this->registerEloquentBuilderMacro($macro);

return true;
}

/**
* Register the `ddRawSql` macro.
* @param $macro
* @return bool
* @throws \Guanguans\LaravelRawSql\Exceptions\InvalidArgumentException
*/
protected function registerDdRawSqlMacro()
protected function registerEloquentBuilderMacro($macro)
{
QueryBuilder::macro('ddRawSql', function () {
dd($this->toRawSql());
});
if (!is_string($macro)) {
throw new InvalidArgumentException('Macro name must be a string');
}

EloquentBuilder::macro('ddRawSql', function () {
return ($this->getQuery()->ddRawSql());
EloquentBuilder::macro($macro, function () use ($macro) {
return ($this->getQuery()->$macro());
});

return true;
}

/**
Expand Down

0 comments on commit 78b5039

Please sign in to comment.