Skip to content

Dynamic invocation supported ? #1046

@henrywood

Description

@henrywood

Sorry for keeping opening issues, but I an trying to figure out if it would be feasible for me to use KPHP, but it is hard to know what is supported when compilation does not work (See #1045 regarding the compilation problem)

In a CLI application that I need to make KPHP compatible, an App class has this method:

    public function run(array $argv): void
    {
        $key = mb_strtolower($argv[1] ?? 'default'); // Are multibyte functions supported ?

        $findArr = array_filter($this->commands, static fn(Command $cmd): bool => mb_strtolower($cmd->key) === $key);
        $command = array_values($findArr)[0] ?? null;

        if ($command === null && $key === self::HELP_CMD) {
            $this->helper();
            return;
        }

        $command ?? throw new InvalidArgumentException('Command not found.');
        $this->writeAppHeader($command);

        // Handle argv
        $argv = array_splice($argv, 1);
        $context = new Context(
            params: $this->parseParams(
                $command, 
                [
                    'cmd',
                    ...$argv, /* What about variadic arguments ? Are they supported */
                ],
            ),
        );

        if (isset($command->service['handler']) && is_callable($command->service['handler'])) {
            $command->service['handler']($context);
            return;
        }

        if (is_string($command->service['instance'])) {
            $instance = new ($command->service['instance'])(); // Is this syntax supported - I tend to say yes due to #726 being merged ?
        } elseif (is_callable($command->service['instance'])) { // since is_callable() appears to be missing in built-in/functions.txt I guess this won't work or ?
            $instance = $command->service['instance']();
        } else {
            throw new InvalidArgumentException('Invalid service provided to command '.$key);
        }

        if (!method_exists($instance, $command->service['entrypoint'])) {    // What about method_exists() ? Is that supported ?
            throw new InvalidArgumentException('Invalid entrypoint provided to command '.$key);
        }

        $instance->{$command->service['entrypoint']}($context);
    }

Both $command->service['entrypoint'] (a string) - example: 'run' ,
and
$command->service['instance'] (a string such typically produced from Foo::class)
are known at compile-time

Please inform me !

Thanks !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions