58 lines
1.6 KiB
PHP
Raw Normal View History

2022-12-10 18:29:42 +08:00
<?php
2022-12-14 19:25:52 +08:00
2023-01-11 17:17:36 +08:00
namespace Modules\Common\Repository\Options;
2022-12-10 18:29:42 +08:00
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class Components implements OptionInterface
{
/**
* @var array|string[]
*/
protected array $components = [
[
'label' => 'layout',
2024-04-25 20:34:00 +08:00
'value' => '/layout/index.vue',
],
2022-12-10 18:29:42 +08:00
];
public function get(): array
{
2024-03-07 11:28:09 +08:00
try {
2024-04-25 20:34:00 +08:00
$viewRootPath = config('catch.views_path');
2024-03-07 11:28:09 +08:00
if ($module = request()->get('module')) {
2024-04-25 20:34:00 +08:00
if (!File::exists($viewRootPath . $module . DIRECTORY_SEPARATOR)) {
return [];
}
$components = File::allFiles($viewRootPath . $module . DIRECTORY_SEPARATOR);
2024-03-07 11:28:09 +08:00
foreach ($components as $component) {
2024-04-25 20:34:00 +08:00
// 过滤非 vue 文件
if ($component->getExtension() !== 'vue') {
continue;
}
$_component = Str::of($component->getPathname())
->replace($viewRootPath, '')
2024-03-07 11:28:09 +08:00
->explode(DIRECTORY_SEPARATOR);
2024-04-25 20:34:00 +08:00
$_component->shift(1);
2024-03-07 11:28:09 +08:00
$this->components[] = [
'label' => Str::of($_component->implode('/'))->replace('.vue', ''),
2024-04-25 20:34:00 +08:00
'value' => Str::of($component)->replace($viewRootPath, '')->prepend('/'),
2024-03-07 11:28:09 +08:00
];
}
2022-12-10 18:29:42 +08:00
}
2024-03-07 11:28:09 +08:00
return $this->components;
} catch (\Throwable $exception) {
return [];
}
2022-12-10 18:29:42 +08:00
}
}