sarga/packages/Webkul/Theme/src/ViewRenderEventManager.php

91 lines
1.6 KiB
PHP
Raw Permalink Normal View History

2018-12-04 11:05:30 +00:00
<?php
2019-08-14 06:55:40 +00:00
2018-12-04 11:05:30 +00:00
namespace Webkul\Theme;
2019-08-14 06:55:40 +00:00
2018-12-04 11:05:30 +00:00
use Illuminate\Support\Facades\Event;
2019-08-14 06:55:40 +00:00
2018-12-04 11:05:30 +00:00
class ViewRenderEventManager
{
/**
* Contains all themes
*
* @var array
*/
protected $templates = [];
2019-08-14 06:55:40 +00:00
2018-12-04 11:05:30 +00:00
/**
* Paramters passed with event
*
* @var array
*/
protected $params;
2019-08-14 06:55:40 +00:00
2018-12-04 11:05:30 +00:00
/**
* Fires event for rendering template
*
2020-03-05 13:37:08 +00:00
* @param string $eventName
* @param array|null $params
2018-12-04 11:05:30 +00:00
* @return string
*/
public function handleRenderEvent($eventName, $params = null)
{
$this->params = $params ?? [];
2019-12-24 14:01:13 +00:00
Event::dispatch($eventName, $this);
2018-12-04 11:05:30 +00:00
return $this->templates;
}
2019-08-14 06:32:30 +00:00
/**
* get params
*
* @return array
*/
public function getParams()
{
return $this->params;
}
/**
* get param
*
2020-03-05 13:37:08 +00:00
* @param $name
2019-08-14 06:32:30 +00:00
* @return mixed
*/
public function getParam($name)
{
return optional($this->params)[$name];
}
2019-08-14 06:55:40 +00:00
2018-12-04 11:05:30 +00:00
/**
* Add templates for render
*
2020-03-05 13:37:08 +00:00
* @param string $template
2018-12-04 11:05:30 +00:00
* @return void
*/
public function addTemplate($template)
{
array_push($this->templates, $template);
}
2019-08-14 06:55:40 +00:00
2018-12-04 11:05:30 +00:00
/**
* Renders templates
*
* @return string
*/
public function render()
{
$string = "";
2019-12-24 14:01:13 +00:00
2018-12-04 11:05:30 +00:00
foreach ($this->templates as $template) {
if (view()->exists($template)) {
$string .= view($template , $this->params)->render();
2020-02-24 12:19:12 +00:00
} elseif (is_string($template)) {
$string .= $template;
2018-12-04 11:05:30 +00:00
}
}
2019-12-24 14:01:13 +00:00
2018-12-04 11:05:30 +00:00
return $string;
}
}