Skip to content

Commit 6c2d07d

Browse files
committed
feat: add router index
1 parent 8fb8f05 commit 6c2d07d

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

src/main/kotlin/com/github/tempest/framework/TempestFrameworkClasses.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,26 @@ package com.github.tempest.framework
22

33
object TempestFrameworkClasses {
44
const val ConsoleCommand = "\\Tempest\\Console\\ConsoleCommand"
5+
6+
const val ROUTER_GET = "\\Tempest\\Router\\Get"
7+
const val ROUTER_POST = "\\Tempest\\Router\\Post"
8+
const val ROUTER_PUT = "\\Tempest\\Router\\Put"
9+
const val ROUTER_PATCH = "\\Tempest\\Router\\Patch"
10+
const val ROUTER_DELETE = "\\Tempest\\Router\\Delete"
11+
const val ROUTER_OPTIONS = "\\Tempest\\Router\\Options"
12+
const val ROUTER_HEAD = "\\Tempest\\Router\\Head"
13+
const val ROUTER_CONNECT = "\\Tempest\\Router\\Connect"
14+
const val ROUTER_TRACE = "\\Tempest\\Router\\Trace"
15+
16+
val ROUTES = listOf(
17+
ROUTER_GET,
18+
ROUTER_POST,
19+
ROUTER_PUT,
20+
ROUTER_PATCH,
21+
ROUTER_DELETE,
22+
ROUTER_OPTIONS,
23+
ROUTER_HEAD,
24+
ROUTER_CONNECT,
25+
ROUTER_TRACE,
26+
)
527
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.github.tempest.framework.router.index
2+
3+
import com.github.tempest.framework.TempestFrameworkClasses
4+
import com.github.tempest.framework.TempestFrameworkUtil
5+
import com.github.tempest.framework.common.index.AbstractIndex
6+
import com.intellij.openapi.util.text.StringUtil
7+
import com.intellij.psi.util.PsiTreeUtil
8+
import com.intellij.util.indexing.DataIndexer
9+
import com.intellij.util.indexing.FileBasedIndex
10+
import com.intellij.util.indexing.FileContent
11+
import com.intellij.util.indexing.ID
12+
import com.intellij.util.io.EnumeratorStringDescriptor
13+
import com.jetbrains.php.lang.PhpFileType
14+
import com.jetbrains.php.lang.psi.elements.Method
15+
import com.jetbrains.php.lang.psi.elements.PhpAttribute
16+
import com.jetbrains.php.lang.psi.elements.PhpNamedElement
17+
18+
private typealias RouterActionsIndexType = String
19+
20+
/**
21+
* Stores uri -> controller/action association
22+
*/
23+
class RouterActionsIndex : AbstractIndex<RouterActionsIndexType>() {
24+
companion object {
25+
val key = ID.create<String, RouterActionsIndexType>("Tempest.Routes.Actions")
26+
}
27+
28+
override fun getVersion() = 1
29+
30+
override fun getName() = key
31+
32+
override fun getValueExternalizer() = EnumeratorStringDescriptor.INSTANCE
33+
34+
override fun getInputFilter() = FileBasedIndex.InputFilter {
35+
it.fileType == PhpFileType.INSTANCE &&
36+
!it.name.endsWith(TempestFrameworkUtil.TEMPLATE_SUFFIX)
37+
}
38+
39+
override fun getIndexer() = DataIndexer<String, RouterActionsIndexType, FileContent> { inputData ->
40+
inputData
41+
.psiFile
42+
.let { PsiTreeUtil.findChildrenOfType(it, PhpAttribute::class.java) }
43+
.filter { it.fqn in TempestFrameworkClasses.ROUTES }
44+
.mapNotNull { attribute ->
45+
attribute.owner to attribute.arguments
46+
.firstOrNull { it.name == "uri" || it.name.isEmpty() }
47+
?.argument
48+
?.value
49+
?.let { StringUtil.unquoteString(it) }
50+
}
51+
.filter { it.first is Method }
52+
.filter { !it.second.isNullOrEmpty() }
53+
.associate { it.second to (it.first as PhpNamedElement).fqn }
54+
// .apply { println("file: ${inputData.file}, result: $this") }
55+
}
56+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.github.tempest.framework.router.index
2+
3+
import com.github.tempest.framework.TempestFrameworkClasses
4+
import com.github.tempest.framework.TempestFrameworkUtil
5+
import com.github.tempest.framework.common.index.AbstractIndex
6+
import com.intellij.openapi.util.text.StringUtil
7+
import com.intellij.psi.util.PsiTreeUtil
8+
import com.intellij.util.indexing.DataIndexer
9+
import com.intellij.util.indexing.FileBasedIndex
10+
import com.intellij.util.indexing.FileContent
11+
import com.intellij.util.indexing.ID
12+
import com.intellij.util.io.EnumeratorStringDescriptor
13+
import com.jetbrains.php.lang.PhpFileType
14+
import com.jetbrains.php.lang.psi.elements.PhpAttribute
15+
16+
private typealias RouterMethodsIndexType = String
17+
18+
/**
19+
* Stores uri -> methods association
20+
*/
21+
class RouterMethodsIndex : AbstractIndex<RouterMethodsIndexType>() {
22+
companion object {
23+
val key = ID.create<String, RouterMethodsIndexType>("Tempest.Routes.Methods")
24+
}
25+
26+
override fun getVersion() = 3
27+
28+
override fun getName() = key
29+
30+
override fun getValueExternalizer() = EnumeratorStringDescriptor.INSTANCE
31+
32+
override fun getInputFilter() = FileBasedIndex.InputFilter {
33+
it.fileType == PhpFileType.INSTANCE &&
34+
!it.name.endsWith(TempestFrameworkUtil.TEMPLATE_SUFFIX)
35+
}
36+
37+
override fun getIndexer() = DataIndexer<String, RouterMethodsIndexType, FileContent> { inputData ->
38+
inputData
39+
.psiFile
40+
.let { PsiTreeUtil.findChildrenOfType(it, PhpAttribute::class.java) }
41+
.filter { it.fqn in TempestFrameworkClasses.ROUTES }
42+
.mapNotNull { attribute ->
43+
attribute.name to attribute.arguments
44+
.firstOrNull { it.name == "uri" || it.name.isEmpty() }
45+
?.argument
46+
?.value
47+
?.let { StringUtil.unquoteString(it) }
48+
}
49+
.filter { !it.first.isNullOrEmpty() && !it.second.isNullOrEmpty() }
50+
.associate { it.second to it.first!!.lowercase() }
51+
// .apply { println("file: ${inputData.file}, result: $this") }
52+
}
53+
}

src/main/resources/META-INF/plugin.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929

3030
<fileBasedIndex
3131
implementation="com.github.tempest.framework.console.index.ConsoleCommandsIndex" />
32+
<fileBasedIndex
33+
implementation="com.github.tempest.framework.router.index.RouterMethodsIndex" />
34+
<fileBasedIndex
35+
implementation="com.github.tempest.framework.router.index.RouterActionsIndex" />
3236
</extensions>
3337
<extensions defaultExtensionNs="com.jetbrains.php">
3438

0 commit comments

Comments
 (0)