This repository has been archived by the owner on May 17, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FunctionAliasFixture.php
103 lines (82 loc) · 3.12 KB
/
FunctionAliasFixture.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
<?php
declare(strict_types=1);
namespace Laminas\Transfer\Fixture;
use Laminas\Transfer\Helper\JsonWriter;
use Laminas\Transfer\Repository;
use function current;
use function file_exists;
use function file_get_contents;
use function file_put_contents;
use function implode;
use function json_decode;
use function preg_match;
use function preg_match_all;
use function realpath;
use function sprintf;
use function str_replace;
use const PHP_EOL;
class FunctionAliasFixture extends AbstractFixture
{
public function process(Repository $repository) : void
{
$composer = current($repository->files('composer.json'));
if (! $composer) {
return;
}
$composerContent = json_decode(file_get_contents($composer), true);
$files = [];
foreach ($composerContent['autoload']['files'] ?? [] as $index => $file) {
if (! file_exists($file)) {
$this->writeln(sprintf('Missing autoloader file %s', $file));
unset($composerContent['autoload']['files'][$index]);
continue;
}
$additionalFile = $this->rewriteFunctions($repository, $file);
if ($additionalFile) {
$composerContent['autoload']['files'][] = $additionalFile;
$files[] = realpath($additionalFile);
}
}
if ($files) {
JsonWriter::write($composer, $composerContent);
$repository->addReplacedContentFiles($files);
}
}
private function rewriteFunctions(Repository $repository, string $file) : ?string
{
$content = file_get_contents($file);
// No namespace detected
if (! preg_match('/<\?php.+?^namespace\s+(.+?);/ms', $content, $namespace)) {
$this->writeln('Cannot detect namespace');
return null;
}
preg_match_all('/^use .+?;/ms', $content, $uses);
$uses = $uses[0] ?? [];
if (! preg_match_all('/^function (.+?)\(.+?{/ms', $content, $functions)) {
$this->writeln('Cannot detect functions');
return null;
}
$newNamespace = $repository->replace($namespace[1]);
$newFile = str_replace('.php', '.legacy.php', $file);
$newContent = $namespace[0] . PHP_EOL;
if ($uses) {
$newContent .= PHP_EOL . $repository->replace(implode(PHP_EOL, $uses)) . PHP_EOL;
}
foreach ($functions[1] as $functionName) {
$newContent .= PHP_EOL . 'use function '
. $newNamespace . '\\'
. $functionName . ' as laminas_' . $functionName . ';';
}
$newContent .= PHP_EOL;
foreach ($functions[0] as $i => $function) {
$newContent .= PHP_EOL . '/**' . PHP_EOL
. sprintf(' * @deprecated Use %s instead', $newNamespace . '\\' . $functions[1][$i])
. PHP_EOL . ' */'
. PHP_EOL . $function . PHP_EOL
. ' laminas_' . $functions[1][$i] . '(...func_get_args());' . PHP_EOL
. '}' . PHP_EOL;
}
file_put_contents($newFile, $newContent);
return $newFile;
}
}