Skip to content

Commit 05d7f3e

Browse files
wb-hx510875peze
wb-hx510875
authored andcommitted
patch php generator
1. Supported emit test files. 2. Optimizing the `ResolveImports` method. 3. Optimizing test case. 4. Optimizing emit @throw comment.
1 parent 26b0675 commit 05d7f3e

File tree

21 files changed

+203
-105
lines changed

21 files changed

+203
-105
lines changed

src/generator.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ class Generator {
116116
let thirdPackageClient = {};
117117
let thirdPackageClientAlias = {};
118118

119+
const selfClientName = this.config.clientName ? this.config.clientName : this.config.client.name;
120+
119121
if (imports.length > 0) {
120122
const lockPath = path.join(this.config.pkgDir, '.libraries.json');
121123
const lock = JSON.parse(fs.readFileSync(lockPath, 'utf8'));
@@ -163,7 +165,7 @@ class Generator {
163165
// resolve third package model client name
164166
if (
165167
clientNameSet.indexOf(clientName.toLowerCase()) > -1 ||
166-
clientName.toLowerCase() === this.config.clientName.toLowerCase()
168+
clientName.toLowerCase() === selfClientName.toLowerCase()
167169
) {
168170
const alias = packageName.split('.').join('') + '->' + clientName.split('.').join('');
169171
thirdPackageClientAlias[aliasId] = alias;

src/langs/common/items.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ class ConstructItem extends Item {
378378
this.params = params;
379379
this.body = body;
380380
this.annotations = annotations;
381+
this.throws = [];
381382
}
382383

383384
addParamNode(node) {
@@ -399,7 +400,7 @@ class ConstructItem extends Item {
399400
class ObjectItem extends Item {
400401
constructor(type) {
401402
super();
402-
assert.equal(true, type === 'client' || type === 'model');
403+
assert.equal(true, type === 'client' || type === 'model' || type === 'test');
403404
this.type = type; // client | model
404405
this.modify = []; // Modify
405406
this.name = ''; // object name

src/langs/php/combinator.js

+42-12
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ const PackageInfo = require('./package_info');
88

99
const {
1010
Symbol,
11-
Exceptions
11+
Modify
1212
} = require('../common/enum');
1313

1414
const {
1515
AnnotationItem,
1616
ConstructItem,
17+
ObjectItem,
1718
FuncItem,
1819
PropItem,
1920

@@ -163,7 +164,6 @@ class Combinator extends CombinatorBase {
163164

164165
combine(objectArr = []) {
165166
if (this.config.packageInfo) {
166-
this.config.dir = this.config.outputDir + '/src/';
167167
const packageInfo = new PackageInfo(this.config);
168168
packageInfo.emit(this.config.packageInfo, this.requirePackage);
169169
}
@@ -183,9 +183,30 @@ class Combinator extends CombinatorBase {
183183
}
184184
});
185185
}
186+
187+
if (this.config.withTest) {
188+
const object = new ObjectItem('test');
189+
object.name = `Tests.${clientObjectItem.name}Test`;
190+
object.extends = ['TestCase'];
191+
object.includeList = [{ import: 'PHPUnit\\Framework\\TestCase', alias: null }];
192+
clientObjectItem.body.forEach(node => {
193+
if (node instanceof FuncItem) {
194+
const func = new FuncItem();
195+
func.name = `test${_upperFirst(node.name)}`;
196+
func.modify.push(Modify.public());
197+
object.addBodyNode(func);
198+
}
199+
});
200+
const outputParts = this.combineOject(object, false);
201+
const config = _deepClone(this.config);
202+
config.filename = `${clientObjectItem.name}Test`;
203+
config.dir = `${config.dir}/tests/`;
204+
config.layer = '';
205+
this.combineOutputParts(config, outputParts);
206+
}
186207
}
187208

188-
combineOject(object) {
209+
combineOject(object, output = true) {
189210
let layer = '';
190211
if (object.type === 'model') {
191212
layer = this.config.model.dir;
@@ -202,7 +223,7 @@ class Combinator extends CombinatorBase {
202223
let tmp = object.name.split('.');
203224
object.name = tmp[tmp.length - 1];
204225
tmp.splice(tmp.length - 1, 1);
205-
layer = layer + '.' + tmp.join('.');
226+
layer = layer ? layer + '.' + tmp.join('.') : tmp.join('.');
206227
}
207228
this.emitClass(emitter, object);
208229
outputParts.body = emitter.output;
@@ -225,12 +246,18 @@ class Combinator extends CombinatorBase {
225246
outputParts.head = emitter.output;
226247

227248
/***************************** combine output ******************************/
228-
const config = _deepClone(this.config);
229-
config.filename = object.name;
230-
config.layer = layer.split('.').map(m => {
231-
return _avoidKeywords(m);
232-
}).join('.');
233-
this.combineOutputParts(config, outputParts);
249+
if (output) {
250+
const config = _deepClone(this.config);
251+
config.filename = object.name;
252+
config.layer = layer.split('.').map(m => {
253+
return _avoidKeywords(m);
254+
}).join('.');
255+
if (config.packageInfo) {
256+
config.dir = config.outputDir + '/src/';
257+
}
258+
this.combineOutputParts(config, outputParts);
259+
}
260+
return outputParts;
234261
}
235262

236263
emitClass(emitter, object) {
@@ -596,8 +623,11 @@ class Combinator extends CombinatorBase {
596623
const desc = returnDesc ? ' ' + returnDesc : '';
597624
emitter.emitln(` * @return ${t}${desc}`, this.level);
598625
}
599-
600-
emitter.emitln(' * @throws ' + _exception(_exception(Exceptions.base())), this.level);
626+
if (func.throws.length) {
627+
func.throws.forEach(exception => {
628+
emitter.emitln(' * @throws ' + _exception(_exception(exception)), this.level);
629+
});
630+
}
601631
}
602632
emitter.emitln(' */', this.level);
603633
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';

src/langs/php/package_info.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const path = require('path');
44
const fs = require('fs');
55
const BasePackageInfo = require('../common/package_info');
66

7+
const { _deepClone } = require('../../lib/helper');
8+
79
const OPTION_LOCAL = 1; // use local tmpl file to render content
810
const OPTION_SOURCE = 2; // config by Darafile.{lang}.packageInfo
911
const OPTION_RENDER = 4; // render content from tmpl
@@ -22,22 +24,32 @@ const files = {
2224

2325
class PackageInfo extends BasePackageInfo {
2426
emit(packageInfo, requirePackage) {
25-
let outputDir = this.resolveOutputDir(packageInfo);
27+
const config = _deepClone(this.config);
28+
let outputDir = this.resolveOutputDir(packageInfo, '');
2629
this.checkParams(packageInfo, ['name', 'desc', 'github']);
2730
const params = {
2831
name: packageInfo.name,
2932
desc: packageInfo.desc,
3033
github: packageInfo.github,
31-
namespace: this.config.package.split('.').join('\\\\')
34+
namespace: config.package.split('.').join('\\\\')
3235
};
36+
if (config.withTest) {
37+
if (!fs.existsSync(path.join(config.dir, 'tests'))) {
38+
fs.mkdirSync(path.join(config.dir, 'tests'), { recursive: true });
39+
}
40+
fs.writeFileSync(
41+
path.join(config.dir, 'tests', 'bootstrap.php'),
42+
fs.readFileSync(path.join(__dirname, './files/bootstrap.php.tmpl'), 'utf-8')
43+
);
44+
}
3345
Object.keys(files).forEach(filename => {
3446
let content = '';
3547
let optional = files[filename];
3648
if (optional & OPTION_UPDATE && fs.existsSync(path.join(outputDir, filename))) {
3749
content = fs.readFileSync(path.join(outputDir, filename), 'utf-8');
3850
} else if (optional & OPTION_SOURCE && packageInfo.files && packageInfo.files[filename]) {
3951
let filepath = path.isAbsolute(packageInfo.files[filename]) ?
40-
packageInfo.files[filename] : path.join(this.config.pkgDir, packageInfo.files[filename]);
52+
packageInfo.files[filename] : path.join(config.pkgDir, packageInfo.files[filename]);
4153
if (!fs.existsSync(filepath)) {
4254
return;
4355
}
@@ -58,9 +70,9 @@ class PackageInfo extends BasePackageInfo {
5870
json.require[p] = v;
5971
});
6072
}
61-
if (this.config.maintainers) {
73+
if (config.maintainers) {
6274
json.authors = [];
63-
this.config.maintainers.forEach(maintainer => {
75+
config.maintainers.forEach(maintainer => {
6476
let name = maintainer.name ? maintainer.name : '';
6577
let email = maintainer.email ? maintainer.email : '';
6678
json.authors.push({ name: name, email: email });

src/resolver/base.js

-53
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,10 @@
22

33
const debug = require('../lib/debug');
44

5-
const {
6-
_isBasicType
7-
} = require('../lib/helper');
8-
95
const {
106
AnnotationItem,
11-
PropItem,
127
} = require('../langs/common/items');
138

14-
const {
15-
// Symbol,
16-
Modify,
17-
// Exceptions,
18-
// Types
19-
} = require('../langs/common/enum');
20-
219
const DSL = require('@darabonba/parser');
2210

2311
class BaseResolver {
@@ -130,47 +118,6 @@ class BaseResolver {
130118
this.object.annotations.push(this.resolveAnnotation(annotation, this.object.index));
131119
}
132120
}
133-
134-
resolveProps(ast) {
135-
this.comments = ast.comments;
136-
137-
ast.moduleBody.nodes.filter((item) => {
138-
return item.type === 'type';
139-
}).forEach(item => {
140-
const prop = new PropItem();
141-
prop.name = item.vid.lexeme.replace('@', '_');
142-
const type = item.value.lexeme ? item.value.lexeme : item.value.type;
143-
prop.type = type;
144-
if (type === 'array') {
145-
prop.itemType = item.value.subType;
146-
if (!_isBasicType(item.value.subType.lexeme)) {
147-
this.combinator.addModelInclude(item.value.subType.lexeme);
148-
}
149-
} else {
150-
if (!_isBasicType(type)) {
151-
if (item.value.idType && item.value.idType === 'module') {
152-
this.combinator.addInclude(type);
153-
} else if (item.value && item.value.returnType) {
154-
if (!_isBasicType(item.value.returnType.lexeme)) {
155-
this.combinator.addModelInclude(type);
156-
}
157-
} else {
158-
debug.stack(item);
159-
}
160-
}
161-
}
162-
prop.addModify(Modify.protected());
163-
if (item.tokenRange) {
164-
let comments = this.getFrontComments(item.tokenRange[0]);
165-
if (comments.length > 0) {
166-
comments.forEach(c => {
167-
this.object.addBodyNode(this.resolveAnnotation(c, this.object.index));
168-
});
169-
}
170-
}
171-
this.object.addBodyNode(prop);
172-
});
173-
}
174121
}
175122

176123
module.exports = BaseResolver;

src/resolver/client.js

+55-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ const debug = require('../lib/debug');
44
const BaseResolver = require('./base');
55

66
const {
7-
ObjectItem,
87
AnnotationItem,
98
ConstructItem,
9+
ObjectItem,
1010
FuncItem,
11+
PropItem,
1112

1213
GrammerVar,
1314
GrammerCall,
@@ -51,6 +52,7 @@ class ClientResolver extends BaseResolver {
5152
constructor(astNode, combinator, globalAst) {
5253
super(astNode, combinator, globalAst);
5354
this.object = new ObjectItem('client');
55+
this.currThrows = {};
5456
}
5557

5658
resolve() {
@@ -117,11 +119,53 @@ class ClientResolver extends BaseResolver {
117119
return object;
118120
}
119121

122+
resolveProps(ast) {
123+
this.comments = ast.comments;
124+
125+
ast.moduleBody.nodes.filter((item) => {
126+
return item.type === 'type';
127+
}).forEach(item => {
128+
const prop = new PropItem();
129+
prop.name = item.vid.lexeme.replace('@', '_');
130+
const type = item.value.lexeme ? item.value.lexeme : item.value.type;
131+
prop.type = type;
132+
if (type === 'array') {
133+
prop.itemType = item.value.subType;
134+
if (!_isBasicType(item.value.subType.lexeme)) {
135+
this.combinator.addModelInclude(item.value.subType.lexeme);
136+
}
137+
} else {
138+
if (!_isBasicType(type)) {
139+
if (item.value.idType && item.value.idType === 'module') {
140+
this.combinator.addInclude(type);
141+
} else if (item.value && item.value.returnType) {
142+
if (!_isBasicType(item.value.returnType.lexeme)) {
143+
this.combinator.addModelInclude(type);
144+
}
145+
} else {
146+
debug.stack(item);
147+
}
148+
}
149+
}
150+
prop.addModify(Modify.protected());
151+
if (item.tokenRange) {
152+
let comments = this.getFrontComments(item.tokenRange[0]);
153+
if (comments.length > 0) {
154+
comments.forEach(c => {
155+
this.object.addBodyNode(this.resolveAnnotation(c, this.object.index));
156+
});
157+
}
158+
}
159+
this.object.addBodyNode(prop);
160+
});
161+
}
162+
120163
resolveInitBody(init) {
121164
const object = this.object;
122165
const combinator = this.combinator;
123166

124167
let constructNode = new ConstructItem();
168+
this.currThrows = {};
125169
if (init.params && init.params.params) {
126170
init.params.params.forEach(param => {
127171
if (param.paramType.idType && param.paramType.idType === 'model') {
@@ -140,10 +184,14 @@ class ClientResolver extends BaseResolver {
140184
this.visitStmt(constructNode, stmt, constructNode.index);
141185
});
142186
}
187+
if (Object.keys(this.currThrows).length > 0) {
188+
constructNode.throws = Object.values(this.currThrows);
189+
}
143190
object.addBodyNode(constructNode);
144191
}
145192

146193
resolveFunc(func, ast, body) {
194+
this.currThrows = {};
147195
if (ast.annotation) {
148196
func.addAnnotation(this.resolveAnnotation(ast.annotation, func.index));
149197
}
@@ -248,6 +296,9 @@ class ClientResolver extends BaseResolver {
248296
this.findComments(func, body, 'between');
249297
}
250298

299+
if (Object.keys(this.currThrows).length > 0) {
300+
func.throws = Object.values(this.currThrows);
301+
}
251302
this.object.addBodyNode(func);
252303
}
253304

@@ -384,6 +435,7 @@ class ClientResolver extends BaseResolver {
384435
]),
385436
new GrammerThrows(null, [exceptionParam])
386437
], catchException);
438+
this.currThrows[Exceptions.base()] = Exceptions.base();
387439
this.requestBody(ast, body, requestTryCatch);
388440
requestTryCatch.addCatch(tryCatch);
389441

@@ -399,6 +451,7 @@ class ClientResolver extends BaseResolver {
399451
]
400452
)
401453
);
454+
this.currThrows['$ExceptionUnretryable'] = this.combinator.addInclude('$ExceptionUnretryable');
402455
}
403456

404457
requestBody(ast, body, func) {
@@ -789,6 +842,7 @@ class ClientResolver extends BaseResolver {
789842
node.expr = new BehaviorToModel(node.expr, stmt.expectedType.name);
790843
}
791844
} else if (stmt.type === 'throw') {
845+
this.currThrows['$Exception'] = this.combinator.addInclude('$Exception');
792846
node = new GrammerThrows(this.combinator.addInclude('$Exception'));
793847
if (Array.isArray(stmt.expr)) {
794848
stmt.expr.forEach(e => {

0 commit comments

Comments
 (0)