diff --git a/src/Exceptions/Exception.php b/src/Exceptions/Exception.php new file mode 100644 index 0000000..60ec9c3 --- /dev/null +++ b/src/Exceptions/Exception.php @@ -0,0 +1,15 @@ + + * + * This source file is subject to the MIT license that is bundled. + */ + +namespace Guanguans\LaravelRawSql\Exceptions; + +class Exception extends \Exception +{ +} diff --git a/src/Exceptions/InvalidArgumentException.php b/src/Exceptions/InvalidArgumentException.php new file mode 100644 index 0000000..f0efc36 --- /dev/null +++ b/src/Exceptions/InvalidArgumentException.php @@ -0,0 +1,15 @@ + + * + * This source file is subject to the MIT license that is bundled. + */ + +namespace Guanguans\LaravelRawSql\Exceptions; + +class InvalidArgumentException extends Exception +{ +} diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 9ef3acb..d2c2139 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -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 { @@ -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; } /**