From cc98f6d34d5d92651e9d1b68db705554e25aee56 Mon Sep 17 00:00:00 2001
From: Akex <a.plokhikh.sas@gmail.com>
Date: Tue, 9 Apr 2024 03:08:52 +0500
Subject: [PATCH] make controller

---
 public/HelloWorld.html                        | 12 +++++++
 .../ReadFileLineByLineController.php          | 32 +++++++++++++++++++
 .../ReadFileLineByLineRepository.php          | 26 +++++++++++++++
 3 files changed, 70 insertions(+)
 create mode 100644 public/HelloWorld.html
 create mode 100644 src/Controller/ReadFileLineByLineController.php
 create mode 100644 src/Repository/ReadFileLineByLineRepository.php

diff --git a/public/HelloWorld.html b/public/HelloWorld.html
new file mode 100644
index 0000000..0e89696
--- /dev/null
+++ b/public/HelloWorld.html
@@ -0,0 +1,12 @@
+<!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
diff --git a/src/Controller/ReadFileLineByLineController.php b/src/Controller/ReadFileLineByLineController.php
new file mode 100644
index 0000000..423d56e
--- /dev/null
+++ b/src/Controller/ReadFileLineByLineController.php
@@ -0,0 +1,32 @@
+<?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);
+    }
+}
diff --git a/src/Repository/ReadFileLineByLineRepository.php b/src/Repository/ReadFileLineByLineRepository.php
new file mode 100644
index 0000000..cd6fa57
--- /dev/null
+++ b/src/Repository/ReadFileLineByLineRepository.php
@@ -0,0 +1,26 @@
+<?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);
+}
-- 
GitLab