Skip to content
Snippets Groups Projects
Commit 702c998d authored by Александр Плохих's avatar Александр Плохих :waxing_gibbous_moon:
Browse files

add controller and action

parent d050cc48
No related branches found
No related tags found
1 merge request!9make controller
...@@ -6,4 +6,6 @@ F ...@@ -6,4 +6,6 @@ F
@ @
public/index.php,0/e/0e61f67641b441bb3ee38e903c594d99cafa14b4 public/index.php,0/e/0e61f67641b441bb3ee38e903c594d99cafa14b4
_ _
/src/Repository/ReadFileLineByLineRepository.php,7/c/7c103dcd8c0314a8821f84f7886754511983a720 /src/Repository/ReadFileLineByLineRepository.php,7/c/7c103dcd8c0314a8821f84f7886754511983a720
\ No newline at end of file X
(src/Actions/ReadFileLineByLineAction.php,b/b/bb599184d4f741824c29e585018cdb8069747a80
\ No newline at end of file
...@@ -6,4 +6,6 @@ F ...@@ -6,4 +6,6 @@ F
@ @
public/index.php,0/e/0e61f67641b441bb3ee38e903c594d99cafa14b4 public/index.php,0/e/0e61f67641b441bb3ee38e903c594d99cafa14b4
_ _
/src/Repository/ReadFileLineByLineRepository.php,7/c/7c103dcd8c0314a8821f84f7886754511983a720 /src/Repository/ReadFileLineByLineRepository.php,7/c/7c103dcd8c0314a8821f84f7886754511983a720
\ No newline at end of file X
(src/Actions/ReadFileLineByLineAction.php,b/b/bb599184d4f741824c29e585018cdb8069747a80
\ No newline at end of file
<?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);
}
}
...@@ -2,10 +2,10 @@ ...@@ -2,10 +2,10 @@
namespace App\Controller; namespace App\Controller;
use App\Actions\ReadFileLineByLineAction;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
use function App\Repository\readFileLineByLine;
class ReadFileLineByLineController extends AbstractController class ReadFileLineByLineController extends AbstractController
{ {
...@@ -16,11 +16,13 @@ class ReadFileLineByLineController extends AbstractController ...@@ -16,11 +16,13 @@ class ReadFileLineByLineController extends AbstractController
* @param string $filePath путь до файла * @param string $filePath путь до файла
* @return Response */ * @return Response */
#[Route('/readbyline/{filePath}', name: 'app_read_file_line_by_line')] #[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 = ""; $file = "";
try{ try{
foreach (readFileLineByLine($filePath) as $line) { foreach ($action->act($filePath) as $line) {
$file .= $line; $file .= $line;
} }
} catch (\Exception $exception) { } catch (\Exception $exception) {
......
<?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);
}
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