|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace SlayerBirden\DFCodeGeneration\Writer; |
|
5
|
|
|
|
|
6
|
|
|
use SlayerBirden\DFCodeGeneration\Util\CodeLoader; |
|
7
|
|
|
use SlayerBirden\DFCodeGeneration\Util\Lexer; |
|
8
|
|
|
use Zend\Code\Reflection\FileReflection; |
|
9
|
|
|
use Zend\Code\Scanner\CachingFileScanner; |
|
10
|
|
|
use Zend\Code\Scanner\ClassScanner; |
|
11
|
|
|
use Zend\Filter\Word\CamelCaseToSeparator; |
|
12
|
|
|
|
|
13
|
|
|
class Psr4FileNameProvider implements FileNameProviderInterface |
|
14
|
|
|
{ |
|
15
|
|
|
private $suffixes = [ |
|
16
|
|
|
'cest' |
|
17
|
|
|
]; |
|
18
|
|
|
|
|
19
|
|
|
private $prefixes = [ |
|
20
|
|
|
'get', |
|
21
|
|
|
'add', |
|
22
|
|
|
'create', |
|
23
|
|
|
'delete', |
|
24
|
|
|
'remove', |
|
25
|
|
|
'update' |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
9 |
|
public function getFileName(string $contents) |
|
29
|
|
|
{ |
|
30
|
9 |
|
$tmpFile = sys_get_temp_dir() . '/' . uniqid('generation'); |
|
31
|
9 |
|
file_put_contents($tmpFile, $contents); |
|
32
|
9 |
|
$scanner = new CachingFileScanner($tmpFile); |
|
33
|
|
|
|
|
34
|
9 |
|
$classes = $scanner->getClasses(); |
|
35
|
|
|
/** @var ClassScanner $class */ |
|
36
|
9 |
|
$class = reset($classes); |
|
37
|
|
|
|
|
38
|
9 |
|
return $this->getFileNameFromClassName($class->getName()); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
9 |
|
private function getFileNameFromClassName(string $className): string |
|
42
|
|
|
{ |
|
43
|
|
|
// assumption is that first 2 parts of the namespace are PSR4 "src" dir |
|
44
|
9 |
|
$parts = explode('\\', $className); |
|
45
|
9 |
|
if (count($parts) === 1) { |
|
46
|
6 |
|
$name = reset($parts); |
|
47
|
6 |
|
$shortName = strtolower(Lexer::getSingularForm($this->purgeName($name))); |
|
48
|
|
|
$paths = [ |
|
49
|
6 |
|
'tests', |
|
50
|
6 |
|
'api', |
|
51
|
6 |
|
$shortName, |
|
52
|
6 |
|
$name . '.php', |
|
53
|
|
|
]; |
|
54
|
|
|
} else { |
|
55
|
5 |
|
$paths = array_slice($parts, 2, -1); |
|
56
|
5 |
|
$file = end($parts); |
|
57
|
|
|
|
|
58
|
5 |
|
array_unshift($paths, 'src'); |
|
59
|
5 |
|
array_push($paths, $file . '.php'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
9 |
|
return implode('/', $paths); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Remove prefixes and suffixes from the name |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $name |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
6 |
|
public function purgeName(string $name): string |
|
72
|
|
|
{ |
|
73
|
6 |
|
$nameParts = explode(':', (new CamelCaseToSeparator(':'))->filter($name)); |
|
74
|
|
|
|
|
75
|
6 |
|
switch (count($nameParts)) { |
|
76
|
6 |
|
case 3: |
|
77
|
4 |
|
if (in_array(strtolower($nameParts[0]), $this->prefixes, true)) { |
|
78
|
3 |
|
unset($nameParts[0]); |
|
79
|
|
|
} |
|
80
|
4 |
|
if (in_array(strtolower($nameParts[2]), $this->suffixes, true)) { |
|
81
|
4 |
|
unset($nameParts[2]); |
|
82
|
|
|
} |
|
83
|
4 |
|
break; |
|
84
|
3 |
|
case 2: |
|
85
|
3 |
|
if (in_array(strtolower($nameParts[0]), $this->prefixes, true)) { |
|
86
|
|
|
unset($nameParts[0]); |
|
87
|
|
|
} |
|
88
|
3 |
|
if (in_array(strtolower($nameParts[1]), $this->suffixes, true)) { |
|
89
|
1 |
|
unset($nameParts[1]); |
|
90
|
|
|
} |
|
91
|
3 |
|
break; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
6 |
|
if (empty($nameParts)) { |
|
95
|
|
|
return uniqid('generated'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
6 |
|
return implode($nameParts); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|