Tools.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. * @return $this
  35. */
  36. public function add($tool)
  37. {
  38. if ($tool instanceof AbstractTool) {
  39. $tool->setParent($this->tree);
  40. }
  41. $this->tools->push($tool);
  42. return $this;
  43. }
  44. /**
  45. * Render header tools bar.
  46. *
  47. * @return string
  48. */
  49. public function render()
  50. {
  51. return $this->tools->map([Helper::class, 'render'])->implode(' ');
  52. }
  53. }