Skip to content

Commit 5e69d55

Browse files
committed
wip
1 parent 42184a4 commit 5e69d55

File tree

6 files changed

+166
-26
lines changed

6 files changed

+166
-26
lines changed

src/app/Console/Commands/Upgrade/Concerns/InteractsWithCrudControllers.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,6 @@ protected function filterCrudControllerPaths(array $paths, ?callable $contentsVa
4141
return $filtered;
4242
}
4343

44-
/**
45-
* Build a short list of preview lines for the provided paths.
46-
*/
47-
protected function previewLines(array $paths, int $limit = 10, ?callable $formatter = null): array
48-
{
49-
if (empty($paths)) {
50-
return [];
51-
}
52-
53-
$formatter ??= static fn (string $path): string => "- {$path}";
54-
55-
$preview = array_slice($paths, 0, $limit);
56-
$details = array_map($formatter, $preview);
57-
58-
$remaining = count($paths) - count($preview);
59-
60-
if ($remaining > 0) {
61-
$details[] = sprintf('… %d more occurrence(s) omitted.', $remaining);
62-
}
63-
64-
return $details;
65-
}
66-
6744
protected function isCrudControllerPath(string $path): bool
6845
{
6946
return str_contains($path, 'CrudController');

src/app/Console/Commands/Upgrade/Step.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,34 @@ public function fix(StepResult $result): StepResult
3636
{
3737
return StepResult::skipped('No automatic fix available.');
3838
}
39+
40+
/**
41+
* Build a preview of items with an optional formatter and overflow message.
42+
*
43+
* @param array<int, mixed> $items
44+
* @param callable|null $formatter
45+
* @return array<int, string>
46+
*/
47+
protected function previewList(
48+
array $items,
49+
int $limit = 10,
50+
?callable $formatter = null,
51+
?string $overflowMessage = null
52+
): array {
53+
if (empty($items)) {
54+
return [];
55+
}
56+
57+
$formatter ??= static fn ($item): string => '- '.(string) $item;
58+
$preview = array_slice($items, 0, $limit);
59+
$details = array_map($formatter, $preview);
60+
61+
$remaining = count($items) - count($preview);
62+
63+
if ($remaining > 0) {
64+
$details[] = sprintf($overflowMessage ?? '... %d more item(s) omitted.', $remaining);
65+
}
66+
67+
return $details;
68+
}
3969
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
namespace Backpack\CRUD\app\Console\Commands\Upgrade\v7\Steps;
4+
5+
use Backpack\CRUD\app\Console\Commands\Upgrade\Step;
6+
use Backpack\CRUD\app\Console\Commands\Upgrade\StepResult;
7+
use Illuminate\Filesystem\Filesystem;
8+
9+
class CheckFileManagerPublishedViewsStep extends Step
10+
{
11+
private const LEGACY_VIEWS_DIRECTORY = 'resources/views/vendor/elfinder';
12+
13+
/**
14+
* @var array<int, string>
15+
*/
16+
protected array $legacyFiles = [];
17+
18+
protected bool $legacyDirectoryDetected = false;
19+
20+
public function title(): string
21+
{
22+
return 'Legacy File Manager views';
23+
}
24+
25+
public function run(): StepResult
26+
{
27+
$this->legacyFiles = [];
28+
$this->legacyDirectoryDetected = false;
29+
30+
if (! $this->context()->hasComposerPackage('backpack/filemanager')) {
31+
return StepResult::success('File Manager add-on not detected; no published views to review.');
32+
}
33+
34+
if (! $this->context()->fileExists(self::LEGACY_VIEWS_DIRECTORY)) {
35+
return StepResult::success('No legacy File Manager views found in resources/views/vendor/elfinder.');
36+
}
37+
38+
$filesystem = new Filesystem();
39+
$absoluteDirectory = $this->context()->basePath(self::LEGACY_VIEWS_DIRECTORY);
40+
41+
if (! $filesystem->isDirectory($absoluteDirectory)) {
42+
return StepResult::success('No legacy File Manager views found in resources/views/vendor/elfinder.');
43+
}
44+
45+
$this->legacyDirectoryDetected = true;
46+
47+
$relativeFiles = $this->collectRelativeFiles($filesystem, $absoluteDirectory);
48+
$this->legacyFiles = $relativeFiles;
49+
50+
if (empty($relativeFiles)) {
51+
return StepResult::warning(
52+
'Legacy File Manager directory detected. Delete resources/views/vendor/elfinder if you have not customized those views.',
53+
['Published directory: '.self::LEGACY_VIEWS_DIRECTORY]
54+
);
55+
}
56+
57+
$details = array_merge(
58+
['Published directory: '.self::LEGACY_VIEWS_DIRECTORY],
59+
$this->previewList(
60+
$relativeFiles,
61+
10,
62+
static fn (string $path): string => "- {$path}",
63+
'... %d more file(s) omitted.'
64+
)
65+
);
66+
67+
return StepResult::warning(
68+
'Legacy File Manager views detected. Delete resources/views/vendor/elfinder if you have not customized those views.',
69+
$details,
70+
['paths' => $relativeFiles]
71+
);
72+
}
73+
74+
public function canFix(StepResult $result): bool
75+
{
76+
if (! $result->status->isWarning()) {
77+
return false;
78+
}
79+
80+
return $this->legacyDirectoryDetected;
81+
}
82+
83+
public function fixMessage(StepResult $result): string
84+
{
85+
return 'We can delete resources/views/vendor/elfinder so Backpack uses the updated File Manager views. Proceed?';
86+
}
87+
88+
public function fix(StepResult $result): StepResult
89+
{
90+
if (! $this->legacyDirectoryDetected) {
91+
return StepResult::skipped('Legacy File Manager directory no longer present.');
92+
}
93+
94+
foreach ($this->legacyFiles as $path) {
95+
if (! $this->context()->deleteFile($path)) {
96+
return StepResult::failure("Could not delete {$path} automatically.");
97+
}
98+
}
99+
100+
$filesystem = new Filesystem();
101+
$absoluteDirectory = $this->context()->basePath(self::LEGACY_VIEWS_DIRECTORY);
102+
103+
if ($filesystem->isDirectory($absoluteDirectory) && ! $filesystem->deleteDirectory($absoluteDirectory)) {
104+
return StepResult::failure('Could not delete resources/views/vendor/elfinder automatically.');
105+
}
106+
107+
$this->legacyFiles = [];
108+
$this->legacyDirectoryDetected = false;
109+
110+
return StepResult::success('Removed resources/views/vendor/elfinder so the package views are used.');
111+
}
112+
113+
protected function collectRelativeFiles(Filesystem $filesystem, string $absoluteDirectory): array
114+
{
115+
$files = [];
116+
117+
foreach ($filesystem->allFiles($absoluteDirectory) as $file) {
118+
$realPath = $file->getRealPath();
119+
120+
if ($realPath === false) {
121+
continue;
122+
}
123+
124+
$files[] = $this->context()->relativePath($realPath);
125+
}
126+
127+
sort($files);
128+
129+
return $files;
130+
}
131+
}

src/app/Console/Commands/Upgrade/v7/Steps/DetectDeprecatedWysiwygUsageStep.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function run(): StepResult
3030
return StepResult::success('No wysiwyg aliases detected.');
3131
}
3232

33-
$details = $this->previewLines($paths);
33+
$details = $this->previewList($paths);
3434

3535
return StepResult::warning(
3636
'Replace the wysiwyg field/column with ckeditor or text (the alias was removed).',

src/app/Console/Commands/Upgrade/v7/Steps/DetectEditorAddonRequirementsStep.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ public function run(): StepResult
5555
continue;
5656
}
5757

58-
$details = $this->previewLines(
58+
$details = $this->previewList(
5959
$paths,
6060
10,
61-
fn (string $path): string => "- {$keyword} usage: {$path}"
61+
fn (string $path): string => "- {$keyword} usage: {$path}",
62+
'... %d more occurrence(s) omitted.'
6263
);
6364

6465
if ($composerConstraint === null) {

src/app/Console/Commands/Upgrade/v7/UpgradeCommandConfig.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public function steps(): array
2020
Step\DetectEditorAddonRequirementsStep::class,
2121
Step\CheckShowOperationViewPublishedStep::class,
2222
Step\CheckShowOperationComponentConfigStep::class,
23+
Step\CheckFileManagerPublishedViewsStep::class,
2324
Step\CheckListOperationViewPublishedStep::class,
2425
];
2526
}

0 commit comments

Comments
 (0)