/
Resolver.php
74 lines (67 loc) · 1.76 KB
/
Resolver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/**
*
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Model\Layer;
class Resolver
{
const CATALOG_LAYER_CATEGORY = 'category';
const CATALOG_LAYER_SEARCH = 'search';
/**
* Catalog view layer models list
*
* @var array
*/
protected $layersPool;
/**
* Filter factory
*
* @var \Magento\Framework\ObjectManagerInterface
*/
protected $objectManager;
/**
* @var \Magento\Catalog\Model\Layer
*/
protected $layer = null;
/**
* @param \Magento\Framework\ObjectManagerInterface $objectManager
* @param array $layersPool
*/
public function __construct(
\Magento\Framework\ObjectManagerInterface $objectManager,
array $layersPool
) {
$this->objectManager = $objectManager;
$this->layersPool = $layersPool;
}
/**
* Create Catalog Layer by specified type
*
* @param string $layerType
* @return void
*/
public function create($layerType)
{
if (isset($this->layer)) {
throw new \RuntimeException('Catalog Layer has been already created');
}
if (!isset($this->layersPool[$layerType])) {
throw new \InvalidArgumentException($layerType . ' does not belong to any registered layer');
}
$this->layer = $this->objectManager->create($this->layersPool[$layerType]);
}
/**
* Get current Catalog Layer
*
* @return \Magento\Catalog\Model\Layer
*/
public function get()
{
if (!isset($this->layer)) {
$this->layer = $this->objectManager->create($this->layersPool[self::CATALOG_LAYER_CATEGORY]);
}
return $this->layer;
}
}