This repository was archived by the owner on Dec 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBread.php
155 lines (127 loc) · 3.59 KB
/
Bread.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
<?php
namespace Bread;
use Bread\Classes\Bread as BreadClass;
use Bread\Traits\Translatable;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use TCG\Voyager\Facades\Voyager;
class Bread
{
use Translatable;
protected $breads;
protected $breadPath;
protected $formfields;
public function getTables()
{
return array_diff(
app('BreadDB')->getDoctrineSchemaManager()->listTableNames(),
config('voyager.database.tables.hidden', [])
);
}
public function getTableColumns($table)
{
return app('BreadDB')->getSchemaBuilder()->getColumnListing($table);
}
public function breadPath($path = null)
{
if ($path) {
$this->breadPath = Str::finish($path, '/');
}
return $this->breadPath;
}
public function getBreads()
{
if (!$this->breads) {
// Cache BREADs
if (!File::isDirectory($this->breadPath)) {
File::makeDirectory($this->breadPath);
}
$this->breads = collect(File::files($this->breadPath))->transform(function ($bread) {
return new BreadClass($bread->getPathName());
})->filter(function ($bread) {
return $bread->isValid();
});
}
return $this->breads;
}
public function getBread($table)
{
if (!$this->breads) {
$this->getBreads();
}
return $this->breads->where('table', $table)->first();
}
public function getBreadBySlug($slug)
{
if (!$this->breads) {
$this->getBreads();
}
return $this->breads->whereTranslation('slug', $slug)->first();
}
public function getBreadAccessors($table)
{
if ($bread = $this->getBread($table)) {
return $bread->getModel()->accessors ?? [];
}
return [];
}
public function getBreadRelationships($table)
{
if ($bread = $this->getBread($table)) {
$relationships = collect();
foreach (($bread->getModel()->relationships ?? []) as $name => $table) {
$relationships->push([
'name' => $name,
'table' => $table,
'fields' => $this->getTableColumns($table),
'layouts' => $this->getBread($table)->layouts ?? [],
'type' => (new \ReflectionClass($bread->getModel()->{$name}()))->getShortName(),
]);
}
return $relationships;
}
return [];
}
public function debug($message)
{
if (function_exists('debug')) {
debug($message);
}
}
public function translatable()
{
return true;
}
public function getLocales()
{
return ['de', 'en'];
}
public function getLocale()
{
return app()->getLocale();
}
public function addFormfield($class)
{
if (!$this->formfields) {
$this->formfields = collect();
}
$class = new $class();
$this->formfields->push($class);
}
public function formfields()
{
return $this->formfields->unique();
}
public function formfield($type)
{
return $this->formfields->filter(function ($formfield) use ($type) {
return $formfield->getType() == $type;
})->first();
}
public function getAllRoles()
{
return Voyager::model('Role')->all()->transform(function ($role) {
return $role->only(['name', 'id']);
});
}
}