diff --git a/public/HelloWorld.html b/public/HelloWorld.html new file mode 100644 index 0000000000000000000000000000000000000000..0e8969623370152066b2484563e8947c08a2d10e --- /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 0000000000000000000000000000000000000000..423d56ec4eec3aa863e51761bcf155043b20cc65 --- /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 0000000000000000000000000000000000000000..cd6fa57a32277f46f597f7ba7cd0fc304cfbe4e7 --- /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); +}