BaseObserver.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 24/02/2017
  6. * Time: 01:01
  7. */
  8. namespace app\common\observers;
  9. use Illuminate\Database\Eloquent\Model;
  10. class BaseObserver {
  11. public function saving(Model $model) {}
  12. public function saved(Model $model) {}
  13. public function updating(Model $model) {}
  14. public function updated(Model $model) {}
  15. public function creating(Model $model) {}
  16. public function created(Model $model) {}
  17. public function deleting(Model $model) {}
  18. public function deleted(Model $model) {}
  19. public function restoring(Model $model) {}
  20. public function restored(Model $model) {}
  21. /**
  22. * 插件观察
  23. * @param $key
  24. * @param $model
  25. * @param string $operate
  26. * @return array
  27. */
  28. protected function pluginObserver($key, $model, $operate = 'created', $type = null)
  29. {
  30. $observerConfigs = \app\common\modules\shop\ShopConfig::current()->get($key);
  31. //按照权重从小到大排序执行
  32. $observerConfigs = collect($observerConfigs)->sortBy('weight')->toArray();
  33. $result = [];
  34. if($observerConfigs){
  35. foreach ($observerConfigs as $pluginName=>$pluginOperators){
  36. if(isset($pluginOperators) && $pluginOperators) {
  37. $class = array_get($pluginOperators,'class');
  38. $function =array_get($pluginOperators,$operate == 'validator' ? 'function_validator':'function_save');
  39. $data = array_get($model->widgets,$pluginName,[]);
  40. if(class_exists($class) && method_exists($class,$function) && is_callable([$class,$function])){
  41. if (!$type) {
  42. $result[$pluginName] = $class::$function($model->id, $data, $operate);
  43. } else {
  44. $result[$pluginName] = $class::$function($model);
  45. }
  46. }
  47. }
  48. }
  49. }
  50. return $result;
  51. }
  52. }