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

add reader action and controller

parent 31431e3c
No related branches found
No related tags found
1 merge request!8make controller
<?php
namespace App\Actions;
use Exception;
class ReadLogFileAction
{
/**
* Принимает путь до файла,
* проверяет, что файл существует и выводит пользователю весь контент файла
* (файл можешь создать любой)
*
* @param string $filePath путь до файла
* @return false|string
* @throws Exception при отсутсвии фаила по переданному пути
*/
public function act(string $filePath)
{
if (!file_exists($filePath)) {
throw new Exception("По данному пути ничего не найдено: $filePath");
}
return file_get_contents($filePath);
}
}
<?php
namespace App\Controller;
use App\Actions\ReadLogFileAction;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
......@@ -16,13 +16,12 @@ class ReadLogFileController extends AbstractController
* @param string $filePath путь до файла
* @return Response */
#[Route('/read/{filePath}', name: 'app_read_log_file', methods: ['GET'])]
public function index(string $filePath): Response
public function index(string $filePath, ReadLogFileAction $action): Response
{
$fileSystem = new Filesystem();
if (!$fileSystem->exists($filePath)) {
return new Response('File not found', Response::HTTP_NOT_FOUND);
try {
return new Response($action->act($filePath));
} catch (\Exception $exception) {
return new Response('Searching file not found :/', 422);
}
return new Response(file_get_contents($filePath), Response::HTTP_OK);
}
}
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