123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
-
- namespace Illuminate\View\Concerns;
-
- use Illuminate\Support\HtmlString;
-
- trait ManagesComponents
- {
- /**
- * The components being rendered.
- *
- * @var array
- */
- protected $componentStack = [];
-
- /**
- * The original data passed to the component.
- *
- * @var array
- */
- protected $componentData = [];
-
- /**
- * The slot contents for the component.
- *
- * @var array
- */
- protected $slots = [];
-
- /**
- * The names of the slots being rendered.
- *
- * @var array
- */
- protected $slotStack = [];
-
- /**
- * Start a component rendering process.
- *
- * @param string $name
- * @param array $data
- * @return void
- */
- public function startComponent($name, array $data = [])
- {
- if (ob_start()) {
- $this->componentStack[] = $name;
-
- $this->componentData[$this->currentComponent()] = $data;
-
- $this->slots[$this->currentComponent()] = [];
- }
- }
-
- /**
- * Render the current component.
- *
- * @return string
- */
- public function renderComponent()
- {
- $name = array_pop($this->componentStack);
-
- return $this->make($name, $this->componentData($name))->render();
- }
-
- /**
- * Get the data for the given component.
- *
- * @param string $name
- * @return array
- */
- protected function componentData($name)
- {
- return array_merge(
- $this->componentData[count($this->componentStack)],
- ['slot' => new HtmlString(trim(ob_get_clean()))],
- $this->slots[count($this->componentStack)]
- );
- }
-
- /**
- * Start the slot rendering process.
- *
- * @param string $name
- * @param string|null $content
- * @return void
- */
- public function slot($name, $content = null)
- {
- if (count(func_get_args()) === 2) {
- $this->slots[$this->currentComponent()][$name] = $content;
- } else {
- if (ob_start()) {
- $this->slots[$this->currentComponent()][$name] = '';
-
- $this->slotStack[$this->currentComponent()][] = $name;
- }
- }
- }
-
- /**
- * Save the slot content for rendering.
- *
- * @return void
- */
- public function endSlot()
- {
- last($this->componentStack);
-
- $currentSlot = array_pop(
- $this->slotStack[$this->currentComponent()]
- );
-
- $this->slots[$this->currentComponent()]
- [$currentSlot] = new HtmlString(trim(ob_get_clean()));
- }
-
- /**
- * Get the index for the current component.
- *
- * @return int
- */
- protected function currentComponent()
- {
- return count($this->componentStack) - 1;
- }
- }
|