Skip to content
Snippets Groups Projects
ReadFileLineByLineController.php 1.11 KiB
Newer Older
Akex's avatar
Akex committed
<?php

namespace App\Controller;

use App\Actions\ReadFileLineByLineAction;
Akex's avatar
Akex committed
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class ReadFileLineByLineController extends AbstractController
{
    /**
     * Принимает путь до файла,
     * проверяет, что файл существует и выводит пользователю построчный вывод используя yield
     *
     * @param string $filePath путь до файла
     * @return Response  */
    #[Route('/readbyline/{filePath}', name: 'app_read_file_line_by_line')]
    public function index(
        string $filePath,
        ReadFileLineByLineAction $action
    ) : Response {
Akex's avatar
Akex committed
        $file = "";
        try{
            foreach ($action->act($filePath) as $line) {
Akex's avatar
Akex committed
                $file .= $line;
            }
        } catch (\Exception $exception) {
            return new Response($exception->getMessage(), Response::HTTP_NOT_FOUND);
        }

        return new Response($file, Response::HTTP_OK);
    }
}