Hi..
I have a similer question.
I have injected a service under my cli.php called beanstalk and extending \Phalcon\CLI\Task perfectly.
My question is as per doc (https://docs.phalcon.io/en/latest/api/Phalcon_CLI_Task.html), Phalcon\CLI\Task extends Phalcon\DI\Injectable and implements Phalcon\Events\EventsAwareInterface, Phalcon\DI\InjectionAwareInterface so why i am not getting my service if i am calling $this->getDI(''beanstalk). As per doc, \Phalcon\CLI\Task is already having a method called getDI through which i can get my service.
cli.php
<?php
use Phalcon\DI\FactoryDefault\CLI as CliDI,
Phalcon\CLI\Console as ConsoleApp;
define('VERSION', '1.0.0');
//Using the CLI factory default services container
$di = new CliDI();
// Define path to application directory
defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__)));
/**
* Register the autoloader and tell it to register the tasks directory
*/
$loader = new \Phalcon\Loader();
$loader->registerDirs(
array(
APPLICATION_PATH . '/tasks',
APPLICATION_PATH . '/models'
)
);
$loader->register();
// Load the configuration file (if any)
if (is_readable(APPLICATION_PATH . '/config/config.php')) {
$config = include APPLICATION_PATH . '/config/config.php';
$di->set('config', $config);
}
$config = $di->get('config');
//Setup the database service
$di->set('db', function() use ($config) {
return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
"host" => $config->database->host,
"username" => $config->database->username,
"password" => $config->database->password,
"dbname" => $config->database->dbname
));
});
//Setup the beanstalk service
$di->set('beanstalk', function() use ($config) {
return new \Phalcon\Queue\Beanstalk(array(
"host" => $config->beanstalk->host,
"port" => $config->beanstalk->port
));
});
//Create a console application
$console = new ConsoleApp();
$console->setDI($di);
/**
* Process the console arguments
*/
$arguments = array();
$params = array();
foreach ($argv as $k => $arg) {
if ($k == 1) {
$arguments['task'] = $arg;
} elseif ($k == 2) {
$arguments['action'] = $arg;
} elseif ($k >= 3) {
$params[] = $arg;
}
}
if (count($params) > 0) {
$arguments['params'] = $params;
}
// define global constants for the current task and action
define('CURRENT_TASK', (isset($argv[1]) ? $argv[1] : null));
define('CURRENT_ACTION', (isset($argv[2]) ? $argv[2] : null));
try {
// handle incoming arguments
$console->handle($arguments);
} catch (\Phalcon\Exception $e) {
echo $e->getMessage();
exit(255);
}
MainTask.php
<?php
class mainTask extends \Phalcon\CLI\Task {
public function mainAction() {
echo "\nThis is the default task and the default action \n";
}
/**
* @param array $params
*/
public function testAction() {
$users = Users::find();
if($users)
{
foreach($users as $user)
{
echo $user->name . '-' . $user->email . PHP_EOL;
}
}
}
public function consumerAction() {
// Get Beanstalk Service
$queue = $this->getDI('beanstalk');
while (($job = $queue->peekReady()) !== false) {
$job = $queue->reserve();
$message = $job->getBody();
var_dump($message);
$job->delete();
}
}
public function producerAction() {
// Get Beanstalk Service
$queue = $this->getDI('beanstalk');
//Insert the job in the queue with options
$jobId = $queue->put(
array('processVideo' => 4871)
);
echo $jobId . PHP_EOL;
}
public function checkAction() {
// Get Beanstalk Service
$queue = $this->getDI('beanstalk');
//echo 'hello' . PHP_EOL;
}
}
Please suggest.
Note: If i am using suggested solution provided by @ajreal than i am getting my service (beanstalk) perfectly.
Thanks in advance.
Thanks & Regards
Tapan Thapa