4

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";
    }
}
3
  • I tried php cli.php SomeOther but is not working (SomeOther wihtout the suffix Task) Jan 26, 2017 at 13:11
  • And you sure that your task have mainAction?
    – Juri
    Jan 26, 2017 at 13:25
  • +Juri I've pasted the task..please see the edited post Jan 26, 2017 at 13:28

1 Answer 1

9

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
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.