forked from Flynsarmy/laravel-db-blade-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDbView.php
executable file
·205 lines (176 loc) · 5.24 KB
/
DbView.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php namespace Flynsarmy\DbBladeCompiler;
use View, Closure, ArrayAccess;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Config\Repository;
use Illuminate\Database\Eloquent\Model;
class DbView extends \Illuminate\View\View implements ArrayAccess, Renderable
{
protected $content_field = null;
/** @var Repository */
protected $config;
/**
* DbView constructor.
*
* @param Repository $config
* @param DbBladeCompilerEngine $engine
*/
public function __construct(Repository $config, DbBladeCompilerEngine $engine)
{
$this->config = $config;
$this->engine = $engine;
}
/**
* Get a evaluated view contents for the given view.
*
* @param Model $model
* @param array $data
* @param array $mergeData
* @param string $content_field
* @return DbView
*/
public function make(Model $model, $data = array(), $mergeData = array(), $content_field = null)
{
$this->path = $model;
$this->data = array_merge($mergeData, $this->parseData($data));
if (!is_null($content_field)) {
$this->content_field = $content_field;
} else {
$this->content_field = $this->config->get('db-blade-compiler.model_default_field');
}
return $this;
}
/**
* @param string $content_field
* @return DbView
*/
public function field($content_field)
{
$this->content_field = $content_field;
return $this;
}
/**
* Get the string contents of the view.
*
* @param callable $callback
* @return string
*/
public function render(callable $callback = null)
{
$contents = $this->renderContents();
$response = isset($callback) ? $callback($this, $contents) : null;
// Once we have the contents of the view, we will flush the sections if we are
// done rendering all views so that there is nothing left hanging over when
// anothoer view is rendered in the future by the application developers.
// Before flushing, check Laravel version for correct method use
if (version_compare(app()->version(), '5.4.0') >= 0)
View::flushStateIfDoneRendering();
else
View::flushSectionsIfDoneRendering();
return $response ?: $contents;
}
/**
* Get the contents of the view instance.
*
* @return string
*/
protected function renderContents()
{
// We will keep track of the amount of views being rendered so we can flush
// the section after the complete rendering operation is done. This will
// clear out the sections for any separate views that may be rendered.
View::incrementRender();
$contents = $this->getContents();
// Once we've finished rendering the view, we'll decrement the render count
// so that each sections get flushed out next time a view is created and
// no old sections are staying around in the memory of an environment.
View::decrementRender();
return $contents;
}
protected function getContents()
{
$field = $this->config->get('db-blade-compiler.model_property');
$this->path->{$field} = $this->content_field;
$compiler = $this->engine->getCompiler();
$compiler->compile($this->path);
return $this->engine->getContent($compiler->getCompiledPath($this->path), $this->data);
}
/**
* Parse the given data into a raw array.
*
* @param mixed $data
* @return array
*/
protected function parseData($data)
{
return $data instanceof Arrayable ? $data->toArray() : $data;
}
/**
* Get the data bound to the view instance.
*
* @return array
*/
public function gatherData()
{
$data = array_merge(View::getShared(), $this->data);
foreach ($data as $key => $value) {
if ($value instanceof Renderable) {
$data[$key] = $value->render();
}
}
return $data;
}
/**
* Add a view instance to the view data.
*
* @param string $key
* @param string $view
* @param array $data
* @return \Illuminate\View\View
*/
public function nest($key, $view, array $data = array())
{
return $this->with($key, View::make($view, $data));
}
/**
* Determine if a piece of data is bound.
*
* @param string $key
* @return bool
*/
public function offsetExists($key): bool
{
return array_key_exists($key, $this->data);
}
/**
* Get a piece of bound data to the view.
*
* @param string $key
* @return mixed
*/
public function offsetGet($key): mixed
{
return $this->data[$key];
}
/**
* Set a piece of data on the view.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function offsetSet($key, $value): void
{
$this->with($key, $value);
}
/**
* Unset a piece of data from the view.
*
* @param string $key
* @return void
*/
public function offsetUnset($key): void
{
unset($this->data[$key]);
}
}