Commit cc98f6d3 authored by Akex's avatar Akex Committed by Александр Плохих
Browse files

make controller

parent 295a6342
Loading
Loading
Loading
Loading

public/HelloWorld.html

0 → 100644
+12 −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!
Hello World !!
Hello World !!!
</body>
</html>
 No newline at end of file
+32 −0
Original line number Diff line number Diff line
<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use function App\Repository\readFileLineByLine;

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) : Response
    {
        $file = "";
        try{
            foreach (readFileLineByLine($filePath) as $line) {
                $file .= $line;
            }
        } catch (\Exception $exception) {
            return new Response($exception->getMessage(), Response::HTTP_NOT_FOUND);
        }

        return new Response($file, Response::HTTP_OK);
    }
}
+26 −0
Original line number Diff line number Diff line
<?php

namespace App\Repository;
use Exception;
use Generator;

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

    $file = fopen($filePath, 'r');
    while (!feof($file)){
        yield fgets($file);
    }
    fclose($file);
}