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

Dynamic invocation supported ? #1046

Open
henrywood opened this issue Jul 17, 2024 · 0 comments
Open

Dynamic invocation supported ? #1046

henrywood opened this issue Jul 17, 2024 · 0 comments

Comments

@henrywood
Copy link

henrywood commented Jul 17, 2024

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 !

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

1 participant