MenuObserver.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace App\Observers;
  3. use App\Menu;
  4. class MenuObserver
  5. {
  6. /**
  7. * Handle the menu "created" event.
  8. *
  9. * @param \App\Menu $menu
  10. * @return void
  11. */
  12. public function created(Menu $menu)
  13. {
  14. if (config('app.env')!='production')return;
  15. $txt = "INSERT INTO menus";
  16. $columns = "(";
  17. $values = "(";
  18. foreach ($menu->toArray() as $col=>$val){
  19. $columns .= "{$col},";
  20. if (!$val) $values .= "null,";
  21. else $values .= "'{$val}',";
  22. }
  23. $columns = mb_substr($columns,0,-1);
  24. $values = mb_substr($values,0,-1);
  25. $txt .= "{$columns}) VALUES{$values});\r\n";
  26. $this->write($txt);
  27. }
  28. /**
  29. * Handle the menu "updated" event.
  30. *
  31. * @param \App\Menu $menu
  32. * @return void
  33. */
  34. public function updated(Menu $menu)
  35. {
  36. if (config('app.env')!='production')return;
  37. $txt = "UPDATE menus SET ";
  38. foreach ($menu->toArray() as $col=>$val){
  39. if (!$val) $txt .= "{$col}=null,";
  40. else $txt .= "{$col}='{$val}',";
  41. }
  42. $txt = mb_substr($txt,0,-1);
  43. $txt .= " WHERE id = {$menu->id};\r\n";
  44. $this->write($txt);
  45. }
  46. /**
  47. * Handle the menu "deleted" event.
  48. *
  49. * @param \App\Menu $menu
  50. * @return void
  51. */
  52. public function deleted(Menu $menu)
  53. {
  54. if (config('app.env')!='production')return;
  55. $txt = "DELETE FROM menus WHERE id = {$menu->id};\r\n";
  56. $this->write($txt);
  57. }
  58. private function write($txt)
  59. {
  60. $myfile = @fopen(base_path()."/database/data/menus.data", "a+");
  61. fwrite($myfile, $txt);
  62. fclose($myfile);
  63. }
  64. }