我有MainTask.php和SomeOtherTask.php文件的文件夹任务......
如何只运行SomeOtherTask?
要运行主要任务,我只需要编写
php cli.php
,但我想要一些严肃的文件名而不是MainTask ......
这是我的另一项任务:
<?php
use Phalcon\Cli\Task;
class DeleteExpiredAuthTokens extends Task{
public function mainAction()
{
echo "Hello world war Z";
}
}
答案 0 :(得分:5)
目录设置
您需要在tasks/
中创建一个app/
目录,然后您可以创建适当的文件名;例如;
app/
├── tasks/
│ ├── SomeTask.php
│ ├── SomeOtherTask.php
│
├── cli.php
现在,您需要创建一个cli.php
文件,命令行将调用该文件。在这里,您需要查看调用参数并将其传递到$console->handle()
方法中。例如;
<强> 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);
}
现在,在app/tasks/SomeTask.php
中说你有以下内容;
应用/任务/ 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";
}
}
您可以通过在参数中传递它们来调用您的任务,例如;
$ php cli.php some
This is the main action
或者,通过传递来调用另一个方法;
$ php cli.php some other
This is other action
备注强>
Task
结尾。调用此类时,省略它。看看我如何调用someTask
类。DeleteExpiredAuthTokens
重命名为DeleteExpiredAuthTokensTask
,否则您将收到以下错误消息:DeleteExpiredAuthTokensTask handler class cannot be loaded