@@ -997,29 +997,50 @@ function generateFunctionEntries(?string $className, array $funcInfos): string {
997
997
return $ code ;
998
998
}
999
999
1000
- function initPhpParser () {
1001
- $ version = "4.3.0 " ;
1002
- $ phpParserDir = __DIR__ . "/PHP-Parser- $ version " ;
1003
- if (!is_dir ($ phpParserDir )) {
1000
+ function installPhpParser (string $ version , string $ phpParserDir ) {
1001
+ $ lockFile = __DIR__ . "/PHP-Parser-install-lock " ;
1002
+ $ lockFd = fopen ($ lockFile , 'w+ ' );
1003
+ if (!flock ($ lockFd , LOCK_EX )) {
1004
+ throw new Exception ("Failed to acquire installation lock " );
1005
+ }
1006
+
1007
+ try {
1008
+ // Check whether a parallel process has already installed PHP-Parser.
1009
+ if (is_dir ($ phpParserDir )) {
1010
+ return ;
1011
+ }
1012
+
1004
1013
$ cwd = getcwd ();
1005
1014
chdir (__DIR__ );
1006
1015
1007
- passthru ("wget https://github.com/nikic/PHP-Parser/archive/v $ version.tar.gz " , $ exit );
1016
+ $ tarName = "v $ version.tar.gz " ;
1017
+ passthru ("wget https://github.com/nikic/PHP-Parser/archive/ $ tarName " , $ exit );
1008
1018
if ($ exit !== 0 ) {
1009
- passthru ("curl -LO https://github.com/nikic/PHP-Parser/archive/v $ version .tar.gz " , $ exit );
1019
+ passthru ("curl -LO https://github.com/nikic/PHP-Parser/archive/ $ tarName " , $ exit );
1010
1020
}
1011
1021
if ($ exit !== 0 ) {
1012
1022
throw new Exception ("Failed to download PHP-Parser tarball " );
1013
1023
}
1014
1024
if (!mkdir ($ phpParserDir )) {
1015
1025
throw new Exception ("Failed to create directory $ phpParserDir " );
1016
1026
}
1017
- passthru ("tar xvzf v $ version .tar.gz -C PHP-Parser-$ version --strip-components 1 " , $ exit );
1027
+ passthru ("tar xvzf $ tarName -C PHP-Parser- $ version --strip-components 1 " , $ exit );
1018
1028
if ($ exit !== 0 ) {
1019
1029
throw new Exception ("Failed to extract PHP-Parser tarball " );
1020
1030
}
1021
- unlink (__DIR__ . "/v $ version .tar.gz " );
1031
+ unlink (__DIR__ . "/ $ tarName " );
1022
1032
chdir ($ cwd );
1033
+ } finally {
1034
+ flock ($ lockFd , LOCK_UN );
1035
+ @unlink ($ lockFile );
1036
+ }
1037
+ }
1038
+
1039
+ function initPhpParser () {
1040
+ $ version = "4.3.0 " ;
1041
+ $ phpParserDir = __DIR__ . "/PHP-Parser- $ version " ;
1042
+ if (!is_dir ($ phpParserDir )) {
1043
+ installPhpParser ($ version , $ phpParserDir );
1023
1044
}
1024
1045
1025
1046
spl_autoload_register (function (string $ class ) use ($ phpParserDir ) {
0 commit comments