1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration; |
||
6 | |||
7 | use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\AbstractGenerator; |
||
8 | use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\FileCreationTransaction; |
||
9 | use EdmondsCommerce\DoctrineStaticMeta\Config; |
||
10 | use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
||
11 | use Exception; |
||
12 | use RuntimeException; |
||
13 | use Symfony\Component\Filesystem\Filesystem; |
||
14 | |||
15 | use function basename; |
||
16 | use function dirname; |
||
17 | use function realpath; |
||
18 | |||
19 | class PathHelper |
||
20 | { |
||
21 | /** |
||
22 | * @var Filesystem |
||
23 | */ |
||
24 | protected $filesystem; |
||
25 | /** |
||
26 | * @var FileCreationTransaction |
||
27 | */ |
||
28 | protected $fileCreationTransaction; |
||
29 | |||
30 | public function __construct(Filesystem $filesystem, FileCreationTransaction $fileCreationTransaction) |
||
31 | { |
||
32 | $this->filesystem = $filesystem; |
||
33 | $this->fileCreationTransaction = $fileCreationTransaction; |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @return string |
||
38 | * @throws DoctrineStaticMetaException |
||
39 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
40 | */ |
||
41 | public function getProjectRootDirectory(): string |
||
42 | { |
||
43 | return Config::getProjectRootDirectory(); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param string $path |
||
48 | */ |
||
49 | public function ensurePathExists(string $path): void |
||
50 | { |
||
51 | if ($this->filesystem->exists($path)) { |
||
52 | return; |
||
53 | } |
||
54 | $this->filesystem->mkdir($path); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @param string $pathToProjectRoot |
||
59 | * @param string $templatePath |
||
60 | * @param string $destinationFileName |
||
61 | * @param array $subDirectories |
||
62 | * |
||
63 | * @return string |
||
64 | * @throws DoctrineStaticMetaException |
||
65 | */ |
||
66 | public function copyTemplateAndGetPath( |
||
67 | string $pathToProjectRoot, |
||
68 | string $templatePath, |
||
69 | string $destinationFileName, |
||
70 | array $subDirectories |
||
71 | ): string { |
||
72 | try { |
||
73 | $path = $this->createSubDirectoriesAndGetPath($pathToProjectRoot, $subDirectories); |
||
74 | if (false === \ts\stringContains($destinationFileName, '.php')) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
75 | $destinationFileName = "$destinationFileName.php"; |
||
76 | } |
||
77 | $filePath = "$path/$destinationFileName"; |
||
78 | $this->filesystem->copy($templatePath, $filePath, true); |
||
79 | $this->fileCreationTransaction::setPathCreated($filePath); |
||
80 | |||
81 | return $filePath; |
||
82 | } catch (Exception $e) { |
||
83 | throw new DoctrineStaticMetaException( |
||
84 | 'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
||
85 | $e->getCode(), |
||
86 | $e |
||
87 | ); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param string $pathToProjectRoot |
||
93 | * @param array $subDirectories |
||
94 | * |
||
95 | * @return string |
||
96 | * @throws DoctrineStaticMetaException |
||
97 | */ |
||
98 | public function createSubDirectoriesAndGetPath(string $pathToProjectRoot, array $subDirectories): string |
||
99 | { |
||
100 | if (!$this->filesystem->exists($pathToProjectRoot)) { |
||
101 | throw new DoctrineStaticMetaException("path to project root $pathToProjectRoot does not exist"); |
||
102 | } |
||
103 | foreach ($subDirectories as $sd) { |
||
104 | $pathToProjectRoot .= "/$sd"; |
||
105 | try { |
||
106 | $this->filesystem->mkdir($pathToProjectRoot); |
||
107 | } catch (Exception $e) { |
||
108 | throw new DoctrineStaticMetaException( |
||
109 | 'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
||
110 | $e->getCode(), |
||
111 | $e |
||
112 | ); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | return realpath($pathToProjectRoot); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @param string $pathToProjectRoot |
||
121 | * @param string $templatePath |
||
122 | * @param string $destPath |
||
123 | * |
||
124 | * @return string |
||
125 | * @throws DoctrineStaticMetaException |
||
126 | */ |
||
127 | public function copyTemplateDirectoryAndGetPath( |
||
128 | string $pathToProjectRoot, |
||
129 | string $templatePath, |
||
130 | string $destPath |
||
131 | ): string { |
||
132 | $realTemplatePath = realpath($templatePath); |
||
133 | if (false === $realTemplatePath) { |
||
134 | throw new DoctrineStaticMetaException('path ' . $templatePath . ' does not exist'); |
||
135 | } |
||
136 | |||
137 | $relativeDestPath = $this->filesystem->makePathRelative($destPath, $pathToProjectRoot); |
||
138 | $subDirectories = explode('/', $relativeDestPath); |
||
139 | $path = $this->createSubDirectoriesAndGetPath($pathToProjectRoot, $subDirectories); |
||
140 | $this->filesystem->mirror($realTemplatePath, $path); |
||
141 | $this->fileCreationTransaction::setPathCreated($path); |
||
142 | |||
143 | return $path; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @param string $path |
||
148 | * @param string $singular |
||
149 | * @param string $plural |
||
150 | * |
||
151 | * @return string |
||
152 | * @throws DoctrineStaticMetaException |
||
153 | */ |
||
154 | public function renamePathBasenameSingularOrPlural( |
||
155 | string $path, |
||
156 | string $singular, |
||
157 | string $plural |
||
158 | ): string { |
||
159 | $find = AbstractGenerator::FIND_ENTITY_NAME; |
||
160 | $replace = $singular; |
||
161 | $basename = basename($path); |
||
162 | if (\ts\stringContains($basename, AbstractGenerator::FIND_ENTITY_NAME_PLURAL)) { |
||
163 | $find = AbstractGenerator::FIND_ENTITY_NAME_PLURAL; |
||
164 | $replace = $plural; |
||
165 | } |
||
166 | |||
167 | return $this->renamePathBasename($find, $replace, $path); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Move the basename of a path to the find/replaced version |
||
172 | * |
||
173 | * Then return the updated path |
||
174 | * |
||
175 | * @param string $find |
||
176 | * @param string $replace |
||
177 | * @param string $path |
||
178 | * |
||
179 | * @return string |
||
180 | * @throws DoctrineStaticMetaException |
||
181 | */ |
||
182 | public function renamePathBasename(string $find, string $replace, string $path): string |
||
183 | { |
||
184 | $basename = basename($path); |
||
185 | $newBasename = str_replace($find, $replace, $basename); |
||
186 | $moveTo = dirname($path) . '/' . $newBasename; |
||
187 | if ($moveTo === $path) { |
||
188 | return $path; |
||
189 | } |
||
190 | if (is_dir($moveTo) || file_exists($moveTo)) { |
||
191 | throw new DoctrineStaticMetaException( |
||
192 | "Error trying to move:\n[$path]\n to \n[$moveTo]\ndestination already exists" |
||
193 | ); |
||
194 | } |
||
195 | try { |
||
196 | $this->filesystem->rename($path, $moveTo); |
||
197 | } catch (Exception $e) { |
||
198 | throw new DoctrineStaticMetaException( |
||
199 | 'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
||
200 | $e->getCode(), |
||
201 | $e |
||
202 | ); |
||
203 | } |
||
204 | |||
205 | return $moveTo; |
||
206 | } |
||
207 | |||
208 | public function getPathFromNameAndSubDirs(string $pathToProjectRoot, string $name, array $subDirectories): string |
||
209 | { |
||
210 | $path = realpath($pathToProjectRoot) . '/' . implode('/', $subDirectories) . '/' . $name . '.php'; |
||
211 | |||
212 | return $path; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Take a potentially non existent path and resolve the relativeness into a normal path |
||
217 | * |
||
218 | * @param string $relativePath |
||
219 | * |
||
220 | * @return string |
||
221 | * @throws RuntimeException |
||
222 | */ |
||
223 | public function resolvePath(string $relativePath): string |
||
224 | { |
||
225 | $path = []; |
||
226 | $absolute = (strpos($relativePath, '/') === 0); |
||
227 | foreach (explode('/', $relativePath) as $part) { |
||
228 | // ignore parts that have no value |
||
229 | if (empty($part) || $part === '.') { |
||
230 | continue; |
||
231 | } |
||
232 | |||
233 | if ($part !== '..') { |
||
234 | $path[] = $part; |
||
235 | continue; |
||
236 | } |
||
237 | if (count($path) > 0) { |
||
238 | // going back up? sure |
||
239 | array_pop($path); |
||
240 | continue; |
||
241 | } |
||
242 | throw new RuntimeException('Relative path resolves above root path.'); |
||
243 | } |
||
244 | |||
245 | $return = implode('/', $path); |
||
246 | if ($absolute) { |
||
247 | $return = "/$return"; |
||
248 | } |
||
249 | |||
250 | return $return; |
||
251 | } |
||
252 | } |
||
253 |