3
0

Compare commits

...

58 Commits

Author SHA1 Message Date
brabli efc9b17136 Fix outdated code 2026-05-05 11:00:43 +01:00
brabli d3de91ea0b Update deps 2026-05-05 11:00:29 +01:00
brabli f9700e1959 Add general composer command 2026-05-05 10:41:53 +01:00
brabli 92bd22ad99 Update container php ver 2026-05-05 10:41:46 +01:00
brabli 5548df12bd Replace makefile with justfile 2026-05-05 10:38:28 +01:00
mf 6e2c241349 Adding formatting 2023-01-09 16:19:16 +00:00
mf 802413f754 Fixing deprecation 2023-01-09 16:07:13 +00:00
mf e56ea29018 Adding batching to index command for performance 2023-01-09 16:06:37 +00:00
mf 3faec6370c Merge tag '0.0.18' into develop
Trying to load custom Doctrine config
2022-07-29 11:27:46 +01:00
mf 361cd7baf8 Merge branch 'release/0.0.18' 2022-07-29 11:27:31 +01:00
mf 6111bd07e5 Updating Doctrine settings 2022-07-29 11:27:21 +01:00
mf 76ab724d1a Merge branch 'develop' of ssh://git.pcmdev.co.uk:2222/pcm-libraries/pcm-search-bundle into develop 2022-07-29 11:18:59 +01:00
mf d7a7d0b6e8 Merge tag '0.0.17' into develop
- Adding loader for doctrine.yaml to bring in MATCH AGAINST function
2022-07-29 11:18:49 +01:00
mf 3f57c78d01 Merge branch 'release/0.0.17' 2022-07-29 11:18:35 +01:00
mf e88da82267 Added loader for doctrine.yaml to bring in MATCH AGAINST method 2022-07-29 11:18:20 +01:00
Brabli 709c641fc9 Migrate xml schema 2022-07-21 15:10:19 +01:00
Brabli ff506e2aef Add tests command 2022-07-21 15:10:05 +01:00
Brabli 702fe24be4 Add space 2022-07-21 11:51:47 +01:00
mf 319334834b Merge tag '0.0.16' into develop
Working on DI 0.0.16
2022-07-20 21:29:45 +01:00
mf 9bad1cb2b9 Merge branch 'release/0.0.16' 2022-07-20 21:29:43 +01:00
mf df927a8197 Adding doctrine.yaml config 2022-07-20 21:29:33 +01:00
mf 5c98989631 Merge tag '0.0.15' into develop
Working on DI 0.0.15
2022-07-20 21:26:13 +01:00
mf 373087a5d5 Merge branch 'release/0.0.15' 2022-07-20 21:26:11 +01:00
mf 205b29e07c Working on DependencyInjection 2022-07-20 21:26:00 +01:00
mf e1d5f094de Merge tag '0.0.14' into develop
Working on DI 0.0.14
2022-07-20 21:23:55 +01:00
mf 8145b87242 Merge branch 'release/0.0.14' 2022-07-20 21:23:53 +01:00
mf d027933ad9 Working on DependencyInjection 2022-07-20 21:23:43 +01:00
mf cbad86a01c Merge tag '0.0.13' into develop
Working on DI 0.0.13
2022-07-20 21:21:37 +01:00
mf 15bf7cb324 Merge branch 'release/0.0.13' 2022-07-20 21:21:35 +01:00
mf 1b9787aa49 Working on DependencyInjection 2022-07-20 21:21:15 +01:00
mf 9d4297659d Merge tag '0.0.12' into develop
Working on DI 0.0.12
2022-07-20 21:16:15 +01:00
mf 92842c552b Merge branch 'release/0.0.12' 2022-07-20 21:16:13 +01:00
mf 80e584e29f Working on DependencyInjection 2022-07-20 21:16:05 +01:00
mf c03d2ae9ad Merge tag '0.0.11' into develop
Working on DI 0.0.11
2022-07-20 21:15:24 +01:00
mf c27ae18aa0 Merge branch 'release/0.0.11' 2022-07-20 21:15:22 +01:00
mf 89c88520d9 Working on DependencyInjection 2022-07-20 21:15:13 +01:00
mf c19a755ed1 Merge tag '0.0.10' into develop
Working on DI 0.0.10
2022-07-20 21:09:54 +01:00
mf 12925cc7d7 Merge branch 'release/0.0.10' 2022-07-20 21:09:52 +01:00
mf 393a704f1c Working on DependencyInjection 2022-07-20 21:09:43 +01:00
mf 398e2903ea Merge tag '0.0.9' into develop
Working on DI 0.0.9
2022-07-20 21:06:17 +01:00
mf 95db65f56d Merge branch 'release/0.0.9' 2022-07-20 21:06:16 +01:00
mf 1ec9b05413 Working on DependencyInjection 2022-07-20 21:05:54 +01:00
mf 6a4ce3bef3 Merge tag '0.0.8' into develop
Working on DI
2022-07-20 21:02:39 +01:00
mf bf89b962ec Merge branch 'release/0.0.8' 2022-07-20 21:02:33 +01:00
mf 7de30e78ac Working on DependencyInjection 2022-07-20 21:02:17 +01:00
mf 6d65df0b9c Merge tag '0.0.7' into develop
Working on DependencyInjection
2022-07-20 21:01:11 +01:00
mf 5d56d98325 Merge branch 'release/0.0.7' 2022-07-20 21:01:06 +01:00
mf 2f6597a957 Working on DependencyInjection 2022-07-20 21:00:54 +01:00
mf 93bb85f69f Merge tag '0.0.6' into develop
Working on DependencyInjection
2022-07-20 20:54:28 +01:00
mf 218b174d4f Merge branch 'release/0.0.6' 2022-07-20 20:54:19 +01:00
mf 2258fa0c96 Working on DependencyInjection 2022-07-20 20:54:10 +01:00
mf 34b0e7f146 Merge tag '0.0.5' into develop
Working on DependencyInjection
2022-07-20 20:47:30 +01:00
mf ab25540462 Merge branch 'release/0.0.5' 2022-07-20 20:47:22 +01:00
mf da950685c0 Working on DependencyInjection 2022-07-20 20:47:05 +01:00
mf 954e6aa712 Merge tag '0.0.4' into develop
Working on DependencyInjection
2022-07-20 20:44:45 +01:00
mf 3f410b8f55 Merge branch 'release/0.0.4' 2022-07-20 20:44:36 +01:00
mf 78f356f030 Working on DependencyInjection 2022-07-20 20:44:25 +01:00
mf c35da20deb Merge tag '0.0.3' into develop
- Adding services.yaml
- Fleshing out the codebase further
2022-07-20 20:38:38 +01:00
12 changed files with 278 additions and 63 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
FROM php:8.1-alpine
FROM php:8.4-alpine
WORKDIR /code
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
-8
View File
@@ -1,8 +0,0 @@
PHP = docker compose run php
composer_install:
@$(PHP) composer install
static_analysis:
@$(PHP) vendor/bin/psalm
+9 -8
View File
@@ -3,16 +3,17 @@
"description": "Provides a search system for Symfony projects",
"type": "symfony-bundle",
"require": {
"php": ">=8.0",
"beberlei/doctrineextensions": "^1.3",
"doctrine/doctrine-bundle": "^2.7",
"doctrine/orm": "^2.12",
"symfony/framework-bundle": "*"
"php": ">=8.2",
"beberlei/doctrineextensions": "^1.5",
"doctrine/doctrine-bundle": "^2.13",
"doctrine/orm": "^2.20 || ^3.3",
"doctrine/persistence": "^3.3 || ^4.0",
"symfony/framework-bundle": "^6.4 || ^7.0"
},
"require-dev": {
"vimeo/psalm": "^4.24",
"psalm/plugin-symfony": "^3.1",
"phpunit/phpunit": "^9.5"
"vimeo/psalm": "^6.0",
"psalm/plugin-symfony": "^5.2",
"phpunit/phpunit": "^11.0 || ^12.0"
},
"autoload": {
"psr-4": {
+19 -2
View File
@@ -1,4 +1,21 @@
services:
Pcm\SearchBundle\EventSubscriber\SearchableSubscriber:
_defaults:
autowire: true
autoconfigure: true
pcm_search.command.index:
class: Pcm\SearchBundle\Command\SearchIndexCommand
tags:
- { name: doctrine.event_subscriber }
- { name: 'console.command', command: 'pcm:search:reindex' }
Pcm\SearchBundle\EventSubscriber\SearchableSubscriber: ~
pcm_search.search_service:
alias: Pcm\SearchBundle\Service\SearchService
public: true
Pcm\SearchBundle\Service\SearchService: ~
Pcm\SearchBundle\Repository\SearchIndexRepository:
autowire: true
tags: ['doctrine.repository_service']
+17
View File
@@ -0,0 +1,17 @@
php := "docker compose run php"
composer-install:
@{{php}} composer install
composer-update:
@{{php}} composer update
composer *args:
@{{php}} composer {{args}}
static-analysis:
@{{php}} vendor/bin/psalm
tests:
@{{php}} rm -rf var/cache
@{{php}} vendor/bin/phpunit
-13
View File
@@ -1,13 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>
</phpunit>
+19
View File
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" backupGlobals="false" colors="true" bootstrap="./vendor/autoload.php">
<coverage>
<include>
<directory>./src</directory>
</include>
</coverage>
<php>
<ini name="error_reporting" value="-1"/>
<ini name="intl.default_locale" value="en"/>
<ini name="intl.error_level" value="0"/>
<ini name="memory_limit" value="-1"/>
</php>
<testsuites>
<testsuite name="Test suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>
+36 -7
View File
@@ -2,10 +2,12 @@
namespace Pcm\SearchBundle\Command;
use Doctrine\ORM\EntityManagerInterface;
use Pcm\SearchBundle\Entity\SearchIndex;
use Pcm\SearchBundle\Service\SearchService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Attribute\AsCommand;
@@ -23,7 +25,16 @@ class SearchIndexCommand extends Command
parent::__construct();
}
protected function configure(): void {}
protected function configure(): void
{
$this->addOption(
'batchsize',
'b',
InputOption::VALUE_REQUIRED,
'Number of rows to process before flushing',
2000
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
@@ -42,29 +53,47 @@ class SearchIndexCommand extends Command
$progress_bar->setProgressCharacter("<fg=green>➤</>");
$progress_bar->start();
$batchSize = $input->getOption('batchsize');
$searchEntities = null;
foreach ($searchables as $searchable) {
$entities = $this
->em
->getRepository($searchable)
->findAll();
$searchEntities[] = [$searchable, count($entities)];
$this->em->clear();
$counter = 1;
foreach ($entities as $entity) {
$search_result = $this->searchService->createSearchResult($entity);
$this->em->persist($search_result);
if (($counter % $batchSize) === 0) {
$this->em->flush();
$this->em->clear();
}
$counter++;
}
$progress_bar->advance();
$this->em->flush();
$this->em->clear();
}
$this->em->flush();
$progress_bar->finish();
$io->writeln('');
$io->writeln('');
$table = new Table($output);
$table
->setHeaders(['Class', 'Count'])
->setRows($searchEntities)
;
$table->render();
$io->writeln('');
$io->success('Index updated');
return Command::SUCCESS;
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace Pcm\SearchBundle\DependencyInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
class PcmSearchExtension extends Extension implements PrependExtensionInterface
{
public function prepend(ContainerBuilder $container)
{
$container->loadFromExtension(
'doctrine',
[
'orm' => [
'dql' => [
'string_functions' => [
'match' => 'DoctrineExtensions\Query\Mysql\MatchAgainst'
]
]
]
]
);
}
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__.'/../../config')
);
$loader->load('services.yaml');
}
}
+11 -15
View File
@@ -6,24 +6,20 @@ namespace Pcm\SearchBundle\EventSubscriber;
use Pcm\SearchBundle\Entity\Interface\SearchableInterface;
use Pcm\SearchBundle\Service\SearchService;
use Doctrine\Common\EventSubscriber;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
use Doctrine\ORM\Event\PostPersistEventArgs;
use Doctrine\ORM\Event\PostUpdateEventArgs;
use Doctrine\ORM\Event\PreRemoveEventArgs;
use Doctrine\ORM\Events;
class SearchableSubscriber implements EventSubscriber
#[AsDoctrineListener(event: Events::postPersist)]
#[AsDoctrineListener(event: Events::postUpdate)]
#[AsDoctrineListener(event: Events::preRemove)]
class SearchableSubscriber
{
public function __construct(private SearchService $searchService) {}
public function getSubscribedEvents()
{
return [
Events::postPersist,
Events::postUpdate,
Events::preRemove
];
}
public function postUpdate(LifecycleEventArgs $args)
public function postUpdate(PostUpdateEventArgs $args): void
{
$entity = $args->getObject();
@@ -32,7 +28,7 @@ class SearchableSubscriber implements EventSubscriber
}
}
public function postPersist(LifecycleEventArgs $args)
public function postPersist(PostPersistEventArgs $args): void
{
$entity = $args->getObject();
@@ -41,7 +37,7 @@ class SearchableSubscriber implements EventSubscriber
}
}
public function preRemove(LifecycleEventArgs $args)
public function preRemove(PreRemoveEventArgs $args): void
{
$entity = $args->getObject();
+124
View File
@@ -0,0 +1,124 @@
<?php
declare(strict_types=1);
namespace Pcm\SearchBundle\Repository;
use Pcm\SearchBundle\Entity\SearchIndex;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Query;
use Doctrine\Persistence\ManagerRegistry;
class SearchIndexRepository extends ServiceEntityRepository
{
/**
* @param ManagerRegistry $registry
*/
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, SearchIndex::class);
}
/**
* Saves a SearchIndex entity
*
* @param SearchIndex $entity
* @param boolean $flush
* @return void
*/
public function add(SearchIndex $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
/**
* Removes a SearchIndex entity
*
* @param SearchIndex $entity
* @param boolean $flush
* @return void
*/
public function remove(SearchIndex $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
/**
* Returns a Query object ready to be paginated or used to present results.
*
* @param string $query
* @param integer $minScore
* @return Query
*/
public function findAllPagination(string $query, int $minScore = 0): Query
{
$qb = $this->createQueryBuilder('r')
->addSelect('MATCH(r.data) AGAINST(:searchText boolean) AS score')
->where(
sprintf(
'MATCH(r.data) AGAINST(:searchText boolean) > %f',
$minScore
)
)->orderBy('score', 'DESC')
->setParameter(
'searchText',
$this->convertSearchTerm($query)
);
return $qb->getQuery();
}
/**
* Takes a string $query and explodes into individual words. Each word
* is then prefixed with + and ends with *, making the full text search
* operate as wildcard on all words
*
* @param string $query
* @return string
*/
private function convertSearchTerm(string $query): string
{
$extractedWords = [];
$sanitisedString = preg_replace('/[^\w^\d]/', ' ', $query);
$words = mb_split('\s', preg_replace(['/([^\w+])/','/(\s+)/'], ' ', $sanitisedString));
foreach ($words as $word) {
if (strlen($word)< 1) {
//
continue;
}
$word = strtoupper($word);
$extractedWords[$word] = $word;
}
array_walk(
$extractedWords,
function(&$word) {
// require every word but allow matching just the start
$word = '+' . $word . '*';
}
);
return implode(' ', $extractedWords);
}
/**
* Clears the index table of all results
* @return void
*/
public function clearIndex(): void
{
$this
->createQueryBuilder('s')
->delete()
->getQuery()
->execute();
}
}
+2 -8
View File
@@ -6,9 +6,7 @@ namespace Pcm\SearchBundle\Service;
use Pcm\SearchBundle\Entity\SearchIndex;
use Pcm\SearchBundle\Entity\Interface\SearchableInterface;
use Doctrine\Common\Util\ClassUtils;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Proxy\Proxy;
class SearchService
{
@@ -38,7 +36,7 @@ class SearchService
*/
public function unIndex(SearchableInterface $entity)
{
$class = get_class($entity);
$class = $this->em->getClassMetadata($entity::class)->getName();
$search_result = $this
->em
@@ -69,11 +67,7 @@ class SearchService
$data = implode(' ', $values);
$class = get_class($entity);
if ($entity instanceof Proxy) {
$class = ClassUtils::getRealClass($class);
}
$class = $this->em->getClassMetadata($entity::class)->getName();
$searchResult = $this
->em