-
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.
Merge pull request #3 from Larium/feature/mongodb-support
Feature/mongodb support
- Loading branch information
Showing
18 changed files
with
476 additions
and
34 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,34 @@ | ||
FROM php:8.3-cli | ||
|
||
WORKDIR "/opt/php" | ||
|
||
ENV DEBIAN_FRONTEND=noninteractive | ||
|
||
RUN apt-get update \ | ||
&& apt-get install -y --no-install-recommends \ | ||
libcurl4-openssl-dev \ | ||
libssl-dev \ | ||
libzip-dev \ | ||
zlib1g-dev \ | ||
unzip \ | ||
git \ | ||
&& rm -rf /var/lib/apt/lists/* \ | ||
&& apt-get clean -y | ||
|
||
RUN yes | pecl install xdebug | ||
|
||
RUN pecl channel-update pecl.php.net && \ | ||
pecl install mongodb && \ | ||
docker-php-ext-enable mongodb && \ | ||
docker-php-source delete && \ | ||
rm -r /tmp/* /var/cache/* | ||
|
||
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \ | ||
&& php composer-setup.php \ | ||
&& php -r "unlink('composer-setup.php');" \ | ||
&& chmod +x composer.phar \ | ||
&& mv composer.phar /usr/local/bin/composer | ||
|
||
RUN curl -L https://cs.symfony.com/download/php-cs-fixer-v3.phar -o php-cs-fixer \ | ||
&& chmod a+x php-cs-fixer \ | ||
&& mv php-cs-fixer /usr/bin/php-cs-fixer |
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,15 @@ | ||
set -e | ||
|
||
user=$MONGO_INITDB_USER | ||
password=$MONGO_INITDB_PWD | ||
db=$MONGO_INITDB_DATABASE | ||
|
||
mongosh -u $MONGO_INITDB_ROOT_USERNAME -p $MONGO_INITDB_ROOT_PASSWORD <<EOF | ||
use ${db} | ||
db = db.getSiblingDB("${db}") | ||
db.createUser({ | ||
user: "${user}", | ||
pwd: "${password}", | ||
roles: [{ role: "readWrite", db: "${db}"}], | ||
}) | ||
EOF |
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,8 @@ | ||
zend_extension=xdebug.so | ||
xdebug.mode=develop,coverage,debug,profile | ||
xdebug.idekey=docker | ||
xdebug.start_with_request=no | ||
xdebug.log=/dev/stdout | ||
xdebug.log_level=0 | ||
xdebug.client_port=9003 | ||
xdebug.client_host=host.docker.internal |
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,6 @@ | ||
MONGO_ROOT_USERNAME=root | ||
MONGO_ROOT_PASSWORD=s3cr3t | ||
MONGODB=test | ||
MONGODB_USER=admin | ||
MONGODB_PASSWORD=s3cr3t | ||
MONGODB_HOST=mongo-server |
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,6 @@ | ||
MONGO_ROOT_USERNAME=root | ||
MONGO_ROOT_PASSWORD= | ||
MONGODB=test | ||
MONGODB_USER=admin | ||
MONGODB_PASSWORD= | ||
MONGODB_HOST=mongo-server |
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 |
---|---|---|
|
@@ -2,4 +2,6 @@ build | |
vendor | ||
.DS_Store | ||
.phpunit.result.cache | ||
.phpunit.cache | ||
composer.lock | ||
.vscode |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
$finder = PhpCsFixer\Finder::create() | ||
->in(__DIR__) | ||
; | ||
|
||
return (new PhpCsFixer\Config()) | ||
->setUsingCache(false) | ||
->setRules([ | ||
'@PSR12' => true, | ||
'ordered_imports' => ['sort_algorithm' => 'length', 'imports_order' => ['const', 'class', 'function']], | ||
'array_syntax' => ['syntax' => 'short'], | ||
'concat_space' => ['spacing' => 'one'], | ||
'blank_line_between_import_groups' => true, | ||
])->setFinder($finder) | ||
; |
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
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 @@ | ||
version: "3.1" | ||
services: | ||
php-cli: | ||
build: .docker | ||
container_name: larium-search | ||
image: larium-search:latest | ||
working_dir: /opt/php | ||
tty: true | ||
stdin_open: true | ||
volumes: | ||
- .:/opt/php | ||
- ./.docker/xdebug.ini:/usr/local/etc/php/conf.d/15-xdebug.ini | ||
networks: | ||
- search-network | ||
mongo-server: | ||
image: mongo:latest | ||
environment: | ||
- MONGO_INITDB_ROOT_USERNAME=${MONGO_ROOT_USERNAME} | ||
- MONGO_INITDB_ROOT_PASSWORD=${MONGO_ROOT_PASSWORD} | ||
- MONGO_INITDB_DATABASE=${MONGODB} | ||
- MONGO_INITDB_USER=${MONGODB_USER} | ||
- MONGO_INITDB_PWD=${MONGODB_PASSWORD} | ||
ports: | ||
- "27022:27017" | ||
volumes: | ||
- ./.docker/mongo/init.sh:/docker-entrypoint-initdb.d/mongo-init.sh:ro | ||
- mongo-data:/data/db | ||
networks: | ||
- search-network | ||
volumes: | ||
mongo-data: | ||
networks: | ||
search-network: | ||
driver: bridge |
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
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Larium\Search\MongoDb; | ||
|
||
use Larium\Search\Criteria; | ||
|
||
interface Builder | ||
{ | ||
/** | ||
* Checks if current builder supports given criteria. | ||
*/ | ||
public function supports(Criteria $criteria): bool; | ||
|
||
/** | ||
* Apply criteria to given filter builder. | ||
*/ | ||
public function build(Criteria $criteria, FilterBuilder $filterBuilder): void; | ||
} |
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Larium\Search\MongoDb; | ||
|
||
use ArrayObject; | ||
|
||
class FilterBuilder extends ArrayObject | ||
{ | ||
} |
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,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Larium\Search\MongoDb; | ||
|
||
use MongoDB\Collection; | ||
use Larium\Search\Result; | ||
|
||
class MongoDbResult implements Result | ||
{ | ||
private array $filter = []; | ||
|
||
public function __construct( | ||
private readonly FilterBuilder $filterBuilder, | ||
private readonly Collection $collection, | ||
) { | ||
|
||
} | ||
|
||
public function fetch(int $offset, int $limit): array | ||
{ | ||
$filter = $this->normalizeFilter(); | ||
$iterator = $this->collection->find($filter, ['skip' => $offset, 'limit' => $limit]); | ||
|
||
return iterator_to_array($iterator); | ||
} | ||
|
||
public function getCountField(): ?string | ||
{ | ||
return null; | ||
} | ||
|
||
public function setCountField(string $countField): void | ||
{ | ||
} | ||
|
||
public function setCountCallable(callable $function): void | ||
{ | ||
} | ||
|
||
public function count(): int | ||
{ | ||
return $this->collection->countDocuments($this->normalizeFilter()); | ||
} | ||
|
||
private function normalizeFilter(): array | ||
{ | ||
if (!empty($this->filter)) { | ||
return $this->filter; | ||
} | ||
|
||
$this->filter = array_reduce($this->filterBuilder->getArrayCopy(), function (array $result, array $item) { | ||
$result = array_merge($result, $item); | ||
return $result; | ||
}, []); | ||
|
||
return $this->filter; | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Larium\Search\MongoDb; | ||
|
||
use MongoDB\Collection; | ||
use Larium\Search\Result; | ||
use Larium\Search\Criteria; | ||
use Larium\Search\SearchEngine; | ||
|
||
class MongoDbSearchEngine implements SearchEngine | ||
{ | ||
private $builders = []; | ||
|
||
public function __construct( | ||
private readonly FilterBuilder $filterBuilder, | ||
private readonly Collection $collection, | ||
) { | ||
} | ||
|
||
public function match(Criteria $criteria): Result | ||
{ | ||
foreach ($this->builders as $builder) { | ||
if ($builder->supports($criteria)) { | ||
$builder->build($criteria, $this->filterBuilder); | ||
} | ||
} | ||
|
||
return new MongoDbResult($this->filterBuilder, $this->collection); | ||
} | ||
|
||
public function add(Builder $builder): self | ||
{ | ||
$this->builders[] = $builder; | ||
|
||
return $this; | ||
} | ||
} |
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
Oops, something went wrong.