-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HW15: ElasticSearch - new architecture.
- Loading branch information
Showing
23 changed files
with
20,995 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
ELASTIC_SERVER=otus-elasticsearch | ||
ELASTIC_USERNAME=elastic | ||
ELASTIC_PASSWORD=elastic | ||
ELASTIC_INDEX_NAME=otus-index |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,33 @@ | ||
# PHP_2023 | ||
# ElasticSearch | ||
|
||
https://otus.ru/lessons/razrabotchik-php/?utm_source=github&utm_medium=free&utm_campaign=otus | ||
Переделанная версия домашней работы по еластику, в соответствии с правильной архитектурой кода. | ||
Запуск скриптов и результаты не поменялись. | ||
|
||
Запуск PHP-CLI контейнера. | ||
Обязательно указать --network, иначе не увидит Elastic. | ||
```` | ||
docker run --rm -v ${PWD}/www:/www --env-file ${PWD}/.env --network homework_otus-network -it cli php console.php | ||
```` | ||
|
||
Загрузка данных в индекс: | ||
```` | ||
console.php load | ||
```` | ||
|
||
Поиск: | ||
```` | ||
console.php find | ||
```` | ||
|
||
Пример запроса для загрузки данных: | ||
```` | ||
docker run --rm -v ${PWD}/www:/www --env-file ${PWD}/.env --network homework_otus-network -it cli php console.php load | ||
```` | ||
|
||
Пример запроса для поиска: | ||
```` | ||
docker run --rm -v ${PWD}/www:/www --env-file ${PWD}/.env --network homework_otus-network -it cli php console.php find --title="рыцОри" --category="Исторический" --price="<2000" --stock=">=1" | ||
```` | ||
|
||
Скрин ответа (начало таблицы): | ||
![img.png](img.png) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
FROM php:8.2-cli | ||
|
||
RUN apt-get update && apt-get install -y \ | ||
curl \ | ||
libfreetype6-dev \ | ||
libjpeg62-turbo-dev \ | ||
libpng-dev \ | ||
libonig-dev \ | ||
libzip-dev \ | ||
libmemcached-dev libssl-dev \ | ||
libmcrypt-dev \ | ||
libpq-dev \ | ||
&& docker-php-ext-install sockets \ | ||
&& pecl install mcrypt-1.0.6 \ | ||
&& docker-php-ext-enable mcrypt \ | ||
&& docker-php-ext-install -j$(nproc) iconv mbstring mysqli pdo_mysql pdo_pgsql pgsql zip \ | ||
&& docker-php-ext-configure gd --with-freetype --with-jpeg \ | ||
&& docker-php-ext-install -j$(nproc) gd \ | ||
&& pecl install memcached \ | ||
&& docker-php-ext-enable memcached \ | ||
&& pecl install redis-5.3.7 \ | ||
&& docker-php-ext-enable redis \ | ||
&& pecl install xdebug-3.2.2 \ | ||
&& docker-php-ext-enable xdebug \ | ||
&& curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer | ||
|
||
COPY --from=composer /usr/bin/composer /usr/local/bin/composer | ||
RUN composer self-update | ||
|
||
WORKDIR /www | ||
|
||
COPY ./php.ini /usr/local/etc/php/conf.d/php-custom.ini | ||
|
||
#CMD ["php", "composer", "update"] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
version: "3" | ||
|
||
services: | ||
|
||
# php-cli service | ||
otus-cli: | ||
build: | ||
context: ./cli | ||
dockerfile: Dockerfile | ||
image: cli | ||
container_name: otus-cli | ||
volumes: | ||
- ./www:/www | ||
networks: | ||
- otus-network | ||
|
||
# ElasticSearch | ||
otus-elasticsearch: | ||
image: elasticsearch:8.7.0 | ||
container_name: ${ELASTIC_SERVER} | ||
environment: | ||
- ELASTIC_USERNAME=${ELASTIC_USERNAME} | ||
- ELASTIC_PASSWORD=${ELASTIC_PASSWORD} | ||
- node.name=otus_elasticsearch | ||
- discovery.type=single-node | ||
- http.host=0.0.0.0 | ||
- transport.host=localhost | ||
ports: | ||
- "9200:9200" | ||
networks: | ||
- otus-network | ||
|
||
# Docker Network | ||
networks: | ||
otus-network: | ||
driver: bridge |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "syalansky/homework15", | ||
"description": "Homework 15", | ||
"type": "project", | ||
"autoload": { | ||
"psr-4": { | ||
"Yalanskiy\\SearchApp\\": "src/" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Sergey Yalanskiy" | ||
} | ||
], | ||
"require": { | ||
"elasticsearch/elasticsearch": "^8.11", | ||
"symfony/console": "^7.0" | ||
}, | ||
"config": { | ||
"allow-plugins": { | ||
"php-http/discovery": true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Symfony\Component\Console\Application as ConsoleApplication; | ||
use Yalanskiy\SearchApp\Application; | ||
use Yalanskiy\SearchApp\Infrastructure\Command\LoadCommand; | ||
|
||
require __DIR__ . '/vendor/autoload.php'; | ||
|
||
const APP_ROOT = __DIR__; | ||
|
||
define("ELASTIC_SERVER", $_ENV['ELASTIC_SERVER'] ?? ''); | ||
define("ELASTIC_USERNAME", $_ENV['ELASTIC_USERNAME'] ?? ''); | ||
define("ELASTIC_PASSWORD", $_ENV['ELASTIC_PASSWORD'] ?? ''); | ||
define("ELASTIC_INDEX_NAME", $_ENV['ELASTIC_INDEX_NAME'] ?? ''); | ||
|
||
try { | ||
$application = new Application(); | ||
$application->run(); | ||
} catch (Exception $exeption) { | ||
echo $exeption->getMessage(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yalanskiy\SearchApp; | ||
|
||
use Exception; | ||
use Yalanskiy\SearchApp\Domain\Repository\DataRepositoryInterface; | ||
use Symfony\Component\Console\Application as ConsoleApplication; | ||
use Yalanskiy\SearchApp\Infrastructure\Command\FindCommand; | ||
use Yalanskiy\SearchApp\Infrastructure\Command\LoadCommand; | ||
|
||
/** | ||
* Main Application class | ||
*/ | ||
class Application { | ||
private const DB_PROVIDER = 'Yalanskiy\SearchApp\Infrastructure\Db\ElasticDatabaseProvider'; | ||
private DataRepositoryInterface $dbConnection; | ||
|
||
public function __construct() | ||
{ | ||
$this->dbConnection = new (self::DB_PROVIDER)(); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function run(): void | ||
{ | ||
$console = new ConsoleApplication('ElasticSearch New (15)', '1.0'); | ||
$console->addCommands([ | ||
new LoadCommand($this->dbConnection), | ||
new FindCommand($this->dbConnection), | ||
]); | ||
$console->run(); | ||
} | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yalanskiy\SearchApp\Application; | ||
|
||
use Yalanskiy\SearchApp\Application\Dto\AddBookRequest; | ||
use Yalanskiy\SearchApp\Domain\Entity\Book; | ||
use Yalanskiy\SearchApp\Domain\Repository\DataRepositoryInterface; | ||
|
||
/** | ||
* AddBook | ||
*/ | ||
class AddBook { | ||
public function __construct( | ||
private DataRepositoryInterface $provider | ||
) | ||
{ | ||
|
||
} | ||
|
||
public function __invoke(AddBookRequest $request): void | ||
{ | ||
$book = new Book( | ||
$request->sku, | ||
$request->title, | ||
$request->category, | ||
$request->price, | ||
$request->stock | ||
); | ||
if (!empty($request->id)) { | ||
$book->setId($request->id); | ||
} | ||
|
||
$this->provider->add($book); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yalanskiy\SearchApp\Application; | ||
|
||
use Yalanskiy\SearchApp\Application\Dto\AddBookBulkRequest; | ||
use Yalanskiy\SearchApp\Domain\Entity\Book; | ||
use Yalanskiy\SearchApp\Domain\Entity\BookCollection; | ||
use Yalanskiy\SearchApp\Domain\Repository\DataRepositoryInterface; | ||
|
||
/** | ||
* AddBookBulk | ||
*/ | ||
class AddBookBulk { | ||
public function __construct( | ||
private DataRepositoryInterface $provider | ||
) | ||
{ | ||
|
||
} | ||
|
||
public function __invoke(AddBookBulkRequest $request): void | ||
{ | ||
$data = new BookCollection(); | ||
foreach ($request->books as $item) { | ||
$book = new Book( | ||
$item->sku, | ||
$item->title, | ||
$item->category, | ||
$item->price, | ||
$item->stock | ||
); | ||
if (!empty($item->id)) { | ||
$book->setId($item->id); | ||
} | ||
|
||
$data->add($book); | ||
} | ||
|
||
$this->provider->addBulk($data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yalanskiy\SearchApp\Application\Dto; | ||
|
||
/** | ||
* AddBookBulkRequest | ||
*/ | ||
class AddBookBulkRequest { | ||
|
||
public array $books = []; | ||
|
||
public function __construct(array $books) | ||
{ | ||
foreach ($books as $book) { | ||
$this->books[] = new AddBookRequest( | ||
$book['sku'], | ||
$book['title'], | ||
$book['category'], | ||
$book['price'], | ||
$book['stock'], | ||
$book['id'] ?? '' | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yalanskiy\SearchApp\Application\Dto; | ||
|
||
/** | ||
* AddBookRequest | ||
*/ | ||
class AddBookRequest { | ||
public function __construct( | ||
public string $sku, | ||
public string $title, | ||
public string $category, | ||
public float $price, | ||
public array $stock, | ||
public string $id | ||
) | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yalanskiy\SearchApp\Application\Dto; | ||
|
||
/** | ||
* FindBookRequest | ||
*/ | ||
class FindBookRequest { | ||
public function __construct( | ||
public array $params, | ||
) | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yalanskiy\SearchApp\Application\Dto; | ||
|
||
use Yalanskiy\SearchApp\Domain\Entity\BookCollection; | ||
|
||
/** | ||
* FindBookResponse | ||
*/ | ||
class FindBookResponse { | ||
public function __construct( | ||
public BookCollection $response, | ||
) | ||
{ | ||
|
||
} | ||
} |
Oops, something went wrong.