2026-05-23 22:41:46 -05:00
|
|
|
<?php
|
|
|
|
|
/**
|
2026-06-07 09:25:45 -05:00
|
|
|
* @package MokoSuite
|
|
|
|
|
* @subpackage com_mokosuite
|
2026-05-23 22:41:46 -05:00
|
|
|
* @copyright Copyright (C) 2026 Moko Consulting. All rights reserved.
|
|
|
|
|
* @license GNU General Public License version 3 or later; see LICENSE
|
|
|
|
|
*/
|
|
|
|
|
|
2026-06-07 09:25:45 -05:00
|
|
|
namespace Moko\Component\MokoSuite\Api\Controller;
|
2026-05-23 22:41:46 -05:00
|
|
|
|
|
|
|
|
defined('_JEXEC') or die;
|
|
|
|
|
|
|
|
|
|
use Joomla\CMS\Factory;
|
|
|
|
|
use Joomla\CMS\MVC\Controller\BaseController;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cache management API controller.
|
|
|
|
|
*
|
2026-06-07 09:25:45 -05:00
|
|
|
* POST /api/index.php/v1/mokosuite/cache
|
2026-05-23 22:41:46 -05:00
|
|
|
*
|
|
|
|
|
* @since 1.0.0
|
|
|
|
|
*/
|
|
|
|
|
class CacheController extends BaseController
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Clear all Joomla caches.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*
|
|
|
|
|
* @since 1.0.0
|
|
|
|
|
*/
|
2026-06-06 06:25:18 -05:00
|
|
|
public function execute($task = 'cache'): void
|
2026-05-23 22:41:46 -05:00
|
|
|
{
|
|
|
|
|
$app = Factory::getApplication();
|
|
|
|
|
|
|
|
|
|
if ($app->input->getMethod() !== 'POST')
|
|
|
|
|
{
|
|
|
|
|
$this->sendJson(405, ['error' => 'POST required']);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$user = $app->getIdentity();
|
|
|
|
|
if (!$user->authorise('core.manage', 'com_plugins'))
|
|
|
|
|
{
|
|
|
|
|
$this->sendJson(403, ['error' => 'Not authorized']);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
$cache = Factory::getCache('');
|
|
|
|
|
$cache->clean('');
|
|
|
|
|
|
|
|
|
|
$adminCache = Factory::getCache('', 'callback', 'administrator');
|
|
|
|
|
$adminCache->clean('');
|
|
|
|
|
|
|
|
|
|
if (function_exists('opcache_reset'))
|
|
|
|
|
{
|
|
|
|
|
opcache_reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->sendJson(200, [
|
|
|
|
|
'status' => 'ok',
|
|
|
|
|
'message' => 'Cache cleared',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
catch (\Throwable $e)
|
|
|
|
|
{
|
|
|
|
|
$this->sendJson(500, [
|
|
|
|
|
'error' => 'Cache clear failed',
|
|
|
|
|
'message' => $e->getMessage(),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $code HTTP status code
|
|
|
|
|
* @param array $payload Response data
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private function sendJson(int $code, array $payload): void
|
|
|
|
|
{
|
|
|
|
|
$app = Factory::getApplication();
|
|
|
|
|
$app->setHeader('Content-Type', 'application/json', true);
|
|
|
|
|
$app->setHeader('Status', (string) $code, true);
|
|
|
|
|
echo json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
|
|
|
|
|
$app->close();
|
|
|
|
|
}
|
|
|
|
|
}
|