1
|
|
|
<?php |
2
|
|
|
namespace keeko\tools\model; |
3
|
|
|
|
4
|
|
|
use phootwork\file\File; |
5
|
|
|
use keeko\framework\schema\PackageSchema; |
6
|
|
|
use keeko\framework\schema\CodegenSchema; |
7
|
|
|
|
8
|
|
|
class Project { |
9
|
20 |
|
|
10
|
20 |
|
/** @var string */ |
11
|
20 |
|
private $root; |
12
|
|
|
|
13
|
15 |
|
/** @var PackageSchema */ |
14
|
15 |
|
private $package; |
15
|
|
|
|
16
|
|
|
/** @var CodegenSchema */ |
17
|
20 |
|
private $codegen; |
18
|
20 |
|
|
19
|
|
|
public function __construct($workdir) { |
20
|
|
|
$this->root = $workdir; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return string |
25
|
|
|
*/ |
26
|
|
|
public function getRootPath() { |
27
|
|
|
return $this->root; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function getComposerFileName() { |
34
|
|
|
return $this->root . '/composer.json'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* |
39
|
|
|
* @return boolean |
40
|
|
|
*/ |
41
|
|
|
public function hasComposerFile() { |
42
|
|
|
$file = new File($this->getComposerFileName()); |
43
|
|
|
return $file->exists(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getApiFileName() { |
50
|
|
|
return $this->root . '/api.json'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return boolean |
55
|
|
|
*/ |
56
|
|
|
public function hasApiFile() { |
57
|
|
|
$file = new File($this->getApiFileName()); |
58
|
|
|
return $file->exists(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function getSchemaFileName() { |
65
|
|
|
return $this->root . '/res/database/schema.xml'; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return boolean |
70
|
|
|
*/ |
71
|
|
|
public function hasSchemaFile() { |
72
|
|
|
$file = new File($this->getSchemaFileName()); |
73
|
|
|
return $file->exists(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function getCodegenFileName() { |
80
|
|
|
return $this->root . '/codegen.json'; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return boolean |
85
|
|
|
*/ |
86
|
|
|
public function hasCodegenFile() { |
87
|
|
|
$file = new File($this->getCodegenFileName()); |
88
|
|
|
return $file->exists(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns the package from 'composer.json' or creates a blank one, if composer.json |
93
|
|
|
* doesn't exist. |
94
|
|
|
* |
95
|
|
|
* @return PackageSchema |
96
|
|
|
*/ |
97
|
|
|
public function getPackage() { |
98
|
|
|
if ($this->package === null) { |
99
|
|
|
$this->package = $this->hasComposerFile() |
100
|
|
|
? PackageSchema::fromFile($this->getComposerFileName()) |
101
|
|
|
: new PackageSchema(); |
102
|
|
|
} |
103
|
|
|
return $this->package; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns a codegen schema from 'codegen.json' or creates a blank one, if 'codegen.json' |
108
|
|
|
* doesn't exist. |
109
|
|
|
* |
110
|
|
|
* @throws JsonEmptyException |
111
|
|
|
* @throws \RuntimeException |
112
|
|
|
* @return CodegenSchema |
113
|
|
|
*/ |
114
|
|
|
public function getCodegen() { |
115
|
|
|
if ($this->codegen === null) { |
116
|
|
|
$this->codegen = $this->hasCodegenFile() |
117
|
|
|
? CodegenSchema::fromFile($this->getCodegenFileName()) |
118
|
|
|
: new CodegenSchema(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $this->codegen; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|