-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathphp_box.py
313 lines (297 loc) · 11.5 KB
/
php_box.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# -*- coding: utf-8 -*-
import sublime, sublime_plugin, sys
# import sublime_lib
import os, codecs, subprocess, string, json, threading, re, time,webbrowser
import base64,binascii
from .thread_progress import ThreadProgress
package_name = 'PhpNinJaManual'
packages_path = os.path.split(os.path.realpath(__file__))[0]
command_bin = packages_path + os.sep + 'tp5' + os.sep + 'public' + os.sep + 'index.php'
def open_tab(url):
webbrowser.open_new_tab(url)
def PKGPATH():
global package_name
return os.path.join(sublime.packages_path(), package_name)
def fs_reader(path):
return codecs.open(path, mode='r', encoding='utf8').read()
def fs_writer(path, raw):
codecs.open(path, mode='w', encoding='utf8', errors='ignore').write(raw)
class php_execute(threading.Thread):
def __init__(self, cmd, args, view, window):
self.cmd = cmd
self.args = args
self.view = view
self.window = window
settings = sublime.load_settings("PhpBox.sublime-settings")
self.php_bin = settings.get('php_path')
global command_bin
threading.Thread.__init__(self)
def run(self):
command_text = self.php_bin+' "' + command_bin + '" sublime/index/run/call/'+self.cmd+'/args/'+ base64.b64encode(json.dumps(self.args, sort_keys=True).encode('utf-8')).decode('utf-8')
print(command_text)
cloums = os.popen(command_text)
data = cloums.read()
print(data)
self.parse_php_result(data)
def parse_php_result(self, out):
try:
print(out)
result_str_raw = out
# out = out.decode("UTF-8")
except UnicodeDecodeError as e:
print(out)
try:
result_str = base64.b64decode(out)
except (TypeError):
print(out)
pass
except (binascii.Error):
print(out)
pass
result = 0;
try:
result = json.loads(result_str.decode('utf-8'))
except (ValueError):
print('The return value for the php plugin is wrong JSON.',True)
if len(result_str) > 0:
try:
sublime.error_message(u"PHP ERROR:\n{0}".format(result_str.decode('utf-8')))
except(UnicodeDecodeError):
sublime.error_message(u"PHP ERROR:\n{0}".format(result_str_raw))
pass
print(type(result))
print(result)
# -------------------------------------------------------------------
# PHP 通信完成,开始处理结果
# -------------------------------------------------------------------
self.executeReceive(result)
def executeReceive(self, result):
print(result)
curr_view = sublime.active_window().active_view()
self.window = curr_view.window()
if result['code'] == 200:
if result['type'] == 'error_dialog':
sublime.error_message(u"{0}".format(result['msg']))
elif result['type'] == 'msg_dialog':
sublime.message_dialog(u"{0}".format(result['msg']))
elif result['type'] == 'ok_cancel_dialog':
ret = sublime.ok_cancel_dialog(u"{0}".format(result['msg']), u"{0}".format(result['ok_title']))
if ret is True:
if result['ok_cmd'] != []:
self.executeReceive(result['ok_cmd'])
elif result['type'] == 'yes_no_cancel_dialog':
ret = sublime.yes_no_cancel_dialog(u"{0}".format(result['msg']), u"{0}".format(result['yes_title']), u"{0}".format(result['no_title']))
if ret == sublime.DIALOG_CANCEL:
pass
elif ret == sublime.DIALOG_YES:
if result['yes_cmd'] != []:
self.executeReceive(result['yes_cmd'])
elif ret == sublime.DIALOG_NO:
if result['no_cmd'] != []:
self.executeReceive(result['no_cmd'])
elif result['type'] == 'status_message':
sublime.status_message(u"{0}".format(result['msg']));
elif result['type'] == 'set_clipboard':
ret = sublime.set_clipboard(u"{0}".format(result['str']))
elif result['type'] == 'run_command':
self.window.run_command(u"{0}".format(result['cmd']), result['args'])
elif result['type'] == 'show_quick_panel':
def on_quick_done(index):
if result['on_done_cmd'] == []:
pass
else:
# result.args 合并 index
# print('index:')
# print(index)
# print(result['on_done_cmd'])
if 'cmd_args' in result['on_done_cmd']['args']:
result['on_done_cmd']['args']['cmd_args']['index'] = index
result['on_done_cmd']['args']['inner'] = 1
else:
result['on_done_cmd']['args']['index'] = index
# print(result['on_done_cmd']['args'])
self.window.run_command(u"{0}".format(result['on_done_cmd']['cmd']), result['on_done_cmd']['args'])
def on_quick_highlighted(index):
if result['on_highlighted_cmd'] == []:
pass
else:
# result.args 合并 index
print(index)
if 'cmd_args' in result['on_highlighted_cmd']['args']:
result['on_highlighted_cmd']['args']['cmd_args']['index'] = index
result['on_highlighted_cmd']['args']['inner'] = 1
else:
result['on_highlighted_cmd']['args']['index'] = index
print(result['on_highlighted_cmd']['args'])
self.window.run_command(u"{0}".format(result['on_highlighted_cmd']['cmd']), result['on_highlighted_cmd']['args'])
ret = self.window.show_quick_panel(result['items'], on_quick_done, result['flag'], -1, on_quick_highlighted)
elif result['type'] == 'show_input_panel':
def on_input_done(str):
if result['on_done_cmd'] == []:
pass
else:
# result.args 合并 str
if 'cmd_args' in result['on_done_cmd']['args']:
result['on_done_cmd']['args']['inner'] = 1
result['on_done_cmd']['args']['cmd_args']['str'] = str
else:
result['on_done_cmd']['args']['str'] = str
self.window.run_command(u"{0}".format(result['on_done_cmd']['cmd']), result['on_done_cmd']['args'])
def on_input_change(str):
if result['on_change_cmd'] == []:
pass
else:
# result['args'] 合并 str
if 'cmd_args' in result['on_change_cmd']['args']:
result['on_change_cmd']['args']['inner'] = 1
result['on_change_cmd']['args']['cmd_args']['str'] = str
else:
result['on_change_cmd']['args']['str'] = str
print(result['on_change_cmd']['args'])
self.window.run_command(u"{0}".format(result['on_change_cmd']['cmd']), result['on_change_cmd']['args'])
def on_input_cancel():
if result['on_cancel_cmd'] == []:
pass
else:
if 'cmd_args' in result['on_cancel_cmd']['args']:
result['on_cancel_cmd']['args']['inner'] = 1
self.window.run_command(u"{0}".format(result['on_cancel_cmd']['cmd']), result['on_cancel_cmd']['args'])
ret = self.window.show_input_panel(u"{0}".format(result['caption']), u"{0}".format(result['initial_text']), on_input_done, on_input_change, on_input_cancel)
elif result['type'] == 'show_popup_menu':
def on_done(index):
if result['on_done_cmd'] == []:
pass
else:
# result['args'] 合并 str
if 'cmd_args' in result['on_done_cmd']['args']:
result['on_done_cmd']['args']['cmd_args']['index'] = index
result['on_done_cmd']['args']['inner'] = 1
else:
result['on_done_cmd']['args']['index'] = index
self.window.run_command(u"{0}".format(result['on_done_cmd']['cmd']), result['on_done_cmd']['args'])
self.view.show_popup_menu(result['items'], on_done)
elif result['type'] == 'show_popup':
def on_navigate(url):
if result['on_navigate_cmd'] == []:
pass
else:
# result['args'] 合并 str
if 'cmd_args' in result['on_navigate_cmd']['args']:
result['on_navigate_cmd']['args']['cmd_args']['url'] = url
result['on_navigate_cmd']['args']['inner'] = 1
else:
result['on_navigate_cmd']['args']['url'] = url
print(json.dumps(result['on_navigate_cmd']))
self.window.run_command(u"{0}".format(result['on_navigate_cmd']['cmd']), result['on_navigate_cmd']['args'])
def on_hide():
if result['on_hide_cmd'] == []:
pass
else:
# result['args'] 合并 str
if 'cmd_args' in result['on_hide_cmd']['args']:
result['on_hide_cmd']['args']['inner'] = 1
self.window.run_command(u"{0}".format(result['on_hide_cmd']['cmd']), result['on_hide_cmd']['args'])
curr_view.show_popup(result['content'], result['flags'], result['location'], result['max_width'], result['max_height'], on_navigate, on_hide)
elif result['type'] == 'open_tab':
open_tab(result['url'])
elif result['type'] == 'run_command':
cmd = u"{0}".format(result['cmd'])
result['args']['inner'] = 1
if result['from'] == 'window':
self.window.run_command(cmd, result['args'])
elif result['from'] == 'view':
self.view.run_command(cmd, result['args'])
elif result['from'] == 'applicant':
sublime.run_command(cmd, result['args'])
else:
sublime.error_message(u"{0}".format(result['msg']))
def check_php(path):
php_path = path
check_php_path = os.popen(php_path + ' -v').read()
# print(php_path);
# print(check_php_path)
pattern = re.compile(r'^PHP \d+.\d+');
if pattern.match(check_php_path):
check_php_path = True;
else:
check_php_path = False;
if check_php_path == False:
sublime.windows()[0].show_input_panel(u'Please input php bin path', '/usr/local/bin/php', self.done, None, None)
return check_php_path
def done(self,path):
settings = sublime.load_settings("PhpBox.sublime-settings")
settings.set('php_path', path)
class PhpBoxCommand(sublime_plugin.TextCommand):
def refresh_curr(self, curr):
obj = packages_path + os.sep + 'curr.json'
fs_writer(obj, json.dumps(curr, sort_keys=True, indent=4, separators=(',', ': ')))
# self.settings.get('curr')
def get_sel(self):
region = self.view.sel()[0]
if region.begin() != region.end():
return self.view.substr(region)
else:
return ''
def run(self, edit, call, cmd_args, inner = 0):
curr_win = sublime.active_window()
# sublime.error_message(self.get_sel())
curr_view = self.view
curr = {
"window":{
"window":{
"id":curr_win.id(),
"num_groups":curr_win.num_groups(),
"active_group":curr_win.active_group(),
"is_menu_visible":curr_win.is_menu_visible(),
"is_sidebar_visible":curr_win.is_sidebar_visible(),
"get_tabs_visible":curr_win.get_tabs_visible(),
"is_minimap_visible":curr_win.is_minimap_visible(),
"is_status_bar_visible":curr_win.is_status_bar_visible(),
"folders":curr_win.folders(),
"project_file_name":curr_win.project_file_name(),
"project_data":curr_win.project_data(),
"active_panel":curr_win.active_panel(),
"panels":curr_win.panels(),
}
},
"view":{
"id":curr_view.id(),
"buffer_id":curr_view.buffer_id(),
"is_primary":curr_view.is_primary(),
"file_name":curr_view.file_name(),
"name":curr_view.name(),
"is_dirty":curr_view.is_dirty(),
"is_read_only":curr_view.is_read_only(),
"is_scratch":curr_view.is_scratch(),
"size":curr_view.size(),
"viewport_extent":curr_view.viewport_extent(),
"layout_extent":curr_view.layout_extent(),
"line_height":curr_view.line_height(),
"em_width":curr_view.em_width(),
"change_count":curr_view.change_count(),
"encoding":curr_view.encoding(),
"line_endings":curr_view.line_endings(),
"is_popup_visible":curr_view.is_popup_visible(),
"is_auto_complete_visible":curr_view.is_auto_complete_visible(),
"style":curr_view.style(),
"sel":self.get_sel()
},
"package_path":PKGPATH()
}
print('curr')
# print(curr)
if('inner' not in cmd_args):
self.refresh_curr(curr)
# return
self.settings = sublime.load_settings("PhpBox.sublime-settings")
if(check_php(self.settings.get('php_path'))) == False:
return
# print(command_bin)
# print(call)
# print(cmd_args)
if call == '':
thread = php_execute('app\\sublime\\command\\ListCmd', cmd_args, self.view, sublime.windows()[0])
else:
thread = php_execute(call, cmd_args, self.view, sublime.windows()[0])
thread.start()
ThreadProgress(thread, 'Is excuting', 'Finding Done')