File tree Expand file tree Collapse file tree 4 files changed +56
-11
lines changed
src/Nqxcode/LuceneSearch/Model Expand file tree Collapse file tree 4 files changed +56
-11
lines changed Original file line number Diff line number Diff line change 1+ <?php namespace Nqxcode \LuceneSearch \Model ;
2+
3+ use App ;
4+
5+ /**
6+ * Class SearchObserver
7+ * @package LuceneSearch\Model
8+ */
9+ class SearchObserver
10+ {
11+ /** @var bool */
12+ private static $ enabled = true ;
13+
14+ /**
15+ * @param bool $enabled
16+ */
17+ public static function setEnabled ($ enabled )
18+ {
19+ self ::$ enabled = $ enabled ;
20+ }
21+
22+ public function saved ($ model )
23+ {
24+ if (self ::$ enabled ) {
25+ App::offsetGet ('search ' )->update ($ model );
26+ }
27+ }
28+
29+ public function deleting ($ model )
30+ {
31+ if (self ::$ enabled ) {
32+ App::offsetGet ('search ' )->delete ($ model );
33+ }
34+ }
35+ }
Original file line number Diff line number Diff line change @@ -13,16 +13,13 @@ trait SearchTrait
1313 */
1414 public static function bootSearchTrait ()
1515 {
16- self ::saved (
17- function ($ model ) {
18- App::offsetGet ('search ' )->update ($ model );
19- }
20- );
16+ self ::observe (new SearchObserver );
17+ }
2118
22- self :: deleting (
23- function ( $ model ) {
24- App:: offsetGet ( ' search ' )-> delete ( $ model );
25- }
26- );
19+ public static function withoutSyncingToSearch ( \ Closure $ closure )
20+ {
21+ SearchObserver:: setEnabled ( false );
22+ $ closure ();
23+ SearchObserver:: setEnabled ( true );
2724 }
2825}
Original file line number Diff line number Diff line change @@ -46,4 +46,17 @@ public function testDelete()
4646
4747 $ this ->assertEquals (0 , Search::query ('observer ' )->count ());
4848 }
49+
50+ public function testWithoutSyncingToSearch ()
51+ {
52+ $ this ->assertEquals (0 , Search::query ('observer ' )->count ());
53+
54+ Product::withoutSyncingToSearch (function () {
55+ $ p = Product::first ();
56+ $ p ->name = 'observer ' ;
57+ $ p ->save ();
58+ });
59+
60+ $ this ->assertEquals (0 , Search::query ('observer ' )->count ());
61+ }
4962}
Original file line number Diff line number Diff line change 1111 * @property string $name
1212 * @property string $description
1313 * @property boolean $publish
14- * @method Builder wherePublish(boolean $publish)
14+ * @method static Builder wherePublish(boolean $publish)
1515 * @package tests\models
1616 */
1717class Product extends Model implements SearchableInterface
You can’t perform that action at this time.
0 commit comments