From 52046616abf5666524c059528953856653bc2019 Mon Sep 17 00:00:00 2001 From: Eser DENIZ Date: Fri, 14 Nov 2025 11:00:31 +0100 Subject: [PATCH 1/3] feat: implement external directory mounting in custom PHAR stub --- CLAUDE.md | 52 ++++++++++++++++++++- stubs/box.json | 13 ++---- stubs/custom-stub.php | 40 ++++++++++++++++ tests/Feature/BundleCreationTest.php | 69 ++++++++++++---------------- 4 files changed, 125 insertions(+), 49 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 5461d60..05debdd 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -49,6 +49,13 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co ### Key Stub Files +**Custom PHAR Stub (`stubs/custom-stub.php`):** +- Entry point for NativePHP PHAR bundles +- Handles NATIVEPHP_APP_DATA_PATH environment variable configuration +- Auto-creates external runtime directories (storage/, database/, bootstrap/cache/) +- Sets Laravel environment variables from single NATIVEPHP_APP_DATA_PATH +- Allows cli, cli-server, phpdbg, and embed SAPIs + **NativePHP Path Helper (`stubs/box-helper.php`):** - Cross-platform path normalization with `nativephp_rp()` function - Windows/Unix path compatibility @@ -61,7 +68,7 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co **Bundle Configuration (`stubs/box.json`):** - Pre-configured Box settings for Laravel/NativePHP -- Optimized file exclusions and inclusions +- Optimized file exclusions and inclusions (excludes .sqlite files) - Integrated RealPathMod compactor usage **Custom Request Class (`stubs/App/Native/Request.php`):** @@ -94,4 +101,45 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co ### Path Resolution Architecture -Most Laravel PHAR issues stem from `realpath()` not working in PHAR context. The RealPathMod compactor automatically replaces these calls with `nativephp_rp()` which provides cross-platform path normalization and PHAR-aware resolution. \ No newline at end of file +Most Laravel PHAR issues stem from `realpath()` not working in PHAR context. The RealPathMod compactor automatically replaces these calls with `nativephp_rp()` which provides cross-platform path normalization and PHAR-aware resolution. + +### Runtime Path Management with Phar::mount() + +Parcel uses `Phar::mount()` to transparently redirect runtime directories from inside the PHAR to external filesystem locations, configured via a single environment variable: + +**NATIVEPHP_APP_DATA_PATH Environment Variable:** +- Single variable that drives all runtime path mounting +- Automatically creates external directory structure: `$NATIVEPHP_APP_DATA_PATH/app/{storage,database,bootstrap/cache}` +- Uses `Phar::mount()` to redirect PHAR paths to external filesystem +- No environment variable pollution in the stub + +**External Directory Structure:** +``` +$NATIVEPHP_APP_DATA_PATH/ +└── app/ + ├── storage/ + │ ├── logs/ (mounted to phar://storage/logs) + │ └── framework/ (mounted to phar://storage/framework) + ├── database/ (mounted to phar://database) + │ └── database.sqlite + └── bootstrap/ + └── cache/ (mounted to phar://bootstrap/cache) +``` + +**PHAR Exclusions (box.json):** +- `storage/logs` - excluded from PHAR, mounted at runtime +- `storage/framework` - excluded from PHAR, mounted at runtime +- `bootstrap/cache` - excluded from PHAR, mounted at runtime +- `database/*.sqlite` - excluded from PHAR +- `storage/app` - kept in PHAR for placeholder/gitignore files + +**Mounting Implementation:** +The custom stub checks for `NATIVEPHP_APP_DATA_PATH`, creates the external directory structure, and uses `Phar::mount()` with relative paths: +```php +Phar::mount('storage/logs', $externalPath); +Phar::mount('storage/framework', $externalPath); +Phar::mount('bootstrap/cache', $externalPath); +Phar::mount('database', $externalPath); +``` + +Laravel accesses these paths normally (e.g., `storage()->path('logs/laravel.log')`) and they're transparently redirected to external filesystem by the PHAR mounting. \ No newline at end of file diff --git a/stubs/box.json b/stubs/box.json index 3c01462..71d5c3b 100644 --- a/stubs/box.json +++ b/stubs/box.json @@ -47,19 +47,16 @@ ], "in": "vendor" }, - { - "exclude": [ - "logs", - "app/livewire-tmp", - "framework/sessions" - ], - "in": "storage" - }, { "notName": "*.sqlite", "in": "database" } ], + "blacklist": [ + "storage/logs", + "storage/framework", + "bootstrap/cache" + ], "git-version": "package_version", "output": "build/__nativephp_app_bundle", "compression": "GZ", diff --git a/stubs/custom-stub.php b/stubs/custom-stub.php index f50c895..8876e91 100644 --- a/stubs/custom-stub.php +++ b/stubs/custom-stub.php @@ -17,6 +17,46 @@ Phar::mapPhar('__nativephp_app_bundle'); +// Mount external directories if NATIVEPHP_APP_DATA_PATH is set +$appDataPath = getenv('NATIVEPHP_APP_DATA_PATH'); +if ($appDataPath !== false && $appDataPath !== '') { + $appDir = rtrim($appDataPath, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.'app'; + + // Create external directory structure + $storageLogs = $appDir.DIRECTORY_SEPARATOR.'storage'.DIRECTORY_SEPARATOR.'logs'; + $storageFramework = $appDir.DIRECTORY_SEPARATOR.'storage'.DIRECTORY_SEPARATOR.'framework'; + $bootstrapCache = $appDir.DIRECTORY_SEPARATOR.'bootstrap'.DIRECTORY_SEPARATOR.'cache'; + $database = $appDir.DIRECTORY_SEPARATOR.'database'; + + $directories = [ + $storageLogs, + $storageFramework.DIRECTORY_SEPARATOR.'cache', + $storageFramework.DIRECTORY_SEPARATOR.'sessions', + $storageFramework.DIRECTORY_SEPARATOR.'views', + $storageFramework.DIRECTORY_SEPARATOR.'testing', + $bootstrapCache, + $database, + ]; + + foreach ($directories as $dir) { + if (! is_dir($dir)) { + mkdir($dir, 0755, true); + } + } + + // Create empty database.sqlite if it doesn't exist + $databaseFile = $database.DIRECTORY_SEPARATOR.'database.sqlite'; + if (! file_exists($databaseFile)) { + touch($databaseFile); + } + + // Mount external directories to PHAR paths using relative paths + Phar::mount('storage/logs', $storageLogs); + Phar::mount('storage/framework', $storageFramework); + Phar::mount('bootstrap/cache', $bootstrapCache); + Phar::mount('database', $database); +} + require 'phar://__nativephp_app_bundle/phar-router.php'; __HALT_COMPILER(); diff --git a/tests/Feature/BundleCreationTest.php b/tests/Feature/BundleCreationTest.php index ed01c8c..b8de423 100644 --- a/tests/Feature/BundleCreationTest.php +++ b/tests/Feature/BundleCreationTest.php @@ -62,30 +62,25 @@ $bundleFile = $projectDir.'/build/__nativephp_app_bundle'; expect($filesystem->exists($bundleFile))->toBeTrue('Bundle file not found: '.$bundleFile); - // Create necessary directories for NativePHP runtime - $filesystem->mkdir($projectDir.'/storage/logs'); - $filesystem->mkdir($projectDir.'/storage/framework/cache'); - $filesystem->mkdir($projectDir.'/storage/framework/sessions'); - $filesystem->mkdir($projectDir.'/storage/framework/views'); - $filesystem->mkdir($projectDir.'/storage/framework/testing'); - $filesystem->mkdir($projectDir.'/bootstrap/cache'); - $filesystem->mkdir($projectDir.'/database'); - $filesystem->dumpFile($projectDir.'/database/database.sqlite', ''); - - // Prepare environment variables similar to NativePHP + // Create app data directory for NativePHP runtime + $appDataDir = $this->tempWorkingDir.'/app-data'; + $filesystem->mkdir($appDataDir); + + // Configure environment - stub will mount based on NATIVEPHP_APP_DATA_PATH + $appDir = $appDataDir.'/app'; $env = [ + 'NATIVEPHP_APP_DATA_PATH' => $appDataDir, 'APP_ENV' => 'production', 'APP_DEBUG' => 'false', - 'LARAVEL_STORAGE_PATH' => $projectDir.'/storage', 'NATIVEPHP_RUNNING' => 'true', - 'NATIVEPHP_STORAGE_PATH' => $projectDir.'/storage', - 'NATIVEPHP_DATABASE_PATH' => $projectDir.'/database/database.sqlite', - // Cache paths for production mode - 'APP_SERVICES_CACHE' => $projectDir.'/bootstrap/cache/services.php', - 'APP_PACKAGES_CACHE' => $projectDir.'/bootstrap/cache/packages.php', - 'APP_CONFIG_CACHE' => $projectDir.'/bootstrap/cache/config.php', - 'APP_ROUTES_CACHE' => $projectDir.'/bootstrap/cache/routes-v7.php', - 'APP_EVENTS_CACHE' => $projectDir.'/bootstrap/cache/events.php', + 'LARAVEL_STORAGE_PATH' => $appDir.'/storage', + 'NATIVEPHP_STORAGE_PATH' => $appDir.'/storage', + 'NATIVEPHP_DATABASE_PATH' => $appDir.'/database/database.sqlite', + 'APP_SERVICES_CACHE' => $appDir.'/bootstrap/cache/services.php', + 'APP_PACKAGES_CACHE' => $appDir.'/bootstrap/cache/packages.php', + 'APP_CONFIG_CACHE' => $appDir.'/bootstrap/cache/config.php', + 'APP_ROUTES_CACHE' => $appDir.'/bootstrap/cache/routes-v7.php', + 'APP_EVENTS_CACHE' => $appDir.'/bootstrap/cache/events.php', ]; // Test that the bundle can execute artisan commands @@ -141,29 +136,25 @@ $bundleFile = $projectDir.'/build/__nativephp_app_bundle'; expect($filesystem->exists($bundleFile))->toBeTrue('Bundle file not found: '.$bundleFile); - // Create necessary directories for NativePHP runtime - $filesystem->mkdir($projectDir.'/storage/logs'); - $filesystem->mkdir($projectDir.'/storage/framework/cache'); - $filesystem->mkdir($projectDir.'/storage/framework/sessions'); - $filesystem->mkdir($projectDir.'/storage/framework/views'); - $filesystem->mkdir($projectDir.'/storage/framework/testing'); - $filesystem->mkdir($projectDir.'/bootstrap/cache'); - $filesystem->mkdir($projectDir.'/database'); - $filesystem->dumpFile($projectDir.'/database/database.sqlite', ''); - - // Prepare environment variables + // Create app data directory for NativePHP runtime + $appDataDir = $this->tempWorkingDir.'/app-data-http'; + $filesystem->mkdir($appDataDir); + + // Configure environment - stub will mount based on NATIVEPHP_APP_DATA_PATH + $appDir = $appDataDir.'/app'; $env = [ + 'NATIVEPHP_APP_DATA_PATH' => $appDataDir, 'APP_ENV' => 'production', 'APP_DEBUG' => 'false', - 'LARAVEL_STORAGE_PATH' => $projectDir.'/storage', 'NATIVEPHP_RUNNING' => 'true', - 'NATIVEPHP_STORAGE_PATH' => $projectDir.'/storage', - 'NATIVEPHP_DATABASE_PATH' => $projectDir.'/database/database.sqlite', - 'APP_SERVICES_CACHE' => $projectDir.'/bootstrap/cache/services.php', - 'APP_PACKAGES_CACHE' => $projectDir.'/bootstrap/cache/packages.php', - 'APP_CONFIG_CACHE' => $projectDir.'/bootstrap/cache/config.php', - 'APP_ROUTES_CACHE' => $projectDir.'/bootstrap/cache/routes-v7.php', - 'APP_EVENTS_CACHE' => $projectDir.'/bootstrap/cache/events.php', + 'LARAVEL_STORAGE_PATH' => $appDir.'/storage', + 'NATIVEPHP_STORAGE_PATH' => $appDir.'/storage', + 'NATIVEPHP_DATABASE_PATH' => $appDir.'/database/database.sqlite', + 'APP_SERVICES_CACHE' => $appDir.'/bootstrap/cache/services.php', + 'APP_PACKAGES_CACHE' => $appDir.'/bootstrap/cache/packages.php', + 'APP_CONFIG_CACHE' => $appDir.'/bootstrap/cache/config.php', + 'APP_ROUTES_CACHE' => $appDir.'/bootstrap/cache/routes-v7.php', + 'APP_EVENTS_CACHE' => $appDir.'/bootstrap/cache/events.php', ]; // Start PHP built-in server on random port (0 = let OS choose) From 6e974729082b9c408789e71c302cc99dca8805e9 Mon Sep 17 00:00:00 2001 From: Eser DENIZ Date: Fri, 14 Nov 2025 11:28:45 +0100 Subject: [PATCH 2/3] feat: enhance external directory mounting logic in PHAR stub --- stubs/box-helper.php | 18 ++- stubs/custom-stub.php | 7 +- tests/Feature/BundleCreationTest.php | 185 ++++++++++++++++++++++++++- 3 files changed, 204 insertions(+), 6 deletions(-) diff --git a/stubs/box-helper.php b/stubs/box-helper.php index 31b74d5..923d5aa 100644 --- a/stubs/box-helper.php +++ b/stubs/box-helper.php @@ -7,12 +7,28 @@ function nativephp_rp($value = null, $pharPath = null): mixed } // Normalize slashes for cross-platform compatibility + $valPath = str_replace('\\', '/', (string) $value); + + // Check if NATIVEPHP_APP_DATA_PATH is set and path should use mounted external directory + $appDataPath = getenv('NATIVEPHP_APP_DATA_PATH'); + if ($appDataPath !== false && $appDataPath !== '') { + // Normalize the value path + $normalizedValue = ltrim($valPath, '/'); + + // Check if path starts with mounted directories (storage, database, or bootstrap/cache) + if (preg_match('#^(storage(/|$)|database(/|$)|bootstrap/cache(/|$))#', $normalizedValue)) { + // Return direct filesystem path to external mounted directory + $externalPath = rtrim($appDataPath, '/\\') . '/app/' . $normalizedValue; + return str_replace('/', DIRECTORY_SEPARATOR, $externalPath); + } + } + + // Original realpath logic for non-mounted paths if ($pharPath === null) { $pharPath = str_replace('\\', '/', Phar::running()); } else { $pharPath = str_replace('\\', '/', $pharPath); } - $valPath = str_replace('\\', '/', (string) $value); // Check for absolute paths (Windows: C:/, D:/, etc. and Unix: /) $isAbsolute = preg_match('/^(?:[a-zA-Z]:[\/\\\\]|\/|\\\\)/', $valPath); diff --git a/stubs/custom-stub.php b/stubs/custom-stub.php index 8876e91..285d414 100644 --- a/stubs/custom-stub.php +++ b/stubs/custom-stub.php @@ -17,14 +17,15 @@ Phar::mapPhar('__nativephp_app_bundle'); -// Mount external directories if NATIVEPHP_APP_DATA_PATH is set +// Configure runtime paths based on NATIVEPHP_APP_DATA_PATH $appDataPath = getenv('NATIVEPHP_APP_DATA_PATH'); if ($appDataPath !== false && $appDataPath !== '') { $appDir = rtrim($appDataPath, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.'app'; // Create external directory structure - $storageLogs = $appDir.DIRECTORY_SEPARATOR.'storage'.DIRECTORY_SEPARATOR.'logs'; - $storageFramework = $appDir.DIRECTORY_SEPARATOR.'storage'.DIRECTORY_SEPARATOR.'framework'; + $storagePath = $appDir.DIRECTORY_SEPARATOR.'storage'; + $storageLogs = $storagePath.DIRECTORY_SEPARATOR.'logs'; + $storageFramework = $storagePath.DIRECTORY_SEPARATOR.'framework'; $bootstrapCache = $appDir.DIRECTORY_SEPARATOR.'bootstrap'.DIRECTORY_SEPARATOR.'cache'; $database = $appDir.DIRECTORY_SEPARATOR.'database'; diff --git a/tests/Feature/BundleCreationTest.php b/tests/Feature/BundleCreationTest.php index b8de423..42dbef2 100644 --- a/tests/Feature/BundleCreationTest.php +++ b/tests/Feature/BundleCreationTest.php @@ -66,13 +66,14 @@ $appDataDir = $this->tempWorkingDir.'/app-data'; $filesystem->mkdir($appDataDir); - // Configure environment - stub will mount based on NATIVEPHP_APP_DATA_PATH + // nativephp-desktop sets ONE variable and derives the rest from it $appDir = $appDataDir.'/app'; $env = [ 'NATIVEPHP_APP_DATA_PATH' => $appDataDir, 'APP_ENV' => 'production', 'APP_DEBUG' => 'false', 'NATIVEPHP_RUNNING' => 'true', + // Derived from NATIVEPHP_APP_DATA_PATH 'LARAVEL_STORAGE_PATH' => $appDir.'/storage', 'NATIVEPHP_STORAGE_PATH' => $appDir.'/storage', 'NATIVEPHP_DATABASE_PATH' => $appDir.'/database/database.sqlite', @@ -91,6 +92,185 @@ })->with('repositories'); +test('can optimize laravel outside the phar', function (string $repository) { + // Note: We test optimization outside the PHAR because Laravel checks if the + // bootstrap/cache directory is writable. This verifies that Phar::mount() + // successfully redirects writes to the external filesystem. + + $filesystem = new Filesystem; + + // Extract repository name from URL + $repoName = basename($repository, '.git'); + $projectDir = $this->tempWorkingDir.'/'.$repoName; + + // Clone the repository + $cloneProcess = runProcessInTemp(['git', 'clone', '--depth', '1', $repository, $repoName]); + expect($cloneProcess->isSuccessful())->toBeTrue('Failed to clone repository: '.$cloneProcess->getErrorOutput()); + + // Get path to parcel binary + $parcelBin = realpath(__DIR__.'/../../bin/parcel'); + expect($filesystem->exists($parcelBin))->toBeTrue('Parcel binary not found at: '.$parcelBin); + + // Run parcel prepare with --force to ensure we get the latest stubs + $prepareProcess = runProcessInTemp(['php', $parcelBin, 'prepare', '--force'], $projectDir); + expect($prepareProcess->isSuccessful())->toBeTrue('Parcel prepare failed: '.$prepareProcess->getErrorOutput()); + + // Create .env file if it doesn't exist (required by box.json) + if (! $filesystem->exists($projectDir.'/.env') && $filesystem->exists($projectDir.'/.env.example')) { + $filesystem->copy($projectDir.'/.env.example', $projectDir.'/.env'); + } elseif (! $filesystem->exists($projectDir.'/.env')) { + $filesystem->dumpFile($projectDir.'/.env', ''); + } + + // Run composer install to ensure dependencies are available + $composerProcess = runProcessInTemp(['composer', 'install', '--no-dev', '--no-interaction'], $projectDir); + expect($composerProcess->isSuccessful())->toBeTrue('Composer install failed: '.$composerProcess->getErrorOutput()); + + // Run parcel compile + runProcessInTemp(['php', 'artisan', 'key:generate'], $projectDir); + $compileProcess = runProcessInTemp(['php', $parcelBin, 'compile'], $projectDir); + + // If compile fails, provide detailed output + if (! $compileProcess->isSuccessful()) { + $errorOutput = "Parcel compile failed:\n"; + $errorOutput .= 'Exit code: '.$compileProcess->getExitCode()."\n"; + $errorOutput .= 'Error output: '.$compileProcess->getErrorOutput()."\n"; + $errorOutput .= 'Standard output: '.$compileProcess->getOutput()."\n"; + $this->fail($errorOutput); + } + + $bundleFile = $projectDir.'/build/__nativephp_app_bundle'; + expect($filesystem->exists($bundleFile))->toBeTrue('Bundle file not found: '.$bundleFile); + + // Create app data directory for NativePHP runtime + $appDataDir = $this->tempWorkingDir.'/app-data'; + $filesystem->mkdir($appDataDir); + + // nativephp-desktop sets ONE variable and derives the rest from it + $appDir = $appDataDir.'/app'; + $env = [ + 'NATIVEPHP_APP_DATA_PATH' => $appDataDir, + 'APP_ENV' => 'production', + 'APP_DEBUG' => 'false', + 'NATIVEPHP_RUNNING' => 'true', + // Derived from NATIVEPHP_APP_DATA_PATH + 'LARAVEL_STORAGE_PATH' => $appDir.'/storage', + 'NATIVEPHP_STORAGE_PATH' => $appDir.'/storage', + 'NATIVEPHP_DATABASE_PATH' => $appDir.'/database/database.sqlite', + 'APP_SERVICES_CACHE' => $appDir.'/bootstrap/cache/services.php', + 'APP_PACKAGES_CACHE' => $appDir.'/bootstrap/cache/packages.php', + 'APP_CONFIG_CACHE' => $appDir.'/bootstrap/cache/config.php', + 'APP_ROUTES_CACHE' => $appDir.'/bootstrap/cache/routes-v7.php', + 'APP_EVENTS_CACHE' => $appDir.'/bootstrap/cache/events.php', + ]; + + // Test artisan optimize creates cache files in external mounted directory + $optimizeProcess = runNativePhpCommand(['__nativephp_app_bundle', 'artisan', 'optimize'], $projectDir.'/build/', $env); + expect($optimizeProcess->isSuccessful())->toBeTrue('Failed to run artisan optimize: '.$optimizeProcess->getErrorOutput()); + + // Verify cache files were created in external directory (not in PHAR) + $bootstrapCacheDir = $appDir.'/bootstrap/cache'; + expect($filesystem->exists($bootstrapCacheDir.'/config.php'))->toBeTrue('config.php cache not created'); + expect($filesystem->exists($bootstrapCacheDir.'/routes-v7.php'))->toBeTrue('routes cache not created'); + expect($filesystem->exists($bootstrapCacheDir.'/events.php'))->toBeTrue('events cache not created'); + +})->with('repositories'); + +test('can bundle pre-optimized laravel application', function (string $repository) { + $filesystem = new Filesystem; + + // Extract repository name from URL + $repoName = basename($repository, '.git'); + $projectDir = $this->tempWorkingDir.'/'.$repoName; + + // Clone the repository + $cloneProcess = runProcessInTemp(['git', 'clone', '--depth', '1', $repository, $repoName]); + expect($cloneProcess->isSuccessful())->toBeTrue('Failed to clone repository: '.$cloneProcess->getErrorOutput()); + + // Get path to parcel binary + $parcelBin = realpath(__DIR__.'/../../bin/parcel'); + expect($filesystem->exists($parcelBin))->toBeTrue('Parcel binary not found at: '.$parcelBin); + + // Run parcel prepare with --force to ensure we get the latest stubs + $prepareProcess = runProcessInTemp(['php', $parcelBin, 'prepare', '--force'], $projectDir); + expect($prepareProcess->isSuccessful())->toBeTrue('Parcel prepare failed: '.$prepareProcess->getErrorOutput()); + + // Create .env file if it doesn't exist (required by box.json) + if (! $filesystem->exists($projectDir.'/.env') && $filesystem->exists($projectDir.'/.env.example')) { + $filesystem->copy($projectDir.'/.env.example', $projectDir.'/.env'); + } elseif (! $filesystem->exists($projectDir.'/.env')) { + $filesystem->dumpFile($projectDir.'/.env', ''); + } + + // Run composer install to ensure dependencies are available + $composerProcess = runProcessInTemp(['composer', 'install', '--no-dev', '--no-interaction'], $projectDir); + expect($composerProcess->isSuccessful())->toBeTrue('Composer install failed: '.$composerProcess->getErrorOutput()); + + // Generate key and optimize BEFORE compiling the PHAR + runProcessInTemp(['php', 'artisan', 'key:generate'], $projectDir); + $optimizeProcess = runProcessInTemp(['php', 'artisan', 'optimize'], $projectDir); + expect($optimizeProcess->isSuccessful())->toBeTrue('Artisan optimize failed: '.$optimizeProcess->getErrorOutput()); + + // Verify cache files exist before bundling + expect($filesystem->exists($projectDir.'/bootstrap/cache/config.php'))->toBeTrue('config.php not cached before bundling'); + expect($filesystem->exists($projectDir.'/bootstrap/cache/routes-v7.php'))->toBeTrue('routes not cached before bundling'); + expect($filesystem->exists($projectDir.'/bootstrap/cache/events.php'))->toBeTrue('events not cached before bundling'); + + // Run parcel compile with optimized caches + $compileProcess = runProcessInTemp(['php', $parcelBin, 'compile'], $projectDir); + + // If compile fails, provide detailed output + if (! $compileProcess->isSuccessful()) { + $errorOutput = "Parcel compile failed:\n"; + $errorOutput .= 'Exit code: '.$compileProcess->getExitCode()."\n"; + $errorOutput .= 'Error output: '.$compileProcess->getErrorOutput()."\n"; + $errorOutput .= 'Standard output: '.$compileProcess->getOutput()."\n"; + $this->fail($errorOutput); + } + + $bundleFile = $projectDir.'/build/__nativephp_app_bundle'; + expect($filesystem->exists($bundleFile))->toBeTrue('Bundle file not found: '.$bundleFile); + + // Create app data directory for NativePHP runtime + $appDataDir = $this->tempWorkingDir.'/app-data-preoptimized'; + $filesystem->mkdir($appDataDir); + + // nativephp-desktop sets ONE variable and derives the rest from it + $appDir = $appDataDir.'/app'; + $env = [ + 'NATIVEPHP_APP_DATA_PATH' => $appDataDir, + 'APP_ENV' => 'production', + 'APP_DEBUG' => 'false', + 'NATIVEPHP_RUNNING' => 'true', + // Derived from NATIVEPHP_APP_DATA_PATH + 'LARAVEL_STORAGE_PATH' => $appDir.'/storage', + 'NATIVEPHP_STORAGE_PATH' => $appDir.'/storage', + 'NATIVEPHP_DATABASE_PATH' => $appDir.'/database/database.sqlite', + 'APP_SERVICES_CACHE' => $appDir.'/bootstrap/cache/services.php', + 'APP_PACKAGES_CACHE' => $appDir.'/bootstrap/cache/packages.php', + 'APP_CONFIG_CACHE' => $appDir.'/bootstrap/cache/config.php', + 'APP_ROUTES_CACHE' => $appDir.'/bootstrap/cache/routes-v7.php', + 'APP_EVENTS_CACHE' => $appDir.'/bootstrap/cache/events.php', + // Prevent artisan about from trying to detect composer + 'COMPOSER_BINARY' => 'false', + ]; + + // Verify cached files are accessible (they should be in the PHAR or mounted directory) + // Run a command that would fail if config cache doesn't work + $configShowProcess = runNativePhpCommand(['__nativephp_app_bundle', 'artisan', 'config:show', 'app.name'], $projectDir.'/build/', $env); + expect($configShowProcess->isSuccessful())->toBeTrue('Config cache test failed: '.$configShowProcess->getErrorOutput()); + + // Run a command that would fail if routes cache doesn't work + $routeListProcess = runNativePhpCommand(['__nativephp_app_bundle', 'artisan', 'route:list', '--json'], $projectDir.'/build/', $env); + expect($routeListProcess->isSuccessful())->toBeTrue('Routes cache test failed: '.$routeListProcess->getErrorOutput()); + + // Parse route list to verify routes are loaded from cache + $routes = json_decode($routeListProcess->getOutput(), true); + expect($routes)->toBeArray('Routes not returned as JSON array'); + expect(count($routes))->toBeGreaterThan(0, 'No routes found - cache may not be working'); + +})->with('repositories'); + test('can serve bundle via PHP built-in server and receive HTTP responses', function (string $repository) { $filesystem = new Filesystem; @@ -140,13 +320,14 @@ $appDataDir = $this->tempWorkingDir.'/app-data-http'; $filesystem->mkdir($appDataDir); - // Configure environment - stub will mount based on NATIVEPHP_APP_DATA_PATH + // nativephp-desktop sets ONE variable and derives the rest from it $appDir = $appDataDir.'/app'; $env = [ 'NATIVEPHP_APP_DATA_PATH' => $appDataDir, 'APP_ENV' => 'production', 'APP_DEBUG' => 'false', 'NATIVEPHP_RUNNING' => 'true', + // Derived from NATIVEPHP_APP_DATA_PATH 'LARAVEL_STORAGE_PATH' => $appDir.'/storage', 'NATIVEPHP_STORAGE_PATH' => $appDir.'/storage', 'NATIVEPHP_DATABASE_PATH' => $appDir.'/database/database.sqlite', From f0fa5fff5e8559f96585e9861ebe453da0956e76 Mon Sep 17 00:00:00 2001 From: Eser DENIZ Date: Fri, 14 Nov 2025 12:00:37 +0100 Subject: [PATCH 3/3] chore: linting --- stubs/box-helper.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stubs/box-helper.php b/stubs/box-helper.php index 923d5aa..6e36728 100644 --- a/stubs/box-helper.php +++ b/stubs/box-helper.php @@ -18,7 +18,8 @@ function nativephp_rp($value = null, $pharPath = null): mixed // Check if path starts with mounted directories (storage, database, or bootstrap/cache) if (preg_match('#^(storage(/|$)|database(/|$)|bootstrap/cache(/|$))#', $normalizedValue)) { // Return direct filesystem path to external mounted directory - $externalPath = rtrim($appDataPath, '/\\') . '/app/' . $normalizedValue; + $externalPath = rtrim($appDataPath, '/\\').'/app/'.$normalizedValue; + return str_replace('/', DIRECTORY_SEPARATOR, $externalPath); } }