Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HW15: ElasticSearch - new architecture. #1163

Open
wants to merge 2 commits into
base: SYalanskiy/main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.example
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
34 changes: 32 additions & 2 deletions README.md
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)
34 changes: 34 additions & 0 deletions cli/Dockerfile
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 added cli/php.ini
Empty file.
36 changes: 36 additions & 0 deletions docker-compose.yml
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
Binary file added img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20,002 changes: 20,002 additions & 0 deletions www/books.json

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions www/composer.json
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
}
}
}
23 changes: 23 additions & 0 deletions www/console.php
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();
}
37 changes: 37 additions & 0 deletions www/src/Application.php
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 {

Check failure on line 16 in www/src/Application.php

View workflow job for this annotation

GitHub Actions / phpcs

Opening brace of a class must be on the line after the definition
private const DB_PROVIDER = 'Yalanskiy\SearchApp\Infrastructure\Db\ElasticDatabaseProvider';
private DataRepositoryInterface $dbConnection;

Check failure on line 19 in www/src/Application.php

View workflow job for this annotation

GitHub Actions / phpcs

Whitespace found at end of line
public function __construct()
{
$this->dbConnection = new (self::DB_PROVIDER)();
}

Check failure on line 24 in www/src/Application.php

View workflow job for this annotation

GitHub Actions / phpcs

Whitespace found at end of line
/**
* @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();
}
}

Check failure on line 37 in www/src/Application.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 1 newline at end of file; 0 found
37 changes: 37 additions & 0 deletions www/src/Application/AddBook.php
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 {

Check failure on line 14 in www/src/Application/AddBook.php

View workflow job for this annotation

GitHub Actions / phpcs

Opening brace of a class must be on the line after the definition
public function __construct(
private DataRepositoryInterface $provider
)
{

Check failure on line 18 in www/src/Application/AddBook.php

View workflow job for this annotation

GitHub Actions / phpcs

The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line

Check failure on line 19 in www/src/Application/AddBook.php

View workflow job for this annotation

GitHub Actions / phpcs

Whitespace found at end of line
}

Check failure on line 20 in www/src/Application/AddBook.php

View workflow job for this annotation

GitHub Actions / phpcs

Function closing brace must go on the next line following the body; found 1 blank lines before brace

Check failure on line 21 in www/src/Application/AddBook.php

View workflow job for this annotation

GitHub Actions / phpcs

Whitespace found at end of line
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);
}

Check failure on line 34 in www/src/Application/AddBook.php

View workflow job for this annotation

GitHub Actions / phpcs

Whitespace found at end of line
$this->provider->add($book);
}
}
43 changes: 43 additions & 0 deletions www/src/Application/AddBookBulk.php
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);
}
}
18 changes: 18 additions & 0 deletions www/src/Application/Dto/AddBookBulkRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Yalanskiy\SearchApp\Application\Dto;

use Yalanskiy\SearchApp\Application\ValueObject\AddBookBulkCollection;

/**
* AddBookBulkRequest
*/
class AddBookBulkRequest {
public function __construct(
public AddBookBulkCollection $books
)
{
}
}
42 changes: 42 additions & 0 deletions www/src/Application/Dto/AddBookRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Yalanskiy\SearchApp\Application\Dto;

/**
* AddBookRequest
*/
class AddBookRequest {
public string $sku;
public string $title;
public string $category;
public float $price;
public array $stock;
public string $id;

/**
* @param $sku
* @param $title
* @param $category
* @param $price
* @param $stock
* @param string $id
*/
public function __construct(
$sku,
$title,
$category,
$price,
$stock,
string $id = ''
)
{
$this->sku = $sku;
$this->title = $title;
$this->category = $category;
$this->price = $price;
$this->stock = $stock;
$this->id = $id;
}
}
17 changes: 17 additions & 0 deletions www/src/Application/Dto/FindBookRequest.php
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,
)
{

}
}
19 changes: 19 additions & 0 deletions www/src/Application/Dto/FindBookResponse.php
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,
)
{

}
}
Loading
Loading