|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Swaggest\PhpCodeBuilder\App; |
|
4
|
|
|
|
|
5
|
|
|
use Swaggest\CodeBuilder\App\App; |
|
6
|
|
|
use Swaggest\PhpCodeBuilder\JsonSchema\PhpBuilder; |
|
7
|
|
|
use Swaggest\PhpCodeBuilder\PhpClassTraitInterface; |
|
8
|
|
|
use Swaggest\PhpCodeBuilder\PhpFile; |
|
9
|
|
|
|
|
10
|
|
|
class PhpApp extends App |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var PhpClassTraitInterface[] */ |
|
13
|
|
|
private $classes = []; |
|
|
|
|
|
|
14
|
|
|
/** @var PhpFile[][] */ |
|
15
|
|
|
private $phpFiles = []; |
|
16
|
|
|
/** @var PhpBuilder */ |
|
17
|
|
|
private $builder; |
|
18
|
|
|
|
|
19
|
|
|
private $psr4Namespaces = []; |
|
20
|
|
|
|
|
21
|
4 |
|
public function setNamespaceRoot($namespace, $relativePath = './src/') |
|
22
|
|
|
{ |
|
23
|
4 |
|
$relativePath = rtrim($relativePath, '/') . '/'; |
|
24
|
|
|
|
|
25
|
4 |
|
$this->psr4Namespaces[$namespace] = $relativePath; |
|
26
|
4 |
|
return $this; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param PhpBuilder $builder |
|
31
|
|
|
* @return PhpApp |
|
32
|
|
|
*/ |
|
33
|
|
|
public function setBuilder($builder) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->builder = $builder; |
|
36
|
|
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
4 |
|
public function addClass(PhpClassTraitInterface $class) |
|
40
|
|
|
{ |
|
41
|
|
|
//$this->classes[] = $class; |
|
42
|
|
|
|
|
43
|
4 |
|
if (!$class->getName()) { |
|
44
|
|
|
throw new \Exception('Can not store unnamed class'); |
|
45
|
|
|
} |
|
46
|
4 |
|
$ns = $class->getNamespace(); |
|
47
|
4 |
|
$filepath = $class->getName() . '.php'; |
|
48
|
4 |
|
if ($ns) { |
|
49
|
4 |
|
$namespaceFound = false; |
|
50
|
4 |
|
foreach ($this->psr4Namespaces as $namespace => $relativePath) { |
|
51
|
4 |
|
$nns = substr($ns, 0, strlen($namespace)); |
|
52
|
4 |
|
if ($namespace === $nns) { |
|
53
|
4 |
|
$innerPath = str_replace('\\', '/', substr($ns, strlen($namespace))); |
|
54
|
4 |
|
$innerPath = trim($innerPath, '/') . '/'; |
|
55
|
4 |
|
$filepath = $relativePath . $innerPath . $class->getName() . '.php'; |
|
56
|
4 |
|
$namespaceFound = true; |
|
57
|
4 |
|
break; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
4 |
|
if (!$namespaceFound) { |
|
61
|
|
|
throw new \Exception('Namespace not found: ' . $ns); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
4 |
|
$file = new PhpFile(); |
|
65
|
4 |
|
if ($ns) { |
|
66
|
4 |
|
$file->setNamespace($ns); |
|
67
|
|
|
$file->getNamespaces()->add($class->getFullyQualifiedName()); |
|
68
|
4 |
|
} |
|
69
|
4 |
|
$file->getCode()->addSnippet($class); |
|
70
|
|
|
$this->files[$filepath] = $file; |
|
71
|
4 |
|
|
|
72
|
|
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function addFile($contents, $relativePath) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->files[$relativePath] = $contents; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function addPhpFile(PhpFile $file, $relativePath = './') |
|
81
|
|
|
{ |
|
82
|
|
|
$this->phpFiles[$relativePath][] = $file; |
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
} |