Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file_exists(): Unable to find the wrapper "phar" #410

Closed
hazzy opened this issue Mar 18, 2023 · 10 comments
Closed

file_exists(): Unable to find the wrapper "phar" #410

hazzy opened this issue Mar 18, 2023 · 10 comments

Comments

@hazzy
Copy link

hazzy commented Mar 18, 2023

Hi All,

Getting this error message in a Magento script using Kint5.0.1. I am sure Phar is configured in PHP (8.1) running on Apache2. Tried Kint4 but the same thing

Warning: file_exists(): Unable to find the wrapper "phar" - did you forget to enable it when you configured PHP? ..../kint5/kint.phar/init_phar.php on line 41

Fatal error: Uncaught Error: Class "Kint\CallFinder" not found in phar:///...../kint5/kint.phar/src/Kint.php:661 Stack trace: #0 phar:///....../kint5/kint.phar/src/Kint.php(449): Kint\Kint::getSingleCall() #1 phar:///...../kint5/kint.phar/src/Kint.php(552): Kint\Kint::getCallInfo() #2 ...../testShowProducts.php(32): Kint\Kint::dump() #3 {main} thrown in phar:///......../kint5/kint.phar/src/Kint.php on line 661

Any suggestions please. I have hunted the internet to no avail so far for a solution (..... replaces the full path name for privacy) -
Thanks very much.

@jnvsor
Copy link
Member

jnvsor commented Mar 18, 2023

Well first off, you should probably replace the path in your other stack frames too.

Anyway, phar is enabled (or disabled) at compile time in PHP, it's not something you can just "Turn on" and since the phar file wrapper is missing it looks like your PHP was compiled without it.

You can run phpinfo() in your script to see a bunch of compilation information. Under "Registered PHP Streams" phar should be present, and in the phar section it should be enabled all over.

Alternatively, you can add kint to your composer, or make a separate composer in a subfolder with only kint in it and require that autoloader beside your existing one

@hazzy
Copy link
Author

hazzy commented Mar 19, 2023

Thanks @jnvsor.

I did check phpinfo() before and Phar is enabled. See extract below,

Phar
Phar: PHP Archive support enabled
Phar API version 1.1.1
Phar-based phar archives enabled
Tar-based phar archives enabled
ZIP-based phar archives enabled
gzip compression enabled
bzip2 compression enabled
Native OpenSSL support enabled
Phar based on pear/PHP_Archive, original concept by Davey Shafik.
Phar fully realized by Gregory Beaver and Marcus Boerger.
Portions of tar implementation Copyright (c) 2003-2009 Tim Kientzle.

@jnvsor
Copy link
Member

jnvsor commented Mar 19, 2023

Did you check "Registered PHP streams"? PHP is loading the phar fine, but the file_exists call in the autoloader is failing because the stream isn't registered for some reason (Which makes me wonder how it got there in the first place since the stub is requiring the autoloader through phar://)

But this makes no sense, if the phar stream really wasn't enabled then it shouldn't have been able to autoload far enough to cause the error... Can you try downloading the latest phar and see what that does? Maybe you just have a corrupt phar file. Since no-one else has this issue (And a failing autoloader would be rather important) I'm inclined to think the problem's on your end somewhere

@hazzy
Copy link
Author

hazzy commented Mar 19, 2023

Yes @jnvsor I checked that:-
Registered PHP Streams | https, ftps, compress.zlib, php, file, glob, data, http, ftp, compress.bzip2, phar, zip

I don't think the Phar file is corrupt. I downloaded it from here yesterday. And as i say above the same thing happens with Kint 4.

I think the problem is an interaction with Magento (since the Magento 2.4 upgrade).

I added the statement
d('Dumped with Kint');
immediately after the include Kint statement
require_once (realpath(dirname(__FILE__)) . DS . 'kint5' . DS .'kint.phar');

and got this output:-
string (16) "Dumped with Kint"
Called from .../testShowProducts.php:20
So kint is working

Then a bit further down the script I initialise the Magento environment

//Load Magento core
use Magento\Framework\App\Bootstrap;
require (APP_DIR . DS . 'app' . DS .'bootstrap.php');
  
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
kint::dump($state);

and the kint::dump($state) produces the error.

@jnvsor
Copy link
Member

jnvsor commented Mar 19, 2023

When you put it that way it makes perfect sense if magento is blowing away the phar autoloader. What happens if you include the phar after magento bootstraps?

@hazzy
Copy link
Author

hazzy commented Mar 19, 2023

Good idea - i will trry.....

Nope - same error.

@jnvsor
Copy link
Member

jnvsor commented Mar 19, 2023

So if you first include the phar and dump something right after that after your $state->setAreaCode call it still doesn't work?

Does phpinfo() still say phars are enabled after the magento bootstrap? Maybe it's disabling them for security or something

@hazzy
Copy link
Author

hazzy commented Mar 19, 2023

Exactly.

phpinfo() reports phar is enabled after the script runs.

Can i run kint outside of a Phar somehow?

@jnvsor
Copy link
Member

jnvsor commented Mar 19, 2023

Should have checked the stream handler: magento/magento2#21973

So they deliberately disable the phar stream handler for security reasons. Apparently you can use stream_wrapper_restore to fix it

@hazzy
Copy link
Author

hazzy commented Mar 19, 2023

Ok great - well spotted! Thanks for your help. Will try that.

Surprised it's not come up here before because that dates back to Magento 2.3.1 in 2019

Revised working code

//Load Magento core
use Magento\Framework\App\Bootstrap;
require (APP_DIR . DS . 'app' . DS .'bootstrap.php');
  
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

stream_wrapper_restore('phar'); // Restore the Phar stream that Magento app/bootstrap.php disables
kint::dump($state); // Now working

@jnvsor jnvsor closed this as not planned Won't fix, can't repro, duplicate, stale Mar 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants