Proxy.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace Dcat\Admin\Repositories;
  3. use Dcat\Admin\Form;
  4. use Dcat\Admin\Show;
  5. use Dcat\Admin\Grid;
  6. class Proxy implements \Dcat\Admin\Contracts\Repository
  7. {
  8. protected $repository;
  9. protected $__listeners = [];
  10. protected $__caches = [
  11. 'edit' => [],
  12. 'detail' => [],
  13. 'updating' => [],
  14. ];
  15. public function __construct(Repository $repository)
  16. {
  17. $this->repository = $repository;
  18. $this->__listeners = Repository::getListeners(get_class($repository));
  19. }
  20. public function getOriginalClassName()
  21. {
  22. return get_class($this->repository);
  23. }
  24. public function getKeyName()
  25. {
  26. return $this->repository->getKeyName();
  27. }
  28. public function isSoftDeletes()
  29. {
  30. return $this->repository->isSoftDeletes();
  31. }
  32. public function getCreatedAtColumn()
  33. {
  34. return $this->repository->getCreatedAtColumn();
  35. }
  36. public function getUpdatedAtColumn()
  37. {
  38. return $this->repository->getUpdatedAtColumn();
  39. }
  40. public function get(Grid\Model $model)
  41. {
  42. return $this->repository->get($model);
  43. }
  44. public function edit(Form $form): array
  45. {
  46. $id = $form->getKey();
  47. if (array_key_exists($id, $this->__caches['edit'])) {
  48. return $this->__caches['edit'][$id];
  49. }
  50. return $this->__caches['edit'][$id] = $this->repository->edit($form);
  51. }
  52. public function detail(Show $show): array
  53. {
  54. $id = $show->getKey();
  55. if (array_key_exists($id, $this->__caches['detail'])) {
  56. return $this->__caches['detail'][$id];
  57. }
  58. return $this->__caches['detail'][$id] = $this->repository->detail($show);
  59. }
  60. public function store(Form $form)
  61. {
  62. foreach ($this->__listeners as $listener) {
  63. $listener->creating($form);
  64. }
  65. $newId = $this->repository->store($form);
  66. foreach ($this->__listeners as $listener) {
  67. $listener->created($form, $newId);
  68. }
  69. return $newId;
  70. }
  71. public function getDataWhenUpdating(Form $form): array
  72. {
  73. $id = $form->getKey();
  74. if (array_key_exists($id, $this->__caches['updating'])) {
  75. return $this->__caches['updating'][$id];
  76. }
  77. return $this->__caches['updating'][$id] = $this->repository->getDataWhenUpdating($form);
  78. }
  79. public function update(Form $form)
  80. {
  81. $editAttributes = $this->__caches['edit'] ?? [];
  82. foreach ($this->__listeners as $listener) {
  83. $listener->updating($form, $editAttributes);
  84. }
  85. $result = $this->repository->update($form);
  86. foreach ($this->__listeners as $listener) {
  87. $listener->updated($form, $editAttributes, $result);
  88. }
  89. return $result;
  90. }
  91. public function destroy(Form $form, array $deletingData)
  92. {
  93. foreach ($this->__listeners as $listener) {
  94. $listener->deleting($form, $deletingData);
  95. }
  96. $result = $this->repository->destroy($form, $deletingData);
  97. foreach ($this->__listeners as $listener) {
  98. $listener->deleted($form, $deletingData, $result);
  99. }
  100. return $result;
  101. }
  102. public function getDataWhenDeleting(Form $form): array
  103. {
  104. return $this->repository->getDataWhenDeleting($form);
  105. }
  106. public function __call($method, $arguments)
  107. {
  108. return $this->repository->$method(...$arguments);
  109. }
  110. public function __get($name)
  111. {
  112. return $this->repository->$name;
  113. }
  114. public function __set($name, $value)
  115. {
  116. $this->repository->$name = $value;
  117. }
  118. }