forked from rmunate/PHP2JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRender.php
106 lines (89 loc) Β· 2.24 KB
/
Render.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
namespace Rmunate\Php2Js;
use Rmunate\Php2Js\Bases\BaseRender;
use Rmunate\Php2Js\Elements\Generator;
class Render extends BaseRender
{
/**
* Propierties Object
* Set From Contrcutor.
*/
private $view;
private $data;
private $alias;
private $dataJS = [];
/**
* Constructor.
*
* @param string $view
*/
public function __construct(string $view)
{
$this->view = $view;
}
/**
* Add data to the Render instance.
*
* @param array $data
*
* @return static
*/
public function with(array $data): static
{
$this->data = $data;
return $this;
}
/**
* @param string $alias
*
* @return static
*/
public function toJS(string $alias = 'PHP2JS'): static
{
$this->dataJS = $this->data;
$this->alias = $alias;
return $this;
}
/**
* @param array $data
* @param string $alias
*
* @return static
*/
public function toStrictJS(array $data = [], string $alias = 'PHP2JS'): static
{
$this->dataJS = $data;
$this->alias = $alias;
return $this;
}
/**
* Return the View.
*
* @return mixed
*/
public function compose()
{
$view = view($this->view)->with($this->data);
$html = $view->render();
$metas = Generator::alias($this->alias)->data($this->dataJS);
$scripts = Generator::alias($this->alias)->script();
$posicionCierreHead = strpos($html, '</head>');
$posicionCierreBody = strpos($html, '</body>');
if ($posicionCierreHead !== false) {
$ssr[0] = substr($html, 0, $posicionCierreHead);
$ssr[1] = $metas;
$ssr[2] = $scripts;
$ssr[3] = substr($html, $posicionCierreHead);
$html = implode(PHP_EOL, $ssr);
} elseif ($posicionCierreBody !== false) {
$ssr[0] = substr($html, 0, $posicionCierreBody);
$ssr[1] = $metas;
$ssr[2] = $scripts;
$ssr[3] = substr($html, $posicionCierreBody);
$html = implode(PHP_EOL, $ssr);
} else {
$html .= $metas.$scripts;
}
return response($html);
}
}