Completed
Push — 3.1 ( d59679...4b8741 )
by Jeroen
62:38 queued 13s
created

engine/classes/Elgg/BootService.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Elgg;
4
5
use Elgg\Database\SiteSecret;
6
use Elgg\Di\ServiceProvider;
7
use ElggCache;
8
9
/**
10
 * Boots Elgg and manages a cache of data needed during boot
11
 *
12
 * @internal
13
 * @since  2.1
14
 */
15
class BootService {
16
17
	use Profilable;
18
	use Cacheable;
19
20
	/**
21
	 * The default TTL if not set in settings.php
22
	 */
23
	const DEFAULT_BOOT_CACHE_TTL = 3600;
24
25
	/**
26
	 * The default limit for the number of plugin settings a plugin can have before it won't be loaded into bootdata
27
	 *
28
	 * Can be set in settings.php
29
	 */
30
	const DEFAULT_BOOTDATA_PLUGIN_SETTINGS_LIMIT = 40;
31
32
	/**
33
	 * Cache
34
	 *
35
	 * @param ElggCache $cache Cache
36
	 */
37 4999
	public function __construct(ElggCache $cache) {
38 4999
		$this->cache = $cache;
39 4999
	}
40
41
	/**
42
	 * Boots the engine
43
	 *
44
	 * @param ServiceProvider $services Services
45
	 *
46
	 * @return void
47
	 * @throws \ClassException
48
	 * @throws \DatabaseException
49
	 * @throws \InstallationException
50
	 * @throws \InvalidParameterException
51
	 * @throws \SecurityException
52
	 */
53 5459
	public function boot(ServiceProvider $services) {
54 5459
		$db = $services->db;
55 5459
		$config = $services->config;
56
57
		// set cookie values for session and remember me
58 5459
		$config->getCookieConfig();
59
60
		// defaults in case these aren't in config table
61 5459
		if ($config->boot_cache_ttl === null) {
62 5
			$config->boot_cache_ttl = self::DEFAULT_BOOT_CACHE_TTL;
63
		}
64 5459
		if ($config->bootdata_plugin_settings_limit === null) {
65 4999
			$config->bootdata_plugin_settings_limit = self::DEFAULT_BOOTDATA_PLUGIN_SETTINGS_LIMIT;
1 ignored issue
show
The property bootdata_plugin_settings_limit is declared read-only in Elgg\Config.
Loading history...
66
		}
67 5459
		if ($config->simplecache_enabled === null) {
68 5
			$config->simplecache_enabled = false;
69
		}
70 5459
		if ($config->system_cache_enabled === null) {
71 5
			$config->system_cache_enabled = false;
72
		}
73 5459
		if ($config->simplecache_lastupdate === null) {
74 4999
			$config->simplecache_lastupdate = 0;
75
		}
76 5459
		if ($config->min_password_length === null) {
77 4999
			$config->min_password_length = 6;
78
		}
79 5459
		if ($config->minusername === null) {
80 5
			$config->minusername = 4;
81
		}
82 5459
		if ($config->batch_run_time_in_secs === null) {
83 4999
			$config->batch_run_time_in_secs = 4;
84
		}
85
86
		// we were using NOTICE temporarily so we can't just check for null
87 5459
		if (!$config->hasInitialValue('debug') && !$config->debug) {
88 5
			$config->debug = '';
89
		}
90
91
		// copy all table values into config
92 5459
		$config->mergeValues($services->configTable->getAll());
93
94
		// needs to be set before [init, system] for links in html head
95 5459
		$config->lastcache = (int) $config->simplecache_lastupdate;
96
97 5459
		if (!$config->elgg_config_set_secret) {
98 5459
			$site_secret = SiteSecret::fromConfig($config);
99 5459
			if ($site_secret) {
100 5459
				$services->setValue('siteSecret', $site_secret);
101
			} else {
102
				throw new \RuntimeException('The site secret is not set.');
103
			}
104
		}
105
106 5459
		$installed = isset($config->installed);
107
108 5459
		if ($this->timer) {
109
			$this->timer->begin([__CLASS__ . '::getBootData']);
110
		}
111
112
		// early config is done, now get the core boot data
113 5459
		$data = $this->getBootData($config, $db, $installed);
114
115 5459
		$site = $data->getSite();
116 5459
		if (!$site) {
117
			// must be set in config
118
			$site = $config->site;
119
			if (!$site instanceof \ElggSite) {
120
				throw new \RuntimeException('Before installation, config->site must have an unsaved ElggSite.');
121
			}
122
		}
123
124 5459
		$config->site = $site;
125 5459
		$config->sitename = $site->name;
126 5459
		$config->sitedescription = $site->description;
127
128 5459
		$settings = $data->getPluginSettings();
129 5459
		foreach ($settings as $guid => $entity_settings) {
130 82
			$services->privateSettingsCache->save($guid, $entity_settings);
131
		}
132
133 5459
		foreach ($data->getPluginMetadata() as $guid => $metadata) {
134 82
			$services->dataCache->metadata->save($guid, $metadata);
135
		}
136
137 5459
		$services->plugins->setBootPlugins($data->getActivePlugins(), false);
138
139
		// use value in settings.php if available
140 5459
		$debug = $config->hasInitialValue('debug') ? $config->getInitialValue('debug') : $config->debug;
141 5459
		$services->logger->setLevel($debug);
142
143 5459
		if ($config->system_cache_enabled) {
144 80
			$config->system_cache_loaded = false;
145
146 80
			if ($services->views->configureFromCache($services->systemCache)) {
147 74
				$config->system_cache_loaded = true;
148
			}
149
		}
150
151
		// we don't store langs in boot data because it varies by user
152 5459
		$services->translator->bootTranslations();
153 5459
	}
154
155
	/**
156
	 * Invalidate the cache item
157
	 *
158
	 * @return void
159
	 */
160 397
	public function invalidateCache() {
161 397
		$this->cache->clear();
162 397
		_elgg_services()->plugins->setBootPlugins(null);
163 397
		_elgg_config()->system_cache_loaded = false;
164 397
		_elgg_config()->_boot_cache_hit = false;
165 397
	}
166
167
	/**
168
	 * Get the boot data
169
	 *
170
	 * @param Config   $config    Elgg config object
171
	 * @param Database $db        Elgg database
172
	 * @param bool     $installed Is the site installed?
173
	 *
174
	 * @return BootData
175
	 *
176
	 * @throws \ClassException
177
	 * @throws \DatabaseException
178
	 * @throws \InstallationException
179
	 * @throws \InvalidParameterException
180
	 */
181 5459
	private function getBootData(Config $config, Database $db, $installed) {
182 5459
		$config->_boot_cache_hit = false;
183
184 5459
		$data = null;
185 5459
		if ($config->boot_cache_ttl > 0) {
186 85
			$data = $this->cache->load('boot_data');
187
		}
188
189 5459
		if (!isset($data)) {
190 5401
			$data = new BootData();
191 5401
			$data->populate($config, $db, _elgg_services()->entityTable, _elgg_services()->plugins, $installed);
192 5401
			if ($config->boot_cache_ttl && $installed) {
193 5401
				$this->cache->save('boot_data', $data, $config->boot_cache_ttl);
194
			}
195
		} else {
196 64
			$config->_boot_cache_hit = true;
197
		}
198
199 5459
		return $data;
200
	}
201
202
}
203