From bf18cfe49e64d6701eca55997e0d1c309c669ff3 Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Tue, 21 Jun 2022 21:29:39 +0800 Subject: [PATCH 1/5] Merge pull request #2521 from yajra/closure-di [9.x] Add support for dependency injection when using closure. (cherry picked from commit 3e6846ab53d8a10f4f5eb1381395ffb2f226f19d) --- src/Utilities/Helper.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Utilities/Helper.php b/src/Utilities/Helper.php index 5683ed91..3894720c 100644 --- a/src/Utilities/Helper.php +++ b/src/Utilities/Helper.php @@ -63,7 +63,16 @@ public static function compileContent($content, array $data, array|object $param { if (is_string($content)) { return static::compileBlade($content, static::getMixedValue($data, $param)); - } elseif (is_callable($content)) { + } + + if (is_callable($content)) { + $reflection = new \ReflectionFunction($content); + $arguments = $reflection->getParameters(); + + if (count($arguments) > 0) { + return app()->call($content, [$arguments[0]->name => $param]); + } + return $content($param); } From 9fd242f6198eb04ecb22fef33f43571668ab541f Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 21 Jun 2022 13:40:11 +0000 Subject: [PATCH 2/5] Apply fixes from StyleCI --- src/Utilities/Helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Utilities/Helper.php b/src/Utilities/Helper.php index 3894720c..c06b95e3 100644 --- a/src/Utilities/Helper.php +++ b/src/Utilities/Helper.php @@ -67,7 +67,7 @@ public static function compileContent($content, array $data, array|object $param if (is_callable($content)) { $reflection = new \ReflectionFunction($content); - $arguments = $reflection->getParameters(); + $arguments = $reflection->getParameters(); if (count($arguments) > 0) { return app()->call($content, [$arguments[0]->name => $param]); From 4f2db322fdf5a0d8a11f7318b71a88da9ef7670f Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Tue, 21 Jun 2022 21:45:57 +0800 Subject: [PATCH 3/5] Fix stan --- src/Utilities/Helper.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Utilities/Helper.php b/src/Utilities/Helper.php index c06b95e3..4359d000 100644 --- a/src/Utilities/Helper.php +++ b/src/Utilities/Helper.php @@ -2,10 +2,12 @@ namespace Yajra\DataTables\Utilities; +use Closure; use DateTime; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Support\Arr; use Illuminate\Support\Str; +use ReflectionFunction; class Helper { @@ -58,6 +60,7 @@ protected static function isItemOrderInvalid($item, $array) * @param array $data data to use with blade template * @param array|object $param parameter to call with callable * @return mixed + * @throws \ReflectionException */ public static function compileContent($content, array $data, array|object $param) { @@ -65,8 +68,8 @@ public static function compileContent($content, array $data, array|object $param return static::compileBlade($content, static::getMixedValue($data, $param)); } - if (is_callable($content)) { - $reflection = new \ReflectionFunction($content); + if ($content instanceof Closure) { + $reflection = new ReflectionFunction($content); $arguments = $reflection->getParameters(); if (count($arguments) > 0) { From 5a4460e7e50e2274b9b5d8fc7f64614ef84e25a6 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 21 Jun 2022 13:48:38 +0000 Subject: [PATCH 4/5] Apply fixes from StyleCI --- src/Utilities/Helper.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Utilities/Helper.php b/src/Utilities/Helper.php index 4359d000..4852df6b 100644 --- a/src/Utilities/Helper.php +++ b/src/Utilities/Helper.php @@ -60,6 +60,7 @@ protected static function isItemOrderInvalid($item, $array) * @param array $data data to use with blade template * @param array|object $param parameter to call with callable * @return mixed + * * @throws \ReflectionException */ public static function compileContent($content, array $data, array|object $param) From d118a100cc3a6851747346278e68014d65228041 Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Tue, 21 Jun 2022 21:56:49 +0800 Subject: [PATCH 5/5] Add test for closure di --- tests/Integration/QueryDataTableTest.php | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/Integration/QueryDataTableTest.php b/tests/Integration/QueryDataTableTest.php index 40d8d1de..e3e20ccb 100644 --- a/tests/Integration/QueryDataTableTest.php +++ b/tests/Integration/QueryDataTableTest.php @@ -339,6 +339,26 @@ public function it_can_return_formatted_columns() $this->assertEquals(Carbon::parse($user->created_at)->format('Y-m-d'), $data['created_at_formatted']); } + /** @test */ + public function it_can_return_added_column_with_dependency_injection() + { + $crawler = $this->call('GET', '/closure-di'); + + $crawler->assertJson([ + 'draw' => 0, + 'recordsTotal' => 20, + 'recordsFiltered' => 20, + ]); + + $user = DB::table('users')->find(1); + $data = $crawler->json('data')[0]; + + $this->assertTrue(isset($data['name'])); + $this->assertTrue(isset($data['name_di'])); + + $this->assertEquals($user->name.'_di', $data['name_di']); + } + protected function setUp(): void { parent::setUp(); @@ -451,5 +471,13 @@ protected function setUp(): void ->setFilteredRecords(10) ->toJson(); }); + + $router->get('/closure-di', function (DataTables $dataTable) { + return $dataTable->query(DB::table('users')) + ->addColumn('name_di', function ($user, User $u) { + return $u->newQuery()->find($user->id)->name.'_di'; + }) + ->toJson(); + }); } }