Skip to content

Commit 728ffb7

Browse files
feat: New Folder Migration commands (#93)
1 parent c2a8dd9 commit 728ffb7

File tree

7 files changed

+282
-0
lines changed

7 files changed

+282
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ This should print the Kirby CLI version and a list of available commands
6262
- kirby make:snippet
6363
- kirby make:template
6464
- kirby make:user
65+
- kirby migrate:to:public-folder
66+
- kirby migrate:to:root-folder
6567
- kirby plugin:install
6668
- kirby plugin:remove
6769
- kirby plugin:upgrade
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
use Kirby\Cms\App as Kirby;
4+
5+
require dirname(__DIR__) . '/kirby/bootstrap.php';
6+
7+
$kirby = new Kirby([
8+
'roots' => [
9+
'index' => __DIR__,
10+
'base' => $base = dirname(__DIR__),
11+
'site' => $base . '/site',
12+
'content' => $base . '/content',
13+
]
14+
]);
15+
16+
echo $kirby->render();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
require __DIR__ . '/kirby/bootstrap.php';
4+
5+
echo (new Kirby)->render();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
use Kirby\CLI\Commands\Migrate\To\PublicFolder;
6+
7+
return [
8+
'description' => 'Switch to a public folder setup',
9+
'command' => PublicFolder::command(...)
10+
];
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
use Kirby\CLI\Commands\Migrate\To\RootFolder;
6+
7+
return [
8+
'description' => 'Switch to a root folder setup',
9+
'command' => RootFolder::command(...)
10+
];
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Kirby\CLI\Commands\Migrate\To;
6+
7+
use Kirby\CLI\CLI;
8+
use Kirby\Filesystem\Dir;
9+
use Kirby\Filesystem\F;
10+
11+
class PublicFolder
12+
{
13+
public static function command(CLI $cli): void
14+
{
15+
$dir = $cli->dir();
16+
17+
// A valid kirby installation is needed
18+
$cli->kirby();
19+
20+
static::confirmMigration($cli);
21+
22+
$cli->out('Migrating to a public folder setup …');
23+
$cli->br();
24+
25+
$publicDir = static::publicDir($dir);
26+
27+
static::makePublicDir($cli, $publicDir);
28+
29+
static::moveDirs($cli, $publicDir, static::movableDirs($dir));
30+
static::moveFiles($cli, $publicDir, static::movableFiles($dir));
31+
32+
static::makeIndexPHP($cli, $publicDir);
33+
static::removeOldIndexPHP($cli);
34+
35+
$cli->br();
36+
$cli->success('Migrated to a public folder setup');
37+
}
38+
39+
protected static function confirmMigration(CLI $cli): void
40+
{
41+
$cli->br();
42+
$cli->confirmToContinue("💡 Migrating your folder setup can lead to a broken site.\n\nMake sure to backup your current installation. If you have modified your index.php you might need to adjust the new index.php after the migration.\n\nDo you want to continue?");
43+
$cli->br();
44+
}
45+
46+
protected static function makeIndexPHP(CLI $cli, string $publicDir)
47+
{
48+
$template = $cli->root('commands.core') . '/migrate/to/_templates/index.public.simple.php';
49+
50+
$cli->make($publicDir . '/index.php', $template);
51+
52+
$cli->out('✅ The index.php has been created');
53+
}
54+
55+
protected static function makePublicDir(CLI $cli, string $publicDir): void
56+
{
57+
if (is_dir($publicDir) === true) {
58+
$cli->confirmToContinue('⚠️ The public folder exists. Do you still want to continue?');
59+
$cli->br();
60+
}
61+
62+
Dir::make($publicDir);
63+
64+
$cli->out('✅ The public folder has been created');
65+
}
66+
67+
protected static function movableDirs(string $dir): array
68+
{
69+
return [
70+
$dir . '/.well-known',
71+
$dir . '/assets',
72+
$dir . '/media',
73+
];
74+
}
75+
76+
protected static function movableFiles(string $dir): array
77+
{
78+
return [
79+
$dir . '/.htaccess',
80+
$dir . '/favicon.ico',
81+
$dir . '/favicon.png',
82+
$dir . '/favicon.svg',
83+
$dir . '/favicon.gif',
84+
$dir . '/robots.txt',
85+
];
86+
}
87+
88+
protected static function moveDirs(CLI $cli, string $destination, array $dirs): void
89+
{
90+
foreach ($dirs as $dir) {
91+
if (is_dir($dir) === false) {
92+
continue;
93+
}
94+
95+
$dirname = basename($dir);
96+
$target = $destination . '/' . $dirname;
97+
98+
// avoid overwriting directories that should not be overwritten
99+
if (is_dir($target) === true) {
100+
$input = $cli->confirm('⚠️ The directory ' . $dirname . ' exists. Do you want to overwrite it?');
101+
$cli->br();
102+
103+
if ($input->confirmed() === false) {
104+
continue;
105+
}
106+
}
107+
108+
Dir::remove($target);
109+
110+
if (Dir::move($dir, $target) === true) {
111+
$cli->out('✅ The ' . $dirname . ' directory has been moved');
112+
} else {
113+
$cli->out('🚨 The ' . $dirname . ' directory could not be moved');
114+
}
115+
}
116+
}
117+
118+
protected static function moveFiles(CLI $cli, string $destination, array $files): void
119+
{
120+
foreach ($files as $file) {
121+
if (is_file($file) === false) {
122+
continue;
123+
}
124+
125+
$filename = basename($file);
126+
$target = $destination . '/' . $filename;
127+
128+
// avoid overwriting directories that should not be overwritten
129+
if (is_dir($target) === true) {
130+
$input = $cli->confirm('⚠️ The file ' . $filename . ' exists. Do you want to overwrite it?');
131+
$cli->br();
132+
133+
if ($input->confirmed() === false) {
134+
continue;
135+
}
136+
}
137+
138+
F::remove($target);
139+
140+
if (F::move($file, $destination . '/' . $filename) === true) {
141+
$cli->out('✅ The ' . $filename . ' file has been moved');
142+
} else {
143+
$cli->out('🚨 The ' . $filename . ' file could not be moved');
144+
}
145+
}
146+
}
147+
148+
protected static function publicDir(string $dir): string
149+
{
150+
return $dir . '/public';
151+
}
152+
153+
protected static function removeOldIndexPHP(CLI $cli): void
154+
{
155+
$file = $cli->dir() . '/index.php';
156+
157+
if (is_file($file) === false) {
158+
return;
159+
}
160+
161+
if (F::remove($file)) {
162+
$cli->out('✅ The old index.php has been removed');
163+
} else {
164+
$cli->out('🚨 The old index.php could not been removed');
165+
}
166+
}
167+
168+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Kirby\CLI\Commands\Migrate\To;
6+
7+
use Kirby\CLI\CLI;
8+
use Kirby\Filesystem\Dir;
9+
use Kirby\Filesystem\F;
10+
11+
class RootFolder extends PublicFolder
12+
{
13+
public static function command(CLI $cli): void
14+
{
15+
$dir = $cli->dir();
16+
17+
// A valid kirby installation is needed
18+
$cli->kirby();
19+
20+
static::confirmMigration($cli);
21+
22+
$cli->out('Migrating to a root folder setup …');
23+
$cli->br();
24+
25+
$publicDir = static::publicDir($dir);
26+
27+
if (is_dir($publicDir) === false) {
28+
$cli->error('The public folder directory does not exist');
29+
return;
30+
}
31+
32+
$dirs = Dir::dirs($publicDir, null, true);
33+
$files = Dir::files($publicDir, ['index.php'], true);
34+
35+
static::moveDirs($cli, $dir, $dirs);
36+
static::moveFiles($cli, $dir, $files);
37+
38+
static::makeIndexPHP($cli, $dir);
39+
static::removePublicDir($cli, $publicDir);
40+
41+
$cli->br();
42+
$cli->success('Migrated to a root folder setup');
43+
}
44+
45+
protected static function makeIndexPHP(CLI $cli, string $dir)
46+
{
47+
$template = $cli->root('commands.core') . '/migrate/to/_templates/index.root.simple.php';
48+
49+
$cli->make($dir . '/index.php', $template);
50+
51+
$cli->out('✅ The new index.php has been created');
52+
53+
// remove the old index.php
54+
F::remove($dir . '/public/index.php');
55+
}
56+
57+
protected static function removePublicDir(CLI $cli, string $publicDir): void
58+
{
59+
if (Dir::isEmpty($publicDir) === false) {
60+
$cli->out('🚨 The public directory is not empty yet. Please make sure to move remaining files and directories yourself.');
61+
return;
62+
}
63+
64+
if (Dir::remove($publicDir) === true) {
65+
$cli->out('✅ The public directory has been removed');
66+
} else {
67+
$cli->out('🚨 The public directory could not been removed');
68+
}
69+
}
70+
71+
}

0 commit comments

Comments
 (0)