Commit a817a270 authored by Nikita Chernykh's avatar Nikita Chernykh
Browse files

Merge branch 'PTPS|Controller_8' into 'main'

make controller

See merge request !8
parents 1482daa5 b77df9d7
Loading
Loading
Loading
Loading

public/helloWorld.html

0 → 100644
+10 −0
Original line number Diff line number Diff line
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
hello World
</body>
</html>
 No newline at end of file
+26 −0
Original line number Diff line number Diff line
<?php

namespace App\Actions;

use Exception;

class ReadLogFileAction
{
    /**
     * Принимает путь до файла,
     * проверяет, что файл существует и выводит пользователю весь контент файла
     * (файл можешь создать любой)
     *
     * @param string $filePath путь до файла
     * @return false|string
     * @throws Exception при отсутсвии фаила по переданному пути
     */
    public function act(string $filePath)
    {
        if (!file_exists($filePath)) {
            throw new Exception("По данному пути ничего не найдено: $filePath");
        }

        return file_get_contents($filePath);
    }
}
+29 −0
Original line number Diff line number Diff line
<?php

namespace App\Controller;
use App\Actions\ReadLogFileAction;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class ReadLogFileController extends AbstractController
{
    /**
     * Принимает путь до файла,
     * проверяет, что файл существует и выводит пользователю весь контент файла
     * (файл можешь создать любой)
     *
     * @param Request $request
     * @param ReadLogFileAction $action
     * @return Response
     */
    #[Route('/read', name: 'app_read_log_file', methods: ['POST'])]
    public function index(Request $request, ReadLogFileAction $action): Response
    {
        $file = $request->files->get("File");
        return new Response($action->act($file->getPathname()));
    }

    }