Fix PHP 8 router parameter dispatching and lesson slug playback lookup

This commit is contained in:
Hamza-Ayed
2026-08-31 17:45:27 +03:00
parent ccf5f13a09
commit 78b703f625
3 changed files with 39 additions and 9 deletions
+12 -6
View File
@@ -385,15 +385,17 @@ class VideoController
VideoService::ensureSchema(); VideoService::ensureSchema();
CurriculumService::ensureSchema(); CurriculumService::ensureSchema();
$lessonId = (int)$request->getParam('id'); $rawId = $request->getParam('id') ?? '';
$lesson = null; $lesson = null;
if ($lessonId > 0) { if (is_numeric($rawId) && (int)$rawId > 0) {
$lesson = Database::selectOne("SELECT * FROM lessons WHERE id = ? LIMIT 1", [$lessonId]); $lesson = Database::selectOne("SELECT * FROM lessons WHERE id = ? LIMIT 1", [(int)$rawId]);
} elseif (!empty($rawId)) {
$lesson = Database::selectOne("SELECT * FROM lessons WHERE file_path LIKE ? OR title LIKE ? LIMIT 1", ["%{$rawId}%", "%{$rawId}%"]);
} }
if (!$lesson) { if (!$lesson) {
// Fallback to latest available lesson // Fallback to latest available lesson or create one
$lesson = Database::selectOne("SELECT * FROM lessons ORDER BY id DESC LIMIT 1"); $lesson = Database::selectOne("SELECT * FROM lessons ORDER BY id DESC LIMIT 1");
} }
@@ -408,8 +410,12 @@ class VideoController
$existingCount = Database::selectOne("SELECT COUNT(*) as cnt FROM exams WHERE lesson_id = ? AND scope = 'in_video_checkpoint'", [$lessonId]); $existingCount = Database::selectOne("SELECT COUNT(*) as cnt FROM exams WHERE lesson_id = ? AND scope = 'in_video_checkpoint'", [$lessonId]);
$existingQuestions = Database::selectOne("SELECT COUNT(*) as cnt FROM questions q JOIN exams e ON q.exam_id = e.id WHERE e.lesson_id = ?", [$lessonId]); $existingQuestions = Database::selectOne("SELECT COUNT(*) as cnt FROM questions q JOIN exams e ON q.exam_id = e.id WHERE e.lesson_id = ?", [$lessonId]);
if (empty($existingCount['cnt']) || empty($existingQuestions['cnt'])) { if (empty($existingCount['cnt']) || empty($existingQuestions['cnt'])) {
AiVideoAnalyzerService::processLessonAutonomously($lessonId); try {
$lesson = Database::selectOne("SELECT * FROM lessons WHERE id = ? LIMIT 1", [$lessonId]); AiVideoAnalyzerService::processLessonAutonomously($lessonId);
$lesson = Database::selectOne("SELECT * FROM lessons WHERE id = ? LIMIT 1", [$lessonId]);
} catch (\Throwable $e) {
error_log("Autonomous video analysis notice: " . $e->getMessage());
}
} }
// Fetch attached in-video Socratic Checkpoints with Questions and Options // Fetch attached in-video Socratic Checkpoints with Questions and Options
+16
View File
@@ -13,6 +13,7 @@ class Request
private array $queryParams; private array $queryParams;
private array $bodyParams; private array $bodyParams;
private array $headers; private array $headers;
private array $routeParams = [];
// Explicit properties to store authentication details to avoid deprecation warnings in PHP 8.2+ // Explicit properties to store authentication details to avoid deprecation warnings in PHP 8.2+
public ?int $user_id = null; public ?int $user_id = null;
@@ -78,6 +79,21 @@ class Request
$this->queryParams = $queryParams; $this->queryParams = $queryParams;
} }
public function setParams(array $params): void
{
$this->routeParams = $params;
}
public function getParams(): array
{
return $this->routeParams;
}
public function getParam(string $key, $default = null)
{
return $this->routeParams[$key] ?? $default;
}
public function get(string $key, $default = null) public function get(string $key, $default = null)
{ {
return $this->bodyParams[$key] ?? ($this->queryParams[$key] ?? $default); return $this->bodyParams[$key] ?? ($this->queryParams[$key] ?? $default);
+11 -3
View File
@@ -88,6 +88,7 @@ class Router
if ($route['method'] === $method && preg_match($route['pattern'], $path, $matches)) { if ($route['method'] === $method && preg_match($route['pattern'], $path, $matches)) {
// Filter named captures from regex match // Filter named captures from regex match
$params = array_filter($matches, 'is_string', ARRAY_FILTER_USE_KEY); $params = array_filter($matches, 'is_string', ARRAY_FILTER_USE_KEY);
$request->setParams($params);
// Run global middleware first // Run global middleware first
foreach ($this->globalMiddleware as $mw) { foreach ($this->globalMiddleware as $mw) {
@@ -108,13 +109,20 @@ class Router
if (class_exists($controllerClass)) { if (class_exists($controllerClass)) {
$controller = new $controllerClass(); $controller = new $controllerClass();
if (method_exists($controller, $action)) { if (method_exists($controller, $action)) {
// Call action with Request, Response and URI dynamic parameters $ref = new \ReflectionMethod($controller, $action);
call_user_func_array([$controller, $action], array_merge([$request, $response], $params)); $paramCount = $ref->getNumberOfParameters();
if ($paramCount <= 2) {
$controller->$action($request, $response);
} else {
$args = array_values(array_merge([$request, $response], array_values($params)));
call_user_func_array([$controller, $action], $args);
}
return; return;
} }
} }
} elseif (is_callable($handler)) { } elseif (is_callable($handler)) {
call_user_func_array($handler, array_merge([$request, $response], $params)); $args = array_values(array_merge([$request, $response], array_values($params)));
call_user_func_array($handler, $args);
return; return;
} }