cli_dispatch.phpsh:带有 Extbase 存储库注入的 Cli 脚本
cli_dispatch.phpsh: Cli-Script with Extbase repository injection
所以,我有一个这样调用的脚本:
./cli_dispatch.phpsh varnish_instance_update update 123.4.5.6,45.29.102.3
基本上它会获取应在数据库中更新的 IP 列表。
<?php
namespace Bene\VarnishInstanceUpdate\Cli;
if (!defined('TYPO3_cliMode')) {
die('You cannot run this script directly!');
}
class Updater {
/**
* console arguments
*
* @var array
*/
protected $args;
/**
* @var \Mittwald\Varnishcache\Domain\Repository\ServerRepository
* @inject
*/
protected $serverRepository;
function __construct() {
$this->args = $_SERVER['argv'];
$this->clearServers();
}
function clearServers() {
$this->serverRepository->removeAll();
}
}
$instance = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('\Bene\VarnishInstanceUpdate\Cli\Updater');
不幸的是,这将以错误 'Fatal error: Call to a member function removeAll() on null' 结束。 findAll(), count(), add()等都是一样的
我将如何访问 extbase 方法?我错过了什么?
编辑:(某种)解决方案
多亏了 Jost,我才让它开始工作。
<?php
namespace Bene\VarnishInstanceUpdate\Command;
use \TYPO3\CMS\Extbase\Mvc\Controller\CommandController;
if (!defined('TYPO3_cliMode')) {
die('You cannot run this script directly!');
}
class UpdateCommandController extends CommandController {
/**
* @var \Mittwald\Varnishcache\Domain\Repository\ServerRepository
* @inject
*/
protected $serverRepository;
/**
* Clears the server table
*/
function clearServersCommand() {
$this->serverRepository->removeAll();
}
}
在ext_localconf.php
if (TYPO3_MODE === 'BE') {
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'][$_EXTKEY] =
\Bene\VarnishInstanceUpdate\Command\UpdateCommandController::class;
}
你这样称呼它:
./cli_dispatch.phpsh extbase update:clearservers
但是:removeAll() 使用 findAll(),它需要一个需要 TypoScript 配置才能工作的 storagePid。我想这是一个后续问题。
您可能没有通过 ObjectManager
获取 Updater
实例,因此没有完成依赖项注入。
要解决这个问题,您应该按照 CommandController
. It works similar to ActionControllers
, here 的方式执行脚本。然后,您可以将该脚本用作调度程序任务以及 cli 脚本,并进行自动参数解析。对于 CLI 执行检查
的输出
./cli_dispatch.phpsh extbase help
获取可用任务列表及其参数。
为了更轻松地使用 CLI,您还可以使用扩展名 typo3_console
。
所以,我有一个这样调用的脚本:
./cli_dispatch.phpsh varnish_instance_update update 123.4.5.6,45.29.102.3
基本上它会获取应在数据库中更新的 IP 列表。
<?php
namespace Bene\VarnishInstanceUpdate\Cli;
if (!defined('TYPO3_cliMode')) {
die('You cannot run this script directly!');
}
class Updater {
/**
* console arguments
*
* @var array
*/
protected $args;
/**
* @var \Mittwald\Varnishcache\Domain\Repository\ServerRepository
* @inject
*/
protected $serverRepository;
function __construct() {
$this->args = $_SERVER['argv'];
$this->clearServers();
}
function clearServers() {
$this->serverRepository->removeAll();
}
}
$instance = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('\Bene\VarnishInstanceUpdate\Cli\Updater');
不幸的是,这将以错误 'Fatal error: Call to a member function removeAll() on null' 结束。 findAll(), count(), add()等都是一样的
我将如何访问 extbase 方法?我错过了什么?
编辑:(某种)解决方案
多亏了 Jost,我才让它开始工作。
<?php
namespace Bene\VarnishInstanceUpdate\Command;
use \TYPO3\CMS\Extbase\Mvc\Controller\CommandController;
if (!defined('TYPO3_cliMode')) {
die('You cannot run this script directly!');
}
class UpdateCommandController extends CommandController {
/**
* @var \Mittwald\Varnishcache\Domain\Repository\ServerRepository
* @inject
*/
protected $serverRepository;
/**
* Clears the server table
*/
function clearServersCommand() {
$this->serverRepository->removeAll();
}
}
在ext_localconf.php
if (TYPO3_MODE === 'BE') {
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'][$_EXTKEY] =
\Bene\VarnishInstanceUpdate\Command\UpdateCommandController::class;
}
你这样称呼它:
./cli_dispatch.phpsh extbase update:clearservers
但是:removeAll() 使用 findAll(),它需要一个需要 TypoScript 配置才能工作的 storagePid。我想这是一个后续问题。
您可能没有通过 ObjectManager
获取 Updater
实例,因此没有完成依赖项注入。
要解决这个问题,您应该按照 CommandController
. It works similar to ActionControllers
, here 的方式执行脚本。然后,您可以将该脚本用作调度程序任务以及 cli 脚本,并进行自动参数解析。对于 CLI 执行检查
./cli_dispatch.phpsh extbase help
获取可用任务列表及其参数。
为了更轻松地使用 CLI,您还可以使用扩展名 typo3_console
。