box icon indicating copy to clipboard operation
box copied to clipboard

Working Symfony 6 example?

Open imper86 opened this issue 2 years ago • 1 comments

Hello,

I just started having fun with Box and tried to compile simple (empty) symfony 6 application. After successfully compiling I have an error:

PHP Warning:  Constant  already defined in /home/imper/projects/phartest/build/app.phar on line 16
PHP Fatal error:  Uncaught TypeError: Invalid return value: callable object expected, "int" returned from "./build/app.phar". in phar:///home/imper/projects/phartest/build/app.phar/vendor/autoload_runtime.php:12
Stack trace:
#0 phar:///home/imper/projects/phartest/build/app.phar/bin/console(10): require_once()
#1 /home/imper/projects/phartest/build/app.phar(14): require('...')
#2 {main}
  thrown in phar:///home/imper/projects/phartest/build/app.phar/vendor/autoload_runtime.php on line 12

I'm stuck, don't know what I should do with this. Do you have any working Symfony 6 example repository? Should I maybe modify bin/console somehow?

imper86 avatar Mar 27 '22 09:03 imper86

Hello,

I had the same error and I found this issue: https://github.com/symfony/symfony/issues/41771

I did the following modifications and it worked for me.

bin/console:

#!/usr/bin/env php
<?php

if (!is_file(dirname(__DIR__).'/config/autoload_runtime.php')) {
    throw new LogicException('Symfony Runtime is missing. Try running "composer require symfony/runtime".');
}

require_once dirname(__DIR__).'/config/autoload_runtime.php';

config/autoload_runtime.php

<?php

use App\Kernel;
use Symfony\Bundle\FrameworkBundle\Console\Application;

if (true === (require_once __DIR__.'/../vendor/autoload.php')) {
    return;
}

$app = function (array $context) {
    $kernel = new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
    return new Application($kernel);
};

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'] ?? 'Symfony\\Component\\Runtime\\SymfonyRuntime';
$runtime = new $runtime(($_SERVER['APP_RUNTIME_OPTIONS'] ?? $_ENV['APP_RUNTIME_OPTIONS'] ?? []) + [
        'project_dir' => dirname(__DIR__, 1),
    ]);

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

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

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

Braxilior avatar May 05 '22 09:05 Braxilior

I've created https://github.com/symfony/symfony/issues/49443 to workout a solution.

theofidry avatar Feb 19 '23 13:02 theofidry

(commenting here because for some reasons I currently cannot comment on the PR itself, I'm getting a 500...)

Thank you for the PR (#881) @aradoje.

I'm unfortunately not going to merge this immediately for a few reasons:

  • I would like to add an E2E test within Box to ensure it works fine with Symfony6, with or without the suggested solution (see #886)
  • I think the current suggested solution is sub-par: when you end up doing is circumventing the Symfony runtime component: so it will work within the PHAR, but the regular console script no longer really use the runtime component. IMO a cleaner solution would be to remove the runtime component altogether.*

*: I opened https://github.com/symfony/symfony/issues/49443 because I do think the runtime component should just work with PHARs out of the box, rather than requiring more fiddling.

theofidry avatar Feb 19 '23 13:02 theofidry

Thanks, sounds good 👍🏻

aradoje avatar Feb 19 '23 17:02 aradoje