1
|
|
|
<?php |
2
|
|
|
namespace Malezha\Menu\Render; |
3
|
|
|
|
4
|
|
|
use Illuminate\Contracts\Container\Container; |
5
|
|
|
use Malezha\Menu\Contracts\MenuRender; |
6
|
|
|
|
7
|
|
|
class Basic implements MenuRender |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var Container |
11
|
|
|
*/ |
12
|
|
|
protected $container; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $variables = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $view; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @inheritDoc |
26
|
|
|
*/ |
27
|
36 |
|
public function __construct(Container $container) |
28
|
|
|
{ |
29
|
36 |
|
$this->container = $container; |
30
|
36 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @inheritDoc |
34
|
|
|
*/ |
35
|
13 |
|
public function make($view) |
36
|
|
|
{ |
37
|
13 |
|
$view = $this->replaceNameFromBlade($view); |
38
|
|
|
|
39
|
13 |
|
if (!$template = $this->findFullPath($view)) { |
40
|
1 |
|
throw new \Exception('View not found'); |
41
|
|
|
} |
42
|
|
|
|
43
|
12 |
|
$this->view = $template; |
44
|
|
|
|
45
|
12 |
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritDoc |
50
|
|
|
*/ |
51
|
12 |
|
public function with($params, $value = null) |
52
|
|
|
{ |
53
|
12 |
|
if (is_array($params)) { |
54
|
11 |
|
$this->variables = array_merge($this->variables, $params); |
55
|
|
|
|
56
|
11 |
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
$this->variables[$params] = $value; |
60
|
|
|
|
61
|
1 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritDoc |
66
|
|
|
*/ |
67
|
12 |
|
public function render() |
68
|
|
|
{ |
69
|
12 |
|
$__e = extract($this->variables); |
70
|
|
|
|
71
|
12 |
|
ob_start(); |
72
|
12 |
|
include($this->view); |
73
|
12 |
|
return ob_get_clean(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritDoc |
78
|
|
|
*/ |
79
|
28 |
|
public function exists($view) |
80
|
|
|
{ |
81
|
28 |
|
$view = $this->replaceNameFromBlade($view); |
82
|
28 |
|
return (bool) $this->findFullPath($view); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $view |
87
|
|
|
* @return bool|string |
88
|
|
|
*/ |
89
|
32 |
|
protected function findFullPath($view) |
90
|
|
|
{ |
91
|
32 |
|
$paths = $this->container->make('config')->get('menu.paths'); |
92
|
|
|
|
93
|
32 |
|
foreach ($paths as $path) { |
94
|
32 |
|
$template = $path . '/' . $view . '.php'; |
95
|
32 |
|
if (file_exists($template)) { |
96
|
32 |
|
return $template; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
28 |
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $view |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
32 |
|
protected function replaceNameFromBlade($view) |
108
|
|
|
{ |
109
|
32 |
|
return str_replace('.', '/', preg_replace("/(.*)(::)(.*)/", "$3", $view)); |
110
|
|
|
} |
111
|
|
|
} |