Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`):**
Expand Down Expand Up @@ -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.
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.
19 changes: 18 additions & 1 deletion stubs/box-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,29 @@ 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);
Expand Down
13 changes: 5 additions & 8 deletions stubs/box.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
41 changes: 41 additions & 0 deletions stubs/custom-stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,47 @@

Phar::mapPhar('__nativephp_app_bundle');

// 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
$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';

$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();
Loading
Loading