11package com.github.xepozz.php_dump.services
22
3+ import com.github.xepozz.php_dump.command.PhpCommandExecutor
34import com.github.xepozz.php_dump.configuration.PhpDumpSettingsService
45import com.intellij.execution.configurations.GeneralCommandLine
5- import com.intellij.execution.process.KillableColoredProcessHandler
66import com.intellij.execution.process.ProcessAdapter
77import com.intellij.execution.process.ProcessEvent
88import com.intellij.execution.process.ProcessOutputTypes
9- import com.intellij.execution.ui.ConsoleView
10- import com.intellij.execution.ui.ConsoleViewContentType
11- import com.intellij.openapi.Disposable
129import com.intellij.openapi.components.Service
1310import com.intellij.openapi.project.Project
1411import com.intellij.openapi.util.Key
1512import com.jetbrains.php.config.PhpProjectConfigurationFacade
1613import com.jetbrains.php.config.interpreters.PhpInterpretersManagerImpl
17- import kotlinx.coroutines.CoroutineScope
1814import kotlinx.coroutines.Dispatchers
19- import kotlinx.coroutines.launch
2015import kotlinx.coroutines.withContext
2116
2217@Service(Service .Level .PROJECT )
23- class OpcodesDumperService (var project : Project ) : Disposable, DumperServiceInterface {
24- var consoleView: ConsoleView ? = null
25-
18+ class OpcodesDumperService (var project : Project ) : DumperServiceInterface {
2619 val state = PhpDumpSettingsService .getInstance(project)
27-
28- override fun dispose () {
29- consoleView?.dispose()
30- }
31-
32- override suspend fun dump (file : String ) {
20+ override suspend fun dump (file : String ): Any? {
3321 val interpretersManager = PhpInterpretersManagerImpl .getInstance(project)
3422 val interpreter = PhpProjectConfigurationFacade .getInstance(project).interpreter
35- ? : interpretersManager.interpreters.firstOrNull() ? : return
23+ ? : interpretersManager.interpreters.firstOrNull() ? : return null
3624
37- // php -l \
38- // -ddisplay_errors=0 \
39- // -derror_reporting=0 \
40- // -dopcache.enable_cli=1 \
41- // -dopcache.save_comments=1 \
42- // -dopcache.opt_debug_level=0x10000 \
43- // -dopcache.optimization_level=0 \
44- // playground/test.php \
45- // 1>/dev/null
46-
47- val interpreterPath = interpreter.pathToPhpExecutable ? : return
48- val debugLevel = maxOf(1 , minOf(2 , state.debugLevel))
25+ val interpreterPath = interpreter.pathToPhpExecutable ? : return null
26+ val debugLevel = state.debugLevel.value
4927 val preloadFile = state.preloadFile
50-
51- val commandArgs = buildList {
28+ val command = GeneralCommandLine (buildList {
5229 add(interpreterPath)
5330 add(" -l" )
5431 add(" -ddisplay_errors=0" )
5532 add(" -derror_reporting=0" )
5633
5734 add(" -dopcache.enable_cli=1" )
5835 add(" -dopcache.save_comments=1" )
59- add(" -dopcache.opt_debug_level=0x${debugLevel} 0000" )
60- add(" -dopcache.optimization_level=0" )
36+ add(" -dopcache.opt_debug_level=${debugLevel} " )
6137 if (preloadFile != null ) {
6238 add(" -dopcache.preload=${preloadFile} " )
6339 }
6440
41+ add(" 1>/dev/null" )
6542 add(file)
66- }
43+ }).commandLineString
44+
6745
68- CoroutineScope (Dispatchers .IO ).launch {
69- executeCommand(commandArgs)
70- }
71- }
7246
73- private suspend fun executeCommand (commandArgs : List <String >) = withContext(Dispatchers .IO ) {
74- val command = GeneralCommandLine (commandArgs)
75- command.withRedirectErrorStream(false )
47+ // language=injectablephp
48+ val phpSnippet = $$"""
49+ opcache_compile_file($argv [1]);
50+ passthru('$$command ');
51+ """ .trimIndent()
7652
77- // println("running command ${command.commandLineString}")
78- val processHandler = KillableColoredProcessHandler .Silent (command)
79- processHandler.setShouldKillProcessSoftly(false )
80- processHandler.setShouldDestroyProcessRecursively(true )
81- processHandler.addProcessListener(object : ProcessAdapter () {
82- override fun onTextAvailable (event : ProcessEvent , outputType : Key <* >) {
83- if (outputType == ProcessOutputTypes .STDERR ) {
84- consoleView?.print (event.text, ConsoleViewContentType .NORMAL_OUTPUT )
53+ return withContext(Dispatchers .IO ) {
54+ val output = StringBuilder ()
55+
56+ PhpCommandExecutor .execute(file, phpSnippet, project, object : ProcessAdapter () {
57+ override fun onTextAvailable (event : ProcessEvent , outputType : Key <* >) {
58+ when (outputType) {
59+ ProcessOutputTypes .STDERR -> output.append(event.text)
60+ ProcessOutputTypes .STDOUT -> output.append(event.text)
61+ }
8562 }
86- }
87- })
63+ }, listOf (" -dopcache.enable_cli=1" ))
8864
89- consoleView?.clear()
90- // consoleView?.attachToProcess(processHandler)
91- // consoleView?.requestScrollingToEnd()
9265
93- processHandler.startNotify()
66+ return @withContext output.toString()
67+ }
9468 }
95- }
69+ }
0 commit comments