Context.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace Dcat\Admin\Support;
  3. use Illuminate\Support\Arr;
  4. use Illuminate\Support\Fluent;
  5. /**
  6. * Class Context.
  7. *
  8. * @property string $favicon
  9. * @property string $metaTitle
  10. * @property string $pjaxContainerId
  11. * @property array|null $html
  12. * @property array|null $ignoreQueries
  13. * @property array|null $jsVariables
  14. * @property string $translation
  15. */
  16. class Context extends Fluent
  17. {
  18. public function set($key, $value = null)
  19. {
  20. $data = is_array($key) ? $key : [$key => $value];
  21. foreach ($data as $key => $value) {
  22. Arr::set($this->attributes, $key, $value);
  23. }
  24. return $this;
  25. }
  26. public function get($key, $default = null)
  27. {
  28. return Arr::get($this->attributes, $key, $default);
  29. }
  30. public function remember($key, \Closure $callback)
  31. {
  32. if (($value = $this->get($key)) !== null) {
  33. return $value;
  34. }
  35. return tap($callback(), function ($value) use ($key) {
  36. $this->set($key, $value);
  37. });
  38. }
  39. public function getArray($key, $default = null)
  40. {
  41. return Helper::array($this->get($key, $default));
  42. }
  43. public function add($key, $value, $k = null)
  44. {
  45. $results = $this->getArray($key);
  46. if ($k === null) {
  47. $results[] = $value;
  48. } else {
  49. $results[$k] = $value;
  50. }
  51. return $this->set($key, $results);
  52. }
  53. public function merge($key, array $value)
  54. {
  55. $results = $this->getArray($key);
  56. return $this->set($key, array_merge($results, $value));
  57. }
  58. public function forget($keys)
  59. {
  60. Arr::forget($this->attributes, $keys);
  61. }
  62. public function flush()
  63. {
  64. $this->attributes = [];
  65. }
  66. }