Proxy.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. if (array_key_exists('get', $this->__caches)) {
  43. return $this->__caches['get'];
  44. }
  45. return $this->__caches['get'] = $this->repository->get($model);
  46. }
  47. public function edit(Form $form): array
  48. {
  49. $id = $form->getKey();
  50. if (array_key_exists($id, $this->__caches['edit'])) {
  51. return $this->__caches['edit'][$id];
  52. }
  53. return $this->__caches['edit'][$id] = $this->repository->edit($form);
  54. }
  55. public function detail(Show $show): array
  56. {
  57. $id = $show->getId();
  58. if (array_key_exists($id, $this->__caches['detail'])) {
  59. return $this->__caches['detail'][$id];
  60. }
  61. return $this->__caches['detail'][$id] = $this->repository->detail($show);
  62. }
  63. public function store(Form $form)
  64. {
  65. foreach ($this->__listeners as $listener) {
  66. $listener->creating($form);
  67. }
  68. $newId = $this->repository->store($form);
  69. foreach ($this->__listeners as $listener) {
  70. $listener->created($form, $newId);
  71. }
  72. return $newId;
  73. }
  74. public function getDataWhenUpdating(Form $form): array
  75. {
  76. $id = $form->getKey();
  77. if (array_key_exists($id, $this->__caches['updating'])) {
  78. return $this->__caches['updating'][$id];
  79. }
  80. return $this->__caches['updating'][$id] = $this->repository->getDataWhenUpdating($form);
  81. }
  82. public function update(Form $form)
  83. {
  84. $editAttributes = $this->__caches['edit'] ?? [];
  85. foreach ($this->__listeners as $listener) {
  86. $listener->updating($form, $editAttributes);
  87. }
  88. $result = $this->repository->update($form);
  89. foreach ($this->__listeners as $listener) {
  90. $listener->updated($form, $editAttributes, $result);
  91. }
  92. return $result;
  93. }
  94. public function destroy(Form $form, array $deletingData)
  95. {
  96. foreach ($this->__listeners as $listener) {
  97. $listener->deleting($form, $deletingData);
  98. }
  99. $result = $this->repository->destroy($form, $deletingData);
  100. foreach ($this->__listeners as $listener) {
  101. $listener->deleted($form, $deletingData, $result);
  102. }
  103. return $result;
  104. }
  105. public function getDataWhenDeleting(Form $form): array
  106. {
  107. return $this->repository->getDataWhenDeleting($form);
  108. }
  109. public function __call($method, $arguments)
  110. {
  111. return $this->repository->$method(...$arguments);
  112. }
  113. public function __get($name)
  114. {
  115. return $this->repository->$name;
  116. }
  117. public function __set($name, $value)
  118. {
  119. $this->repository->$name = $value;
  120. }
  121. }