-
Notifications
You must be signed in to change notification settings - Fork 1
/
WireframeRendererBlade.module.php
executable file
·136 lines (114 loc) · 3.23 KB
/
WireframeRendererBlade.module.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
<?php
declare(strict_types=1);
namespace ProcessWire;
use Jenssegers\Blade\Blade;
/**
* Wireframe Renderer Blade
*
* @version 0.2.0
* @author Maurizio Bonani <[email protected]>
* @license Mozilla Public License v2.0 https://mozilla.org/MPL/2.0/
*/
class WireframeRendererBlade extends Wire implements Module
{
/**
* The Blade instance.
*
* @var Blade
*/
protected $blade;
/**
* Default extension.
*
* @var string
*/
protected $ext = 'blade.php';
/**
* Init method
*
* @param array $settings Additional settings.
* @return WireframeRendererBlade
*/
public function ___init(array $settings = []): WireframeRendererBlade
{
// autoload Blade classes
if (!class_exists('\Jenssegers\Blade\Blade')) {
require_once(__DIR__ . '/vendor/autoload.php' /*NoCompile*/);
}
$this->blade = $this->initBlade($settings);
return $this;
}
/**
* Init Blade
*
* @param array $settings Blade settings.
* @return Blade
*/
public function ___initBlade(array $settings = []): Blade
{
$wireframe = $this->wire('modules')->get('Wireframe');
$viewPaths = $wireframe->getViewPaths();
$views = $settings['views'] ?? $viewPaths['view'];
$cache = $settings['cache'] ?? $this->wire('config')->paths->cache . 'WireframeRendererBlade';
$blade = new Blade($views, $cache);
$blade->addNamespace('layout', $viewPaths['layout']);
$blade->addNamespace('partial', $viewPaths['partial']);
$blade->addNamespace('component', $viewPaths['component']);
return $blade;
}
/**
* Render method
*
* @param string $type Type of file to render (view, layout, partial, or component).
* @param string $view Name of the view file to render.
* @param array $context Variables used for rendering.
* @return string Rendered markup.
* @throws WireException if param $type has an unexpected value.
*/
public function render(string $type, string $view, array $context = []): string
{
if (! in_array($type, array_keys($this->wire('modules')->get('Wireframe')->getViewPaths()))) {
throw new WireException(sprintf('Unexpected type (%s).', $type));
}
if ($type !== 'view') {
$view = $this->namespaceView($type, $view);
}
$view = $this->adaptView($view);
return $this->blade->make($view, $context)->render();
}
/**
* Namespace the view.
*
* @param string $type
* @param string $view
* @return string
*/
protected function namespaceView(string $type, string $view)
{
return $type . '::' . $view;
}
/**
* Adapt view path to Blade notation.
*
* @param string $view
* @return string
*/
protected function adaptView($view)
{
return preg_replace('/.'. $this->ext . '$/', '', str_replace('/', '.', $view));
}
/**
* @return Blade
*/
public function getBladeInstance(): Blade
{
return $this->blade;
}
/**
* @return string
*/
public function getExt(): string
{
return $this->ext;
}
}