1
votes

So, I've got a script that is called like this:

./cli_dispatch.phpsh varnish_instance_update update 123.4.5.6,45.29.102.3

Basically it gets a list of IPs that should be updated in the database.

<?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');

Unfortunately this will end with the error 'Fatal error: Call to a member function removeAll() on null'. It's the same with findAll(), count(), add(), etc.

How will I get access to extbase methods? What am I missing?


Edit: (some sort of) solution

Thanks to Jost I got it to work, kinda.

<?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();
    }

}

In ext_localconf.php

if (TYPO3_MODE === 'BE') {
    $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'][$_EXTKEY] =
        \Bene\VarnishInstanceUpdate\Command\UpdateCommandController::class;
}

You call it like this:

./cli_dispatch.phpsh extbase update:clearservers

But: removeAll() uses findAll() which needs a storagePid which needs the TypoScript config to work. I guess this a follow up question.

1
Just as a hint: get an instance of the configuration manager and use its API to get the ts you need.sven

1 Answers

3
votes

You're probably not getting your Updater instance via an ObjectManager, and thus no dependency injection is done.

To fix that, you should implement your script as a CommandController. It works similar to ActionControllers, here is how to do it. You can then use the script as scheduler task as well as cli script, with automatic argument parsing. For CLI execution check the output of

./cli_dispatch.phpsh extbase help

to get the list of available tasks and their parameters.

To make working on the CLI easier, you could also use the extension typo3_console.