Completed
Push — master ( 7a3631...317ee4 )
by Maxence
03:02 queued 01:14
created

Application::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
5
/**
6
 * FullTextSearch - Full text search framework for Nextcloud
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later. See the COPYING file.
10
 *
11
 * @author Maxence Lange <[email protected]>
12
 * @copyright 2018
13
 * @license GNU AGPL version 3 or any later version
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
 *
28
 */
29
30
31
namespace OCA\FullTextSearch\AppInfo;
32
33
34
use Closure;
35
use OC;
36
use OCA\FullTextSearch\Capabilities;
37
use OCA\FullTextSearch\Search\UnifiedSearchProvider;
38
use OCA\FullTextSearch\Service\ConfigService;
39
use OCA\FullTextSearch\Service\IndexService;
40
use OCA\FullTextSearch\Service\ProviderService;
41
use OCA\FullTextSearch\Service\SearchService;
42
use OCP\AppFramework\App;
43
use OCP\AppFramework\Bootstrap\IBootContext;
44
use OCP\AppFramework\Bootstrap\IBootstrap;
45
use OCP\AppFramework\Bootstrap\IRegistrationContext;
46
use OCP\FullTextSearch\IFullTextSearchManager;
47
use OCP\INavigationManager;
48
use OCP\IServerContainer;
49
use Throwable;
50
51
52
class Application extends App implements IBootstrap {
53
54
55
	const APP_NAME = 'fulltextsearch';
56
57
58
	/**
59
	 * Application constructor.
60
	 *
61
	 * @param array $params
62
	 */
63
	public function __construct(array $params = []) {
64
		parent::__construct(self::APP_NAME, $params);
65
	}
66
67
68
	/**
69
	 * @param IRegistrationContext $context
70
	 */
71
	public function register(IRegistrationContext $context): void {
72
		$context->registerCapability(Capabilities::class);
73
		$context->registerSearchProvider(UnifiedSearchProvider::class);
74
	}
75
76
	/**
77
	 * @param IBootContext $context
78
	 *
79
	 * @throws Throwable
80
	 */
81
	public function boot(IBootContext $context): void {
82
		$context->injectFn(Closure::fromCallable([$this, 'registerServices']));
83
		$context->injectFn(Closure::fromCallable([$this, 'registerNavigation']));
84
	}
85
86
87
	/**
88
	 * Register Navigation Tab
89
	 *
90
	 * @param IServerContainer $container
91
	 */
92
	protected function registerServices(IServerContainer $container) {
93
		/** @var IFullTextSearchManager $fullTextSearchManager */
94
		$fullTextSearchManager = $container->get(IFullTextSearchManager::class);
95
96
		$providerService = $container->get(ProviderService::class);
97
		$indexService = $container->get(IndexService::class);
98
		$searchService = $container->get(SearchService::class);
99
100
		$fullTextSearchManager->registerProviderService($providerService);
101
		$fullTextSearchManager->registerIndexService($indexService);
102
		$fullTextSearchManager->registerSearchService($searchService);
103
	}
104
105
106
	/**
107
	 * Register Navigation Tab
108
	 *
109
	 * @param IServerContainer $container
110
	 */
111
	protected function registerNavigation(IServerContainer $container) {
112
		/** @var ConfigService $configService */
113
		$configService = $container->get(ConfigService::class);
114
		if ($configService->getAppValue(ConfigService::APP_NAVIGATION) !== '1') {
115
			return;
116
		}
117
118
		$container->get(INavigationManager::class)
119
				  ->add($this->fullTextSearchNavigation());
120
	}
121
122
123
	/**
124
	 * @return array
125
	 */
126
	private function fullTextSearchNavigation(): array {
127
		$urlGen = OC::$server->getURLGenerator();
128
		$navName = OC::$server->getL10N(self::APP_NAME)
129
							  ->t('Search');
130
131
		return [
132
			'id'    => self::APP_NAME,
133
			'order' => 5,
134
			'href'  => $urlGen->linkToRoute('fulltextsearch.Navigation.navigate'),
135
			'icon'  => $urlGen->imagePath(self::APP_NAME, 'fulltextsearch.svg'),
136
			'name'  => $navName
137
		];
138
	}
139
140
}
141
142