Skip to content
Snippets Groups Projects
Commit 17eb5a11 authored by Адлан Шамавов's avatar Адлан Шамавов
Browse files

add readFileLineByLine

parent dd80058b
No related branches found
No related tags found
1 merge request!9Ptps controller 9
vphp:S121"0Add curly braces around the nested statement(s).(8Ƌ1J$562771d0-0b91-4f4c-beaf-90c25c2ac224
\ No newline at end of file
...@@ -12,26 +12,26 @@ use Symfony\Component\Routing\Attribute\Route; ...@@ -12,26 +12,26 @@ use Symfony\Component\Routing\Attribute\Route;
class HomeController extends AbstractController 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; $filePath = $this->getParameter('kernel.project_dir') . "/public/files/" . $fileName;
if (file_exists($filePath)) { if (file_exists($filePath)) {
$text = "";
$file = fopen($filePath, "r"); $file = fopen($filePath, "r");
while(!feof($file)) { while(!feof($file)) {
$line = fgets($file); yield fgets($file);
$text .= $line;
} }
fclose($file); fclose($file);
return $text;
} }
return "Такого файла не существует."; else yield "Такого файла не существует.";
} }
#[Route('/{fileName}', name: 'home')] #[Route('/{fileName}', name: 'home')]
public function home(string $fileName): Response // text.txt 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]); return $this->render('home.html.twig', ['text' => $text]);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment