Skip to content

Commit 136bd77

Browse files
author
syarig
committed
feat: add namespaces, parser for php in code parse
1 parent ba6620f commit 136bd77

11 files changed

Lines changed: 119 additions & 62 deletions

File tree

example-project/src-php-namespace/Http/Controllers/CommentController.php renamed to example-project/src-php/Http/Controllers/CommentController.php

File renamed without changes.

example-project/src-php-namespace/Http/Controllers/PostController.php renamed to example-project/src-php/Http/Controllers/PostController.php

File renamed without changes.

example-project/src-php-namespace/Http/Requests/PostRequest.php renamed to example-project/src-php/Http/Requests/PostRequest.php

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/index.dev.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ const namespaceTypeScriptExample = {
2424
clientPort: 2018
2525
};
2626

27+
const namespacePhpExample = {
28+
projectNameAlias: 'php-example-server',
29+
projectDir: `example-project/src-php`,
30+
entryPoint: `example-project/src-php/index.php`,
31+
clientPort: 2018
32+
};
33+
2734
const namespaceDebug = {
2835
projectNameAlias: 'debug',
2936
projectDir: `example-project/debug`,
@@ -41,7 +48,8 @@ const namespaceLanguageTest = {
4148
const args = process.argv.slice(2);
4249
const namespaces = {
4350
two: namespaceTwo,
44-
ts: namespaceTypeScriptExample
51+
ts: namespaceTypeScriptExample,
52+
php: namespacePhpExample
4553
};
4654
const namespace = namespaces[args[0]] !== undefined ? namespaces[args[0]] : namespaceOne;
4755
server.setup(namespace, { isDev: true });

src/public/js/core/dataBus/reducer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const getMergeState = (state, namespace) => namespaceStateUpdate => ({
3434
}
3535
});
3636

37-
const FULL_FEATURES_LANG_LIST = ['javascript', 'typescript'];
37+
const FULL_FEATURES_LANG_LIST = ['javascript', 'typescript', 'php'];
3838

3939
export default (state = DefaultState, action) => {
4040
const namespace = action.namespace;
@@ -150,7 +150,7 @@ export default (state = DefaultState, action) => {
150150
const { fileNode } = action.payload;
151151
const dependenciesEntryName = fileNode ? fileNode.path : namespaceState.dependenciesEntryName;
152152

153-
// do some reducerr
153+
// do some reducer
154154
return mergeState({
155155
dependenciesEntryName,
156156
selectedDependencyEdgeNodes: null
Lines changed: 50 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,67 @@
1-
const defaultDependencies = require('../default/dependencies');
2-
const readdirRecursive = require('fs-readdir-recursive')
3-
const file = require('../../../utils/file')
4-
5-
const engine = require('php-parser');
61
const path = require('path');
2+
const namespaces = require('./namespaces')
3+
const parser = require('./parser')
74

8-
// initialize a new parser instance
9-
var parser = new engine({
10-
// some options :
11-
parser: {
12-
extractDoc: true,
13-
php7: true
14-
},
15-
ast: {
16-
withPositions: true
17-
}
18-
});
19-
20-
21-
const getDependencies = (entryPoint, projectDir) => {
22-
// const rootPath = path.resolve()
23-
const separator = path.sep;
5+
const getDependencies = async (entryPoint, projectDir) => {
6+
await namespaces.setNamespaces(projectDir)
7+
const phpNamespaces = namespaces.getNamespaces()
248

25-
const phpFileNamespaces = {};
26-
27-
const tasks = readdirRecursive(projectDir).map(async f => {
28-
const filePath = `${projectDir}${separator}${f.replace(/\//g, separator)}`
29-
30-
phpFile = await file.read(filePath, 'utf8')
31-
32-
const parsed = parser.parseCode(phpFile);
9+
const dependencies = {
10+
[entryPoint]: phpNamespaces[entryPoint]
11+
}
3312

34-
parsed.children?.forEach(parsedChild => {
35-
const namespace = {
36-
moduleName: parsedChild.name,
37-
importedModuleNames: []
13+
phpNamespaces[entryPoint]?.importedModuleNames.forEach((moduleName) => {
14+
Object.keys(phpNamespaces).forEach(itemPath => {
15+
if (phpNamespaces[itemPath].moduleName !== moduleName) {
16+
return
3817
}
39-
parsedChild.children?.forEach(c => {
40-
switch (c.kind) {
41-
case 'usegroup':
42-
c.items.forEach(item => {
43-
namespace.importedModuleNames.push(item.name)
44-
})
45-
break
46-
case 'class':
47-
namespace.moduleName = `${parsedChild.name}\\${c.name.name}`
48-
}
49-
})
5018

51-
phpFileNamespaces[filePath] = namespace
19+
dependencies[itemPath] = phpNamespaces[itemPath]
5220
})
5321
})
54-
return Promise.all(tasks).then(() => {
55-
const dependencies = {}
56-
dependencies[entryPoint] = phpFileNamespaces[entryPoint]
22+
return dependencies
23+
};
5724

58-
phpFileNamespaces[entryPoint]?.importedModuleNames.forEach((moduleName) => {
59-
Object.keys(phpFileNamespaces).forEach(filePath => {
60-
if (phpFileNamespaces[filePath].moduleName !== moduleName) {
61-
return
62-
}
25+
const getImports = (fileCode, itemPath) => {
26+
const phpNamespaces = namespaces.getNamespaces();
27+
const parsed = parser.parseCode(fileCode, path.basename(itemPath));
28+
const dependencies = [];
6329

64-
dependencies[filePath] = phpFileNamespaces[filePath]
65-
})
66-
})
30+
const findSourceFile = (phpNamespace) => {
31+
return Object.keys(phpNamespaces).find(key => {
32+
return phpNamespaces[key].moduleName === phpNamespace
33+
}
34+
) ?? null
35+
}
6736

68-
return dependencies
37+
parsed.children?.forEach(parsedChild => {
38+
const dependence = {
39+
importNodeLines: [],
40+
sourceFile: null,
41+
specifiers: [],
42+
}
43+
parsedChild.children?.forEach(c => {
44+
switch (c.kind) {
45+
case 'usegroup':
46+
c.items.forEach(item => {
47+
dependence.importNodeLines = [
48+
item.loc.start.line,
49+
item.loc.end.line,
50+
]
51+
dependence.sourceFile = findSourceFile(item.name)
52+
})
53+
break
54+
case 'class':
55+
// dependence.sourceFile = `${parsedChild.name}\\${c.name.name}`
56+
}
57+
})
58+
dependencies.push(dependence)
6959
})
70-
};
60+
return dependencies
61+
}
7162

7263
// replace with own implementation if needed
7364
module.exports = {
74-
getImports: defaultDependencies.getImports,
65+
getImports: getImports,
7566
getDependencies: getDependencies
7667
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const readdirRecursive = require('fs-readdir-recursive')
2+
const file = require('../../../utils/file')
3+
const extensions = require('./extensions');
4+
const path = require('path');
5+
const parser = require('./parser')
6+
7+
const namespaces = {};
8+
9+
const setNamespaces = async (projectDir) => {
10+
const separator = path.sep;
11+
const tasks = readdirRecursive(projectDir)
12+
.filter(f => extensions.test('.' + f.split('.').pop()))
13+
.map(async f => {
14+
const itemPath = `${projectDir}${separator}${f.replace(/\//g, separator)}`
15+
const fileCode = await file.read(itemPath, 'utf8')
16+
const parsed = parser.parseCode(fileCode, path.basename(f));
17+
18+
parsed.children?.forEach(parsedChild => {
19+
const namespace = {
20+
moduleName: parsedChild.name,
21+
importedModuleNames: []
22+
}
23+
parsedChild.children?.forEach(c => {
24+
switch (c.kind) {
25+
case 'usegroup':
26+
c.items.forEach(item => {
27+
namespace.importedModuleNames.push(item.name)
28+
})
29+
break
30+
case 'class':
31+
namespace.moduleName = `${parsedChild.name}\\${c.name.name}`
32+
}
33+
})
34+
35+
namespaces[itemPath] = namespace
36+
})
37+
});
38+
39+
await Promise.all(tasks)
40+
}
41+
const getNamespaces = () => namespaces
42+
43+
module.exports = {
44+
setNamespaces,
45+
getNamespaces
46+
}

0 commit comments

Comments
 (0)