1313use SPC \store \Config ;
1414use SPC \store \FileSystem ;
1515use SPC \store \pkg \GoXcaddy ;
16+ use SPC \store \SourceManager ;
1617use SPC \toolchain \GccNativeToolchain ;
1718use SPC \toolchain \ToolchainManager ;
1819use SPC \util \DependencyUtil ;
@@ -265,22 +266,69 @@ protected function patchPhpScripts(): void
265266 }
266267 }
267268
269+ /**
270+ * Process the --with-frankenphp-app option
271+ * Creates app.tar and app.checksum in source/frankenphp directory
272+ */
273+ protected function processFrankenphpApp (): void
274+ {
275+ $ frankenphpSourceDir = SOURCE_PATH . '/frankenphp ' ;
276+ SourceManager::initSource (['frankenphp ' ], ['frankenphp ' ]);
277+ $ frankenphpAppPath = $ this ->getOption ('with-frankenphp-app ' );
278+
279+ if ($ frankenphpAppPath ) {
280+ if (!is_dir ($ frankenphpAppPath )) {
281+ throw new WrongUsageException ("The path provided to --with-frankenphp-app is not a valid directory: {$ frankenphpAppPath }" );
282+ }
283+ $ appTarPath = $ frankenphpSourceDir . '/app.tar ' ;
284+ logger ()->info ("Creating app.tar from {$ frankenphpAppPath }" );
285+
286+ shell ()->exec ('tar -cf ' . escapeshellarg ($ appTarPath ) . ' -C ' . escapeshellarg ($ frankenphpAppPath ) . ' . ' );
287+
288+ $ checksum = hash_file ('md5 ' , $ appTarPath );
289+ file_put_contents ($ frankenphpSourceDir . '/app_checksum.txt ' , $ checksum );
290+ } else {
291+ FileSystem::removeFileIfExists ($ frankenphpSourceDir . '/app.tar ' );
292+ FileSystem::removeFileIfExists ($ frankenphpSourceDir . '/app_checksum.txt ' );
293+ file_put_contents ($ frankenphpSourceDir . '/app.tar ' , '' );
294+ file_put_contents ($ frankenphpSourceDir . '/app_checksum.txt ' , '' );
295+ }
296+ }
297+
298+ protected function getFrankenPHPVersion (): string
299+ {
300+ $ goModPath = SOURCE_PATH . '/frankenphp/caddy/go.mod ' ;
301+
302+ if (!file_exists ($ goModPath )) {
303+ throw new SPCInternalException ("FrankenPHP caddy/go.mod file not found at {$ goModPath }, why did we not download FrankenPHP? " );
304+ }
305+
306+ $ content = file_get_contents ($ goModPath );
307+ if (preg_match ('/github\.com\/dunglas\/frankenphp\s+v?(\d+\.\d+\.\d+)/ ' , $ content , $ matches )) {
308+ return $ matches [1 ];
309+ }
310+
311+ throw new SPCInternalException ('Could not find FrankenPHP version in caddy/go.mod ' );
312+ }
313+
268314 protected function buildFrankenphp (): void
269315 {
270316 GlobalEnvManager::addPathIfNotExists (GoXcaddy::getPath ());
317+ $ this ->processFrankenphpApp ();
271318 $ nobrotli = $ this ->getLib ('brotli ' ) === null ? ',nobrotli ' : '' ;
272319 $ nowatcher = $ this ->getLib ('watcher ' ) === null ? ',nowatcher ' : '' ;
273320 $ xcaddyModules = getenv ('SPC_CMD_VAR_FRANKENPHP_XCADDY_MODULES ' );
274- // make it possible to build from a different frankenphp directory!
275- if (!str_contains ($ xcaddyModules , '--with github.com/dunglas/frankenphp ' )) {
276- $ xcaddyModules = '--with github.com/dunglas/frankenphp ' . $ xcaddyModules ;
277- }
321+ $ frankenphpSourceDir = SOURCE_PATH . '/frankenphp ' ;
322+
323+ $ xcaddyModules = preg_replace ('#--with github.com/dunglas/frankenphp\S*# ' , '' , $ xcaddyModules );
324+ $ xcaddyModules = "--with github.com/dunglas/frankenphp= {$ frankenphpSourceDir } " .
325+ "--with github.com/dunglas/frankenphp/caddy= {$ frankenphpSourceDir }/caddy {$ xcaddyModules }" ;
278326 if ($ this ->getLib ('brotli ' ) === null && str_contains ($ xcaddyModules , '--with github.com/dunglas/caddy-cbrotli ' )) {
279327 logger ()->warning ('caddy-cbrotli module is enabled, but brotli library is not built. Disabling caddy-cbrotli. ' );
280328 $ xcaddyModules = str_replace ('--with github.com/dunglas/caddy-cbrotli ' , '' , $ xcaddyModules );
281329 }
282- [, $ out ] = shell ()-> execWithResult ( ' go list -m github.com/dunglas/frankenphp@latest ' );
283- $ frankenPhpVersion = str_replace ( ' github.com/dunglas/frankenphp v ' , '' , $ out [ 0 ] );
330+
331+ $ frankenPhpVersion = $ this -> getFrankenPHPVersion ( );
284332 $ libphpVersion = $ this ->getPHPVersion ();
285333 $ dynamic_exports = '' ;
286334 if (getenv ('SPC_CMD_VAR_PHP_EMBED_TYPE ' ) === 'shared ' ) {
@@ -290,7 +338,7 @@ protected function buildFrankenphp(): void
290338 $ dynamic_exports = ' ' . $ dynamicSymbolsArgument ;
291339 }
292340 }
293- $ debugFlags = $ this ->getOption ('no-strip ' ) ? '-w -s ' : ' ' ;
341+ $ debugFlags = $ this ->getOption ('no-strip ' ) ? '' : ' -w -s ' ;
294342 $ extLdFlags = "-extldflags '-pie {$ dynamic_exports } {$ this ->arch_ld_flags }' " ;
295343 $ muslTags = '' ;
296344 $ staticFlags = '' ;
0 commit comments