From dd80058baf3aab647ab190ec904d5be6395aacd0 Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Tue, 9 Apr 2024 10:47:44 +0500 Subject: [PATCH 01/13] add readLogFile --- .../994b95f5d0e2f9641bf4e2c30422e6a0a6a105b5 | 0 .idea/sonarlint/issuestore/index.pb | 4 +++- .../994b95f5d0e2f9641bf4e2c30422e6a0a6a105b5 | 0 .idea/sonarlint/securityhotspotstore/index.pb | 4 +++- public/files/text.txt | 2 ++ src/Controller/HomeController.php | 24 ++++++++++++++----- templates/home.html.twig | 2 +- 7 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 .idea/sonarlint/issuestore/9/9/994b95f5d0e2f9641bf4e2c30422e6a0a6a105b5 create mode 100644 .idea/sonarlint/securityhotspotstore/9/9/994b95f5d0e2f9641bf4e2c30422e6a0a6a105b5 create mode 100644 public/files/text.txt diff --git a/.idea/sonarlint/issuestore/9/9/994b95f5d0e2f9641bf4e2c30422e6a0a6a105b5 b/.idea/sonarlint/issuestore/9/9/994b95f5d0e2f9641bf4e2c30422e6a0a6a105b5 new file mode 100644 index 0000000..e69de29 diff --git a/.idea/sonarlint/issuestore/index.pb b/.idea/sonarlint/issuestore/index.pb index bc34c28..dae00e9 100644 --- a/.idea/sonarlint/issuestore/index.pb +++ b/.idea/sonarlint/issuestore/index.pb @@ -8,4 +8,6 @@ V C assets/bootstrap.js,0\3\03141036569b5869397180192f8b98c1360e1556 E -assets/styles/app.css,5\8\58d82e459ad700473925afc6a4d3ceb1cbdfdf19 \ No newline at end of file +assets/styles/app.css,5\8\58d82e459ad700473925afc6a4d3ceb1cbdfdf19 +E +public/files/text.txt,9\9\994b95f5d0e2f9641bf4e2c30422e6a0a6a105b5 \ No newline at end of file diff --git a/.idea/sonarlint/securityhotspotstore/9/9/994b95f5d0e2f9641bf4e2c30422e6a0a6a105b5 b/.idea/sonarlint/securityhotspotstore/9/9/994b95f5d0e2f9641bf4e2c30422e6a0a6a105b5 new file mode 100644 index 0000000..e69de29 diff --git a/.idea/sonarlint/securityhotspotstore/index.pb b/.idea/sonarlint/securityhotspotstore/index.pb index bc34c28..dae00e9 100644 --- a/.idea/sonarlint/securityhotspotstore/index.pb +++ b/.idea/sonarlint/securityhotspotstore/index.pb @@ -8,4 +8,6 @@ V C assets/bootstrap.js,0\3\03141036569b5869397180192f8b98c1360e1556 E -assets/styles/app.css,5\8\58d82e459ad700473925afc6a4d3ceb1cbdfdf19 \ No newline at end of file +assets/styles/app.css,5\8\58d82e459ad700473925afc6a4d3ceb1cbdfdf19 +E +public/files/text.txt,9\9\994b95f5d0e2f9641bf4e2c30422e6a0a6a105b5 \ No newline at end of file diff --git a/public/files/text.txt b/public/files/text.txt new file mode 100644 index 0000000..3ba1fcf --- /dev/null +++ b/public/files/text.txt @@ -0,0 +1,2 @@ +Раз, два, три, четыре, +пять, шесть, семь, восемь. \ No newline at end of file diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index adf7c3d..51d19fa 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -12,14 +12,26 @@ use Symfony\Component\Routing\Attribute\Route; class HomeController extends AbstractController { - private function diffDays(DateTimeImmutable $dateStart, DateTimeImmutable $dateEnd): int { - return date_diff($dateStart, $dateEnd)->format("%a") ; + private function readLogFile(string $fileName): string { + $filePath = $this->getParameter('kernel.project_dir') . "/public/files/" . $fileName; + if (file_exists($filePath)) { + $text = ""; + $file = fopen($filePath, "r"); + while(!feof($file)) { + $line = fgets($file); + $text .= $line; + } + fclose($file); + return $text; + } + return "Такого файла не существует."; } - #[Route('/{startDate}/{endDate}', name: 'home')] // 01-01-2024 - public function home(string $startDate, string $endDate): Response + + #[Route('/{fileName}', name: 'home')] + public function home(string $fileName): Response // text.txt { - $countDays = $this->diffDays(new DateTimeImmutable($startDate), new DateTimeImmutable($endDate)); - return $this->render('home.html.twig', ['count' => $countDays]); + $text = $this->readLogFile($fileName); + return $this->render('home.html.twig', ['text' => $text]); } } diff --git a/templates/home.html.twig b/templates/home.html.twig index 3814758..6ac2ed4 100644 --- a/templates/home.html.twig +++ b/templates/home.html.twig @@ -1,3 +1,3 @@ {% block body %} -

Кол-во дней между датами: {{ count }}

+

{{ text }}

{% endblock %} \ No newline at end of file -- GitLab From 75bb559ba2566b885536b19c6966df1d897f6b0e Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Fri, 12 Apr 2024 16:54:22 +0500 Subject: [PATCH 02/13] refactoring --- .idea/.gitignore | 8 - .idea/iqdevTranningProgram.iml | 142 ---------------- .idea/modules.xml | 8 - .idea/php.xml | 158 ------------------ .idea/phpunit.xml | 10 -- .../03141036569b5869397180192f8b98c1360e1556 | 0 .../38ac5fc300d52f94b369e87fbb1937abe29f831f | 0 .../58d82e459ad700473925afc6a4d3ceb1cbdfdf19 | 0 .../994b95f5d0e2f9641bf4e2c30422e6a0a6a105b5 | 0 .../ad8b439416d1e02614f47c5b471c7c4e587dca82 | 0 .../b5aea0f996f6645c77ef6ac38e15316374d0fba2 | 0 .idea/sonarlint/issuestore/index.pb | 13 -- .../03141036569b5869397180192f8b98c1360e1556 | 0 .../38ac5fc300d52f94b369e87fbb1937abe29f831f | 0 .../58d82e459ad700473925afc6a4d3ceb1cbdfdf19 | 0 .../994b95f5d0e2f9641bf4e2c30422e6a0a6a105b5 | 0 .../ad8b439416d1e02614f47c5b471c7c4e587dca82 | 0 .../b5aea0f996f6645c77ef6ac38e15316374d0fba2 | 0 .idea/sonarlint/securityhotspotstore/index.pb | 13 -- .idea/vcs.xml | 7 - 20 files changed, 359 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/iqdevTranningProgram.iml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/php.xml delete mode 100644 .idea/phpunit.xml delete mode 100644 .idea/sonarlint/issuestore/0/3/03141036569b5869397180192f8b98c1360e1556 delete mode 100644 .idea/sonarlint/issuestore/3/8/38ac5fc300d52f94b369e87fbb1937abe29f831f delete mode 100644 .idea/sonarlint/issuestore/5/8/58d82e459ad700473925afc6a4d3ceb1cbdfdf19 delete mode 100644 .idea/sonarlint/issuestore/9/9/994b95f5d0e2f9641bf4e2c30422e6a0a6a105b5 delete mode 100644 .idea/sonarlint/issuestore/a/d/ad8b439416d1e02614f47c5b471c7c4e587dca82 delete mode 100644 .idea/sonarlint/issuestore/b/5/b5aea0f996f6645c77ef6ac38e15316374d0fba2 delete mode 100644 .idea/sonarlint/issuestore/index.pb delete mode 100644 .idea/sonarlint/securityhotspotstore/0/3/03141036569b5869397180192f8b98c1360e1556 delete mode 100644 .idea/sonarlint/securityhotspotstore/3/8/38ac5fc300d52f94b369e87fbb1937abe29f831f delete mode 100644 .idea/sonarlint/securityhotspotstore/5/8/58d82e459ad700473925afc6a4d3ceb1cbdfdf19 delete mode 100644 .idea/sonarlint/securityhotspotstore/9/9/994b95f5d0e2f9641bf4e2c30422e6a0a6a105b5 delete mode 100644 .idea/sonarlint/securityhotspotstore/a/d/ad8b439416d1e02614f47c5b471c7c4e587dca82 delete mode 100644 .idea/sonarlint/securityhotspotstore/b/5/b5aea0f996f6645c77ef6ac38e15316374d0fba2 delete mode 100644 .idea/sonarlint/securityhotspotstore/index.pb delete mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/iqdevTranningProgram.iml b/.idea/iqdevTranningProgram.iml deleted file mode 100644 index 3f9338e..0000000 --- a/.idea/iqdevTranningProgram.iml +++ /dev/null @@ -1,142 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index aedf51d..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/php.xml b/.idea/php.xml deleted file mode 100644 index af015eb..0000000 --- a/.idea/php.xml +++ /dev/null @@ -1,158 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/phpunit.xml b/.idea/phpunit.xml deleted file mode 100644 index 4f8104c..0000000 --- a/.idea/phpunit.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/sonarlint/issuestore/0/3/03141036569b5869397180192f8b98c1360e1556 b/.idea/sonarlint/issuestore/0/3/03141036569b5869397180192f8b98c1360e1556 deleted file mode 100644 index e69de29..0000000 diff --git a/.idea/sonarlint/issuestore/3/8/38ac5fc300d52f94b369e87fbb1937abe29f831f b/.idea/sonarlint/issuestore/3/8/38ac5fc300d52f94b369e87fbb1937abe29f831f deleted file mode 100644 index e69de29..0000000 diff --git a/.idea/sonarlint/issuestore/5/8/58d82e459ad700473925afc6a4d3ceb1cbdfdf19 b/.idea/sonarlint/issuestore/5/8/58d82e459ad700473925afc6a4d3ceb1cbdfdf19 deleted file mode 100644 index e69de29..0000000 diff --git a/.idea/sonarlint/issuestore/9/9/994b95f5d0e2f9641bf4e2c30422e6a0a6a105b5 b/.idea/sonarlint/issuestore/9/9/994b95f5d0e2f9641bf4e2c30422e6a0a6a105b5 deleted file mode 100644 index e69de29..0000000 diff --git a/.idea/sonarlint/issuestore/a/d/ad8b439416d1e02614f47c5b471c7c4e587dca82 b/.idea/sonarlint/issuestore/a/d/ad8b439416d1e02614f47c5b471c7c4e587dca82 deleted file mode 100644 index e69de29..0000000 diff --git a/.idea/sonarlint/issuestore/b/5/b5aea0f996f6645c77ef6ac38e15316374d0fba2 b/.idea/sonarlint/issuestore/b/5/b5aea0f996f6645c77ef6ac38e15316374d0fba2 deleted file mode 100644 index e69de29..0000000 diff --git a/.idea/sonarlint/issuestore/index.pb b/.idea/sonarlint/issuestore/index.pb deleted file mode 100644 index dae00e9..0000000 --- a/.idea/sonarlint/issuestore/index.pb +++ /dev/null @@ -1,13 +0,0 @@ - -Q -!src/Controller/HomeController.php,a\d\ad8b439416d1e02614f47c5b471c7c4e587dca82 -H -templates/home.html.twig,3\8\38ac5fc300d52f94b369e87fbb1937abe29f831f -V -&assets/controllers/hello_controller.js,b\5\b5aea0f996f6645c77ef6ac38e15316374d0fba2 -C -assets/bootstrap.js,0\3\03141036569b5869397180192f8b98c1360e1556 -E -assets/styles/app.css,5\8\58d82e459ad700473925afc6a4d3ceb1cbdfdf19 -E -public/files/text.txt,9\9\994b95f5d0e2f9641bf4e2c30422e6a0a6a105b5 \ No newline at end of file diff --git a/.idea/sonarlint/securityhotspotstore/0/3/03141036569b5869397180192f8b98c1360e1556 b/.idea/sonarlint/securityhotspotstore/0/3/03141036569b5869397180192f8b98c1360e1556 deleted file mode 100644 index e69de29..0000000 diff --git a/.idea/sonarlint/securityhotspotstore/3/8/38ac5fc300d52f94b369e87fbb1937abe29f831f b/.idea/sonarlint/securityhotspotstore/3/8/38ac5fc300d52f94b369e87fbb1937abe29f831f deleted file mode 100644 index e69de29..0000000 diff --git a/.idea/sonarlint/securityhotspotstore/5/8/58d82e459ad700473925afc6a4d3ceb1cbdfdf19 b/.idea/sonarlint/securityhotspotstore/5/8/58d82e459ad700473925afc6a4d3ceb1cbdfdf19 deleted file mode 100644 index e69de29..0000000 diff --git a/.idea/sonarlint/securityhotspotstore/9/9/994b95f5d0e2f9641bf4e2c30422e6a0a6a105b5 b/.idea/sonarlint/securityhotspotstore/9/9/994b95f5d0e2f9641bf4e2c30422e6a0a6a105b5 deleted file mode 100644 index e69de29..0000000 diff --git a/.idea/sonarlint/securityhotspotstore/a/d/ad8b439416d1e02614f47c5b471c7c4e587dca82 b/.idea/sonarlint/securityhotspotstore/a/d/ad8b439416d1e02614f47c5b471c7c4e587dca82 deleted file mode 100644 index e69de29..0000000 diff --git a/.idea/sonarlint/securityhotspotstore/b/5/b5aea0f996f6645c77ef6ac38e15316374d0fba2 b/.idea/sonarlint/securityhotspotstore/b/5/b5aea0f996f6645c77ef6ac38e15316374d0fba2 deleted file mode 100644 index e69de29..0000000 diff --git a/.idea/sonarlint/securityhotspotstore/index.pb b/.idea/sonarlint/securityhotspotstore/index.pb deleted file mode 100644 index dae00e9..0000000 --- a/.idea/sonarlint/securityhotspotstore/index.pb +++ /dev/null @@ -1,13 +0,0 @@ - -Q -!src/Controller/HomeController.php,a\d\ad8b439416d1e02614f47c5b471c7c4e587dca82 -H -templates/home.html.twig,3\8\38ac5fc300d52f94b369e87fbb1937abe29f831f -V -&assets/controllers/hello_controller.js,b\5\b5aea0f996f6645c77ef6ac38e15316374d0fba2 -C -assets/bootstrap.js,0\3\03141036569b5869397180192f8b98c1360e1556 -E -assets/styles/app.css,5\8\58d82e459ad700473925afc6a4d3ceb1cbdfdf19 -E -public/files/text.txt,9\9\994b95f5d0e2f9641bf4e2c30422e6a0a6a105b5 \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 8306744..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file -- GitLab From 30785d4f5c76391f9ab3bf4f4d2aa21cf72b6644 Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Fri, 12 Apr 2024 16:54:30 +0500 Subject: [PATCH 03/13] refactoring --- .gitignore | 1 + src/Action/Functions.php | 219 ++++++++++++++++++++++++++++++ src/Controller/HomeController.php | 32 ++--- 3 files changed, 232 insertions(+), 20 deletions(-) create mode 100644 src/Action/Functions.php diff --git a/.gitignore b/.gitignore index 4daae38..930e1e2 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ /public/assets/ /assets/vendor/ ###< symfony/asset-mapper ### +/.idea \ No newline at end of file diff --git a/src/Action/Functions.php b/src/Action/Functions.php new file mode 100644 index 0000000..51b043b --- /dev/null +++ b/src/Action/Functions.php @@ -0,0 +1,219 @@ +12, 'count'=>4], ['price'=>10, 'count'=>2], ['price'=>8, 'count'=>4], + * ['price'=>8, 'count'=>5], ['price'=>5, 'count'=>5],] + */ + + /** + * Найдет элемент с указаным id + * @param array $array - массив, содержащий элементы со структурой + * [ + * 'id' => 30, + * 'name' => 'Jhon', + * 'age' => 23, + * ] + * @param $id - ид искомого элемента + * @return ?array - найденный элемент + */ + + public function search(array $array, int $id): ?array + { + $rowId = array_search($id, array_column($array, 'id'), false); + if ($rowId !== false) { + return $array[$rowId]; + } + return null; + } + + /** + * Удалить дубликаты, оставив только уникальные значения + * @param array $array + * @return array + */ + + public function uniqElements(array $array): array + { + return array_unique($array, SORT_REGULAR); + } + + /** + * Выходной массив: + * Array ( + * [0] => Array([0] => laravel, [1] => php) + * [1] => Array([0] => codeigniter, [1] => php) + * [3] => Array([0] => c++, [1] => java)) + * ) + */ + + /** + * Сгруппировать подразедлы в верхние разделы меню + * Дочерние элементы поместить в массив родителя с ключом submenu + * Значение под ключом depth определяет уровень раздела + * Массив $aMenu всегда начинается с элемента depth = 0, + * все последующие элементы с depth = 1 являются его дочерними + * элементами + * @param array $aMenu + * @return array + */ + + public function prepareMenu(array $aMenu): array + { + $result = []; + foreach ($aMenu as $arr) { + if ($arr['depth'] == 0) { + $result[] = array( + 'name' => $arr['name'], + 'depth' => $arr['depth'], + 'submenu' => [] + ); + continue; + } + $result[array_key_last($result)]['submenu'][] = array( + 'name' => $arr['name'], + 'depth' => $arr['depth'], + ); + } + return $result; + } + + /** + * Выходные данные: + * $aMenu = [ + * [ + * 'name' => 'Смартфоны и гаджеты', + * 'depth' => 0, + * 'submenu' => [ + * ['name' => 'Смартфоны, мобильные телефоны','depth' => 1,], + * ['name' => 'Планшеты','depth' => 1,], + * ['name' => 'Наушники и гарнитуры','depth' => 1,],], + * ], + * [ + * 'name' => 'Компьютеры и ноутбуки', + * 'depth' => 0, + * 'submenu' => [ + * ['name' => 'Ноутбуки и аксессуары','depth' => 1,], + * ['name' => 'Компьютеры и мониторы','depth' => 1,], + * ['name' => 'Компьютерные комплектующие','depth' => 1,],]], + * [ + * 'name' => 'Техника для дома', + * 'depth' => 0, + * 'submenu' => [ + * ['name' => 'Техника для уборки','depth' => 1,], + * ['name' => 'Товары для ухода за одеждой','depth' => 1,], + * ['name' => 'Аксессуары для техники','depth' => 1,],] + * ], + * [ + * 'name' => 'Товары для дома и кухни', + * 'depth' => 0, + * 'submenu' => [ + * ['name' => 'Посуда','depth' => 1,],]], + * ]; + */ + + /** + * Функция рассчитывает кол-во дней до нового года + * @param DateTimeImmutable $date дата от которой, необходимо рассчитать кол-во дней + * @return int + * @throws Exception + */ + + public function howDaysToNy(DateTimeImmutable $date): int + { + $endYear = date("Y-12-31", date_timestamp_get($date)); + $dateInterval = date_diff(new DateTimeImmutable($endYear), $date); + return (int)$dateInterval->format("%a") + 1; + } + + + /** + * Вернет все пятницы 13 в году + * @param int $year год, в котором необходимо произвести расчет + * @return DateTimeImmutable[] + * @throws Exception + */ + + public function countFriday13(int $year): iterable + { + $startDate = new DateTime("$year-01-01 Friday"); + ++$year; + $endDate = new DateTime("$year-01-01"); + $interval = new DateInterval('P7D'); + foreach (new DatePeriod($startDate, $interval, $endDate) as $day) { + yield new DateTimeImmutable($day->format("Y-m-d")); + } + } + + /** + * Вернет кол-во дней между датами + * @param DateTimeImmutable $dateStart дата начала + * @param DateTimeImmutable $dateEnd дата окончания + * @return int + */ + public function diffDays(DateTimeImmutable $dateStart, DateTimeImmutable $dateEnd): int + { + $dateInterval = date_diff($dateStart, $dateEnd); + return (int)$dateInterval->format("%a") ; + } + + /** + * Напиши функцию, которая принимает путь до файла, + * проверяет, что файл существует и выводит пользователю весь контент файла + * (файл можешь создать любой) + * @param string $filePath путь до файла + * @return string + * @throws RuntimeException + */ + + public function readLogFile(string $filePath): string + { + if (file_exists($filePath)) { + $text = ""; + $file = fopen($filePath, 'rb'); + while(!feof($file)) { + $line = fgets($file); + $text .= $line; + } + fclose($file); + return $text; + } + else { + throw new RuntimeException("File not found: $filePath"); + } + } +} + diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 51d19fa..803667d 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -2,36 +2,28 @@ namespace App\Controller; -use DateInterval; -use DatePeriod; -use DateTime; -use DateTimeImmutable; +use App\Action\Functions; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; class HomeController extends AbstractController { - private function readLogFile(string $fileName): string { - $filePath = $this->getParameter('kernel.project_dir') . "/public/files/" . $fileName; - if (file_exists($filePath)) { - $text = ""; - $file = fopen($filePath, "r"); - while(!feof($file)) { - $line = fgets($file); - $text .= $line; - } - fclose($file); - return $text; - } - return "Такого файла не существует."; - } + private Functions $functions; + public function __construct(Functions $functions) + { + $this->functions = $functions; + } #[Route('/{fileName}', name: 'home')] public function home(string $fileName): Response // text.txt { - $text = $this->readLogFile($fileName); - return $this->render('home.html.twig', ['text' => $text]); + $filePath = $this->getParameter('kernel.project_dir') . "/public/files/"; + $text = $this->functions->readLogFile($filePath . $fileName); + $response = new JsonResponse($text); + $response->setEncodingOptions(JSON_UNESCAPED_UNICODE); + return $response; } } -- GitLab From c6ab1ce2474bf0780a48dfa8349ce953653489c7 Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Fri, 12 Apr 2024 18:41:20 +0500 Subject: [PATCH 04/13] refactoring --- src/Controller/HomeController.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index e2899a3..708cb5e 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -7,6 +7,7 @@ use App\Validation\{ArrayValidation, DateValidation}; use DateTimeImmutable; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Attribute\Route; @@ -87,7 +88,7 @@ class HomeController extends AbstractController return $this->json($fridays); } - #[Route('/func7/{startDate}/{endDate}', name: 'home')] // 01-01-2024 + #[Route('/func7/{startDate}/{endDate}', name: 'func7')] // 01-01-2024 public function func7(string $startDate, string $endDate): Response { if (DateValidation::validate($startDate) && DateValidation::validate($endDate)) { @@ -104,8 +105,8 @@ class HomeController extends AbstractController return new Response("Invalid date format"); } - #[Route('/{fileName}', name: 'home')] - public function home(string $fileName): Response // text.txt + #[Route('/func8/{fileName}', name: 'func8')] + public function func8(string $fileName): Response // text.txt { $filePath = $this->getParameter('kernel.project_dir') . "/public/files/"; $text = $this->functions->readLogFile($filePath . $fileName); -- GitLab From a1f251233940843796239f015343d89bdcde04d2 Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Tue, 16 Apr 2024 10:13:42 +0500 Subject: [PATCH 05/13] temp commit --- src/Requests/BaseRequest.php | 62 ++++++++++++++++++++++++++++ src/Requests/DiffDaysRequest.php | 14 +++++++ src/Requests/MenuRequest.php | 16 +++++++ src/Requests/SearchRequest.php | 20 +++++++++ src/Requests/SortPriceRequest.php | 16 +++++++ src/Requests/UniqElementsRequest.php | 16 +++++++ src/Validation/ArrayValidation.php | 26 ------------ src/Validation/DateValidation.php | 14 ------- 8 files changed, 144 insertions(+), 40 deletions(-) create mode 100644 src/Requests/BaseRequest.php create mode 100644 src/Requests/DiffDaysRequest.php create mode 100644 src/Requests/MenuRequest.php create mode 100644 src/Requests/SearchRequest.php create mode 100644 src/Requests/SortPriceRequest.php create mode 100644 src/Requests/UniqElementsRequest.php delete mode 100644 src/Validation/ArrayValidation.php delete mode 100644 src/Validation/DateValidation.php diff --git a/src/Requests/BaseRequest.php b/src/Requests/BaseRequest.php new file mode 100644 index 0000000..9bebc7d --- /dev/null +++ b/src/Requests/BaseRequest.php @@ -0,0 +1,62 @@ +populate(); + + if ($this->autoValidateRequest()) { + $this->validate(); + } + } + + public function validate(): void + { + $errors = $this->validator->validate($this); + + $messages = ['message' => 'validation_failed', 'errors' => []]; + + /** @var ConstraintViolation $errors */ + foreach ($errors as $message) { + $messages['errors'][] = [ + 'property' => $message->getPropertyPath(), + 'value' => $message->getInvalidValue(), + 'message' => $message->getMessage(), + ]; + } + + if (count($messages['errors']) > 0) { + $response = new JsonResponse($messages, 201); + $response->send(); + + exit; + } + } + + public function getRequest(): Request + { + return Request::createFromGlobals(); + } + + protected function populate(): void + { + foreach ($this->getRequest()->request->all() as $property => $value) { + if (property_exists($this, $property)) { + $this->{$property} = $value; + } + } + } + + protected function autoValidateRequest(): bool + { + return true; + } +} \ No newline at end of file diff --git a/src/Requests/DiffDaysRequest.php b/src/Requests/DiffDaysRequest.php new file mode 100644 index 0000000..8cba54e --- /dev/null +++ b/src/Requests/DiffDaysRequest.php @@ -0,0 +1,14 @@ + new Assert\Type('string'), + 'depth' => new Assert\Type('int'), + ]) + ])] + public array $items; +} \ No newline at end of file diff --git a/src/Requests/SearchRequest.php b/src/Requests/SearchRequest.php new file mode 100644 index 0000000..eb03843 --- /dev/null +++ b/src/Requests/SearchRequest.php @@ -0,0 +1,20 @@ + new Assert\Type('int'), + 'name' => new Assert\Type('string'), + 'age' => new Assert\Type('int'), + ]) + ])] + public array $items; + + #[Assert\Type('int')] + public int $id; +} \ No newline at end of file diff --git a/src/Requests/SortPriceRequest.php b/src/Requests/SortPriceRequest.php new file mode 100644 index 0000000..38bdf00 --- /dev/null +++ b/src/Requests/SortPriceRequest.php @@ -0,0 +1,16 @@ + new Assert\Type('int'), + 'count' => new Assert\Type('int') + ]) + ])] + public array $items; +} \ No newline at end of file diff --git a/src/Requests/UniqElementsRequest.php b/src/Requests/UniqElementsRequest.php new file mode 100644 index 0000000..f53156b --- /dev/null +++ b/src/Requests/UniqElementsRequest.php @@ -0,0 +1,16 @@ + Date: Tue, 16 Apr 2024 10:13:54 +0500 Subject: [PATCH 06/13] temp commit --- composer.json | 2 +- composer.lock | 107 ++++++++++++++++-------------- src/Action/Functions.php | 65 ++---------------- src/Controller/HomeController.php | 102 ++++++++++++++-------------- 4 files changed, 113 insertions(+), 163 deletions(-) diff --git a/composer.json b/composer.json index be86947..5246c9a 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,7 @@ "symfony/translation": "7.0.*", "symfony/twig-bundle": "7.0.*", "symfony/ux-turbo": "^2.16", - "symfony/validator": "7.0.*", + "symfony/validator": "6.4.*", "symfony/web-link": "7.0.*", "symfony/yaml": "7.0.*", "twig/extra-bundle": "^2.12|^3.0", diff --git a/composer.lock b/composer.lock index 5695357..7c3d7d0 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "168d5f8561d288fe5cd6fd2b406687e2", + "content-hash": "91b0c89268c08e0b881610c8ba320eb8", "packages": [ { "name": "composer/semver", @@ -1375,16 +1375,16 @@ }, { "name": "monolog/monolog", - "version": "3.5.0", + "version": "3.6.0", "source": { "type": "git", "url": "https://github.com/Seldaek/monolog.git", - "reference": "c915e2634718dbc8a4a15c61b0e62e7a44e14448" + "reference": "4b18b21a5527a3d5ffdac2fd35d3ab25a9597654" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Seldaek/monolog/zipball/c915e2634718dbc8a4a15c61b0e62e7a44e14448", - "reference": "c915e2634718dbc8a4a15c61b0e62e7a44e14448", + "url": "https://api.github.com/repos/Seldaek/monolog/zipball/4b18b21a5527a3d5ffdac2fd35d3ab25a9597654", + "reference": "4b18b21a5527a3d5ffdac2fd35d3ab25a9597654", "shasum": "" }, "require": { @@ -1407,7 +1407,7 @@ "phpstan/phpstan": "^1.9", "phpstan/phpstan-deprecation-rules": "^1.0", "phpstan/phpstan-strict-rules": "^1.4", - "phpunit/phpunit": "^10.1", + "phpunit/phpunit": "^10.5.17", "predis/predis": "^1.1 || ^2", "ruflin/elastica": "^7", "symfony/mailer": "^5.4 || ^6", @@ -1460,7 +1460,7 @@ ], "support": { "issues": "https://github.com/Seldaek/monolog/issues", - "source": "https://github.com/Seldaek/monolog/tree/3.5.0" + "source": "https://github.com/Seldaek/monolog/tree/3.6.0" }, "funding": [ { @@ -1472,7 +1472,7 @@ "type": "tidelift" } ], - "time": "2023-10-27T15:32:31+00:00" + "time": "2024-04-12T21:02:21+00:00" }, { "name": "phpdocumentor/reflection-common", @@ -1529,28 +1529,35 @@ }, { "name": "phpdocumentor/reflection-docblock", - "version": "5.3.0", + "version": "5.4.0", "source": { "type": "git", "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", - "reference": "622548b623e81ca6d78b721c5e029f4ce664f170" + "reference": "298d2febfe79d03fe714eb871d5538da55205b1a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/622548b623e81ca6d78b721c5e029f4ce664f170", - "reference": "622548b623e81ca6d78b721c5e029f4ce664f170", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/298d2febfe79d03fe714eb871d5538da55205b1a", + "reference": "298d2febfe79d03fe714eb871d5538da55205b1a", "shasum": "" }, "require": { + "doctrine/deprecations": "^1.1", "ext-filter": "*", - "php": "^7.2 || ^8.0", + "php": "^7.4 || ^8.0", "phpdocumentor/reflection-common": "^2.2", - "phpdocumentor/type-resolver": "^1.3", + "phpdocumentor/type-resolver": "^1.7", + "phpstan/phpdoc-parser": "^1.7", "webmozart/assert": "^1.9.1" }, "require-dev": { - "mockery/mockery": "~1.3.2", - "psalm/phar": "^4.8" + "mockery/mockery": "~1.3.5", + "phpstan/extension-installer": "^1.1", + "phpstan/phpstan": "^1.8", + "phpstan/phpstan-mockery": "^1.1", + "phpstan/phpstan-webmozart-assert": "^1.2", + "phpunit/phpunit": "^9.5", + "vimeo/psalm": "^5.13" }, "type": "library", "extra": { @@ -1574,15 +1581,15 @@ }, { "name": "Jaap van Otterdijk", - "email": "account@ijaap.nl" + "email": "opensource@ijaap.nl" } ], "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", "support": { "issues": "https://github.com/phpDocumentor/ReflectionDocBlock/issues", - "source": "https://github.com/phpDocumentor/ReflectionDocBlock/tree/5.3.0" + "source": "https://github.com/phpDocumentor/ReflectionDocBlock/tree/5.4.0" }, - "time": "2021-10-19T17:43:47+00:00" + "time": "2024-04-09T21:13:58+00:00" }, { "name": "phpdocumentor/type-resolver", @@ -6820,53 +6827,55 @@ }, { "name": "symfony/validator", - "version": "v7.0.6", + "version": "v6.4.6", "source": { "type": "git", "url": "https://github.com/symfony/validator.git", - "reference": "a2df2c63b7944a162dee86ab8065f2f91b7d6e36" + "reference": "ca1d78e8677e966e307a63799677b64b194d735d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/validator/zipball/a2df2c63b7944a162dee86ab8065f2f91b7d6e36", - "reference": "a2df2c63b7944a162dee86ab8065f2f91b7d6e36", + "url": "https://api.github.com/repos/symfony/validator/zipball/ca1d78e8677e966e307a63799677b64b194d735d", + "reference": "ca1d78e8677e966e307a63799677b64b194d735d", "shasum": "" }, "require": { - "php": ">=8.2", + "php": ">=8.1", + "symfony/deprecation-contracts": "^2.5|^3", "symfony/polyfill-ctype": "~1.8", "symfony/polyfill-mbstring": "~1.0", "symfony/polyfill-php83": "^1.27", "symfony/translation-contracts": "^2.5|^3" }, "conflict": { + "doctrine/annotations": "<1.13", "doctrine/lexer": "<1.1", - "symfony/dependency-injection": "<6.4", - "symfony/doctrine-bridge": "<7.0", - "symfony/expression-language": "<6.4", - "symfony/http-kernel": "<6.4", - "symfony/intl": "<6.4", - "symfony/property-info": "<6.4", - "symfony/translation": "<6.4.3|>=7.0,<7.0.3", - "symfony/yaml": "<6.4" + "symfony/dependency-injection": "<5.4", + "symfony/expression-language": "<5.4", + "symfony/http-kernel": "<5.4", + "symfony/intl": "<5.4", + "symfony/property-info": "<5.4", + "symfony/translation": "<5.4.35|>=6.0,<6.3.12|>=6.4,<6.4.3|>=7.0,<7.0.3", + "symfony/yaml": "<5.4" }, "require-dev": { + "doctrine/annotations": "^1.13|^2", "egulias/email-validator": "^2.1.10|^3|^4", - "symfony/cache": "^6.4|^7.0", - "symfony/config": "^6.4|^7.0", - "symfony/console": "^6.4|^7.0", - "symfony/dependency-injection": "^6.4|^7.0", - "symfony/expression-language": "^6.4|^7.0", - "symfony/finder": "^6.4|^7.0", - "symfony/http-client": "^6.4|^7.0", - "symfony/http-foundation": "^6.4|^7.0", - "symfony/http-kernel": "^6.4|^7.0", - "symfony/intl": "^6.4|^7.0", - "symfony/mime": "^6.4|^7.0", - "symfony/property-access": "^6.4|^7.0", - "symfony/property-info": "^6.4|^7.0", - "symfony/translation": "^6.4.3|^7.0.3", - "symfony/yaml": "^6.4|^7.0" + "symfony/cache": "^5.4|^6.0|^7.0", + "symfony/config": "^5.4|^6.0|^7.0", + "symfony/console": "^5.4|^6.0|^7.0", + "symfony/dependency-injection": "^5.4|^6.0|^7.0", + "symfony/expression-language": "^5.4|^6.0|^7.0", + "symfony/finder": "^5.4|^6.0|^7.0", + "symfony/http-client": "^5.4|^6.0|^7.0", + "symfony/http-foundation": "^5.4|^6.0|^7.0", + "symfony/http-kernel": "^5.4|^6.0|^7.0", + "symfony/intl": "^5.4|^6.0|^7.0", + "symfony/mime": "^5.4|^6.0|^7.0", + "symfony/property-access": "^5.4|^6.0|^7.0", + "symfony/property-info": "^5.4|^6.0|^7.0", + "symfony/translation": "^5.4.35|~6.3.12|^6.4.3|^7.0.3", + "symfony/yaml": "^5.4|^6.0|^7.0" }, "type": "library", "autoload": { @@ -6894,7 +6903,7 @@ "description": "Provides tools to validate values", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/validator/tree/v7.0.6" + "source": "https://github.com/symfony/validator/tree/v6.4.6" }, "funding": [ { @@ -6910,7 +6919,7 @@ "type": "tidelift" } ], - "time": "2024-03-28T09:20:36+00:00" + "time": "2024-03-27T22:00:14+00:00" }, { "name": "symfony/var-dumper", diff --git a/src/Action/Functions.php b/src/Action/Functions.php index 34e4ecd..b91efe4 100644 --- a/src/Action/Functions.php +++ b/src/Action/Functions.php @@ -4,11 +4,12 @@ declare(strict_types=1); namespace App\Action; +use Exception; +use RuntimeException; use DateTimeImmutable; use DateTime; -use DateInterval; use DatePeriod; -use Exception; +use DateInterval; class Functions { @@ -17,7 +18,6 @@ class Functions * @param array $array * @return array */ - public function sortPrice(array $array): array { $prices = array_column($array, 'price'); @@ -31,13 +31,6 @@ class Functions return $array; } - /** - * На выход должна вернуть отсортированный массив по ключу *price* DESC - * и во вторую очередь по *count* ASC: - * [['price'=>12, 'count'=>4], ['price'=>10, 'count'=>2], ['price'=>8, 'count'=>4], - * ['price'=>8, 'count'=>5], ['price'=>5, 'count'=>5],] - */ - /** * Найдет элемент с указаным id * @param array $array - массив, содержащий элементы со структурой @@ -64,21 +57,11 @@ class Functions * @param array $array * @return array */ - public function uniqElements(array $array): array { return array_unique($array, SORT_REGULAR); } - /** - * Выходной массив: - * Array ( - * [0] => Array([0] => laravel, [1] => php) - * [1] => Array([0] => codeigniter, [1] => php) - * [3] => Array([0] => c++, [1] => java)) - * ) - */ - /** * Сгруппировать подразедлы в верхние разделы меню * Дочерние элементы поместить в массив родителя с ключом submenu @@ -89,7 +72,6 @@ class Functions * @param array $aMenu * @return array */ - public function prepareMenu(array $aMenu): array { $result = []; @@ -110,47 +92,12 @@ class Functions return $result; } - /** - * Выходные данные: - * $aMenu = [ - * [ - * 'name' => 'Смартфоны и гаджеты', - * 'depth' => 0, - * 'submenu' => [ - * ['name' => 'Смартфоны, мобильные телефоны','depth' => 1,], - * ['name' => 'Планшеты','depth' => 1,], - * ['name' => 'Наушники и гарнитуры','depth' => 1,],], - * ], - * [ - * 'name' => 'Компьютеры и ноутбуки', - * 'depth' => 0, - * 'submenu' => [ - * ['name' => 'Ноутбуки и аксессуары','depth' => 1,], - * ['name' => 'Компьютеры и мониторы','depth' => 1,], - * ['name' => 'Компьютерные комплектующие','depth' => 1,],]], - * [ - * 'name' => 'Техника для дома', - * 'depth' => 0, - * 'submenu' => [ - * ['name' => 'Техника для уборки','depth' => 1,], - * ['name' => 'Товары для ухода за одеждой','depth' => 1,], - * ['name' => 'Аксессуары для техники','depth' => 1,],] - * ], - * [ - * 'name' => 'Товары для дома и кухни', - * 'depth' => 0, - * 'submenu' => [ - * ['name' => 'Посуда','depth' => 1,],]], - * ]; - */ - /** * Функция рассчитывает кол-во дней до нового года * @param DateTimeImmutable $date дата от которой, необходимо рассчитать кол-во дней * @return int * @throws Exception */ - public function howDaysToNy(DateTimeImmutable $date): int { $endYear = date("Y-12-31", date_timestamp_get($date)); @@ -164,7 +111,6 @@ class Functions * @return DateTimeImmutable[] * @throws Exception */ - public function countFriday13(int $year): iterable { $startDate = new DateTime("$year-01-01 Friday"); @@ -196,7 +142,6 @@ class Functions * @return string * @throws RuntimeException */ - public function readLogFile(string $filePath): string { if (file_exists($filePath)) { @@ -209,8 +154,6 @@ class Functions fclose($file); return $text; } - else { - throw new RuntimeException("File not found: $filePath"); - } + throw new RuntimeException("File not found: $filePath"); } } \ No newline at end of file diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 708cb5e..38e1feb 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -3,67 +3,64 @@ namespace App\Controller; use App\Action\Functions; -use App\Validation\{ArrayValidation, DateValidation}; +use App\Requests\{ + SortPriceRequest, + SearchRequest, + UniqElementsRequest, + MenuRequest, + DiffDaysRequest +}; use DateTimeImmutable; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Attribute\Route; class HomeController extends AbstractController { - private Functions $functions; + public function __construct(private Functions $functions) {} - public function __construct(Functions $functions) + #[Route('/sortPrice', name: 'sortPrice', methods: ['POST'])] + public function sortPrice(SortPriceRequest $request): Response { - $this->functions = $functions; - } - - #[Route('/func1', name: 'home', methods: ['POST'])] - public function func1(Request $request): Response - { - $array = $request->get('arr'); - if (!ArrayValidation::validateFunc1($array)) { - return new Response("Invalid array"); - } - $array = $this->functions->sortPrice($array); + $array = $this->functions->sortPrice($request->getRequest()->toArray()['items']); return $this->json($array); } - #[Route('/func2', name: 'func2', methods: ['POST'])] - public function func2(Request $request): Response + #[Route('/search', name: 'search', methods: ['POST'])] + public function search(SearchRequest $request): Response { - $id = $request->query->getInt('id'); - $array = $request->get('arr'); - if (!ArrayValidation::validateFunc2($array)) { - return new Response("Invalid array"); - } + $array = $request->getRequest()->toArray()['items']; + $id = $request->getRequest()->query->get('id'); $result = $this->functions->search($array, $id); return $this->json($result); } - #[Route('/func3', name: 'func3', methods: ['POST'])] - public function home(Request $request): Response + #[Route('/uniqElements', name: 'uniqElements', methods: ['POST'])] + public function uniqElements(UniqElementsRequest $request): Response { - $array = $request->get('arr'); - $result = $this->functions->uniqElements($array); + $result = $this->functions->uniqElements($request->getRequest()->toArray()['items']); return $this->json($result); } - #[Route('/func4', name: 'func4', methods: ['POST'])] - public function func4(Request $request): Response + #[Route('/prepareMenu', name: 'prepareMenu', methods: ['POST'])] + public function prepareMenu(MenuRequest $request): Response { - $array = $request->get('arr'); - if (!ArrayValidation::validateFunc4($array)) { - return new Response("Invalid array"); - } - $result = $this->functions->prepareMenu($array); + $result = $this->functions->prepareMenu($request->getRequest()->toArray()['items']); return $this->json($result); } - #[Route('/func5/{day}/{month}/{year}', name: 'func5')] - public function func5(int $day, int $month, int $year): Response + #[Route( + '/howDaysToNy/{day}/{month}/{year}', + name: 'howDaysToNy', + requirements: [ + 'day' => '(? '(? '(?json(["Days before NY:" => $result]); } - #[Route('/func6/{year}', name: 'func6', methods: ['GET'])] - public function func6(int $year): Response + #[Route( + '/countFriday13/{year}', + name: 'countFriday13', + requirements: ['year' => '(?json($fridays); } - #[Route('/func7/{startDate}/{endDate}', name: 'func7')] // 01-01-2024 - public function func7(string $startDate, string $endDate): Response + #[Route('/diffDays/', name: 'diffDays')] // 01-01-2024 + public function diffDays(DiffDaysRequest $request): Response { - if (DateValidation::validate($startDate) && DateValidation::validate($endDate)) { - try { - $result = $this->functions->diffDays( - new DateTimeImmutable($startDate), - new DateTimeImmutable($endDate) - ); - return $this->json(["The difference of days:" => $result]); - } catch (\Exception $e) { - return new Response($e->getMessage()); - } + try { + $result = $this->functions->diffDays( + new DateTimeImmutable($request->getRequest()->get('startDate')), + new DateTimeImmutable($request->getRequest()->get('endDate')), + ); + return $this->json(["The difference of days:" => $result]); + } catch (\Exception $e) { + return new Response($e->getMessage()); } - return new Response("Invalid date format"); } - #[Route('/func8/{fileName}', name: 'func8')] - public function func8(string $fileName): Response // text.txt + #[Route('/readLogFile', name: 'readLogFile')] + public function readLogFile(Request $request): Response { $filePath = $this->getParameter('kernel.project_dir') . "/public/files/"; $text = $this->functions->readLogFile($filePath . $fileName); -- GitLab From 4535963ada6b1c5257b8a08146f4b7861389b05b Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Tue, 16 Apr 2024 12:30:36 +0500 Subject: [PATCH 07/13] fix --- public/files/text.txt | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 public/files/text.txt diff --git a/public/files/text.txt b/public/files/text.txt deleted file mode 100644 index 3ba1fcf..0000000 --- a/public/files/text.txt +++ /dev/null @@ -1,2 +0,0 @@ -Раз, два, три, четыре, -пять, шесть, семь, восемь. \ No newline at end of file -- GitLab From 22848e15cf0037ef8b0f421fc79f4757ada05bfe Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Tue, 16 Apr 2024 12:30:40 +0500 Subject: [PATCH 08/13] fix --- config/services.yaml | 3 +++ src/Controller/HomeController.php | 9 ++++++--- src/Service/FileUploader.php | 33 +++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 src/Service/FileUploader.php diff --git a/config/services.yaml b/config/services.yaml index 2d6a76f..b94693f 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -22,3 +22,6 @@ services: # add more service definitions when explicit configuration is needed # please note that last definitions always *replace* previous ones + App\Service\FileUploader: + arguments: + $targetDirectory: '%kernel.project_dir%/public/files/' \ No newline at end of file diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 38e1feb..6c3fa5d 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -3,6 +3,7 @@ namespace App\Controller; use App\Action\Functions; +use App\Service\FileUploader; use App\Requests\{ SortPriceRequest, SearchRequest, @@ -12,6 +13,7 @@ use App\Requests\{ }; use DateTimeImmutable; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Attribute\Route; @@ -104,10 +106,11 @@ class HomeController extends AbstractController } #[Route('/readLogFile', name: 'readLogFile')] - public function readLogFile(Request $request): Response + public function readLogFile(Request $request, FileUploader $fileUploader): Response { - $filePath = $this->getParameter('kernel.project_dir') . "/public/files/"; - $text = $this->functions->readLogFile($filePath . $fileName); + $file = $request->files->get('file'); + $fileName = $fileUploader->upload($file); + $text = $this->functions->readLogFile($fileName); $response = new JsonResponse($text); $response->setEncodingOptions(JSON_UNESCAPED_UNICODE); return $response; diff --git a/src/Service/FileUploader.php b/src/Service/FileUploader.php new file mode 100644 index 0000000..8bd280f --- /dev/null +++ b/src/Service/FileUploader.php @@ -0,0 +1,33 @@ +targetDirectory = $targetDirectory; + } + + public function upload(UploadedFile $file) + { + $fileName = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME) .'.'. $file->guessExtension(); + try { + $file->move($this->getTargetDirectory(), $fileName); + } catch (FileException $e) { + throw new FileException($e->getMessage()); + } + + return $this->getTargetDirectory() . $fileName; + } + + public function getTargetDirectory() + { + return $this->targetDirectory; + } +} \ No newline at end of file -- GitLab From aa8b589bfc67e304d24bd7402831e071c854fd5a Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Tue, 16 Apr 2024 14:06:35 +0500 Subject: [PATCH 09/13] add request --- src/Controller/HomeController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 84e72fc..9596f63 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -16,6 +16,7 @@ use DateTimeImmutable; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Attribute\Route; class HomeController extends AbstractController -- GitLab From 7319c21ff98eb3f3adb404c4ffd5907f7f9d78ab Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Tue, 16 Apr 2024 14:09:28 +0500 Subject: [PATCH 10/13] add fileUploader --- src/Controller/HomeController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 77e740f..96a7ab2 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -3,6 +3,7 @@ namespace App\Controller; use App\Action\Functions; +use App\Service\FileUploader; use App\Requests\{ SortPriceRequest, SearchRequest, -- GitLab From 66dc373522482611ba88369f83ae003a5ed14753 Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Tue, 16 Apr 2024 14:09:28 +0500 Subject: [PATCH 11/13] fix --- src/Action/Functions.php | 1 + src/Controller/HomeController.php | 1 + 2 files changed, 2 insertions(+) diff --git a/src/Action/Functions.php b/src/Action/Functions.php index d938c2d..b91efe4 100644 --- a/src/Action/Functions.php +++ b/src/Action/Functions.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace App\Action; use Exception; +use RuntimeException; use DateTimeImmutable; use DateTime; use DatePeriod; diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 77e740f..96a7ab2 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -3,6 +3,7 @@ namespace App\Controller; use App\Action\Functions; +use App\Service\FileUploader; use App\Requests\{ SortPriceRequest, SearchRequest, -- GitLab From 1ba873986ceaf85a0a75a6704b3e291146168ab7 Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Wed, 17 Apr 2024 12:33:40 +0500 Subject: [PATCH 12/13] fix --- compose.override.yaml | 19 ------------------ compose.yaml | 26 ------------------------ src/Controller/HomeController.php | 5 ++--- src/Service/FileUploader.php | 33 ------------------------------- 4 files changed, 2 insertions(+), 81 deletions(-) delete mode 100644 compose.override.yaml delete mode 100644 compose.yaml delete mode 100644 src/Service/FileUploader.php diff --git a/compose.override.yaml b/compose.override.yaml deleted file mode 100644 index 4ddb3ff..0000000 --- a/compose.override.yaml +++ /dev/null @@ -1,19 +0,0 @@ -version: '3' - -services: -###> doctrine/doctrine-bundle ### - database: - ports: - - "5432" -###< doctrine/doctrine-bundle ### - -###> symfony/mailer ### - mailer: - image: axllent/mailpit - ports: - - "1025" - - "8025" - environment: - MP_SMTP_AUTH_ACCEPT_ANY: 1 - MP_SMTP_AUTH_ALLOW_INSECURE: 1 -###< symfony/mailer ### diff --git a/compose.yaml b/compose.yaml deleted file mode 100644 index dd791d9..0000000 --- a/compose.yaml +++ /dev/null @@ -1,26 +0,0 @@ -version: '3' - -services: -###> doctrine/doctrine-bundle ### - database: - image: postgres:${POSTGRES_VERSION:-16}-alpine - environment: - POSTGRES_DB: ${POSTGRES_DB:-app} - # You should definitely change the password in production - POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-!ChangeMe!} - POSTGRES_USER: ${POSTGRES_USER:-app} - healthcheck: - test: ["CMD", "pg_isready"] - timeout: 5s - retries: 5 - start_period: 60s - volumes: - - database_data:/var/lib/postgresql/data:rw - # You may use a bind-mounted host directory instead, so that it is harder to accidentally remove the volume and lose all your data! - # - ./docker/db/data:/var/lib/postgresql/data:rw -###< doctrine/doctrine-bundle ### - -volumes: -###> doctrine/doctrine-bundle ### - database_data: -###< doctrine/doctrine-bundle ### diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 96a7ab2..dae67e7 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -96,11 +96,10 @@ class HomeController extends AbstractController } #[Route('/readLogFile', name: 'readLogFile')] - public function readLogFile(Request $request, FileUploader $fileUploader): Response + public function readLogFile(Request $request): Response { $file = $request->files->get('file'); - $fileName = $fileUploader->upload($file); - $text = $this->functions->readLogFile($fileName); + $text = $this->functions->readLogFile($file); $response = new JsonResponse($text); $response->setEncodingOptions(JSON_UNESCAPED_UNICODE); return $response; diff --git a/src/Service/FileUploader.php b/src/Service/FileUploader.php deleted file mode 100644 index 8bd280f..0000000 --- a/src/Service/FileUploader.php +++ /dev/null @@ -1,33 +0,0 @@ -targetDirectory = $targetDirectory; - } - - public function upload(UploadedFile $file) - { - $fileName = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME) .'.'. $file->guessExtension(); - try { - $file->move($this->getTargetDirectory(), $fileName); - } catch (FileException $e) { - throw new FileException($e->getMessage()); - } - - return $this->getTargetDirectory() . $fileName; - } - - public function getTargetDirectory() - { - return $this->targetDirectory; - } -} \ No newline at end of file -- GitLab From de0b4a88bceb381089b5a879f08098933f8c8503 Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Wed, 17 Apr 2024 15:02:12 +0500 Subject: [PATCH 13/13] fix --- config/services.yaml | 5 +---- src/Controller/HomeController.php | 3 +-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/config/services.yaml b/config/services.yaml index b94693f..c18468a 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -21,7 +21,4 @@ services: - '../src/Kernel.php' # add more service definitions when explicit configuration is needed - # please note that last definitions always *replace* previous ones - App\Service\FileUploader: - arguments: - $targetDirectory: '%kernel.project_dir%/public/files/' \ No newline at end of file + # please note that last definitions always *replace* previous ones \ No newline at end of file diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index dae67e7..0570b8c 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -3,7 +3,6 @@ namespace App\Controller; use App\Action\Functions; -use App\Service\FileUploader; use App\Requests\{ SortPriceRequest, SearchRequest, @@ -99,7 +98,7 @@ class HomeController extends AbstractController public function readLogFile(Request $request): Response { $file = $request->files->get('file'); - $text = $this->functions->readLogFile($file); + $text = $this->functions->readLogFile($file->getRealPath()); $response = new JsonResponse($text); $response->setEncodingOptions(JSON_UNESCAPED_UNICODE); return $response; -- GitLab