Loading public/public/Actions/ReadFileLineByLineAction.php 0 → 100644 +29 −0 Original line number Diff line number Diff line <?php namespace App\Actions; use Generator; class ReadFileLineByLineAction { /** * Принимает путь до файла, * проверяет, что файл существует и выводит пользователю построчный вывод используя yield * (файл можешь создать любой) * * @param string $filePath путь до файла * @return string|Generator */ public static function read(string $filePath): Generator { if (!file_exists($filePath)) { throw new Exception("неверный путь"); } $file = fopen($filePath, 'r'); while (!feof($file)) { yield fgets($file); } fclose($file); } } public/public/TestData/FileToRead.txt 0 → 100644 +11 −0 Original line number Diff line number Diff line h e l l o w o r l d No newline at end of file public/public/TestData/TestData.php +1 −1 Original line number Diff line number Diff line Loading @@ -8,5 +8,5 @@ namespace App\TestData; class TestData { public const FILE_PATH = "TestData/FileToRead.txt"; } public/public/index.php +8 −1 Original line number Diff line number Diff line Loading @@ -2,6 +2,13 @@ require_once __DIR__ . '/../vendor/autoload.php'; use App\Actions /*placeholder for a class */; use App\Actions\ReadFileLineByLineAction; use App\TestData\TestData; try { foreach (ReadFileLineByLineAction::read(TestData::FILE_PATH) as $line) { echo $line; } } catch (Exception $ex) { echo $ex->getMessage(); } Loading
public/public/Actions/ReadFileLineByLineAction.php 0 → 100644 +29 −0 Original line number Diff line number Diff line <?php namespace App\Actions; use Generator; class ReadFileLineByLineAction { /** * Принимает путь до файла, * проверяет, что файл существует и выводит пользователю построчный вывод используя yield * (файл можешь создать любой) * * @param string $filePath путь до файла * @return string|Generator */ public static function read(string $filePath): Generator { if (!file_exists($filePath)) { throw new Exception("неверный путь"); } $file = fopen($filePath, 'r'); while (!feof($file)) { yield fgets($file); } fclose($file); } }
public/public/TestData/FileToRead.txt 0 → 100644 +11 −0 Original line number Diff line number Diff line h e l l o w o r l d No newline at end of file
public/public/TestData/TestData.php +1 −1 Original line number Diff line number Diff line Loading @@ -8,5 +8,5 @@ namespace App\TestData; class TestData { public const FILE_PATH = "TestData/FileToRead.txt"; }
public/public/index.php +8 −1 Original line number Diff line number Diff line Loading @@ -2,6 +2,13 @@ require_once __DIR__ . '/../vendor/autoload.php'; use App\Actions /*placeholder for a class */; use App\Actions\ReadFileLineByLineAction; use App\TestData\TestData; try { foreach (ReadFileLineByLineAction::read(TestData::FILE_PATH) as $line) { echo $line; } } catch (Exception $ex) { echo $ex->getMessage(); }