|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sco\Admin\Component; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Foundation\Application; |
|
6
|
|
|
use Sco\Admin\Component\Concerns\HasEvents; |
|
7
|
|
|
use Sco\Admin\Component\Concerns\HasPermission; |
|
8
|
|
|
use Sco\Admin\Contracts\ComponentInterface; |
|
9
|
|
|
use Sco\Admin\Contracts\RepositoryInterface; |
|
10
|
|
|
|
|
11
|
|
|
abstract class Component implements ComponentInterface |
|
12
|
|
|
{ |
|
13
|
|
|
use HasEvents, HasPermission; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $name; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var \Illuminate\Foundation\Application |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $app; |
|
24
|
|
|
|
|
25
|
|
|
protected $title; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var mixed|\Sco\Admin\Contracts\RepositoryInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $repository; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $model; |
|
36
|
|
|
|
|
37
|
|
|
protected static $booted = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var \Illuminate\Contracts\Events\Dispatcher |
|
41
|
|
|
*/ |
|
42
|
|
|
protected static $dispatcher; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
abstract public function getColumns(); |
|
48
|
|
|
|
|
49
|
|
|
public function __construct(Application $app, $modelClass) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->app = $app; |
|
52
|
|
|
|
|
53
|
|
|
$this->repository = $this->app->make(RepositoryInterface::class); |
|
54
|
|
|
$this->repository->setClass($modelClass); |
|
55
|
|
|
|
|
56
|
|
|
$this->model = $this->repository->getModel(); |
|
57
|
|
|
if (!$this->name) { |
|
58
|
|
|
$this->setDefaultName(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$this->registerObserver($this->permissionObserver); |
|
62
|
|
|
|
|
63
|
|
|
$this->bootIfNotBooted(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
protected function setDefaultName() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->name = $this->getModelClassName(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
protected function getModelClassName() |
|
72
|
|
|
{ |
|
73
|
|
|
return snake_case(str_plural(class_basename(get_class($this->getModel())))); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function getName() |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->name; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function getTitle() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->title; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function getModel() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->model; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function getRepository() |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->repository; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function get() |
|
97
|
|
|
{ |
|
98
|
|
|
if (!method_exists($this, 'callView')) { |
|
99
|
|
|
return; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$view = $this->app->call([$this, 'callView']); |
|
103
|
|
|
$view->setRepository($this->getRepository()); |
|
104
|
|
|
|
|
105
|
|
|
return $view->get(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritdoc} |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getConfigs() |
|
112
|
|
|
{ |
|
113
|
|
|
return collect([ |
|
114
|
|
|
'primaryKey' => $this->getModel()->getKeyName(), |
|
115
|
|
|
'title' => $this->getTitle(), |
|
116
|
|
|
'permissions' => $this->getPermissions(), |
|
117
|
|
|
'columns' => $this->getColumns(), |
|
118
|
|
|
//'elements' => $this->getElements()->values(), |
|
|
|
|
|
|
119
|
|
|
]); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
protected function bootIfNotBooted() |
|
123
|
|
|
{ |
|
124
|
|
|
if (!isset(static::$booted[static::class])) { |
|
125
|
|
|
static::$booted[static::class] = true; |
|
126
|
|
|
|
|
127
|
|
|
$this->fireEvent('booting', false); |
|
128
|
|
|
|
|
129
|
|
|
$this->boot(); |
|
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
$this->fireEvent('booted', false); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function boot() |
|
136
|
|
|
{ |
|
137
|
|
|
return true; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.