phpro / grumphp-shim

This repository provides easy way to install GrumPHP without the risk of conflicting dependencies.
MIT License
24 stars 3 forks source link

Task implementing AbstractExternalTask is not working #10

Closed piegus closed 1 year ago

piegus commented 3 years ago

WHen I created custom task and implement TskInterface. I saw that in shim version its chainging names to Humbug. Is tehre a way to create custom tasks and still use grumphp-shim??

obraz

$:~/projects/company$ dphp ./bin/grumphp run --tasks=very_custom_phpstan 
PHP Fatal error:  Uncaught TypeError: Return value of VeryCustom\Bundle\CoreBundle\GrumPHP\Task\VeryCustomPhpStan::getConfigurableOptions() must be an instance of _HumbugBox8bc289e4b8e0\Symfony\Component\OptionsResolver\OptionsResolver, instance of Symfony\Component\OptionsResolver\OptionsResolver returned in /var/www/company/src/VeryCustom/Bundle/CoreBundle/GrumPHP/Task/VeryCustomPhpStan.php:47
Stack trace:
#0 phar:///var/www/company/vendor/phpro/grumphp-shim/grumphp.phar/src/Configuration/Resolver/TaskConfigResolver.php(50): VeryCustom\Bundle\CoreBundle\GrumPHP\Task\VeryCustomPhpStan::getConfigurableOptions()
#1 phar:///var/www/company/vendor/phpro/grumphp-shim/grumphp.phar/src/Configuration/Resolver/TaskConfigResolver.php(32): GrumPHP\Configuration\Resolver\TaskConfigResolver->fetchByName('very_custom_phpstan')
#2 phar:///var/www/company/vendor/phpro/grumphp-shim/grumphp.phar/src/Configuration/Compiler/TaskCompilerPass.php(48): GrumPHP\Configuration\Resolver\TaskConfigResolver->resolve('very_custom_phpstan', Array)
#3 phar:///var/www/company/vendor/phpro/grumphp-shim/grumphp.phar/src/Tas in /var/www/company/src/VeryCustom/Bundle/CoreBundle/GrumPHP/Task/VeryCustomPhpStan.php on line 47
veewee commented 3 years ago

Ah yes, we currently prefix all those things to make sure it doesnt conflict with your vendor dependencies. I am currently not sure how I could solve this issue. Will need to dive into it.

Thanks for reporting!

leonardfischer commented 3 years ago

Having the same issue :( Dies this exist in every version of the phar?

veewee commented 3 years ago

I think it's in issue that has always been there @leonardfischer. It's probably enough to whitelist the options resolver from being prefixed, but sadly haven't found the time to test it out yet.

piegus commented 3 years ago

Or change the return type of ::getConfigurableOptions() to return array instead otionsresolver

veewee commented 3 years ago

That's not how that interface works though ;)

matthijs-va commented 2 years ago

I found a dirty workaround for this problem by dynamically figuring out the prefix and then using eval to define a base class for my custom tasks:

// Get the HumbugBox prefix.
$prefix = null;
foreach (get_declared_classes() as $class) {
    if (strpos($class, '\Symfony\Component\\') === false) {
        continue;
    }

    $matches = null;
    if (!preg_match('#^(_HumbugBox[a-z0-9]+)#', $class, $matches)) {
        continue;
    }

    $prefix = $matches[1];
    break;
}

$class = <<<CLASS
namespace MyPackage\GrumPHP\Task;

use $prefix\Symfony\Component\OptionsResolver\OptionsResolver;
use GrumPHP\Task\TaskInterface;

abstract class TaskBase implements TaskInterface {
    public static function getConfigurableOptions(): OptionsResolver
    {
        \$resolver = new OptionsResolver();

        static::configureOptionsResolver(\$resolver);

        return \$resolver;
    }

    abstract protected static function configureOptionsResolver(\$resolver): void;
}
CLASS;

eval($class);

Far from ideal, but it works. But then the next problem popped up, Amp couldn't find my task class (because it's not available in the phar's autoloader). It seems that defining custom tasks is simply impossible when using the shim version.

jackbentley commented 1 year ago

You can get a custom task working with grumphp-shim by patching the phar file with the task.

It's far from ideal, but works. Here's an example patching script. You need to enable writing to phar files. You can then add it to the composer post install commands to automatically patch the phar.

https://gist.github.com/jackbentley/3da7d39ee842b851b36efcb30cbad027

veewee commented 1 year ago

This will be fixed in grumphp v2. More info, see https://github.com/phpro/grumphp/pull/1090

Feel free to play around with it.