Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
EngineProvider.php
Go to the documentation of this file.
1 <?php
8 
10 
18 {
23  const CONFIG_ENGINE_PATH = 'catalog/search/engine';
24 
28  protected $engine;
29 
34  protected $scopeConfig;
35 
39  private $objectManager;
40 
46  private $enginePool;
47 
51  private $engineResolver;
52 
58  public function __construct(
59  \Magento\Framework\ObjectManagerInterface $objectManager,
60  array $engines,
61  EngineResolverInterface $engineResolver
62  ) {
63  $this->objectManager = $objectManager;
64  $this->enginePool = $engines;
65  $this->engineResolver = $engineResolver;
66  }
67 
73  public function get()
74  {
75  if (!$this->engine) {
76  $currentEngine = $this->engineResolver->getCurrentSearchEngine();
77  if (!isset($this->enginePool[$currentEngine])) {
78  throw new \LogicException(
79  'There is no such engine: ' . $currentEngine
80  );
81  }
82  $engineClassName = $this->enginePool[$currentEngine];
83 
84  $engine = $this->objectManager->create($engineClassName);
85  if (false === $engine instanceof EngineInterface) {
86  throw new \LogicException(
87  $currentEngine . ' doesn\'t implement ' . EngineInterface::class
88  );
89  }
90 
92  if ($engine && !$engine->isAvailable()) {
93  throw new \LogicException(
94  'Engine is not available: ' . $currentEngine
95  );
96  }
97  $this->engine = $engine;
98  }
99 
100  return $this->engine;
101  }
102 }
$objectManager
Definition: bootstrap.php:17
__construct(\Magento\Framework\ObjectManagerInterface $objectManager, array $engines, EngineResolverInterface $engineResolver)