-
Notifications
You must be signed in to change notification settings - Fork 0
/
PHP.cs
74 lines (63 loc) · 2.46 KB
/
PHP.cs
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
/*
@Filename: PHP.cs
@Author: Ni1kko
@Descritpion: Simple class to handle compiling a .php script
*/
using System;
using System.Diagnostics;
using System.Threading.Tasks;
namespace Fallox
{
internal static class PHP
{
private static readonly string CompilerPath = Environment.CurrentDirectory + "\\php";
private static readonly string CompilerPathFull = CompilerPath + "\\php.exe";
internal static async Task<string> Compiler(string filePath)
{
//setup compiler
var PHPProcess = new Process()
{
StartInfo = new ProcessStartInfo()
{
FileName = CompilerPathFull,
Arguments = "-d \"display_errors=1\" -d \"error_reporting=E_PARSE\" \"" + filePath + "\"",
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
},
PriorityClass = ProcessPriorityClass.RealTime
};
//compiler ended event
PHPProcess.Disposed += PHPCompilerExit;
PHPProcess.Exited += PHPCompilerExit;
void PHPCompilerExit(object sender, EventArgs events) => Console.WriteLine($"PHP Compiler Session Ended");
//load compiler
PHPProcess.Start();
Console.WriteLine($"PHP Compiler Session Began");
await Task.Delay(500);
//get compiled page
var PHPDocument = await PHPProcess.CurrrentDocument();
//close compiler
PHPProcess.StandardOutput.Close();
PHPProcess.Close();
PHPProcess.Dispose();
//return compiled page
return PHPDocument;
}
private static async Task<string> CurrrentDocument(this Process PHPProcess)
{
//Gets compiled page result
string loadedPage = PHPProcess.StandardOutput.ReadToEnd();
await Task.Delay(200);
//Tweak result on error
if (string.IsNullOrEmpty(loadedPage) || loadedPage.StartsWith("\nParse error: syntax error"))
{
PHPProcess.StandardError.Close();
loadedPage = "<h2>Error</h2><hr/><h4>Error Details: </h4><pre>" + loadedPage + "</pre>";
};
//return result
return loadedPage;
}
}
}