|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace A17\Twill\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Config\Repository as Config; |
|
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Illuminate\Support\Str; |
|
9
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
10
|
|
|
|
|
11
|
|
|
class ListIcons extends Command |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* The name and signature of the console command. |
|
15
|
|
|
* |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $signature = 'twill:list:icons {filter? : Filter icons by name}'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The console command description. |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $description = 'List available icons'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var Filesystem |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $files; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var Config |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $config; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param Filesystem $files |
|
39
|
|
|
* @param Config $config |
|
40
|
|
|
*/ |
|
41
|
69 |
|
public function __construct(Filesystem $files, Config $config) |
|
42
|
|
|
{ |
|
43
|
69 |
|
parent::__construct(); |
|
44
|
|
|
|
|
45
|
69 |
|
$this->files = $files; |
|
46
|
69 |
|
$this->config = $config; |
|
47
|
69 |
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
private function isAllowed($icon) |
|
50
|
|
|
{ |
|
51
|
1 |
|
if (filled($filter = $this->argument('filter'))) { |
|
52
|
|
|
return Str::contains( |
|
53
|
|
|
Str::lower($icon['name']), |
|
54
|
|
|
Str::lower($filter) |
|
|
|
|
|
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
if (in_array($icon['name'] . '.svg', config('twill.internal_icons'))) { |
|
59
|
|
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
return true; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return \Illuminate\Support\Collection |
|
67
|
|
|
*/ |
|
68
|
1 |
|
protected function getIconList() |
|
69
|
|
|
{ |
|
70
|
1 |
|
return collect( |
|
71
|
1 |
|
config('twill.block_editor.directories.source.icons') |
|
72
|
|
|
)->reduce(function (Collection $keep, $path) { |
|
73
|
1 |
|
if (!$this->files->exists($path)) { |
|
74
|
|
|
$this->displayError("Directory not found: {$path}"); |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
return $keep; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$files = collect($this->files->files($path))->map(function ( |
|
80
|
|
|
SplFileInfo $file |
|
81
|
|
|
) { |
|
82
|
|
|
return [ |
|
83
|
1 |
|
'name' => Str::before($file->getFilename(), '.svg'), |
|
84
|
1 |
|
'url' => route('admin.icons.show', [ |
|
85
|
1 |
|
'file' => $file->getFilename(), |
|
86
|
|
|
]), |
|
87
|
|
|
]; |
|
88
|
1 |
|
}); |
|
89
|
|
|
|
|
90
|
1 |
|
return $keep->merge($files); |
|
91
|
1 |
|
}, collect()); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Executes the console command. |
|
96
|
|
|
*/ |
|
97
|
1 |
|
public function handle() |
|
98
|
|
|
{ |
|
99
|
|
|
$icons = $this->getIconList()->filter(function ($icon) { |
|
100
|
1 |
|
return $this->isAllowed($icon); |
|
101
|
1 |
|
}); |
|
102
|
|
|
|
|
103
|
1 |
|
$this->table(['Icon', 'Preview URL'], $icons->toArray()); |
|
104
|
1 |
|
$this->info("All icons viewable at: " . route('admin.icons.index')); |
|
105
|
|
|
|
|
106
|
1 |
|
return parent::handle(); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|