-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathPostDeployCopy.php
More file actions
executable file
·139 lines (123 loc) · 3.94 KB
/
Copy pathPostDeployCopy.php
File metadata and controls
executable file
·139 lines (123 loc) · 3.94 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace React\React\Plugin;
use Magento\Deploy\Service\DeployStaticContent;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\WriteInterface;
use Psr\Log\LoggerInterface;
/**
* Plugin to copy custom static files after deployment
*/
class PostDeployCopy
{
/**
* @var Filesystem
*/
private $filesystem;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var WriteInterface
*/
private $staticDirectory;
/**
* @param Filesystem $filesystem
* @param LoggerInterface $logger
*/
public function __construct(
Filesystem $filesystem,
LoggerInterface $logger
) {
$this->filesystem = $filesystem;
$this->logger = $logger;
$this->staticDirectory = $filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
}
/**
* After plugin for deploy method
*
* @param DeployStaticContent $subject
* @param void $result
* @param array $options
* @return void
*/
public function afterDeploy(DeployStaticContent $subject, $result, array $options)
{
try {
$this->copyCustomStaticFiles();
$this->logger->info('Custom static files copied successfully after deployment');
} catch (\Exception $e) {
$this->logger->error('Failed to copy custom static files: ' . $e->getMessage());
}
}
/**
* Copy custom static files from module to main static directory
*
* @return void
*/
private function copyCustomStaticFiles()
{
$sourcePath = dirname(__DIR__) . '/pub/static';
$targetPath = BP . '/pub/static';
if (!is_dir($sourcePath)) {
$this->logger->warning('Source directory does not exist: ' . $sourcePath);
return;
}
$this->copyDirectory($sourcePath, $targetPath);
}
/**
* Recursively copy directory contents with security checks
*
* @param string $source
* @param string $destination
* @return void
*/
private function copyDirectory($source, $destination)
{
// Use RecursiveDirectoryIterator instead of opendir/readdir for better security
try {
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $item) {
$sourcePath = $item->getPathname();
$relativePath = substr($sourcePath, strlen($source) + 1);
$destPath = $destination . '/' . $relativePath;
// Security: Prevent symlink traversal attacks
if (is_link($sourcePath)) {
$this->logger->warning('Skipping symlink: ' . $sourcePath);
continue;
}
if ($item->isDir()) {
if (!is_dir($destPath)) {
mkdir($destPath, 0755, true);
}
} else {
$this->copyFile($sourcePath, $destPath);
}
}
} catch (\Exception $e) {
$this->logger->error('Error copying directory: ' . $e->getMessage());
}
}
/**
* Copy a single file
*
* @param string $source
* @param string $destination
*/
private function copyFile($source, $destination)
{
$destinationDir = dirname($destination);
if (!is_dir($destinationDir)) {
mkdir($destinationDir, 0755, true);
}
if (copy($source, $destination)) {
$this->logger->debug('Copied file: ' . $source . ' -> ' . $destination);
} else {
$this->logger->error('Failed to copy file: ' . $source . ' -> ' . $destination);
}
}
}