Fixup DI of QueryCompiler.

As compiler was not using structure on its own, extract it and replace instantiation by a factory.
This commit is contained in:
Benoît Burnichon
2015-11-16 13:08:32 +01:00
parent 3b2d6b05a0
commit 88427efa4c
4 changed files with 22 additions and 9 deletions

View File

@@ -13,6 +13,7 @@ namespace Alchemy\Phrasea\Core\Provider;
use Alchemy\Phrasea\Controller\LazyLocator; use Alchemy\Phrasea\Controller\LazyLocator;
use Alchemy\Phrasea\SearchEngine\Elastic\ElasticsearchOptions; use Alchemy\Phrasea\SearchEngine\Elastic\ElasticsearchOptions;
use Alchemy\Phrasea\SearchEngine\Elastic\Search\QueryVisitor;
use Alchemy\Phrasea\SearchEngine\SearchEngineLogger; use Alchemy\Phrasea\SearchEngine\SearchEngineLogger;
use Alchemy\Phrasea\Exception\InvalidArgumentException; use Alchemy\Phrasea\Exception\InvalidArgumentException;
use Alchemy\Phrasea\SearchEngine\SearchEngineInterface; use Alchemy\Phrasea\SearchEngine\SearchEngineInterface;
@@ -203,10 +204,14 @@ class SearchEngineServiceProvider implements ServiceProviderInterface
return Compiler\Llk\Llk::load(new File\Read($grammarPath)); return Compiler\Llk\Llk::load(new File\Read($grammarPath));
}); });
$app['query_visitor.factory'] = $app->protect(function () use ($app) {
return new QueryVisitor($app['search_engine.structure']);
});
$app['query_compiler'] = $app->share(function ($app) { $app['query_compiler'] = $app->share(function ($app) {
return new QueryCompiler( return new QueryCompiler(
$app['query_parser'], $app['query_parser'],
$app['search_engine.structure'], $app['query_visitor.factory'],
$app['thesaurus'] $app['thesaurus']
); );
}); });

View File

@@ -2,9 +2,7 @@
namespace Alchemy\Phrasea\SearchEngine\Elastic\Search; namespace Alchemy\Phrasea\SearchEngine\Elastic\Search;
use Alchemy\Phrasea\SearchEngine\Elastic\AST;
use Alchemy\Phrasea\SearchEngine\Elastic\Exception\QueryException; use Alchemy\Phrasea\SearchEngine\Elastic\Exception\QueryException;
use Alchemy\Phrasea\SearchEngine\Elastic\Structure\Structure;
use Alchemy\Phrasea\SearchEngine\Elastic\Thesaurus; use Alchemy\Phrasea\SearchEngine\Elastic\Thesaurus;
use Hoa\Compiler\Exception\Exception as CompilerException; use Hoa\Compiler\Exception\Exception as CompilerException;
use Hoa\Compiler\Llk\Parser; use Hoa\Compiler\Llk\Parser;
@@ -15,7 +13,7 @@ use Hoa\Visitor\Visit;
class QueryCompiler class QueryCompiler
{ {
private $parser; private $parser;
private $structure; private $queryVisitorFactory;
private $thesaurus; private $thesaurus;
private static $leftAssociativeOperators = array( private static $leftAssociativeOperators = array(
@@ -24,10 +22,10 @@ class QueryCompiler
NodeTypes::EXCEPT_EXPR NodeTypes::EXCEPT_EXPR
); );
public function __construct(Parser $parser, Structure $structure, Thesaurus $thesaurus) public function __construct(Parser $parser, callable $queryVisitorFactory, Thesaurus $thesaurus)
{ {
$this->parser = $parser; $this->parser = $parser;
$this->structure = $structure; $this->queryVisitorFactory = $queryVisitorFactory;
$this->thesaurus = $thesaurus; $this->thesaurus = $thesaurus;
} }
@@ -60,9 +58,12 @@ class QueryCompiler
return $this->visitString($string, $this->createQueryVisitor(), $postprocessing); return $this->visitString($string, $this->createQueryVisitor(), $postprocessing);
} }
/**
* @return Visit
*/
private function createQueryVisitor() private function createQueryVisitor()
{ {
return new QueryVisitor($this->structure); return call_user_func($this->queryVisitorFactory);
} }
public function dump($string, $postprocessing = true) public function dump($string, $postprocessing = true)

View File

@@ -4,7 +4,6 @@ namespace Alchemy\Phrasea\SearchEngine\Elastic\Search;
use Alchemy\Phrasea\SearchEngine\Elastic\AST; use Alchemy\Phrasea\SearchEngine\Elastic\AST;
use Alchemy\Phrasea\SearchEngine\Elastic\Exception\Exception; use Alchemy\Phrasea\SearchEngine\Elastic\Exception\Exception;
use Alchemy\Phrasea\SearchEngine\Elastic\Search\QueryHelper;
use Alchemy\Phrasea\SearchEngine\Elastic\Structure\Structure; use Alchemy\Phrasea\SearchEngine\Elastic\Structure\Structure;
use Hoa\Compiler\Llk\TreeNode; use Hoa\Compiler\Llk\TreeNode;
use Hoa\Visitor\Element; use Hoa\Visitor\Element;

View File

@@ -3,6 +3,8 @@
namespace Alchemy\Tests\Phrasea\SearchEngine; namespace Alchemy\Tests\Phrasea\SearchEngine;
use Alchemy\Phrasea\SearchEngine\Elastic\Search\QueryCompiler; use Alchemy\Phrasea\SearchEngine\Elastic\Search\QueryCompiler;
use Alchemy\Phrasea\SearchEngine\Elastic\Search\QueryVisitor;
use Alchemy\Phrasea\SearchEngine\Elastic\Structure\Structure;
use Alchemy\Phrasea\SearchEngine\Elastic\Thesaurus; use Alchemy\Phrasea\SearchEngine\Elastic\Thesaurus;
use Alchemy\Tests\Tools\CsvFileIterator; use Alchemy\Tests\Tools\CsvFileIterator;
use Hoa\Compiler; use Hoa\Compiler;
@@ -23,11 +25,17 @@ class QueryCompilerTest extends \PHPUnit_Framework_TestCase
$grammar_path = realpath(implode('/', [__DIR__, $project_root, $grammar_path])); $grammar_path = realpath(implode('/', [__DIR__, $project_root, $grammar_path]));
$parser = Compiler\Llk\Llk::load(new File\Read($grammar_path)); $parser = Compiler\Llk\Llk::load(new File\Read($grammar_path));
$structure = $this->getMock(Structure::class);
$queryVisitorFactory = function () use ($structure) {
return new QueryVisitor($structure);
};
$thesaurus = $this->getMockBuilder(Thesaurus::class) $thesaurus = $this->getMockBuilder(Thesaurus::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$this->compiler = new QueryCompiler($parser, $thesaurus); $this->compiler = new QueryCompiler($parser, $queryVisitorFactory, $thesaurus);
} }
/** /**