forked from jimthunderbird/php-to-c-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_extensions.php
135 lines (99 loc) · 3.64 KB
/
build_extensions.php
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
<?php
if ($argc == 1) {
$prompt = "Please specify the php file to convert to c extension\n";
$prompt .= "Usage: php ".basename(__FILE__)." [php file to convert to c extension]\n";
die($prompt);
}
$zephirCommand = __DIR__."/vendor/bin/zephir";
$curDir = getcwd();
$jsoncDirContent = trim(shell_exec("find ".__DIR__."/vendor/phalcon/zephir/json-c -type f"));
if (strlen($jsoncDirContent) == 0) { //that means zephir is not installed
if (chdir(__DIR__."/vendor/phalcon/zephir")) {
print "Installing zephir...\n";
system("./install-json");
system("./install");
} else {
die("Error changing to zephir vendor directory!\n");
}
if (!chdir($curDir)) {
die("Error changing to current directory!\n");
}
}
require_once __DIR__.'/vendor/autoload.php';
$input = $argv[1];
$curDir = getcwd();
$buildDir = $curDir."/build";
//remove the old build dir, if there is one
system("rm -rf $buildDir/");
$zephirDir = $buildDir."/zephir";
system("mkdir -p $zephirDir");
$extensionNames = [];
$file = "";
$fileContent = "";
$inputDir = "";
if (is_file($input)) {
$file = $curDir."/".$input;
$inputDir = dirname($file);
$fileContent = file_get_contents($file);
} else if (is_dir($input)) {
$inputDir = $input;
$fileContent = "<?php\n";
$files = explode("\n",trim(shell_exec("find $input -type f -name \"*.php\"")));
foreach($files as $f) {
$fc = file_get_contents($f);
$fc = trim($fc);
//remove php beginning tag
if( substr($fc, 0, 5) === "<?php" ) {
$fc = substr($fc, 5, strlen($fc));
}
//remove php ending tag
if ( substr($fc, -2, 2) === "?>") {
$fc = substr($fc, 0, strlen($fc) -2);
}
$fileContent .= $fc."\n\n";
}
//trim file content again
$fileContent = trim($fileContent);
$file = $zephirDir."/".array_pop(explode("/",$input)).".php";
}
file_put_contents($file, $fileContent);
$targetFile = $zephirDir."/".basename($file);
$fileFilter = null;
try {
$fileFilter = new PHPtoCExt\FileFilter($file, $targetFile, $inputDir);
$fileFilter->filter();
$analyser = new PHPtoCExt\FileAnalyser($targetFile);
foreach($analyser->getUserDefinedClasses() as $class) {
$classCode = $analyser->getCodeInClass($class);
$zephirNamespace = strtolower($analyser->getRootNamespaceOfClass($class));
if (chdir($zephirDir)) {
system("$zephirCommand init $zephirNamespace");
$classFileDir = strtolower(str_replace("\\","/",$analyser->getNamespaceOfClass($class)));
system("mkdir -p ".$zephirNamespace."/".$classFileDir);
if (!is_readable($zephirNamespace."/".$classFileDir)) {
throw new PHPtoCExt\PHPtoCExtException("Fail to create directory ".$classFileDir);
}
$classFileName = $zephirNamespace."/".$classFileDir."/".$analyser->getClassNameWithoutNamespace($class).".php";
file_put_contents($classFileName, "<?php\n".$classCode);
$extensionNames[] = $zephirNamespace;
}
}
$extensionNames = array_unique($extensionNames);
} catch (PHPtoCExt\PHPtoCExtException $e) {
die("Error: ".$e->getMessage()."\n");
}
foreach($extensionNames as $extensionName) {
$zephirProjectDir = $zephirDir."/".$extensionName;
if (chdir($zephirProjectDir) ) {
system(__DIR__."/vendor/bin/php-to-zephir phpToZephir:convertDir $extensionName");
//now do post convertion searches and replaces
echo "Performing post conversion processing...\n";
$convertedFiles = explode("\n",trim(shell_exec("find . -type f -name \"*.zep\"")));
foreach($convertedFiles as $file) {
$fileFilter->postFilter($file);
}
echo "Finished post conversion processing\n";
echo "Building extension...\n";
system("$zephirCommand install");
}
}