Commit 17eb5a11 authored by Адлан Шамавов's avatar Адлан Шамавов
Browse files

add readFileLineByLine

parent dd80058b
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line

vphp:S121"0Add curly braces around the nested statement(s).(8Ƌ1J$562771d0-0b91-4f4c-beaf-90c25c2ac224
 No newline at end of file
+7 −7
Original line number Diff line number Diff line
@@ -12,26 +12,26 @@ use Symfony\Component\Routing\Attribute\Route;

class HomeController extends AbstractController
{
    private function readLogFile(string $fileName): string {
    private function readFileLineByLine(string $fileName): iterable  {
        $filePath = $this->getParameter('kernel.project_dir') . "/public/files/" . $fileName;
        if (file_exists($filePath)) {
            $text = "";
            $file = fopen($filePath, "r");
            while(!feof($file)) {
                $line = fgets($file);
                $text .= $line;
                yield fgets($file);
            }
            fclose($file);
            return $text;
        }
        return "Такого файла не существует.";
        else yield "Такого файла не существует.";
    }


    #[Route('/{fileName}', name: 'home')]
    public function home(string $fileName): Response // text.txt
    {
        $text = $this->readLogFile($fileName);
        $text = "";
        foreach($this->readFileLineByLine($fileName) as $line) {
            $text .= $line;
        }
        return $this->render('home.html.twig', ['text' => $text]);
    }
}