昨天我终于让我的Typo3 Scheduler以我想要的方式工作。主要是将CommandController实现到我的扩展中有点"有问题"。
现在我有另外一个关于Scheduler和CommandController的问题。我有以下代码。它是我在我的扩展类的控制器中实现的一个Action:
public function simpleCommand()
{
$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
$apprep = $objectManager->get(\Cjk\Icingaconfgen\Domain\Repository\HostRepository::class);
$hosts = $apprep->findAll();
$objectManager2 = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
$apprep2 = $objectManager2->get(\Cjk\Icingaconfgen\Domain\Repository\ServicesRepository::class);
$services = $apprep2->findAll();
foreach($hosts as $host)
{
$name = $host->getUid();
$address = $host->getIpv4();
$file = '/etc/icinga2/conf.d/hosts/' . $name . '.conf';
$code_a = 'object Host "';
$code_b = '" {
import "generic-host"
address = "';
$code_c = '"
vars.notification["mail"] = {
groups = [ "icingaadmins" ]
}
}';
$fp = fopen("{$file}", 'wb');
fwrite($fp, $code_a . $name . $code_b . $address . $code_c);
fclose($fp);
mkdir('/etc/icinga2/conf.d/hosts/' . $name);
foreach($services as $service)
{
if($service->getHost() == $name)
{
$name = $host->getUid();
$chkcmd = 'http';
$file = '/etc/icinga2/conf.d/hosts/'.$name.'/' . $name . '-service.conf';
$code_a = 'object Service "';
$code_b = '" {
import "generic-service"
host_name = "';
$code_c = '"
check_command = "http"
}';
$fp = fopen("{$file}", 'wb');
fwrite($fp, $code_a . $name.'-service'. $code_b . $name . $code_c);
fclose($fp);
}
}
exec('sudo /etc/init.d/icinga2 restart');
}
}
这是我在CommandController中实现代码的方式,但是以类似的方式,它也在类控制器中的Action中实现。现在,这个函数的作用就是生成一个我需要在另一个应用程序中使用的特定文件。该函数获得了类#34; Host"的复制品。然后找到它的所有对象。然后它只使用每个对象的属性来生成前面提到的文件。它与班级"服务"。
一样在Action的前端,代码完美运行并生成文件,但是在CommandController中,通过Scheduler自动执行它根本不起作用。
我身边有误会吗?无法通过命令访问每个类库:或者#34;存储库是否只能通过操作访问?"
或者是否有其他错误?
答案 0 :(得分:1)
我想这里的原因是前端和后端上下文之间的区别。This answer here, from a different context, sums it up very nice and is worth a read
基本上,在前端上下文中,您有typoscript配置,告诉存储库存储和查找记录的位置。这在后端上下文中不存在。上面的答案用这段代码解释了这一点
module.tx_yourext.persistence < plugin.tx_yourext.persistence
答案 1 :(得分:0)
由于它不能与我的特定情况下的CommandController一起使用,并且我无法使用它来访问持久层,甚至没有依赖注入,我决定根本不使用CommandContoller任务,而是使用正常的Scheduler它的任务。我遇到的最大问题是实际使用findAll()函数用于存储库(它使用findByUid()。这是因为存储库默认需要一个存储页面。因为我没有模块也没有插件,我的在这种情况下,typoscript设置被忽略。 所以我不得不设置存储库以不尊重我的任务中的存储页面。代码如下:
<?php
namespace Cjk\Icingaconfgen\Tasks;
class TestTask extends \TYPO3\CMS\Scheduler\Task\AbstractTask
{
public function execute() {
$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
/** @var CustomerRepository $apprep */
$apprep = $objectManager->get(\Cjk\Icingaconfgen\Domain\Repository\HostRepository::class);
/** @var Typo3QuerySettings $querySettings */
$querySettings = $objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
$querySettings->setRespectStoragePage(FALSE);
$apprep->setDefaultQuerySettings($querySettings);
$hosts = $apprep->findAll();
/** @var CustomerRepository $apprep2 */
$apprep2 = $objectManager->get(\Cjk\Icingaconfgen\Domain\Repository\ServicesRepository::class);
/** @var Typo3QuerySettings $querySettings */
$querySettings = $objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
$querySettings->setRespectStoragePage(FALSE);
$apprep2->setDefaultQuerySettings($querySettings);
$services = $apprep2->findAll();
foreach($hosts as $host)
{
$name = $host->getUid();
$address = $host->getIpv4();
$file = '/etc/icinga2/conf.d/hosts/' . $name . '.conf';
$code_a = 'object Host "';
$code_b = '" {
import "generic-host"
address = "';
$code_c = '"
vars.notification["mail"] = {
groups = [ "icingaadmins" ]
}
}';
$fp = fopen("{$file}", 'wb');
fwrite($fp, $code_a . $name . $code_b . $address . $code_c);
fclose($fp);
mkdir('/etc/icinga2/conf.d/hosts/' . $name);
foreach($services as $service)
{
if($service->getHost() == $name)
{
$name = $host->getUid();
$chkcmd = 'http';
$file = '/etc/icinga2/conf.d/hosts/'.$name.'/' . $name . '-service.conf';
$code_a = 'object Service "';
$code_b = '" {
import "generic-service"
host_name = "';
$code_c = '"
check_command = "http"
}';
$fp = fopen("{$file}", 'wb');
fwrite($fp, $code_a . $name.'-service'. $code_b . $name . $code_c);
fclose($fp);
}
}
exec('sudo /etc/init.d/icinga2 restart');
}
return TRUE;
}
}