1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swaggest\PhpCodeBuilder; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Swaggest\PhpCodeBuilder\JsonSchema\Palette; |
7
|
|
|
|
8
|
|
|
class PhpNamespaces extends PhpTemplate |
9
|
|
|
{ |
10
|
|
|
private $fileNamespace = ''; |
11
|
|
|
private $namespaces = array(); |
12
|
|
|
private $shortNames = array(); |
13
|
|
|
|
14
|
3 |
|
public function add($fullyQualifiedName, $as = null) |
15
|
|
|
{ |
16
|
3 |
|
if ($fullyQualifiedName[0] !== '\\') { |
17
|
|
|
$fullyQualifiedName = '\\' . $fullyQualifiedName; |
18
|
|
|
} |
19
|
3 |
|
if (null === $as) { |
20
|
3 |
|
$i = ''; |
21
|
3 |
|
$short = $this->makeShortName($fullyQualifiedName); |
22
|
3 |
|
while (isset($this->shortNames[$short . $i])) { |
23
|
|
|
$i++; |
24
|
|
|
} |
25
|
3 |
|
$as = $short . $i; |
26
|
|
|
} |
27
|
3 |
|
$this->namespaces[$fullyQualifiedName] = $as; |
28
|
3 |
|
$this->shortNames[$as] = $fullyQualifiedName; |
29
|
3 |
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
3 |
|
private function makeShortName($fqn) |
33
|
|
|
{ |
34
|
3 |
|
$path = explode('\\', $fqn); |
35
|
3 |
|
return array_pop($path); |
36
|
|
|
} |
37
|
|
|
|
38
|
3 |
|
public function getReference($fullyQualifiedName) |
39
|
|
|
{ |
40
|
3 |
|
if ($fullyQualifiedName[0] !== '\\') { |
41
|
3 |
|
$fullyQualifiedName = '\\' . $fullyQualifiedName; |
42
|
|
|
} |
43
|
3 |
|
if (!isset($this->namespaces[$fullyQualifiedName])) { |
44
|
3 |
|
if ($fullyQualifiedName === Palette::schemaClass()->getFullyQualifiedName()) { |
45
|
|
|
$this->add($fullyQualifiedName, 'JsonBasicSchema'); |
46
|
|
|
} else { |
47
|
3 |
|
$this->add($fullyQualifiedName); |
48
|
|
|
} |
49
|
|
|
} |
50
|
3 |
|
return $this->namespaces[$fullyQualifiedName]; |
51
|
|
|
} |
52
|
|
|
|
53
|
3 |
|
protected function toString() |
54
|
|
|
{ |
55
|
3 |
|
$result = ''; |
56
|
3 |
|
ksort($this->namespaces); |
57
|
3 |
|
foreach ($this->namespaces as $namespace => $as) { |
58
|
3 |
|
$namespace = trim($namespace, '\\'); |
59
|
3 |
|
$short = $this->makeShortName($namespace); |
60
|
3 |
|
if ($short === $as) { |
61
|
3 |
|
$as = ''; |
62
|
|
|
} |
63
|
3 |
|
$namespacePieces = explode('\\', $namespace); |
64
|
3 |
|
array_pop($namespacePieces); |
65
|
3 |
|
$packageNamespace = implode('\\', $namespacePieces); |
66
|
3 |
|
if ($this->fileNamespace === '\\' . $packageNamespace) { |
67
|
|
|
continue; |
68
|
|
|
} |
69
|
3 |
|
$renderAs = $as ? ' as ' . $as : ''; |
70
|
3 |
|
if (!$renderAs && $namespace === $this->fileNamespace . '\\' . $short) { |
71
|
2 |
|
continue; |
72
|
|
|
} |
73
|
|
|
$result .= <<<PHP |
74
|
3 |
|
use {$namespace}{$renderAs}; |
75
|
|
|
|
76
|
|
|
PHP; |
77
|
|
|
} |
78
|
3 |
|
return $result; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $fileNamespace |
83
|
|
|
* @return PhpNamespaces |
84
|
|
|
*/ |
85
|
3 |
|
public function setFileNamespace($fileNamespace) |
86
|
|
|
{ |
87
|
3 |
|
$this->fileNamespace = $fileNamespace; |
88
|
3 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
} |