-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSymlinkVipGoDir.php
114 lines (93 loc) · 2.96 KB
/
SymlinkVipGoDir.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/*
* This file is part of the vip-composer-plugin package.
*
* (c) Inpsyde GmbH
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Inpsyde\VipComposer\Task;
use Composer\Util\Filesystem;
use Composer\Util\Platform;
use Inpsyde\VipComposer\Config;
use Inpsyde\VipComposer\Io;
use Inpsyde\VipComposer\VipDirectories;
final class SymlinkVipGoDir implements Task
{
/**
* @var Config
*/
private $config;
/**
* @var VipDirectories
*/
private $directories;
/**
* @var Filesystem
*/
private $filesystem;
/**
* @param Config $config
* @param VipDirectories $directories
* @param Filesystem $filesystem
*/
public function __construct(Config $config, VipDirectories $directories, Filesystem $filesystem)
{
$this->config = $config;
$this->directories = $directories;
$this->filesystem = $filesystem;
}
/**
* @return string
*/
public function name(): string
{
return 'Symlink VIP folders';
}
/**
* @param TaskConfig $taskConfig
* @return bool
*/
public function enabled(TaskConfig $taskConfig): bool
{
return $taskConfig->isLocal() || $taskConfig->forceCoreUpdate();
}
/**
* @param Io $io
* @param TaskConfig $taskConfig
*/
public function run(Io $io, TaskConfig $taskConfig): void
{
$wpDir = $this->config->wpConfig()[Config::WP_LOCAL_DIR_KEY];
$uploadsDir = $this->config->wpConfig()[Config::WP_LOCAL_UPLOADS_DIR_KEY];
$contentDirPath = $this->config->basePath() . "/{$wpDir}/wp-content/";
$uploadsPath = $this->filesystem->normalizePath($this->config->basePath() . '/uploads');
$io->commentLine("Symlinking content to {$contentDirPath}...");
$this->filesystem->ensureDirectoryExists($contentDirPath);
$this->filesystem->emptyDirectory($contentDirPath);
$map = [
$uploadsPath => "{$contentDirPath}/{$uploadsDir}",
$this->directories->vipMuPluginsDir() => "{$contentDirPath}/mu-plugins",
];
foreach ($this->directories->toArray() as $dirPath) {
$map[$dirPath] = "{$contentDirPath}/" . basename($dirPath);
}
$isWindows = Platform::isWindows();
foreach ($map as $target => $link) {
if (is_dir($link)) {
$this->filesystem->removeDirectory($link);
}
$this->filesystem->ensureDirectoryExists($target);
$this->filesystem->normalizePath($link);
if ($isWindows) {
$this->filesystem->junction($target, $link);
continue;
}
$this->filesystem->relativeSymlink($target, $link);
}
file_put_contents("{$contentDirPath}index.php", "<?php\n// Silence is golden.\n");
$io->infoLine('Done!');
}
}