Passed
Push — master ( f13f78...5c1b24 )
by Ismayil
04:22
created

engine/classes/Elgg/Di/ServiceProvider.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Elgg\Di;
3
4
use Elgg\Config;
5
use Elgg\Cache\Pool;
6
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
7
use Symfony\Component\HttpFoundation\Session\Session as SymfonySession;
8
use Zend\Mail\Transport\TransportInterface as Mailer;
9
10
/**
11
 * Provides common Elgg services.
12
 *
13
 * We extend the container because it allows us to document properties in the PhpDoc, which assists
14
 * IDEs to auto-complete properties and understand the types returned. Extension allows us to keep
15
 * the container generic.
16
 *
17
 * @property-read \Elgg\Database\AccessCollections         $accessCollections
18
 * @property-read \ElggStaticVariableCache                 $accessCache
19
 * @property-read \Elgg\ActionsService                     $actions
20
 * @property-read \Elgg\Database\AdminNotices              $adminNotices
21
 * @property-read \Elgg\Ajax\Service                       $ajax
22
 * @property-read \Elgg\Amd\Config                         $amdConfig
23
 * @property-read \Elgg\Database\Annotations               $annotations
24
 * @property-read \ElggAutoP                               $autoP
25
 * @property-read \Elgg\AutoloadManager                    $autoloadManager
26
 * @property-read \Elgg\BatchUpgrader                      $batchUpgrader
27
 * @property-read \Elgg\BootService                        $boot
28
 * @property-read \Elgg\Application\CacheHandler           $cacheHandler
29
 * @property-read \Elgg\ClassLoader                        $classLoader
30
 * @property-read \Elgg\Cli                                $cli
31
 * @property-read \ElggCrypto                              $crypto
32
 * @property-read \Elgg\Config                             $config
33
 * @property-read \Elgg\Database\ConfigTable               $configTable
34
 * @property-read \Elgg\Context                            $context
35
 * @property-read \Elgg\Database                           $db
36
 * @property-read \Elgg\Database\DbConfig                  $dbConfig
37
 * @property-read \Elgg\DeprecationService                 $deprecation
38
 * @property-read \Elgg\EmailService                       $emails
39
 * @property-read \Elgg\Cache\EntityCache                  $entityCache
40
 * @property-read \Elgg\EntityPreloader                    $entityPreloader
41
 * @property-read \Elgg\Database\EntityTable               $entityTable
42
 * @property-read \Elgg\Assets\ExternalFiles               $externalFiles
43
 * @property-read \ElggFileCache                           $fileCache
44
 * @property-read \ElggDiskFilestore                       $filestore
45
 * @property-read \Elgg\FormsService                       $forms
46
 * @property-read \Elgg\HandlersService                    $handlers
47
 * @property-read \Elgg\Security\HmacFactory               $hmac
48
 * @property-read \Elgg\PluginHooksService                 $hooks
49
 * @property-read \Elgg\EntityIconService                  $iconService
50
 * @property-read \Elgg\Http\Input                         $input
51
 * @property-read \Elgg\ImageService                       $imageService
52
 * @property-read \Elgg\Logger                             $logger
53
 * @property-read Mailer                                   $mailer
54
 * @property-read \Elgg\Menu\Service                       $menus
55
 * @property-read \Elgg\Cache\MetadataCache                $metadataCache
56
 * @property-read \Stash\Pool|null                         $memcacheStashPool
57
 * @property-read \Elgg\Database\MetadataTable             $metadataTable
58
 * @property-read \Elgg\Database\Mutex                     $mutex
59
 * @property-read \Elgg\Notifications\NotificationsService $notifications
60
 * @property-read \Elgg\Cache\NullCache                    $nullCache
61
 * @property-read \Elgg\PasswordService                    $passwords
62
 * @property-read \Elgg\PersistentLoginService             $persistentLogin
63
 * @property-read \Elgg\Database\Plugins                   $plugins
64
 * @property-read \Elgg\Cache\PluginSettingsCache          $pluginSettingsCache
65
 * @property-read \Elgg\Database\PrivateSettingsTable      $privateSettings
66
 * @property-read \Elgg\Application\Database               $publicDb
67
 * @property-read \Elgg\Database\QueryCounter              $queryCounter
68
 * @property-read \Elgg\RedirectService                    $redirects
69
 * @property-read \Elgg\Http\Request                       $request
70
 * @property-read \Elgg\Http\ResponseFactory               $responseFactory
71
 * @property-read \Elgg\Database\RelationshipsTable        $relationshipsTable
72
 * @property-read \Elgg\Router                             $router
73
 * @property-read \Elgg\Database\Seeder                    $seeder
74
 * @property-read \Elgg\Application\ServeFileHandler       $serveFileHandler
75
 * @property-read \ElggSession                             $session
76
 * @property-read \Elgg\Cache\SimpleCache                  $simpleCache
77
 * @property-read \Elgg\Database\SiteSecret                $siteSecret
78
 * @property-read \Elgg\Forms\StickyForms                  $stickyForms
79
 * @property-read \Elgg\Database\SubtypeTable              $subtypeTable
80
 * @property-read \Elgg\Cache\SystemCache                  $systemCache
81
 * @property-read \Elgg\SystemMessagesService              $systemMessages
82
 * @property-read \Elgg\Views\TableColumn\ColumnFactory    $table_columns
83
 * @property-read \Elgg\Timer                              $timer
84
 * @property-read \Elgg\I18n\Translator                    $translator
85
 * @property-read \Elgg\Security\UrlSigner                 $urlSigner
86
 * @property-read \Elgg\UpgradeService                     $upgrades
87
 * @property-read \Elgg\Upgrade\Locator                    $upgradeLocator
88
 * @property-read \Elgg\UploadService                      $uploads
89
 * @property-read \Elgg\UserCapabilities                   $userCapabilities
90
 * @property-read \Elgg\Database\UsersTable                $usersTable
91
 * @property-read \Elgg\ViewsService                       $views
92
 * @property-read \Elgg\Cache\ViewCacher                   $viewCacher
93
 * @property-read \Elgg\WidgetsService                     $widgets
94
 *
95
 * @access private
96
 */
97
class ServiceProvider extends DiContainer {
98
99
	/**
100
	 * Constructor
101
	 *
102
	 * @param Config $config Elgg Config service
103
	 */
104 198
	public function __construct(Config $config) {
105
106
		$this->setFactory('autoloadManager', function(ServiceProvider $c) {
107 1
			$manager = new \Elgg\AutoloadManager($c->classLoader);
108 1
			if (!$c->config->AutoloaderManager_skip_storage) {
109
				$manager->setStorage($c->fileCache);
110
				$manager->loadCache();
111
			}
112 1
			return $manager;
113 198
		});
114
115
		$this->setFactory('accessCache', function(ServiceProvider $c) {
116 197
			return new \ElggStaticVariableCache('access');
117 198
		});
118
119
		$this->setFactory('accessCollections', function(ServiceProvider $c) {
120
			return new \Elgg\Database\AccessCollections(
121
					$c->config, $c->db, $c->entityTable, $c->userCapabilities, $c->accessCache, $c->hooks, $c->session, $c->translator);
122 198
		});
123
124
		$this->setFactory('actions', function(ServiceProvider $c) {
125 1
			return new \Elgg\ActionsService($c->config, $c->session, $c->crypto);
126 198
		});
127
128 198
		$this->setClassName('adminNotices', \Elgg\Database\AdminNotices::class);
129
130
		$this->setFactory('ajax', function(ServiceProvider $c) {
131 1
			return new \Elgg\Ajax\Service($c->hooks, $c->systemMessages, $c->input, $c->amdConfig);
132 198
		});
133
134
		$this->setFactory('amdConfig', function(ServiceProvider $c) {
135 3
			$obj = new \Elgg\Amd\Config($c->hooks);
136 3
			$obj->setBaseUrl($c->simpleCache->getRoot());
137 3
			return $obj;
138 198
		});
139
140
		$this->setFactory('annotations', function(ServiceProvider $c) {
141
			return new \Elgg\Database\Annotations($c->db, $c->session, $c->hooks->getEvents());
142 198
		});
143
144 198
		$this->setClassName('autoP', \ElggAutoP::class);
145
146
		$this->setFactory('boot', function(ServiceProvider $c) {
147 8
			$boot = new \Elgg\BootService();
148 8
			if ($c->config->enable_profiling) {
149
				$boot->setTimer($c->timer);
150
			}
151 8
			return $boot;
152 198
		});
153
154
		$this->setFactory('batchUpgrader', function(ServiceProvider $c) {
155 1
			return new \Elgg\BatchUpgrader($c->config);
156 198
		});
157
158
		$this->setFactory('cacheHandler', function(ServiceProvider $c) {
159 1
			$simplecache_enabled = $c->config->simplecache_enabled;
160 1
			if ($simplecache_enabled === null) {
161
				$simplecache_enabled = $c->configTable->get('simplecache_enabled');
162
			}
163 1
			return new \Elgg\Application\CacheHandler($c->config, $c->request, $simplecache_enabled);
164 198
		});
165
166
		$this->setFactory('classLoader', function(ServiceProvider $c) {
167 1
			$loader = new \Elgg\ClassLoader(new \Elgg\ClassMap());
168 1
			$loader->register();
169 1
			return $loader;
170 198
		});
171
172
		$this->setFactory('cli', function(ServiceProvider $c) {
173 1
			$version = elgg_get_version(true);
174 1
			$console = new \Symfony\Component\Console\Application('Elgg', $version);
0 ignored issues
show
It seems like $version defined by elgg_get_version(true) on line 173 can also be of type false; however, Symfony\Component\Consol...lication::__construct() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
175 1
			return new \Elgg\Cli($console, $c->hooks);
176 198
		});
177
178 198
		$this->setValue('config', $config);
179
180
		$this->setFactory('configTable', function(ServiceProvider $c) {
181 1
			return new \Elgg\Database\ConfigTable($c->db, $c->boot, $c->logger);
182 198
		});
183
184
		$this->setFactory('context', function(ServiceProvider $c) {
185 198
			$context = new \Elgg\Context();
186 198
			$context->initialize($c->request);
187 198
			return $context;
188 198
		});
189
190 198
		$this->setClassName('crypto', \ElggCrypto::class);
191
192
		$this->setFactory('db', function(ServiceProvider $c) {
193
			$db = new \Elgg\Database($c->dbConfig);
194
			$db->setLogger($c->logger);
195
196
			if ($c->config->profiling_sql) {
197
				$db->setTimer($c->timer);
198
			}
199
200
			return $db;
201 198
		});
202
203
		$this->setFactory('dbConfig', function(ServiceProvider $c) {
204 198
			$config = $c->config;
205 198
			$db_config = \Elgg\Database\DbConfig::fromElggConfig($config);
206
207
			// get this stuff out of config!
208 198
			unset($config->db);
209 198
			unset($config->dbname);
210 198
			unset($config->dbhost);
211 198
			unset($config->dbuser);
212 198
			unset($config->dbpass);
213
214 198
			return $db_config;
215 198
		});
216
217
		$this->setFactory('deprecation', function(ServiceProvider $c) {
218 4
			return new \Elgg\DeprecationService($c->logger);
219 198
		});
220
221
		$this->setFactory('emails', function(ServiceProvider $c) {
222 4
			return new \Elgg\EmailService($c->config, $c->hooks, $c->mailer, $c->logger);
223 198
		});
224
225
		$this->setFactory('entityCache', function(ServiceProvider $c) {
226 197
			return new \Elgg\Cache\EntityCache($c->session, $c->metadataCache);
227 198
		});
228
229
		$this->setFactory('entityPreloader', function(ServiceProvider $c) {
230 1
			return new \Elgg\EntityPreloader($c->entityCache, $c->entityTable);
231 198
		});
232
233
		$this->setFactory('entityTable', function(ServiceProvider $c) {
234
			return new \Elgg\Database\EntityTable(
235
				$c->config,
236
				$c->db,
237
				$c->entityCache,
238
				$c->metadataCache,
239
				$c->subtypeTable,
240
				$c->hooks->getEvents(),
241
				$c->session,
242
				$c->translator,
243
				$c->logger
244
			);
245 198
		});
246
247 198
		$this->setClassName('externalFiles', \Elgg\Assets\ExternalFiles::class);
248
249
		$this->setFactory('fileCache', function(ServiceProvider $c) {
250 1
			return new \ElggFileCache($c->config->cacheroot . 'system_cache/');
251 198
		});
252
253
		$this->setFactory('filestore', function(ServiceProvider $c) {
254 5
			return new \ElggDiskFilestore($c->config->dataroot);
255 198
		});
256
257
		$this->setFactory('forms', function(ServiceProvider $c) {
258 3
			return new \Elgg\FormsService($c->views, $c->logger);
259 198
		});
260
261 198
		$this->setClassName('handlers', \Elgg\HandlersService::class);
262
263
		$this->setFactory('hmac', function(ServiceProvider $c) {
264 5
			return new \Elgg\Security\HmacFactory($c->siteSecret, $c->crypto);
265 198
		});
266
267
		$this->setFactory('hooks', function(ServiceProvider $c) {
268 198
			$events = new \Elgg\EventsService($c->handlers);
269 198
			if ($c->config->enable_profiling) {
270
				$events->setTimer($c->timer);
271
			}
272 198
			return new \Elgg\PluginHooksService($events);
273 198
		});
274
275
		$this->setFactory('iconService', function(ServiceProvider $c) {
276 1
			return new \Elgg\EntityIconService($c->config, $c->hooks, $c->request, $c->logger, $c->entityTable);
277 198
		});
278
279 198
		$this->setClassName('input', \Elgg\Http\Input::class);
280
281
		$this->setFactory('imageService', function(ServiceProvider $c) {
282 2
			$imagine = new \Imagine\Gd\Imagine();
283 2
			return new \Elgg\ImageService($imagine, $c->config);
284 198
		});
285
286
		$this->setFactory('logger', function (ServiceProvider $c) {
287 198
			$logger = new \Elgg\Logger($c->hooks, $c->context);
288 198
			$logger->setLevel($c->config->debug);
289 198
			return $logger;
290 198
		});
291
292
		// TODO(evan): Support configurable transports...
293 198
		$this->setClassName('mailer', 'Zend\Mail\Transport\Sendmail');
294
295
		$this->setFactory('menus', function(ServiceProvider $c) {
296 1
			return new \Elgg\Menu\Service($c->hooks, $c->config);
297 198
		});
298
299
		$this->setFactory('metadataCache', function (ServiceProvider $c) {
300 197
			$cache = _elgg_get_memcache('metadata');
301 197
			return new \Elgg\Cache\MetadataCache($cache);
302 198
		});
303
304
		$this->setFactory('memcacheStashPool', function(ServiceProvider $c) {
305 197
			if (!$c->config->memcache) {
306 197
				return null;
307
			}
308
309
			$servers = $c->config->memcache_servers;
310
			if (!$servers) {
311
				return null;
312
			}
313
			$driver = new \Stash\Driver\Memcache([
314
				'servers' => $servers,
315
			]);
316
			return new \Stash\Pool($driver);
317 198
		});
318
319
		$this->setFactory('metadataTable', function(ServiceProvider $c) {
320
			// TODO(ewinslow): Use Pool instead of MetadataCache for caching
321
			return new \Elgg\Database\MetadataTable(
322
				$c->metadataCache, $c->db, $c->entityTable, $c->hooks->getEvents(), $c->session);
323 198
		});
324
325
		$this->setFactory('mutex', function(ServiceProvider $c) {
326 1
			return new \Elgg\Database\Mutex(
327 1
				$c->db,
328 1
				$c->logger
329
			);
330 198
		});
331
332
		$this->setFactory('notifications', function(ServiceProvider $c) {
333
			// @todo move queue in service provider
334 2
			$queue_name = \Elgg\Notifications\NotificationsService::QUEUE_NAME;
335 2
			$queue = new \Elgg\Queue\DatabaseQueue($queue_name, $c->db);
336 2
			$sub = new \Elgg\Notifications\SubscriptionsService($c->db);
337 2
			return new \Elgg\Notifications\NotificationsService($sub, $queue, $c->hooks, $c->session, $c->translator, $c->entityTable, $c->logger);
338 198
		});
339
340 198
		$this->setClassName('nullCache', \Elgg\Cache\NullCache::class);
341
342
		$this->setFactory('persistentLogin', function(ServiceProvider $c) {
343 1
			$global_cookies_config = $c->config->getCookieConfig();
344 1
			$cookie_config = $global_cookies_config['remember_me'];
345 1
			$cookie_name = $cookie_config['name'];
346 1
			$cookie_token = $c->request->cookies->get($cookie_name, '');
347 1
			return new \Elgg\PersistentLoginService(
348 1
				$c->db, $c->session, $c->crypto, $cookie_config, $cookie_token);
349 198
		});
350
351 198
		$this->setClassName('passwords', \Elgg\PasswordService::class);
352
353
		$this->setFactory('plugins', function(ServiceProvider $c) {
354
			$pool = new Pool\InMemory();
355
			$plugins = new \Elgg\Database\Plugins($pool, $c->pluginSettingsCache);
356
			if ($c->config->enable_profiling) {
357
				$plugins->setTimer($c->timer);
358
			}
359
			return $plugins;
360 198
		});
361
362 198
		$this->setClassName('pluginSettingsCache', \Elgg\Cache\PluginSettingsCache::class);
363
364
		$this->setFactory('privateSettings', function(ServiceProvider $c) {
365
			return new \Elgg\Database\PrivateSettingsTable($c->db, $c->entityTable, $c->pluginSettingsCache);
366 198
		});
367
368
		$this->setFactory('publicDb', function(ServiceProvider $c) {
369 1
			return new \Elgg\Application\Database($c->db);
370 198
		});
371
372
		$this->setFactory('queryCounter', function(ServiceProvider $c) {
373 1
			return new \Elgg\Database\QueryCounter($c->db);
374 198
		}, false);
375
376
		$this->setFactory('redirects', function(ServiceProvider $c) {
377 1
			$url = current_page_url();
378 1
			$is_xhr = $c->request->isXmlHttpRequest();
379 1
			return new \Elgg\RedirectService($c->session, $is_xhr, $c->config->wwwroot, $url);
380 198
		});
381
382
		$this->setFactory('relationshipsTable', function(ServiceProvider $c) {
383
			return new \Elgg\Database\RelationshipsTable($c->db, $c->entityTable, $c->metadataTable, $c->hooks->getEvents());
384 198
		});
385
386 198
		$this->setFactory('request', [\Elgg\Http\Request::class, 'createFromGlobals']);
387
388
		$this->setFactory('responseFactory', function(ServiceProvider $c) {
389 1
			if (php_sapi_name() === 'cli') {
390 1
				$transport = new \Elgg\Http\OutputBufferTransport();
391
			} else {
392
				$transport = new \Elgg\Http\HttpProtocolTransport();
393
			}
394 1
			return new \Elgg\Http\ResponseFactory($c->request, $c->hooks, $c->ajax, $transport);
395 198
		});
396
397
		$this->setFactory('router', function(ServiceProvider $c) {
398
			// TODO(evan): Init routes from plugins or cache
399 5
			$router = new \Elgg\Router($c->hooks);
400 5
			if ($c->config->enable_profiling) {
401
				$router->setTimer($c->timer);
402
			}
403 5
			return $router;
404 198
		});
405
406
		$this->setFactory('seeder', function(ServiceProvider $c) {
407 1
			return new \Elgg\Database\Seeder($c->hooks);
408 198
		});
409
410
		$this->setFactory('serveFileHandler', function(ServiceProvider $c) {
411 2
			return new \Elgg\Application\ServeFileHandler($c->hmac, $c->config);
412 198
		});
413
414
		$this->setFactory('session', function(ServiceProvider $c) {
415
			return \ElggSession::fromDatabase($c->config, $c->db);
416 198
		});
417
418 198
		$this->setClassName('urlSigner', \Elgg\Security\UrlSigner::class);
419
420
		$this->setFactory('simpleCache', function(ServiceProvider $c) {
421 3
			return new \Elgg\Cache\SimpleCache($c->config);
422 198
		});
423
424
		/**
425
		 * If the key is in the settings file, this is injected early.
426
		 *
427
		 * @see \Elgg\Application::initConfig
428
		 */
429
		$this->setFactory('siteSecret', function(ServiceProvider $c) {
430
			return \Elgg\Database\SiteSecret::fromDatabase($c->configTable);
431 198
		});
432
433 198
		$this->setClassName('stickyForms', \Elgg\Forms\StickyForms::class);
434
435
		$this->setFactory('subtypeTable', function(ServiceProvider $c) {
436
			return new \Elgg\Database\SubtypeTable($c->db);
437 198
		});
438
439
		$this->setFactory('systemCache', function (ServiceProvider $c) {
440 1
			$cache = new \Elgg\Cache\SystemCache($c->fileCache, $c->config);
441 1
			if ($c->config->enable_profiling) {
442
				$cache->setTimer($c->timer);
443
			}
444 1
			return $cache;
445 198
		});
446
447
		$this->setFactory('systemMessages', function(ServiceProvider $c) {
448 1
			return new \Elgg\SystemMessagesService($c->session);
449 198
		});
450
451 198
		$this->setClassName('table_columns', \Elgg\Views\TableColumn\ColumnFactory::class);
452
453 198
		$this->setClassName('timer', \Elgg\Timer::class);
454
455
		$this->setFactory('translator', function(ServiceProvider $c) {
456 197
			return new \Elgg\I18n\Translator($c->config);
457 198
		});
458
459
		$this->setFactory('uploads', function(ServiceProvider $c) {
460 1
			return new \Elgg\UploadService($c->request);
461 198
		});
462
463
		$this->setFactory('upgrades', function(ServiceProvider $c) {
464 1
			return new \Elgg\UpgradeService(
465 1
				$c->translator,
466 1
				$c->hooks,
467 1
				$c->config,
468 1
				$c->logger,
469 1
				$c->mutex
470
			);
471 198
		});
472
473
		$this->setFactory('userCapabilities', function(ServiceProvider $c) {
474 44
			return new \Elgg\UserCapabilities($c->hooks, $c->entityTable, $c->session);
475 198
		});
476
477
		$this->setFactory('usersTable', function(ServiceProvider $c) {
478 197
			return new \Elgg\Database\UsersTable(
479 197
				$c->config,
480 197
				$c->db,
481 197
				$c->entityTable,
482 197
				$c->entityCache,
483 197
				$c->hooks->getEvents()
484
			);
485 198
		});
486
487
		$this->setFactory('upgradeLocator', function(ServiceProvider $c) {
488 8
			return new \Elgg\Upgrade\Locator(
489 8
				$c->plugins,
490 8
				$c->logger,
491 8
				$c->privateSettings
492
			);
493 198
		});
494
495
		$this->setFactory('views', function(ServiceProvider $c) {
496 5
			return new \Elgg\ViewsService($c->hooks, $c->logger, $c->input);
497 198
		});
498
499 198
		$this->setFactory('viewCacher', function(ServiceProvider $c) {
500 1
			return new \Elgg\Cache\ViewCacher($c->views, $c->config);
501 198
		});
502
503 198
		$this->setClassName('widgets', \Elgg\WidgetsService::class);
504 198
	}
505
}
506