Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phalcon CLI task name

Tags:

php

phalcon

I have folder tasks with MainTask.php and SomeOtherTask.php files...

How can I run only the SomeOtherTask?

To run the main task I only need to write

php cli.php

, but I want some serious file name and not MainTask...

Here is my other task:

<?php
use Phalcon\Cli\Task;

class DeleteExpiredAuthTokens extends Task{

    public function mainAction()
    {
        echo "Hello world war Z";
    }
}
like image 773
Alexandru Circus Avatar asked Jan 04 '23 16:01

Alexandru Circus


1 Answers

Directory set-up

You will need to create a tasks/ directory in app/, then you can make appropriate file names; for example;

app/
├── tasks/
│   ├── SomeTask.php
│   ├── SomeOtherTask.php
│
├── cli.php

Now, you'd need to create a cli.php file, which your command line will call. In here, you'd need to look at the call arguments and pass it in your $console->handle() method. For example;

cli.php

<?php

use Phalcon\Di\FactoryDefault\Cli as CliDI;
use Phalcon\Cli\Console as ConsoleApp;
use Phalcon\Loader;


// Using the CLI factory default services container
$di = new CliDI();

$arrBasePath = preg_split("/\//", __DIR__);
$limit = sizeof($arrBasePath);

$strBasePath = "";
for ($count = 0; $count < $limit - 1; $count++) {
    $strBasePath .= $arrBasePath[$count] . '/';
}

define('BASE_PATH', $strBasePath);

/**
 * Register the autoloader and tell it to register the tasks directory
 */
$loader = new Loader();

$loader->registerDirs(
    [
        __DIR__ . "/tasks",
        __DIR__ . "/models",
        __DIR__ . "/controllers"
    ]
);

$loader->register();

// Create a console application
$console = new ConsoleApp();
$console->setDI($di);

//Process the console arguments
$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 {
    // Handle incoming arguments
    $console->handle($arguments);
} catch (\Phalcon\Exception $e) {
    echo $e->getMessage();
    exit(255);
}

Now, say in app/tasks/SomeTask.php you have the following;

app/tasks/SomeTask.php

use Phalcon\Cli\Task;

class SomeTask extends Task
{

    public function MainAction()
    {
        echo "This is the main action\r\n";
    }

    public function OtherAction()
    {
        echo "This is other action\r\n";
    }

}

You can just call your tasks by passing them in the arguments, for example;

$ php cli.php some
This is the main action

Or, call the other method by passing it;

$ php cli.php some other
This is other action

Notes

  • Your class name needs to end in Task. When calling this class, you omit it. See how I called the someTask class.
  • In your example, rename DeleteExpiredAuthTokens to become DeleteExpiredAuthTokensTask otherwise you'll get the following error message: DeleteExpiredAuthTokensTask handler class cannot be loaded
like image 161
ʰᵈˑ Avatar answered Jan 08 '23 05:01

ʰᵈˑ