Repository navigation
Expand file tree
/
Copy pathComposerScripts.php
More file actions
220 lines (192 loc) · 4.91 KB
/
Copy pathComposerScripts.php
File metadata and controls
220 lines (192 loc) · 4.91 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CodeIgniter;
use ReflectionClass;
use ReflectionException;
/**
* ComposerScripts
*
* These scripts are used by Composer during installs and updates
* to move files to locations within the system folder so that end-users
* do not need to use Composer to install a package, but can simply
* download
*
* @codeCoverageIgnore
*/
class ComposerScripts
{
/**
* Base path to use.
*
* @var string
*/
protected static $basePath = 'ThirdParty/';
/**
* After composer install/update, this is called to move
* the bare-minimum required files for our dependencies
* to appropriate locations.
*
* @throws ReflectionException
*/
public static function postUpdate()
{
static::moveEscaper();
static::moveKint();
}
//--------------------------------------------------------------------
/**
* Move a file.
*
* @param string $source
* @param string $destination
*
* @return boolean
*/
protected static function moveFile(string $source, string $destination): bool
{
$source = realpath($source);
if (empty($source))
{
// @codeCoverageIgnoreStart
die('Cannot move file. Source path invalid.');
// @codeCoverageIgnoreEnd
}
if (! is_file($source))
{
return false;
}
return copy($source, $destination);
}
//--------------------------------------------------------------------
/**
* Determine file path of a class.
*
* @param string $class
*
* @return string
* @throws ReflectionException
*/
protected static function getClassFilePath(string $class)
{
$reflector = new ReflectionClass($class);
return $reflector->getFileName();
}
//--------------------------------------------------------------------
/**
* A recursive remove directory method.
*
* @param string $dir
*/
protected static function removeDir($dir)
{
if (is_dir($dir))
{
$objects = scandir($dir);
foreach ($objects as $object)
{
if ($object !== '.' && $object !== '..')
{
if (filetype($dir . '/' . $object) === 'dir')
{
static::removeDir($dir . '/' . $object);
}
else
{
unlink($dir . '/' . $object);
}
}
}
reset($objects);
rmdir($dir);
}
}
protected static function copyDir($source, $dest)
{
$dir = opendir($source);
@mkdir($dest);
while (false !== ($file = readdir($dir)))
{
if (($file !== '.') && ($file !== '..'))
{
if (is_dir($source . '/' . $file))
{
static::copyDir($source . '/' . $file, $dest . '/' . $file);
}
else
{
copy($source . '/' . $file, $dest . '/' . $file);
}
}
}
closedir($dir);
}
/**
* Moves the Laminas Escaper files into our base repo so that it's
* available for packaged releases where the users don't user Composer.
*
* @throws ReflectionException
*/
public static function moveEscaper()
{
if (class_exists('\\Laminas\\Escaper\\Escaper') && is_file(static::getClassFilePath('\\Laminas\\Escaper\\Escaper')))
{
$base = basename(__DIR__) . '/' . static::$basePath . 'Escaper';
foreach ([$base, $base . '/Exception'] as $path)
{
if (! is_dir($path))
{
mkdir($path, 0755);
}
}
$files = [
static::getClassFilePath('\\Laminas\\Escaper\\Exception\\ExceptionInterface') => $base . '/Exception/ExceptionInterface.php',
static::getClassFilePath('\\Laminas\\Escaper\\Exception\\InvalidArgumentException') => $base . '/Exception/InvalidArgumentException.php',
static::getClassFilePath('\\Laminas\\Escaper\\Exception\\RuntimeException') => $base . '/Exception/RuntimeException.php',
static::getClassFilePath('\\Laminas\\Escaper\\Escaper') => $base . '/Escaper.php',
];
foreach ($files as $source => $dest)
{
if (! static::moveFile($source, $dest))
{
// @codeCoverageIgnoreStart
die('Error moving: ' . $source);
// @codeCoverageIgnoreEnd
}
}
}
}
//--------------------------------------------------------------------
/**
* Moves the Kint file into our base repo so that it's
* available for packaged releases where the users don't user Composer.
*/
public static function moveKint()
{
$dir = 'vendor/kint-php/kint/src';
if (is_dir($dir))
{
$base = basename(__DIR__) . '/' . static::$basePath . 'Kint';
// Remove the contents of the previous Kint folder, if any.
if (is_dir($base))
{
static::removeDir($base);
}
// Create Kint if it doesn't exist already
if (! is_dir($base))
{
mkdir($base, 0755);
}
static::copyDir($dir, $base);
static::copyDir($dir . '/../resources', $base . '/resources');
copy($dir . '/../init.php', $base . '/init.php');
copy($dir . '/../init_helpers.php', $base . '/init_helpers.php');
}
}
}