|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\Console\Commands; |
|
6
|
|
|
|
|
7
|
|
|
use Hyde\Console\Concerns\Command; |
|
8
|
|
|
use Hyde\Facades\Filesystem; |
|
9
|
|
|
use Hyde\Hyde; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @see \Hyde\Framework\Testing\Feature\Commands\ChangeSourceDirectoryCommandTest |
|
13
|
|
|
*/ |
|
14
|
|
|
class ChangeSourceDirectoryCommand extends Command |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var string */ |
|
17
|
|
|
protected $signature = 'change:sourceDirectory {name : The new source directory name }'; |
|
18
|
|
|
|
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
protected $description = 'Change the source directory for your project.'; |
|
21
|
|
|
|
|
22
|
|
|
protected $hidden = true; |
|
23
|
|
|
|
|
24
|
|
|
public function handle(): int |
|
25
|
|
|
{ |
|
26
|
|
|
$name = (string) $this->argument('name'); |
|
27
|
|
|
if (realpath(Hyde::path($name)) === realpath(Hyde::path(config('hyde.source_root', '')))) { |
|
28
|
|
|
$this->error("The directory '$name' is already set as the project source root!"); |
|
29
|
|
|
|
|
30
|
|
|
return 409; |
|
31
|
|
|
} |
|
32
|
|
|
$this->infoComment('Setting', $name, 'as the project source directory!'); |
|
33
|
|
|
|
|
34
|
|
|
$directories = array_unique([ |
|
35
|
|
|
\Hyde\Pages\HtmlPage::$sourceDirectory, |
|
36
|
|
|
\Hyde\Pages\BladePage::$sourceDirectory, |
|
37
|
|
|
\Hyde\Pages\MarkdownPage::$sourceDirectory, |
|
38
|
|
|
\Hyde\Pages\MarkdownPost::$sourceDirectory, |
|
39
|
|
|
\Hyde\Pages\DocumentationPage::$sourceDirectory, |
|
40
|
|
|
]); |
|
41
|
|
|
|
|
42
|
|
|
if (Filesystem::isDirectory($name) && ! Filesystem::isEmptyDirectory($name)) { |
|
43
|
|
|
foreach ($directories as $directory) { |
|
44
|
|
|
$directory = "$name/".basename($directory); |
|
45
|
|
|
if (self::isNonEmptyDirectory(Hyde::path($directory))) { |
|
46
|
|
|
$this->error('Directory already exists!'); |
|
47
|
|
|
|
|
48
|
|
|
return 409; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$this->comment('Creating directory'); |
|
54
|
|
|
Filesystem::ensureDirectoryExists($name); |
|
55
|
|
|
|
|
56
|
|
|
$this->comment('Moving source directories'); |
|
57
|
|
|
|
|
58
|
|
|
foreach ($directories as $directory) { |
|
59
|
|
|
Filesystem::moveDirectory($directory, "$name/".basename($directory)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$this->comment('Updating configuration file'); |
|
63
|
|
|
|
|
64
|
|
|
$current = (string) config('hyde.source_root', ''); |
|
65
|
|
|
$search = "'source_root' => '$current',"; |
|
66
|
|
|
|
|
67
|
|
|
$config = Filesystem::getContents('config/hyde.php'); |
|
68
|
|
|
if (str_contains($config, $search)) { |
|
69
|
|
|
$config = str_replace($search, "'source_root' => '$name',", $config); |
|
70
|
|
|
Filesystem::putContents('config/hyde.php', $config); |
|
71
|
|
|
} else { |
|
72
|
|
|
$this->error('Automatic configuration update failed, to finalize the change, please set the `source_root` setting to '."'$name'".' in `config/hyde.php`'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// We could also check if there are any more page classes (from packages) and add a note that they may need manual attention |
|
76
|
|
|
|
|
77
|
|
|
$this->info('All done!'); |
|
78
|
|
|
|
|
79
|
|
|
return Command::SUCCESS; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
protected static function isNonEmptyDirectory(string $directory): bool |
|
83
|
|
|
{ |
|
84
|
|
|
if (is_file($directory)) { |
|
85
|
|
|
return true; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return is_dir($directory) && (count(scandir($directory)) > 2); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|