Skip to content

Commit

Permalink
fix: Reset the PHP script filename in PHARs (#1246)
Browse files Browse the repository at this point in the history
This fixes the issue with the Symfony runtime with (hopefully) no side-effects.
  • Loading branch information
theofidry authored Dec 10, 2023
1 parent 2bec15e commit c91e793
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 98 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ For the full documentation see https://box-project.github.io/box.
1. [Project files](doc/symfony.md#project-files)
1. [Project directory](doc/symfony.md#project-directory)
2. [Cache](doc/symfony.md#cache)
3. [Runtime component](doc/symfony.md#runtime-component)
1. [Reproducible builds](doc/reproducible-builds.md#reproducible-builds)
1. [Creating a reproducible PHAR](doc/reproducible-builds.md#creating-a-reproducible-phar)
1. [PHP-Scoper](doc/reproducible-builds.md#php-scoper)
Expand Down
51 changes: 0 additions & 51 deletions doc/symfony.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
1. [Project files](#project-files)
1. [Project directory](#project-directory)
2. [Cache](#cache)
3. [Runtime component](#runtime-component)

Examples of Symfony applications can be found in the project under `fixtures/build/dir012` (Symfony5) and
`fixtures/build/dir018` (Symfony6 with the Runtime component).
Expand Down Expand Up @@ -99,56 +98,6 @@ I.e.:
This last part takes advantage of Box [dumping the autoloader][composer-autoloader-dump] by default.


## Runtime component

If you are using [`symfony/runtime`][symfony-runtime], you may end up into some errors (see https://github.com/symfony/symfony/issues/49443).

To fix the issue, you need to create your own `autoload_runtime.template` file, for example:

```php
<?php

// autoload_runtime.php @generated by Symfony Runtime

if (true === (require_once __DIR__.'/autoload.php') || empty($_SERVER['SCRIPT_FILENAME'])) {
return;
}

$pharPath = Phar::running();
if (strlen($pharPath) > 0 && !str_starts_with($scriptFileName, 'phar://')) {
$scriptFileName = $pharPath.DIRECTORY_SEPARATOR.$scriptFileName;
} else {
$scriptFileName = $_SERVER['SCRIPT_FILENAME'];
}

$app = require $scriptFileName;

if (!is_object($app)) {
throw new TypeError(sprintf('Invalid return value: callable object expected, "%s" returned from "%s".', get_debug_type($app), $_SERVER['SCRIPT_FILENAME']));
}

$runtime = $_SERVER['APP_RUNTIME'] ?? $_ENV['APP_RUNTIME'] ?? %runtime_class%;
$runtime = new $runtime(($_SERVER['APP_RUNTIME_OPTIONS'] ?? $_ENV['APP_RUNTIME_OPTIONS'] ?? []) + %runtime_options%);

[$app, $args] = $runtime
->getResolver($app)
->resolve();

$app = $app(...$args);

exit(
$runtime
->getRunner($app)
->run()
);
```

Notice the `Phar::running()` section: this is the key of the fix.

If you ended up changing the location of the `console` file (which is `bin/console`), you will need to further customize
this in order to fix the problem.


<br />
<hr />

Expand Down
36 changes: 0 additions & 36 deletions fixtures/build/dir018/autoload_runtime.template

This file was deleted.

1 change: 0 additions & 1 deletion fixtures/build/dir018/box.json.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"files-bin": [
".env.local.php",
"autoload_runtime.template",
"src/Controller/.gitignore"
],
"directories": [
Expand Down
3 changes: 0 additions & 3 deletions fixtures/build/dir018/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@
"symfony": {
"allow-contrib": false,
"require": "6.2.*"
},
"runtime": {
"autoload_template": "autoload_runtime.template"
}
}
}
4 changes: 2 additions & 2 deletions fixtures/build/dir018/composer.lock

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

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

Fatal error: Uncaught Exception: Should not be executed! in phar:///opt/box/index.phar/index.php:13
Stack trace:
#0 /opt/box/index.phar(14): require()
#0 /opt/box/index.phar(15): require()
#1 {main}
thrown in phar:///opt/box/index.phar/index.php on line 13
9 changes: 6 additions & 3 deletions src/StubGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,12 @@ private static function generatePharConfigStmt(
$stub[] = '';
}

$stub[] = null === $alias
? "require 'phar://' . __FILE__ . '/{$index}';"
: "require 'phar://{$alias}/{$index}';";
$indexPath = null === $alias
? "'phar://' . __FILE__ . '/{$index}';"
: "'phar://{$alias}/{$index}';";

$stub[] = "\$_SERVER['SCRIPT_FILENAME'] = {$indexPath}";
$stub[] = "require {$indexPath}";
}

if ([] === $stub) {
Expand Down
4 changes: 4 additions & 0 deletions tests/Console/Command/CompileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ public function test_it_can_build_a_phar_file(): void
require 'phar://alias-test.phar/.box/bin/check-requirements.php';
\$_SERVER['SCRIPT_FILENAME'] = 'phar://alias-test.phar/run.php';
require 'phar://alias-test.phar/run.php';
__HALT_COMPILER(); ?>
Expand Down Expand Up @@ -499,6 +500,7 @@ public function test_it_can_build_a_phar_without_any_configuration(): void
require 'phar://box-auto-generated-alias-__uniqid__.phar/.box/bin/check-requirements.php';
\$_SERVER['SCRIPT_FILENAME'] = 'phar://box-auto-generated-alias-__uniqid__.phar/index.php';
require 'phar://box-auto-generated-alias-__uniqid__.phar/index.php';
__HALT_COMPILER(); ?>
Expand Down Expand Up @@ -674,6 +676,7 @@ public function test_it_can_build_a_phar_with_complete_mapping(): void
Phar::mapPhar('alias-test.phar');
\$_SERVER['SCRIPT_FILENAME'] = 'phar://alias-test.phar/run.php';
require 'phar://alias-test.phar/run.php';
__HALT_COMPILER(); ?>
Expand Down Expand Up @@ -772,6 +775,7 @@ public function test_it_can_build_a_phar_with_complete_mapping_without_an_alias(
Phar::mapPhar('box-auto-generated-alias-__uniqid__.phar');
\$_SERVER['SCRIPT_FILENAME'] = 'phar://box-auto-generated-alias-__uniqid__.phar/run.php';
require 'phar://box-auto-generated-alias-__uniqid__.phar/run.php';
__HALT_COMPILER(); ?>
Expand Down
4 changes: 4 additions & 0 deletions tests/StubGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public static function valuesProvider(): iterable
require 'phar://' . __FILE__ . '/.box/bin/check-requirements.php';
$_SERVER['SCRIPT_FILENAME'] = 'phar://' . __FILE__ . '/acme.php';
require 'phar://' . __FILE__ . '/acme.php';
__HALT_COMPILER(); ?>
Expand All @@ -209,6 +210,7 @@ public static function valuesProvider(): iterable
<<<'STUB'
<?php
$_SERVER['SCRIPT_FILENAME'] = 'phar://' . __FILE__ . '/acme.php';
require 'phar://' . __FILE__ . '/acme.php';
__HALT_COMPILER(); ?>
Expand Down Expand Up @@ -273,6 +275,7 @@ public static function valuesProvider(): iterable
require 'phar://test.phar/.box/bin/check-requirements.php';
$_SERVER['SCRIPT_FILENAME'] = 'phar://test.phar/index.php';
require 'phar://test.phar/index.php';
__HALT_COMPILER(); ?>
Expand Down Expand Up @@ -306,6 +309,7 @@ public static function valuesProvider(): iterable
Phar::mapPhar('test.phar');
Phar::interceptFileFuncs();
$_SERVER['SCRIPT_FILENAME'] = 'phar://test.phar/index.php';
require 'phar://test.phar/index.php';
__HALT_COMPILER(); ?>
Expand Down

0 comments on commit c91e793

Please sign in to comment.