Skip to content
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/feature-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
extensions: json, dom, curl, libxml, mbstring
extensions: json, dom, curl, libxml, mbstring, fileinfo, zip, pdo_sqlite, sqlite3
coverage: none

- name: Cache Composer packages
Expand All @@ -39,4 +39,4 @@ jobs:
run: composer install --prefer-dist --no-progress

- name: Run tests
run: ./vendor/bin/pest --ci --parallel --group Feature
run: ./vendor/bin/pest --ci --parallel --testsuite Feature
2 changes: 1 addition & 1 deletion .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ jobs:
run: composer install --prefer-dist --no-progress

- name: Run tests
run: ./vendor/bin/pest --ci --parallel --group Unit
run: ./vendor/bin/pest --ci --parallel --testsuite Unit
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
"php": "^8.2",
"composer-runtime-api": "^2.0",
"ext-json": "*",
"ext-phar": "*",
"ext-openssl": "*",
"humbug/box": "^4.0",
"ext-phar": "*",
"humbug/box": "^4.7.1",
"symfony/console": "^6.4|^7.0",
"symfony/filesystem": "^6.4|^7.0",
"symfony/process": "^6.4|^7.0"
Expand Down
19 changes: 10 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="Unit">
<directory>tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory>tests/Feature</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
24 changes: 24 additions & 0 deletions src/Compactor/RealPathMod.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class RealPathMod implements Compactor
'SplFileInfo' => '/[\/\\\\]SplFileInfo\.php$/',
'LoadConfiguration' => '/[\/\\\\]LoadConfiguration\.php$/',
'BladeIconsServiceProvider' => '/[\/\\\\]BladeIconsServiceProvider\.php$/',
'Application' => '/[\/\\\\]Illuminate[\/\\\\]Foundation[\/\\\\]Application\.php$/',
];

public function compact(string $file, string $contents): string
Expand All @@ -31,6 +32,8 @@ public function compact(string $file, string $contents): string
$contents = $this->modifyLoadConfiguration($contents);
} elseif (preg_match(self::FILE_PATTERNS['BladeIconsServiceProvider'], $normalizedFile)) {
$contents = $this->modifyBladeIconsServiceProvider($contents);
} elseif (preg_match(self::FILE_PATTERNS['Application'], $normalizedFile)) {
$contents = $this->modifyApplication($contents);
}

return $this->replaceGlobCalls($this->replaceRealpathCalls($contents));
Expand Down Expand Up @@ -125,6 +128,27 @@ private function modifyBladeIconsServiceProvider(string $contents): string
return preg_replace($pattern, $replacement, $contents);
}

/**
* Laravel counts an APP_*_CACHE path as absolute only when it starts
* with / or \. On Windows NativePHP passes C:\... paths, so Laravel put
* the base path in front of them: inside the phar, where the package
* manifest can't be written and the app won't boot. Count drive letters
* as absolute too. The line is the same in Laravel 10 to 13.
*/
private function modifyApplication(string $contents): string
{
$drives = implode(', ', array_map(
fn (string $letter) => "'{$letter}:'",
[...range('A', 'Z'), ...range('a', 'z')],
));

return str_replace(
"protected \$absoluteCachePathPrefixes = ['/', '\\\\'];",
"protected \$absoluteCachePathPrefixes = ['/', '\\\\', {$drives}];",
$contents
);
}

private function replaceRealpathCalls(string $contents): string
{
// Handle fully qualified calls (\realpath) - be more precise with negative lookbehind
Expand Down
17 changes: 14 additions & 3 deletions stubs/box-helper.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

function nativephp_rp($value = null, $pharPath = null): mixed
function nativephp_rp($value = null, $pharPath = null, string $separator = DIRECTORY_SEPARATOR): mixed
{
if ($value === null || $value === '') {
return $value;
Expand All @@ -27,7 +27,7 @@ function nativephp_rp($value = null, $pharPath = null): mixed
$normalizedPath .= '/';
}

return str_replace('/', DIRECTORY_SEPARATOR, $normalizedPath);
return nativephp_rp_separators($normalizedPath, $separator);
}

// Handle relative paths
Expand All @@ -53,7 +53,18 @@ function nativephp_rp($value = null, $pharPath = null): mixed
$fullPath .= '/';
}

return str_replace('/', DIRECTORY_SEPARATOR, $fullPath);
return nativephp_rp_separators($fullPath, $separator);
}

/**
* Real paths get the platform's separator. phar:// paths are stream URLs
* and keep their forward slashes: turned into "phar:\\C:\\..." on Windows,
* PHP no longer reads them as a phar, and a bundled app can't even find
* its config directory.
*/
function nativephp_rp_separators(string $path, string $separator): string
{
return str_starts_with($path, 'phar://') ? $path : str_replace('/', $separator, $path);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/BundleCreationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
// Test that the bundle can execute artisan commands
// php artisan --version doesn't really test the app. But test the basic router functionality
$artisanProcess = runNativePhpCommand(['__nativephp_app_bundle', 'artisan', '--version'], $projectDir.'/build/', $env);
expect($artisanProcess->isSuccessful())->toBeTrue('Failed to run artisan list: '.$artisanProcess->getErrorOutput());
expect($artisanProcess->isSuccessful())->toBeTrue("Failed to run artisan --version (exit {$artisanProcess->getExitCode()}):\n".$artisanProcess->getErrorOutput()."\n".substr($artisanProcess->getOutput(), 0, 4000));
expect($artisanProcess->getOutput())->toContain('Laravel');

})->with('repositories');
Expand Down Expand Up @@ -192,10 +192,10 @@

// Check we get an HTTP response (200 or 500 is fine - NativePHP needs Electron wrapper)
$statusLine = $http_response_header[0] ?? '';
expect($statusLine)->toMatch('/HTTP\/1\.[01] (200|500)/');
expect($statusLine)->toMatch('/HTTP\/1\.[01] (200|500)/', "Unexpected status. Body:\n".substr((string) $response, 0, 4000));

// Check for HTML content (even error pages are HTML from Laravel/Symfony)
expect($response)->toContain('<html');
expect(str_contains((string) $response, '<html'))->toBeTrue("{$statusLine} with no HTML. Body:\n".substr((string) $response, 0, 4000)."\nServer output:\n".substr($serverProcess->getErrorOutput().$serverProcess->getOutput(), -4000));

// Test 2: Try to request a static asset if one exists
// Just verify the server responds to asset requests (200 or 404 is fine)
Expand Down
3 changes: 2 additions & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ function waitForServer(string $host, int $port, int $timeout = 5): bool
function stopPhpServer(Process $process): void
{
if ($process->isRunning()) {
$process->stop(3, SIGTERM);
// There are no signals on Windows; Process::stop() handles that itself.
$process->stop(3, defined('SIGTERM') ? SIGTERM : null);
}
}
23 changes: 18 additions & 5 deletions tests/Unit/NativePhpRpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,22 @@
it('processes paths correctly inside PHAR context', function (mixed $input, mixed $expected) {
expect(nativephp_rp($input, 'phar:///app/test.phar'))->toBe($expected);
})->with([
'relative path without dot' => ['config/app.php', str_replace('/', DIRECTORY_SEPARATOR, 'phar:///app/test.phar/config/app.php')],
'relative path with dot' => ['./config/app.php', str_replace('/', DIRECTORY_SEPARATOR, 'phar:///app/test.phar/config/app.php')],
'parent directory path' => ['../vendor/autoload.php', str_replace('/', DIRECTORY_SEPARATOR, 'phar:///app/test.phar/../vendor/autoload.php')],
'path with backslashes' => ['path\\to\\file.txt', str_replace('/', DIRECTORY_SEPARATOR, 'phar:///app/test.phar/path/to/file.txt')],
'mixed separators' => ['path\\to/file.txt', str_replace('/', DIRECTORY_SEPARATOR, 'phar:///app/test.phar/path/to/file.txt')],
'relative path without dot' => ['config/app.php', 'phar:///app/test.phar/config/app.php'],
'relative path with dot' => ['./config/app.php', 'phar:///app/test.phar/config/app.php'],
'parent directory path' => ['../vendor/autoload.php', 'phar:///app/test.phar/../vendor/autoload.php'],
'path with backslashes' => ['path\\to\\file.txt', 'phar:///app/test.phar/path/to/file.txt'],
'mixed separators' => ['path\\to/file.txt', 'phar:///app/test.phar/path/to/file.txt'],
]);

it('keeps forward slashes in phar paths on Windows', function (string $input, string $expected) {
// A Windows phar path with backslashes ("phar:\\C:\\...") isn't a phar
// stream any more, so Laravel couldn't find the app's config directory.
expect(nativephp_rp($input, 'phar://C:/app/test.phar', '\\'))->toBe($expected);
})->with([
'config path Laravel builds' => ['phar://C:/app/test.phar\\config', 'phar://C:/app/test.phar/config'],
'relative path' => ['config/app.php', 'phar://C:/app/test.phar/config/app.php'],
]);

it('still uses backslashes for real paths on Windows', function () {
expect(nativephp_rp('C:/Program Files/test', null, '\\'))->toBe('C:\\Program Files\\test');
});
32 changes: 32 additions & 0 deletions tests/Unit/RealPathModSpecialFilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,35 @@ protected function getNestedDirectory($file, $configPath) {
it('leaves files that are not PHP alone', function () {
expect((new RealPathMod)->compact('/app/composer.json', '{"x": "realpath(y)"}'))->toBe('{"x": "realpath(y)"}');
});

it('counts Windows drive paths as absolute cache paths in Laravel', function () {
// The line as it is in Laravel 10 to 13.
$code = <<<'PHP'
<?php
namespace ParcelFixture;
class Application {
protected $absoluteCachePathPrefixes = ['/', '\\'];
public function prefixes() { return $this->absoluteCachePathPrefixes; }
}
PHP;

$compacted = (new RealPathMod)->compact('/app/vendor/laravel/framework/src/Illuminate/Foundation/Application.php', $code);

eval(substr($compacted, strlen('<?php')));
$prefixes = (new \ParcelFixture\Application)->prefixes();
// What Laravel's normalizeCachePath() does with them (Str::startsWith).
$isAbsolute = fn (string $path) => array_filter($prefixes, fn ($prefix) => str_starts_with($path, $prefix)) !== [];

expect($isAbsolute('C:\\Users\\app\\bootstrap\\cache\\packages.php'))->toBeTrue()
->and($isAbsolute('d:/app/bootstrap/cache/packages.php'))->toBeTrue()
->and($isAbsolute('/var/app/bootstrap/cache/packages.php'))->toBeTrue()
->and($isAbsolute('\\\\server\\share\\packages.php'))->toBeTrue()
->and($isAbsolute('bootstrap/cache/packages.php'))->toBeFalse();
});

it('leaves other files called Application.php alone', function () {
$code = "<?php\nclass Application {\n protected \$absoluteCachePathPrefixes = ['/', '\\\\'];\n}\n";

expect((new RealPathMod)->compact('/app/vendor/symfony/console/Application.php', $code))->toBe($code)
->and((new RealPathMod)->compact('/app/src/Application.php', $code))->toBe($code);
});
Loading