1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LTDBeget\sphinx\configurator; |
6
|
|
|
|
7
|
|
|
use LTDBeget\sphinx\configurator\configurationEntities\sections\Common; |
8
|
|
|
use LTDBeget\sphinx\configurator\configurationEntities\sections\Index; |
9
|
|
|
use LTDBeget\sphinx\configurator\configurationEntities\sections\Indexer; |
10
|
|
|
use LTDBeget\sphinx\configurator\configurationEntities\sections\Searchd; |
11
|
|
|
use LTDBeget\sphinx\configurator\configurationEntities\sections\Source; |
12
|
|
|
use LTDBeget\sphinx\configurator\exceptions\ConfigurationException; |
13
|
|
|
use LTDBeget\sphinx\enums\eSection; |
14
|
|
|
|
15
|
|
|
class ConfigurationHelper |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param Configuration $configuration |
19
|
|
|
* |
20
|
|
|
* @param string $name |
21
|
|
|
* @param string|null $inheritanceName |
22
|
|
|
* |
23
|
|
|
* @return \LTDBeget\sphinx\configurator\configurationEntities\sections\Source |
24
|
|
|
* @throws \LogicException |
25
|
|
|
* @throws \LTDBeget\sphinx\configurator\exceptions\SectionException |
26
|
|
|
* @throws \InvalidArgumentException |
27
|
|
|
*/ |
28
|
12 |
|
public static function createSource( |
29
|
|
|
Configuration $configuration, |
30
|
|
|
string $name, |
31
|
|
|
string $inheritanceName = null |
32
|
|
|
): Source { |
33
|
12 |
|
$source = new Source($configuration, $name, $inheritanceName); |
34
|
10 |
|
$configuration->addSource($source); |
35
|
|
|
|
36
|
10 |
|
return $source; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Configuration $configuration |
41
|
|
|
* |
42
|
|
|
* @param string $name |
43
|
|
|
* @param string|null $inheritanceName |
44
|
|
|
* |
45
|
|
|
* @return \LTDBeget\sphinx\configurator\configurationEntities\sections\Index |
46
|
|
|
* @throws \LogicException |
47
|
|
|
* @throws \LTDBeget\sphinx\configurator\exceptions\SectionException |
48
|
|
|
* @throws \InvalidArgumentException |
49
|
|
|
*/ |
50
|
8 |
|
public static function createIndex(Configuration $configuration, string $name, string $inheritanceName = null |
51
|
|
|
): Index { |
52
|
8 |
|
$indexDefinition = new Index($configuration, $name, $inheritanceName); |
53
|
8 |
|
$configuration->addIndex($indexDefinition); |
54
|
|
|
|
55
|
8 |
|
return $indexDefinition; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param Configuration $configuration |
60
|
|
|
* |
61
|
|
|
* @return \LTDBeget\sphinx\configurator\configurationEntities\sections\Indexer |
62
|
|
|
*/ |
63
|
4 |
|
public static function getOrCreateIndexer($configuration): Indexer |
64
|
|
|
{ |
65
|
4 |
|
if (!$configuration->hasIndexer()) { |
66
|
4 |
|
$configuration->setIndexer(new Indexer($configuration)); |
67
|
|
|
} |
68
|
|
|
|
69
|
4 |
|
return $configuration->getIndexer(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Configuration $configuration |
74
|
|
|
* |
75
|
|
|
* @return \LTDBeget\sphinx\configurator\configurationEntities\sections\Searchd |
76
|
|
|
*/ |
77
|
4 |
|
public static function getOrCreateSearchd($configuration): Searchd |
78
|
|
|
{ |
79
|
4 |
|
if (!$configuration->hasSearchd()) { |
80
|
4 |
|
$configuration->setSearchd(new Searchd($configuration)); |
81
|
|
|
} |
82
|
|
|
|
83
|
4 |
|
return $configuration->getSearchd(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param Configuration $configuration |
88
|
|
|
* |
89
|
|
|
* @return \LTDBeget\sphinx\configurator\configurationEntities\sections\Common |
90
|
|
|
* @throws \LTDBeget\sphinx\configurator\exceptions\ConfigurationException |
91
|
|
|
*/ |
92
|
4 |
|
public static function getOrCreateCommon($configuration): Common |
93
|
|
|
{ |
94
|
4 |
|
$section = eSection::COMMON(); |
95
|
4 |
|
if (!$configuration->isAllowedSection($section)) { |
96
|
1 |
|
$version = $configuration->getVersion(); |
97
|
1 |
|
throw new ConfigurationException("Sphinx of version {$version} does't have section {$section}"); |
98
|
|
|
} |
99
|
|
|
|
100
|
3 |
|
if (!$configuration->hasCommon()) { |
101
|
3 |
|
$configuration->setCommon(new Common($configuration)); |
102
|
|
|
} |
103
|
|
|
|
104
|
3 |
|
return $configuration->getCommon(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|