-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lazy data load for ngrest plugin (#647)
* Block Preview styling * Tooltip preview fix #246 * Date plugin with custom format * Changelog for #270 * Refactored namespace using * Connection lost handling * Unit test fixed close #229 * CONNECTION_ID only available in mysql * Block Preview styling * Tooltip preview fix #246 * travis tests with php 7.4 * clean up dist * attache find behaviours * Changelogs * Unit tests * phpdocs & changelog * Update NgRestModel.php * Update NgRestModel.php * Update NgRestModel.php * Update TestNgRestModel.php * lazy data load for ngrest plugin * changelog * phpdocs for LazyDataLoadTrait * lazyDataLoad Test * lazyDataLoad Test * lazyDataLoad Test Co-authored-by: Basil <git@nadar.io>
- Loading branch information
1 parent
457cfa2
commit b05bb40
Showing
7 changed files
with
190 additions
and
8 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace luya\admin\traits; | ||
|
||
/** | ||
* Trait to enable lazy load for NgRest plugins. | ||
* | ||
* For a example for CheckboxList: | ||
* | ||
* ```php | ||
* public function ngRestAttributeTypes() | ||
* { | ||
* return [ | ||
* 'genres' => ['checkboxList', 'data' => function () { | ||
* return new Query()->all(); | ||
* }], | ||
* ]; | ||
* } | ||
* ``` | ||
* | ||
* @author Bennet Klarhölter <boehsermoe@me.com> | ||
* @since 4.0.0 | ||
*/ | ||
trait LazyDataLoadTrait | ||
{ | ||
/** | ||
* If the given data is a closure it will call it and return the result of the function. | ||
* Function will only execute if the data are really need to load. | ||
* | ||
* If the given data is no closure it will return the directly data. | ||
* | ||
* ``` | ||
* $this->lazyLoadData(function () { | ||
* return Query::find()->all(); | ||
* }); | ||
* ``` | ||
* | ||
* @param mixed|\Closure $data | ||
* @return mixed|\Traversable | ||
*/ | ||
protected function lazyLoadData($data) | ||
{ | ||
if (is_callable($data)) { | ||
return call_user_func($data); | ||
} | ||
|
||
return $data; | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace admintests\admin\traits; | ||
|
||
use admintests\AdminModelTestCase; | ||
use admintests\data\fixtures\UserFixture; | ||
use luya\admin\traits\LazyDataLoadTrait; | ||
|
||
class LazyDataLoadTraitTest extends AdminModelTestCase | ||
{ | ||
public function testWithAnonymousFunction() | ||
{ | ||
$dataLoaded = false; | ||
|
||
$mock = new LazyDataLoadTraitMock(); | ||
$mock->data = function () use (&$dataLoaded) { | ||
$dataLoaded = true; | ||
return 'FOO'; | ||
}; | ||
|
||
$this->assertEquals('FOO', $mock->getData()); | ||
$this->assertTrue($dataLoaded); | ||
} | ||
|
||
public function testWithCallableArray() | ||
{ | ||
$this->lazyDataLoadFunctionCalled = false; | ||
|
||
$mock = new LazyDataLoadTraitMock(); | ||
$mock->data = [$this, 'lazyDataLoadFunction']; | ||
|
||
$this->assertEquals(self::class . '::lazyDataLoadFunction', $mock->getData()); | ||
$this->assertTrue($this->lazyDataLoadFunctionCalled); | ||
} | ||
|
||
protected $lazyDataLoadFunctionCalled = false; | ||
|
||
public function lazyDataLoadFunction() | ||
{ | ||
$this->lazyDataLoadFunctionCalled = true; | ||
|
||
return __METHOD__; | ||
} | ||
} | ||
|
||
class LazyDataLoadTraitMock | ||
{ | ||
use LazyDataLoadTrait; | ||
|
||
public $data; | ||
|
||
public function getData() | ||
{ | ||
return $this->lazyLoadData($this->data); | ||
} | ||
} |