-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPHP Built-in WebServer.py
128 lines (100 loc) · 4.03 KB
/
PHP Built-in WebServer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import sublime
import sublime_plugin
import thread
import subprocess
import signal
import os
import functools
class ServerController(object):
def __new__(cls, *args, **kwargs):
instance = object.__new__(cls)
instance.load_settings()
instance.proc = None
instance.running = False
cls.__new__ = classmethod(lambda cls, *args, **kwargs: instance)
return cls.__new__(cls, *args, **kwargs)
def set_listener(self, listener):
self.listener = listener
self.panel = self.listener.window.get_output_panel('php_builtin_server_panel')
return self
def start(self):
self.running = True
self.show_panel()
self.proc = subprocess.Popen(self.cmd_array, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
thread.start_new_thread(self.read_stdout, ())
thread.start_new_thread(self.read_stderr, ())
self.append('PHP Buil-in WebServer ver 0.0.1\n')
self.append('Server is running! - http://%s\n' % self.url)
def stop(self):
cmd = ' '.join(self.cmd_array)
proc = subprocess.Popen(['ps', '-A'], stdout = subprocess.PIPE)
out, err = proc.communicate()
for line in out.splitlines():
if cmd in line:
pid = int(line.split(None, 1)[0])
os.kill(pid, signal.SIGKILL)
self.proc = None
self.hide_panel()
self.running = False
def show_panel(self):
self.listener.window.run_command('show_panel', {'panel': 'output.php_builtin_server_panel'})
def hide_panel(self):
self.listener.window.run_command('hide_panel', {'panel': 'output.php_builtin_server_panel'})
def read_stdout(self):
while True:
data = os.read(self.proc.stdout.fileno(), 2 ** 15)
if data != "":
sublime.set_timeout(functools.partial(self.append_data, data), 0)
else:
self.proc.stdout.close()
self.running = False
break
def read_stderr(self):
while True:
data = os.read(self.proc.stderr.fileno(), 2 ** 15)
if data != "":
sublime.set_timeout(functools.partial(self.append, data), 0)
else:
self.proc.stderr.close()
self.running = False
break
def append(self, data):
self.panel.set_read_only(False)
edit = self.panel.begin_edit()
self.panel.insert(edit, self.panel.size(), data.decode("utf-8"))
self.scroll_to_end_view()
self.panel.end_edit(edit)
self.panel.set_read_only(True)
def scroll_to_end_view(self):
(cur_row, _) = self.panel.rowcol(self.panel.size())
self.panel.show(self.panel.text_point(cur_row, 0))
def is_running(self):
return self.running
def load_settings(self):
s = sublime.load_settings("PHP Built-in WebServer.sublime-settings")
ps = sublime.active_window().active_view().settings()
address = ps.get("address", s.get("address"))
port = ps.get("port", s.get("port"))
document_root_path = ps.get("document_root_path", s.get("document_root_path"))
self.url = '%s:%i' % (address, port)
self.cmd_array = ['php', '-S', self.url, '-t', document_root_path]
class StartServerCommand(sublime_plugin.WindowCommand):
def run(self):
ServerController().set_listener(self).start()
def is_enabled(self):
return not ServerController().is_running()
class StopServerCommand(sublime_plugin.WindowCommand):
def run(self):
ServerController().set_listener(self).stop()
def is_enabled(self):
return True
class ShowPanelCommand(sublime_plugin.WindowCommand):
def run(self):
ServerController().set_listener(self).show_panel()
def is_enabled(self):
return not ServerController().is_running()
class HidePanelCommand(sublime_plugin.WindowCommand):
def run(self):
ServerController().set_listener(self).hide_panel()
def is_enabled(self):
return True