|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Widgets; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\View\View; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Illuminate\Contracts\View\Factory; |
|
8
|
|
|
use Illuminate\Contracts\Container\Container; |
|
9
|
|
|
use SleepingOwl\Admin\Contracts\Widgets\WidgetInterface; |
|
10
|
|
|
use SleepingOwl\Admin\Contracts\Widgets\WidgetsRegistryInterface; |
|
11
|
|
|
|
|
12
|
|
|
class WidgetsRegistry implements WidgetsRegistryInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Collection|WidgetInterface[] |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $widgets; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var Container |
|
21
|
|
|
*/ |
|
22
|
|
|
private $container; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* BlocksRegistry constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param Container $container |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(Container $container) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->widgets = new Collection(); |
|
32
|
|
|
$this->container = $container; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param $widget |
|
37
|
|
|
* |
|
38
|
|
|
* @return $this |
|
39
|
|
|
*/ |
|
40
|
|
|
public function registerWidget($widget) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->widgets->push($widget); |
|
43
|
|
|
|
|
44
|
|
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param Factory $factory |
|
49
|
|
|
*/ |
|
50
|
|
|
public function placeWidgets(Factory $factory) |
|
51
|
|
|
{ |
|
52
|
|
|
if ($this->widgets->count() === 0) { |
|
53
|
|
|
return; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$groupedBlocks = $this->widgets |
|
57
|
|
|
->map(function ($class) { |
|
58
|
|
|
return $this->makeWidget($class); |
|
59
|
|
|
}) |
|
60
|
|
|
->filter(function (WidgetInterface $block) { |
|
61
|
|
|
return $block->active(); |
|
62
|
|
|
}) |
|
63
|
|
|
->groupBy(function (WidgetInterface $block) { |
|
64
|
|
|
return $block->template(); |
|
65
|
|
|
}); |
|
66
|
|
|
|
|
67
|
|
|
foreach ($groupedBlocks as $template => $widgets) { |
|
68
|
|
|
$factory->composer($template, function (View $view) use ($widgets) { |
|
69
|
|
|
$factory = $view->getFactory(); |
|
70
|
|
|
|
|
71
|
|
|
/** @var Collection|WidgetInterface[] $widgets */ |
|
72
|
|
|
$widgets = $widgets->sortBy(function (WidgetInterface $block) { |
|
|
|
|
|
|
73
|
|
|
return $block->position(); |
|
74
|
|
|
}); |
|
75
|
|
|
|
|
76
|
|
|
foreach ($widgets as $widget) { |
|
77
|
|
|
$widget->setInjectableView($view); |
|
78
|
|
|
|
|
79
|
|
|
$factory->inject( |
|
80
|
|
|
$widget->block(), |
|
81
|
|
|
$widget->toHtml() |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
}); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param mixed $widget |
|
90
|
|
|
* @return mixed |
|
91
|
|
|
*/ |
|
92
|
|
|
public function makeWidget($widget) |
|
93
|
|
|
{ |
|
94
|
|
|
return is_string($widget) ? $this->createClassWidget($widget) : $widget; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param $widget |
|
99
|
|
|
* |
|
100
|
|
|
* @return \Closure |
|
101
|
|
|
*/ |
|
102
|
|
|
public function createClassWidget($widget) |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->container->make($widget); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
It seems like you are assigning to a variable which was imported through a
usestatement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope