diff --git a/.idea/sonarlint/issuestore/index.pb b/.idea/sonarlint/issuestore/index.pb index b6417dea1d57ed2c40d807f2932aed7cdff27058..952c8a69794caa637ef4b2d74236ebf8f3ae2674 100644 --- a/.idea/sonarlint/issuestore/index.pb +++ b/.idea/sonarlint/issuestore/index.pb @@ -6,4 +6,6 @@ F @ public/index.php,0/e/0e61f67641b441bb3ee38e903c594d99cafa14b4 _ -/src/Repository/ReadFileLineByLineRepository.php,7/c/7c103dcd8c0314a8821f84f7886754511983a720 \ No newline at end of file +/src/Repository/ReadFileLineByLineRepository.php,7/c/7c103dcd8c0314a8821f84f7886754511983a720 +X +(src/Actions/ReadFileLineByLineAction.php,b/b/bb599184d4f741824c29e585018cdb8069747a80 \ No newline at end of file diff --git a/.idea/sonarlint/securityhotspotstore/index.pb b/.idea/sonarlint/securityhotspotstore/index.pb index b6417dea1d57ed2c40d807f2932aed7cdff27058..952c8a69794caa637ef4b2d74236ebf8f3ae2674 100644 --- a/.idea/sonarlint/securityhotspotstore/index.pb +++ b/.idea/sonarlint/securityhotspotstore/index.pb @@ -6,4 +6,6 @@ F @ public/index.php,0/e/0e61f67641b441bb3ee38e903c594d99cafa14b4 _ -/src/Repository/ReadFileLineByLineRepository.php,7/c/7c103dcd8c0314a8821f84f7886754511983a720 \ No newline at end of file +/src/Repository/ReadFileLineByLineRepository.php,7/c/7c103dcd8c0314a8821f84f7886754511983a720 +X +(src/Actions/ReadFileLineByLineAction.php,b/b/bb599184d4f741824c29e585018cdb8069747a80 \ No newline at end of file diff --git a/src/Actions/ReadFileLineByLineAction.php b/src/Actions/ReadFileLineByLineAction.php new file mode 100644 index 0000000000000000000000000000000000000000..cf7f1f6fd14f715c057fe14ef46e7734e2fb468e --- /dev/null +++ b/src/Actions/ReadFileLineByLineAction.php @@ -0,0 +1,29 @@ +<?php + +namespace App\Actions; + +use Generator; + +class ReadFileLineByLineAction +{ + /** + * Принимает путь до файла, + * проверÑет, что файл ÑущеÑтвует и выводит пользователю поÑтрочный вывод иÑÐ¿Ð¾Ð»ÑŒÐ·ÑƒÑ yield + * (файл можешь Ñоздать любой) + * + * @param string $filePath путь до файла + * @return string|Generator */ + public function act(string $filePath): Generator + { + if (!file_exists($filePath)) { + throw new Exception("неверный путь"); + } + + $file = fopen($filePath, 'r'); + while (!feof($file)) { + yield fgets($file); + } + + fclose($file); + } +} diff --git a/src/Controller/ReadFileLineByLineController.php b/src/Controller/ReadFileLineByLineController.php index 423d56ec4eec3aa863e51761bcf155043b20cc65..d63b261239944d2e4076b68d647a9c315b320242 100644 --- a/src/Controller/ReadFileLineByLineController.php +++ b/src/Controller/ReadFileLineByLineController.php @@ -2,10 +2,10 @@ namespace App\Controller; +use App\Actions\ReadFileLineByLineAction; 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 { @@ -16,11 +16,13 @@ class ReadFileLineByLineController extends AbstractController * @param string $filePath путь до файла * @return Response */ #[Route('/readbyline/{filePath}', name: 'app_read_file_line_by_line')] - public function index(string $filePath) : Response - { + public function index( + string $filePath, + ReadFileLineByLineAction $action + ) : Response { $file = ""; try{ - foreach (readFileLineByLine($filePath) as $line) { + foreach ($action->act($filePath) as $line) { $file .= $line; } } catch (\Exception $exception) { diff --git a/src/Repository/ReadFileLineByLineRepository.php b/src/Repository/ReadFileLineByLineRepository.php deleted file mode 100644 index cd6fa57a32277f46f597f7ba7cd0fc304cfbe4e7..0000000000000000000000000000000000000000 --- a/src/Repository/ReadFileLineByLineRepository.php +++ /dev/null @@ -1,26 +0,0 @@ -<?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); -}