Namespaced tasks in cli – why not working?

Hello! How to use tasks in namespace?

/path/to/myapp$ tree -L 2 ..
..
|-- models
|   |-- Base.php
|   |-- Users.php
|-- private
|   `-- cli.php
|-- public
|   `-- index.php
|-- tasks
|   |-- MainTask.php
|   `-- TestTask.php
..
/path/to/myapp/private$ cat cli.php
<?php

define('ROOT_PATH', dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR);

// Load Phalcon
$loader = new \Phalcon\Loader();
$loader
    ->registerNamespaces([
        'MyApp\Models'      => ROOT_PATH . 'models/',
        'MyApp\Controllers' => ROOT_PATH . 'controllers/',
        'MyApp\Tasks'       => ROOT_PATH . 'tasks/', // Does not working
    ])
    ->registerDirs([
        ROOT_PATH . 'models/',
        ROOT_PATH . 'controllers/',
        ROOT_PATH . 'tasks/',
    ])
    ->register()
;

// Load Composer
require_once ROOT_PATH . 'vendor/autoload.php';

// App
$cli = new \Phalcon\CLI\Console();
$cli->setDI($di);

// Parse args
$args   = [];
$params = [];
foreach ($argv as $k => $arg)
    if ($k == 1)
        $args['task'] = $arg;
    elseif ($k == 2)
        $args['action'] = $arg;
    elseif ($k >= 3)
        $params[] = $arg;
if (count($params) > 0)
    $args['params'] = $params;

var_export($loader); // Registering is OK:  \Phalcon\Loader::_registered == true

// Run app
$cli->handle($args);

If MainTask is not namespaced – works perfectly.

But if i just add namespace (MyApp\Tasks) to any task – i get an exception: "Phalcon\CLI\Dispatcher\Exception: MainTask handler class cannot be loaded".

How to solve this? Extending framework? I don't know C/C++ and can't see sources ):

Thanks!

Namespaces are a php thing, it should work regardless of you using phalcon or not. Can you show us the MainTask.php code?



10.4k
edited Dec '14

Sure.

tasks/MainTask.php

<?php

/**
 * With this line i get an exception:
 *     Phalcon\CLI\Dispatcher\Exception: MainTask handler class cannot be loaded in /path/to/project/private/cli.php on line 36
 * Without this line all is ok – i get echoed strings.
 */
//namespace Api\Tasks;

class MainTask extends Phalcon\CLI\Task
{
    public function mainAction()
    {
        echo "\n\tUsage: $ php cli.php [task] [action] [args ...]\n\n";
        echo "\tAvaiable tasks:\n";
        echo "\t\t– migration\n";
    }
}

I use namespaced models too and they are working as expected. So i think that i'm missing something and using this "phalcon thing" wrong.



4.9k

I am having the same Problem. Could you solve this? Any suggestions anybody ?



4.9k
edited Jan '15

Sorry, but your answer does not match the question here. The question is "Why Namespaced tasks in cli not working" and not "Why apc is not working on CLI"



10.4k

Oops, sorry. You're right,my bad. So, i've deleted my comment to set this post as unanswered again.

Sorry, but your answer does not match the question here. The question is "Why Namespaced tasks in cli not working" and not "Why apc is not working on CLI"



4.9k

Post is still marked as "solved" :(



10.4k
edited Jan '15

It's temporarily. Maybe, cache in this forum? Same situation was with another post recently and delete accepted comment make topic un-accept again but after some time.

Example is here. You can see same situation by reading comments.

Post is still marked as "solved" :(

edited Mar '15

In your call to $cli->handle($args), this should be changed to $cli->handle(array("task" => "MyApp\\Tasks\\Main")), otherwise the class name becomes ambiguous.



10.4k
edited Mar '15

Thank you!

I've wrote simple bicycle before send it into Console::handle() and namespaces work now!

// Namespaced tasks
$task         = !empty($args['task']) ? $args['task'] : 'main';
$task         = ucfirst($task);
$task         = "MyApp\\Tasks\\$task";
$args['task'] = $task;

But it requires (why?) names of tasks with "Task" postfix. So, i can't have "App\Tasks\Generate" task but can "App\Tasks\GenerateTask" )-:

In your call to $cli->handle($args), this should be changed to $cli->handle(array("task" => "MyApp\Tasks\Main")), otherwise the class name becomes ambiguous.

edited Mar '15

It's the convention that Phalcon uses, so every object you can create that extends Cli\Task has to have the "Task" suffix; this is true of actions as well, they require "Action".