-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Transformer.php
166 lines (146 loc) · 4.07 KB
/
Transformer.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
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
<?php
declare(strict_types=1);
namespace TypistTech\Imposter;
use SplFileInfo;
class Transformer implements TransformerInterface
{
/**
* @var FilesystemInterface
*/
private $filesystem;
/**
* @var string
*/
private $namespacePrefix;
/**
* Transformer constructor.
*
* @param string $namespacePrefix
* @param FilesystemInterface $filesystem
*/
public function __construct(string $namespacePrefix, FilesystemInterface $filesystem)
{
$this->namespacePrefix = StringUtil::ensureDoubleBackwardSlash($namespacePrefix);
$this->filesystem = $filesystem;
}
/**
* Transform a file or directory recursively.
*
* @todo Skip non-php files.
*
* @param string $target Path to the target file or directory.
*
* @return void
*/
public function transform(string $target)
{
if ($this->filesystem->isFile($target)) {
$this->doTransform($target);
return;
}
$files = $this->filesystem->allFiles($target);
array_walk($files, function (SplFileInfo $file) {
$this->doTransform($file->getRealPath());
});
}
/**
* @param string $targetFile
*
* @return void
*/
private function doTransform(string $targetFile)
{
$this->prefixNamespace($targetFile);
$this->prefixUseConst($targetFile);
$this->prefixUseFunction($targetFile);
$this->prefixUse($targetFile);
}
/**
* Prefix namespace at the given path.
*
* @param string $targetFile
*
* @return void
*/
private function prefixNamespace(string $targetFile)
{
$pattern = sprintf(
'/(\s+)%1$s\\s+(?!(%2$s)|(Composer(\\\\|;)))/',
'namespace',
$this->namespacePrefix
);
$replacement = sprintf('%1$s %2$s', '${1}namespace', $this->namespacePrefix);
$this->replace($pattern, $replacement, $targetFile);
}
/**
* Replace string in the given file.
*
* @param string $pattern
* @param string $replacement
* @param string $targetFile
*
* @return void
*/
private function replace(string $pattern, string $replacement, string $targetFile)
{
$this->filesystem->put(
$targetFile,
preg_replace(
$pattern,
$replacement,
$this->filesystem->get($targetFile)
)
);
}
/**
* Prefix `use const` keywords at the given path.
*
* @param string $targetFile
*
* @return void
*/
private function prefixUseConst(string $targetFile)
{
$pattern = sprintf(
'/%1$s\\s+(?!(%2$s)|(\\\\(?!.*\\\\.*))|(Composer(\\\\|;)|(?!.*\\\\.*)))/',
'use const',
$this->namespacePrefix
);
$replacement = sprintf('%1$s %2$s', 'use const', $this->namespacePrefix);
$this->replace($pattern, $replacement, $targetFile);
}
/**
* Prefix `use function` keywords at the given path.
*
* @param string $targetFile
*
* @return void
*/
private function prefixUseFunction(string $targetFile)
{
$pattern = sprintf(
'/%1$s\\s+(?!(%2$s)|(\\\\(?!.*\\\\.*))|(Composer(\\\\|;)|(?!.*\\\\.*)))/',
'use function',
$this->namespacePrefix
);
$replacement = sprintf('%1$s %2$s', 'use function', $this->namespacePrefix);
$this->replace($pattern, $replacement, $targetFile);
}
/**
* Prefix `use` keywords at the given path.
*
* @param string $targetFile
*
* @return void
*/
private function prefixUse(string $targetFile)
{
$pattern = sprintf(
'/%1$s\\s+(?!(const)|(function)|(%2$s)|(\\\\(?!.*\\\\.*))|(Composer(\\\\|;)|(?!.*\\\\.*)))/',
'use',
$this->namespacePrefix
);
$replacement = sprintf('%1$s %2$s', 'use', $this->namespacePrefix);
$this->replace($pattern, $replacement, $targetFile);
}
}