cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[BUG]: Register CLI application throwing warning

Open dz3n opened this issue 4 years ago • 9 comments

Describe the bug Throwing deprecated warning

To Reproduce Based on documentation, to register CLI application code like this is required:

//cli.php
$console = new Console($container);

$arguments = [];
foreach ($argv as $k => $arg) {
    if ($k === 1) {
        $arguments['task'] = $arg;
    } elseif ($k === 2) {
        $arguments['action'] = $arg;
    } elseif ($k >= 3) {
        $arguments['params'][] = $arg;
    }
}
$console->handle($arguments);

Which cause this kind of warning

Deprecated: Phalcon\Dispatcher\AbstractDispatcher::setActionName(): Passing null to parameter #1 ($actionName) of type string is deprecated in /var/www/application/apps/crontabs/cli.php on line 283

Details

  • Phalcon version: 5.0.4
  • PHP Version: PHP 8.1.7
  • Operating System:
  • Installation type: Ubuntu + Docker
  • Server: Nginx

dz3n avatar Oct 26 '22 09:10 dz3n

Could please specify what is on line 283 in cli.php file?

Jeckerson avatar Oct 26 '22 15:10 Jeckerson

Could please specify what is on line 283 in cli.php file?

I've edited my previous post.

//line 283
$console->handle($arguments);

Thanks!

dz3n avatar Oct 26 '22 17:10 dz3n

@dz3n If possible can you also post the task you are trying to run? (the task class) as well as the command you are invoking in the CLI?

niden avatar Oct 26 '22 17:10 niden

I have the same problem. I solved it by always passing the second parameter, even though it's mainAction

Alistar84 avatar Oct 26 '22 18:10 Alistar84

@dz3n If possible can you also post the task you are trying to run? (the task class) as well as the command you are invoking in the CLI?

@niden

The task class doesn't matter really

the command is as follow: /usr/local/bin/php /var/www/application/apps/crontabs/cli.php taskclass

without any action and params arguments

If I change it with second param like this /usr/local/bin/php /var/www/application/apps/crontabs/cli.php taskclass main

I don't get warnings, so @Alistar84 is right, I need to pass second argument in order to get rid of this error

dz3n avatar Oct 26 '22 19:10 dz3n

@dz3n I am having some trouble recreating this.

First of all in your code you can make a small change so that you don't have to run each command with main. Look at this:

//cli.php
$console = new Console($container);

$arguments = [
    'action' => 'main',
];
foreach ($argv as $k => $arg) {
    if ($k === 1) {
        $arguments['task'] = $arg;
    } elseif ($k === 2) {
        $arguments['action'] = $arg;
    } elseif ($k >= 3) {
        $arguments['params'][] = $arg;
    }
}
$console->handle($arguments);

Now for my test I am using the tasks in our testing suite. My cli.php is as follows:

<?php
declare(strict_types=1);

error_reporting(E_ALL);
ini_set("display_errors", 'On');

use Phalcon\Cli\Console;
use Phalcon\Cli\Dispatcher;
use Phalcon\Di\FactoryDefault\Cli as CliDI;

require_once 'vendor/autoload.php';

$container  = new CliDI();
$dispatcher = new Dispatcher();

$dispatcher->setDefaultNamespace('Phalcon\Tests\Fixtures\Tasks');
$container->setShared('dispatcher', $dispatcher);

$console = new Console($container);

$arguments = [];
foreach ($argv as $k => $arg) {
    if ($k === 1) {
        $arguments['task'] = $arg;
    } elseif ($k === 2) {
        $arguments['action'] = $arg;
    } elseif ($k >= 3) {
        $arguments['params'][] = $arg;
    }
}

try {
    $console->handle($arguments);
} catch (\Exception $e) {
    fwrite(STDERR, $e->getMessage() . PHP_EOL);
    exit(1);
} catch (Throwable $throwable) {
    fwrite(STDERR, $throwable->getMessage() . PHP_EOL);
    exit(1);
}

Then I run (in a localr docker environment)

php -d extension=ext/modules/phalcon.so aa.php params

This executes the ParamsTask (tests/_data/fixtures/Tasks/ParamsTask.php) which does not have a mainAction in it. The result I get back is as expected:

Action 'main' was not found in handler 'params'

If I run it for the EchoTask (tests/_data/fixtures/Tasks/EchoTask.php) and modify that task slightly to echo 'Hello' in the main action I get

php -d extension=ext/modules/phalcon.so aa.php echo
Hello

niden avatar Oct 28 '22 18:10 niden

I am not sure why this happens and I cannot get this to replicate. Feel free to email me directly ([email protected]) so that we can arrange some sort of screen sharing to figure this out. Alternatively, if this is not feasible, have a look above for any changes between my code and yours and let me know.

niden avatar Oct 28 '22 18:10 niden

@dz3n never mind I managed to reproduce it

niden avatar Oct 28 '22 18:10 niden

Resolved in https://github.com/phalcon/cphalcon/pull/16189

Thank you @dz3n

niden avatar Oct 31 '22 15:10 niden