valitron icon indicating copy to clipboard operation
valitron copied to clipboard

Valitron doesn't work in phar

Open Moult opened this issue 5 years ago • 2 comments

When it is packaged in a phar, it gets upset.

Message: Fail to load language file 'phar:///path/to/my.phar/vendor/vlucas/valitron/lang/en.php'
File: phar:///path/to/my.phar/vendor/vlucas/valitron/src/Valitron/Validator.php
Line: 113

The lines in question:

        if (stream_resolve_include_path($langFile)) {
            $langMessages = include $langFile;
            static::$_ruleMessages = array_merge(static::$_ruleMessages, $langMessages);
        } else {
            throw new \InvalidArgumentException("Fail to load language file '" . $langFile . "'");
        }

It seems as though stream_resolve_include_path returns false when in a phar, because $langDir returns a phar:///path/to/lang/.

Maybe is_file is a solution?

Moult avatar May 24 '20 12:05 Moult

Can you check if it works when you replace

if (stream_resolve_include_path($langFile)) {

with

if (stream_resolve_include_path(realpath($langFile))) {

?

willemwollebrants avatar May 25 '20 12:05 willemwollebrants

@willemwollebrants you are correct, that'll fix it. But I'm not sure if it's the right way to fix it.

E.g.

$langFile = 'phar:///path/to/my.phar/vendor/vlucas/valitron/lang/en.php';
var_dump(realpath($langFile)); // bool(false) 
var_dump(stream_resolve_include_path(realpath($langFile)); // string "phar:///path/to/my.phar/vendor/" 

Moult avatar May 26 '20 00:05 Moult