57 lines
1.7 KiB
PHP
57 lines
1.7 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace MageSail\Magesail\Controller\Adminhtml\Tunnel;
|
|
|
|
use Magento\Backend\App\Action;
|
|
use Magento\Backend\App\Action\Context;
|
|
use Magento\Framework\Controller\ResultFactory;
|
|
use MageSail\Magesail\Model\TunnelStatus;
|
|
|
|
class Logtail extends Action
|
|
{
|
|
private const BYTES = 8192;
|
|
private const LINES = 40;
|
|
|
|
public function __construct(
|
|
Context $context,
|
|
private readonly TunnelStatus $tunnelStatus
|
|
) {
|
|
parent::__construct($context);
|
|
}
|
|
|
|
public function execute()
|
|
{
|
|
$path = $this->tunnelStatus->getLogFilePath();
|
|
$lines = [];
|
|
if (\is_readable($path)) {
|
|
$size = filesize($path);
|
|
if ($size !== false && $size > 0) {
|
|
$fp = fopen($path, 'rb');
|
|
if ($fp) {
|
|
$start = max(0, $size - self::BYTES);
|
|
fseek($fp, $start);
|
|
if ($start > 0) {
|
|
fgets($fp);
|
|
}
|
|
$chunk = stream_get_contents($fp) ?: '';
|
|
fclose($fp);
|
|
$lines = preg_split('/\R/', $chunk) ?: [];
|
|
$lines = array_values(array_filter($lines, static fn ($l) => $l !== ''));
|
|
if (\count($lines) > self::LINES) {
|
|
$lines = \array_slice($lines, -self::LINES);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$json = $this->resultFactory->create(ResultFactory::TYPE_JSON);
|
|
$json->setData(['lines' => $lines]);
|
|
return $json;
|
|
}
|
|
|
|
protected function _isAllowed()
|
|
{
|
|
return $this->_authorization->isAllowed('MageSail_Magesail::tunnel');
|
|
}
|
|
}
|