Skip to content

Commit 40346e9

Browse files
feat: New Folder Migration commands
1 parent c2a8dd9 commit 40346e9

File tree

6 files changed

+236
-0
lines changed

6 files changed

+236
-0
lines changed
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 rooter folder setup',
9+
'command' => RootFolder::command(...)
10+
];
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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. Make 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. Do 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+
return;
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 . '/assets',
71+
$dir . '/media',
72+
];
73+
}
74+
75+
protected static function movableFiles(string $dir): array
76+
{
77+
return [
78+
$dir . '/.htaccess',
79+
];
80+
}
81+
82+
protected static function moveDirs(CLI $cli, string $destination, array $dirs): void
83+
{
84+
foreach ($dirs as $dir) {
85+
if (is_dir($dir) === false) {
86+
continue;
87+
}
88+
89+
$dirname = basename($dir);
90+
91+
if (Dir::move($dir, $destination . '/' . $dirname) === true) {
92+
$cli->out('✅ The ' . $dirname . ' directory has been moved');
93+
} else {
94+
$cli->out('🚨 The ' . $dirname . ' directory could not be moved');
95+
}
96+
}
97+
}
98+
99+
protected static function moveFiles(CLI $cli, string $destination, array $files): void
100+
{
101+
foreach ($files as $file) {
102+
if (is_file($file) === false) {
103+
continue;
104+
}
105+
106+
$filename = basename($file);
107+
108+
if (F::move($file, $destination . '/' . $filename) === true) {
109+
$cli->out('✅ The ' . $filename . ' file has been moved');
110+
} else {
111+
$cli->out('🚨 The ' . $filename . ' file could not be moved');
112+
}
113+
}
114+
}
115+
116+
protected static function publicDir(string $dir): string
117+
{
118+
return $dir . '/public';
119+
}
120+
121+
protected static function removeOldIndexPHP(CLI $cli): void
122+
{
123+
$file = $cli->dir() . '/index.php';
124+
125+
if (is_file($file) === false) {
126+
return;
127+
}
128+
129+
if (F::remove($file)) {
130+
$cli->out('✅ The old index.php has been removed');
131+
} else {
132+
$cli->out('🚨 The old index.php could not been removed');
133+
}
134+
}
135+
136+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
10+
class RootFolder extends PublicFolder
11+
{
12+
public static function command(CLI $cli): void
13+
{
14+
$dir = $cli->dir();
15+
16+
// A valid kirby installation is needed
17+
$cli->kirby();
18+
19+
static::confirmMigration($cli);
20+
21+
$cli->out('Migrating to a root folder setup …');
22+
$cli->br();
23+
24+
$publicDir = static::publicDir($dir);
25+
26+
if (is_dir($publicDir) === false) {
27+
$cli->error('The public folder directory does not exist');
28+
return;
29+
}
30+
31+
static::moveDirs($cli, $dir, static::movableDirs($publicDir));
32+
static::moveFiles($cli, $dir, static::movableFiles($publicDir));
33+
34+
static::makeIndexPHP($cli, $dir);
35+
static::removePublicDir($cli, $publicDir);
36+
37+
$cli->br();
38+
$cli->success('Migrated to a root folder setup');
39+
}
40+
41+
protected static function makeIndexPHP(CLI $cli, string $dir)
42+
{
43+
$template = $cli->root('commands.core') . '/migrate/to/_templates/index.root.simple.php';
44+
45+
$cli->make($dir . '/index.php', $template);
46+
47+
$cli->out('✅ The index.php has been created');
48+
}
49+
50+
protected static function removePublicDir(CLI $cli, string $publicDir): void
51+
{
52+
if (Dir::remove($publicDir) === true) {
53+
$cli->out('✅ The public directory has been removed');
54+
} else {
55+
$cli->out('🚨 The public directory could not been removed');
56+
}
57+
}
58+
59+
}

0 commit comments

Comments
 (0)