【问题标题】:cli_dispatch.phpsh: Cli-Script with Extbase repository injectioncli_dispatch.phpsh:带有 Extbase 存储库注入的 Cli 脚本
【发布时间】:2016-07-21 12:23:37
【问题描述】:

所以,我有一个这样调用的脚本:

./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');

不幸的是,这将以错误“致命错误:在 null 上调用成员函数 removeAll()”结束。 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(),它需要一个 storagePid,它需要 TypoScript 配置才能工作。我想这是一个后续问题。

【问题讨论】:

  • 只是作为一个提示:获取配置管理器的实例并使用它的 API 来获取您需要的 ts。

标签: typo3 extbase typo3-6.2.x


【解决方案1】:

您可能没有通过ObjectManager 获取您的Updater 实例,因此没有进行依赖注入。

要解决这个问题,您应该将脚本实现为CommandController。它的工作原理类似于ActionControllershere 是如何做到的。然后,您可以将该脚本用作调度程序任务以及 cli 脚本,并具有自动参数解析。对于 CLI 执行检查

的输出
./cli_dispatch.phpsh extbase help

获取可用任务列表及其参数。

为了更轻松地使用 CLI,您还可以使用扩展名 typo3_console

【讨论】:

    猜你喜欢
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    相关资源
    最近更新 更多