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.