phug-php / phug

Phug - The Pug Template Engine for PHP
https://phug.selfbuild.fr
MIT License
62 stars 3 forks source link

Cannot run phug CLI #81

Closed alessandro-fazzi closed 3 years ago

alessandro-fazzi commented 3 years ago

Hello,

I encountered an issue with the following code:

./vendor/bin/phug compile-file aFile.php

I expected to get: a compiled file and a 0 exit code

But I actually get: 1 exit code with the error

PHP Warning:  call_user_func_array() expects parameter 1 to be a valid callback, class 'Phug' not found in /[...]/vendor/phug/phug/src/Phug/Phug/Phug/Cli.php on line 182

I can't get if in https://github.com/phug-php/phug/blob/f4e169a92711f6bc33fbdb4ce64d63b159a017c6/phug#L22 we should have

$cli = new \Phug\Cli('Phug\Phug', [

or if in https://github.com/phug-php/phug/blob/f4e169a92711f6bc33fbdb4ce64d63b159a017c6/src/Phug/Phug/Phug/Cli.php#L40 we should have

$this->facade = __NAMESPACE__.'\'.$facade;

or if it's just a problem with my setup.

Thanks!

kylekatarnls commented 3 years ago

Hello,

Phug without namespace is also available: https://github.com/phug-php/phug/blob/master/src/Phug/Phug/Phug.php

But still we could 'Phug\Phug' instead of using the alias.

You're the first to report such trouble. So there is likely something preventing the alias Phug to properly autoload Phug\Phug or maybe it's something new in composer.

kylekatarnls commented 3 years ago

Hello, I tried with composer 1 and 2 and PHP 7.4 and 8. Can you provide installation steps from scratch, PHP, composer and phug version so I can try to reproduce it. I will re-open then. Thanks.

alessandro-fazzi commented 3 years ago

@kylekatarnls

PHP 7.2.34 Composer version 2.0.13

{
    "require": {
        "pug-php/pug": "^3.0",
        "mobiledetect/mobiledetectlib": "^2.8",
        "symfony/validator": "^5.0"
    },
    "config": {
        "classmap-authoritative": true
    }
}
 composer show                                                                                                                                                             262ms
js-phpize/js-phpize                     2.8.4   Convert js-like syntax to standalone PHP code.
js-phpize/js-phpize-phug                2.2.1   Convert js-like syntax to standalone PHP code in Phug template.
js-transformer/js-transformer           1.0.0  
mobiledetect/mobiledetectlib            2.8.37  Mobile_Detect is a lightweight PHP class for detecting mobile devices. It uses the User-Agent string combined with specific ...
nodejs-php-fallback/nodejs-php-fallback 1.5.2   Allow you to call node.js module or scripts throught PHP and call a fallback function if node.js is not available
phug/js-transformer-filter              1.2.0   Allow to use any js-transformer as filter in Phug
phug/phug                               1.8.2   Pug (ex-Jade) facade engine for PHP, HTML template engine structured by indentation
pug-php/pug                             3.4.1   HAML-like template engine for PHP
symfony/deprecation-contracts           v2.4.0  A generic function and convention to trigger deprecation notices
symfony/polyfill-ctype                  v1.22.1 Symfony polyfill for ctype functions
symfony/polyfill-mbstring               v1.22.1 Symfony polyfill for the Mbstring extension
symfony/polyfill-php73                  v1.22.1 Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions
symfony/polyfill-php80                  v1.22.1 Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions
symfony/translation-contracts           v2.4.0  Generic abstractions related to translation
symfony/validator                       v5.2.7  Provides tools to validate values
symfony/var-dumper                      v5.2.7  Provides mechanisms for walking through any arbitrary PHP variable
require_once Wordless::join_paths(dirname(__FILE__), '../vendor/autoload.php');

// somewhere else
$pug = new \Pug(WordlessPugOptions::get_options());
$pug->displayFile($template_path, $locals);

and it throws

Fatal error: Uncaught Error: Class 'Pug' not found in [...]/wp-content/plugins/wordless/wordless/helpers/render_helper.php

while using \Pug\Pug works. I know this is Pug and not Phug but following the documentation we should have a root level Pug too. Since the problem sounded related, I'm reporting this case too.


Speaking about the cli ./vendor/bin/phug compile throws

class 'Phug' not found in [...]/wp-content/plugins/wordless/vendor/phug/phug/src/Phug/Phug/Phug/Cli.php on line 182

as per original posting.


Anyway: I've reported this for the sake of completeness, but if it's a problem on my own env I'd don't ask for your time more than this :)

kylekatarnls commented 3 years ago

I think there is something different in the wordpress autoload aggregation.

But then I think I can reproduce and CLI support for WordPress worth to take a look. :)

alessandro-fazzi commented 3 years ago

@kylekatarnls I'm not sturdy enough on the matter, but it seems to me that the classmap-authoritative could be my "problem".

If you're going to ask why I've enabled the authoritative classmap method: optimization. As studied in https://getcomposer.org/doc/articles/autoloader-optimization.md (considering I've studied it the right way :P)

Would be someway feasible to use the autoload.files feature in "your" composer.json in order to require the file with the class_alias? Or am I misusing the classmap-authoritative?

Whatever: thanks for your time and support.

kylekatarnls commented 3 years ago

Hello, I installed a new wordless app from scratch and was able to reproduce it.

However it works fine ./vendor/bin/pug works fine. Is there a particular reason for using phug over pug in your case? If not you can go safely using pug.

alessandro-fazzi commented 3 years ago

That's true. Actually it's a solution for me since I'm switching to pug in order to have the :php filter preloaded.

By the way configuring mine composer.json with

{
    "require": {
        "pug-php/pug": "^3.0",
        "mobiledetect/mobiledetectlib": "^2.8",
        "symfony/validator": "^5.0"
    },
    "config": {
        "classmap-authoritative": true
    },
    "autoload": {
        "files": [ "vendor/phug/phug/src/Phug/Phug/Phug.php" ]
    }
}

Was solving the issue.

Looking at "my own business" we could consider this solved, but there's still the scenario where someone else could use classmap-authoritative and could be bound to phug.

Thanks for the check, thanks for the tip and thanks for the time :)

kylekatarnls commented 3 years ago

This is also fixed in dev-master: https://github.com/phug-php/phug/pull/82 so next release will have it.