Tools.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace Dcat\Admin\Tree;
  3. use Dcat\Admin\Support\Helper;
  4. use Dcat\Admin\Tree;
  5. use Illuminate\Contracts\Support\Htmlable;
  6. use Illuminate\Contracts\Support\Renderable;
  7. use Illuminate\Support\Collection;
  8. class Tools implements Renderable
  9. {
  10. /**
  11. * Parent tree.
  12. *
  13. * @var Tree
  14. */
  15. protected $tree;
  16. /**
  17. * Collection of tools.
  18. *
  19. * @var Collection
  20. */
  21. protected $tools;
  22. /**
  23. * Create a new Tools instance.
  24. */
  25. public function __construct(Tree $tree)
  26. {
  27. $this->tree = $tree;
  28. $this->tools = new Collection();
  29. }
  30. /**
  31. * Prepend a tool.
  32. *
  33. * @param string|\Closure|AbstractTool|Renderable|Htmlable $tool
  34. *
  35. * @return $this
  36. */
  37. public function add($tool)
  38. {
  39. if ($tool instanceof AbstractTool) {
  40. $tool->setParent($this->tree);
  41. }
  42. $this->tools->push($tool);
  43. return $this;
  44. }
  45. /**
  46. * Render header tools bar.
  47. *
  48. * @return string
  49. */
  50. public function render()
  51. {
  52. return $this->tools->map([Helper::class, 'render'])->implode(' ');
  53. }
  54. }