Elgg  Version 2.3
ServiceProvider.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Di;
3 
7 use Zend\Mail\Transport\TransportInterface as Mailer;
8 
91 
97  public function __construct(\Elgg\Config $config) {
98 
99  $this->setFactory('classLoader', function(ServiceProvider $c) {
100  $loader = new \Elgg\ClassLoader(new \Elgg\ClassMap());
101  $loader->register();
102  return $loader;
103  });
104 
105  $this->setFactory('autoloadManager', function(ServiceProvider $c) {
106  $manager = new \Elgg\AutoloadManager($c->classLoader);
107  if (!$c->config->get('AutoloaderManager_skip_storage')) {
108  $manager->setStorage($c->fileCache);
109  $manager->loadCache();
110  }
111  return $manager;
112  });
113 
114  $this->setFactory('accessCache', function(ServiceProvider $c) {
115  return new \ElggStaticVariableCache('access');
116  });
117 
118  $this->setFactory('accessCollections', function(ServiceProvider $c) {
119  return new \Elgg\Database\AccessCollections(
120  $c->config, $c->db, $c->entityTable, $c->accessCache, $c->hooks, $c->session, $c->translator);
121  });
122 
123  $this->setFactory('actions', function(ServiceProvider $c) {
124  return new \Elgg\ActionsService($c->config, $c->session, $c->crypto);
125  });
126 
127  $this->setClassName('adminNotices', \Elgg\Database\AdminNotices::class);
128 
129  $this->setFactory('ajax', function(ServiceProvider $c) {
130  return new \Elgg\Ajax\Service($c->hooks, $c->systemMessages, $c->input, $c->amdConfig);
131  });
132 
133  $this->setFactory('amdConfig', function(ServiceProvider $c) {
134  $obj = new \Elgg\Amd\Config($c->hooks);
135  $obj->setBaseUrl($c->simpleCache->getRoot());
136  return $obj;
137  });
138 
139  $this->setFactory('annotations', function(ServiceProvider $c) {
140  return new \Elgg\Database\Annotations($c->db, $c->session, $c->events);
141  });
142 
143  $this->setClassName('autoP', \ElggAutoP::class);
144 
145  $this->setFactory('boot', function(ServiceProvider $c) {
146  $boot = new \Elgg\BootService();
147  if ($c->config->getVolatile('enable_profiling')) {
148  $boot->setTimer($c->timer);
149  }
150  return $boot;
151  });
152 
153  $this->setValue('config', $config);
154 
155  $this->setClassName('configTable', \Elgg\Database\ConfigTable::class);
156 
157  $this->setClassName('context', \Elgg\Context::class);
158 
159  $this->setFactory('crypto', function(ServiceProvider $c) {
160  return new \ElggCrypto($c->siteSecret);
161  });
162 
163  $this->setFactory('datalist', function(ServiceProvider $c) {
164  // TODO(ewinslow): Add back memcached support
165  $db = $c->db;
166  $dbprefix = $db->prefix;
167  $pool = new Pool\InMemory();
168  return new \Elgg\Database\Datalist($pool, $db, $c->logger, "{$dbprefix}datalists");
169  });
170 
171  $this->setFactory('db', function(ServiceProvider $c) {
172  // gonna need dbprefix from settings
173  $c->config->loadSettingsFile();
174  $db_config = new \Elgg\Database\Config($c->config->getStorageObject());
175 
176  // we inject the logger in _elgg_engine_boot()
177  $db = new \Elgg\Database($db_config);
178 
179  if ($c->config->getVolatile('profiling_sql')) {
180  $db->setTimer($c->timer);
181  }
182 
183  return $db;
184  });
185 
186  $this->setFactory('deprecation', function(ServiceProvider $c) {
187  return new \Elgg\DeprecationService($c->logger);
188  });
189 
190  $this->setFactory('entityCache', function(ServiceProvider $c) {
191  return new \Elgg\Cache\EntityCache($c->session, $c->metadataCache);
192  });
193 
194  $this->setFactory('entityPreloader', function(ServiceProvider $c) {
195  return new \Elgg\EntityPreloader($c->entityCache, $c->entityTable);
196  });
197 
198  $this->setFactory('entityTable', function(ServiceProvider $c) {
199  return new \Elgg\Database\EntityTable(
200  $c->config,
201  $c->db,
202  $c->entityCache,
203  $c->metadataCache,
204  $c->subtypeTable,
205  $c->events,
206  $c->session,
207  $c->translator,
208  $c->logger
209  );
210  });
211 
212  $this->setFactory('events', function(ServiceProvider $c) {
213  return $this->resolveLoggerDependencies('events');
214  });
215 
216  $this->setFactory('externalFiles', function(ServiceProvider $c) {
217  return new \Elgg\Assets\ExternalFiles($c->config->getStorageObject());
218  });
219 
220  $this->setFactory('fileCache', function(ServiceProvider $c) {
221  return new \ElggFileCache($c->config->getCachePath() . 'system_cache/');
222  });
223 
224  $this->setFactory('forms', function(ServiceProvider $c) {
225  return new \Elgg\FormsService($c->views, $c->logger);
226  });
227 
228  $this->setFactory('hooks', function(ServiceProvider $c) {
229  return $this->resolveLoggerDependencies('hooks');
230  });
231 
232  $this->setFactory('iconService', function(ServiceProvider $c) {
233  return new \Elgg\EntityIconService($c->config, $c->hooks, $c->request, $c->logger, $c->entityTable);
234  });
235 
236  $this->setClassName('input', \Elgg\Http\Input::class);
237 
238  $this->setFactory('imageService', function(ServiceProvider $c) {
239  $imagine = new \Imagine\Gd\Imagine();
240  return new \Elgg\ImageService($imagine, $c->config);
241  });
242 
243  $this->setFactory('logger', function(ServiceProvider $c) {
244  return $this->resolveLoggerDependencies('logger');
245  });
246 
247  // TODO(evan): Support configurable transports...
248  $this->setClassName('mailer', 'Zend\Mail\Transport\Sendmail');
249 
250  $this->setFactory('menus', function(ServiceProvider $c) {
251  return new \Elgg\Menu\Service($c->hooks, $c->config);
252  });
253 
254  $this->setFactory('metadataCache', function (ServiceProvider $c) {
255  return new \Elgg\Cache\MetadataCache($c->session);
256  });
257 
258  $this->setFactory('memcacheStashPool', function(ServiceProvider $c) {
259  if (!$c->config->getVolatile('memcache')) {
260  return null;
261  }
262 
263  $servers = $c->config->getVolatile('memcache_servers');
264  if (!$servers) {
265  return null;
266  }
267 
268  if (!\Stash\Driver\Memcache::isAvailable()) {
269  return null;
270  }
271 
272  $driver = new \Stash\Driver\Memcache([
273  'servers' => $servers,
274  ]);
275  return new \Stash\Pool($driver);
276  });
277 
278  $this->setFactory('metadataTable', function(ServiceProvider $c) {
279  // TODO(ewinslow): Use Pool instead of MetadataCache for caching
280  return new \Elgg\Database\MetadataTable(
281  $c->metadataCache, $c->db, $c->entityTable, $c->events, $c->metastringsTable, $c->session);
282  });
283 
284  $this->setFactory('metastringsTable', function(ServiceProvider $c) {
285  // TODO(ewinslow): Use memcache-based Pool if available...
286  $pool = new Pool\InMemory();
287  return new \Elgg\Database\MetastringsTable($pool, $c->db);
288  });
289 
290  $this->setFactory('mutex', function(ServiceProvider $c) {
291  return new \Elgg\Database\Mutex(
292  $c->db,
293  $c->logger
294  );
295  });
296 
297  $this->setFactory('notifications', function(ServiceProvider $c) {
298  // @todo move queue in service provider
300  $queue = new \Elgg\Queue\DatabaseQueue($queue_name, $c->db);
301  $sub = new \Elgg\Notifications\SubscriptionsService($c->db);
302  return new \Elgg\Notifications\NotificationsService($sub, $queue, $c->hooks, $c->session, $c->translator, $c->entityTable, $c->logger);
303  });
304 
305  $this->setClassName('nullCache', \Elgg\Cache\NullCache::class);
306 
307  $this->setFactory('persistentLogin', function(ServiceProvider $c) {
308  $global_cookies_config = $c->config->getCookieConfig();
309  $cookie_config = $global_cookies_config['remember_me'];
310  $cookie_name = $cookie_config['name'];
311  $cookie_token = $c->request->cookies->get($cookie_name, '');
312  return new \Elgg\PersistentLoginService(
313  $c->db, $c->session, $c->crypto, $cookie_config, $cookie_token);
314  });
315 
316  $this->setClassName('passwords', \Elgg\PasswordService::class);
317 
318  $this->setFactory('plugins', function(ServiceProvider $c) {
319  $pool = new Pool\InMemory();
320  $plugins = new \Elgg\Database\Plugins($pool, $c->pluginSettingsCache);
321  if ($c->config->getVolatile('enable_profiling')) {
322  $plugins->setTimer($c->timer);
323  }
324  return $plugins;
325  });
326 
327  $this->setClassName('pluginSettingsCache', \Elgg\Cache\PluginSettingsCache::class);
328 
329  $this->setFactory('privateSettings', function(ServiceProvider $c) {
330  return new \Elgg\Database\PrivateSettingsTable($c->db, $c->entityTable, $c->pluginSettingsCache);
331  });
332 
333  $this->setFactory('publicDb', function(ServiceProvider $c) {
334  return new \Elgg\Application\Database($c->db);
335  });
336 
337  $this->setFactory('queryCounter', function(ServiceProvider $c) {
338  return new \Elgg\Database\QueryCounter($c->db);
339  }, false);
340 
341  $this->setFactory('redirects', function(ServiceProvider $c) {
343  $is_xhr = $c->request->isXmlHttpRequest();
344  return new \Elgg\RedirectService($c->session, $is_xhr, $c->config->getSiteUrl(), $url);
345  });
346 
347  $this->setFactory('relationshipsTable', function(ServiceProvider $c) {
348  return new \Elgg\Database\RelationshipsTable($c->db, $c->entityTable, $c->metadataTable, $c->events);
349  });
350 
351  $this->setFactory('request', [\Elgg\Http\Request::class, 'createFromGlobals']);
352 
353  $this->setFactory('responseFactory', function(ServiceProvider $c) {
354  if (PHP_SAPI === 'cli') {
355  $transport = new \Elgg\Http\OutputBufferTransport();
356  } else {
357  $transport = new \Elgg\Http\HttpProtocolTransport();
358  }
359  return new \Elgg\Http\ResponseFactory($c->request, $c->hooks, $c->ajax, $transport);
360  });
361 
362  $this->setFactory('router', function(ServiceProvider $c) {
363  // TODO(evan): Init routes from plugins or cache
364  $router = new \Elgg\Router($c->hooks);
365  if ($c->config->getVolatile('enable_profiling')) {
366  $router->setTimer($c->timer);
367  }
368  return $router;
369  });
370 
371  $this->setFactory('seeder', function(ServiceProvider $c) {
372  return new \Elgg\Database\Seeder($c->hooks);
373  });
374 
375  $this->setFactory('serveFileHandler', function(ServiceProvider $c) {
376  return new \Elgg\Application\ServeFileHandler($c->crypto, $c->config);
377  });
378 
379  $this->setFactory('session', function(ServiceProvider $c) {
380  $params = $c->config->getCookieConfig()['session'];
381  $options = [
382  // session.cache_limiter is unfortunately set to "" by the NativeSessionStorage
383  // constructor, so we must capture and inject it directly.
384  'cache_limiter' => session_cache_limiter(),
385 
386  'name' => $params['name'],
387  'cookie_path' => $params['path'],
388  'cookie_domain' => $params['domain'],
389  'cookie_secure' => $params['secure'],
390  'cookie_httponly' => $params['httponly'],
391  'cookie_lifetime' => $params['lifetime'],
392  ];
393 
394  $handler = new \Elgg\Http\DatabaseSessionHandler($c->db);
395  $storage = new NativeSessionStorage($options, $handler);
396  $session = new SymfonySession($storage);
397  return new \ElggSession($session);
398  });
399 
400  $this->setClassName('urlSigner', \Elgg\Security\UrlSigner::class);
401 
402  $this->setFactory('simpleCache', function(ServiceProvider $c) {
403  return new \Elgg\Cache\SimpleCache($c->config, $c->datalist, $c->views);
404  });
405 
406  $this->setFactory('siteSecret', function(ServiceProvider $c) {
407  return new \Elgg\Database\SiteSecret($c->datalist);
408  });
409 
410  $this->setClassName('stickyForms', \Elgg\Forms\StickyForms::class);
411 
412  $this->setFactory('subtypeTable', function(ServiceProvider $c) {
413  return new \Elgg\Database\SubtypeTable($c->db);
414  });
415 
416  $this->setFactory('systemCache', function (ServiceProvider $c) {
417  $cache = new \Elgg\Cache\SystemCache($c->fileCache, $c->config, $c->datalist);
418  if ($c->config->getVolatile('enable_profiling')) {
419  $cache->setTimer($c->timer);
420  }
421  return $cache;
422  });
423 
424  $this->setFactory('systemMessages', function(ServiceProvider $c) {
425  return new \Elgg\SystemMessagesService($c->session);
426  });
427 
428  $this->setClassName('table_columns', \Elgg\Views\TableColumn\ColumnFactory::class);
429 
430  $this->setClassName('timer', \Elgg\Timer::class);
431 
432  $this->setClassName('translator', \Elgg\I18n\Translator::class);
433 
434  $this->setFactory('uploads', function(ServiceProvider $c) {
435  return new \Elgg\UploadService($c->request);
436  });
437 
438  $this->setFactory('upgrades', function(ServiceProvider $c) {
439  return new \Elgg\UpgradeService(
440  $c->translator,
441  $c->events,
442  $c->hooks,
443  $c->datalist,
444  $c->logger,
445  $c->mutex
446  );
447  });
448 
449  $this->setFactory('userCapabilities', function(ServiceProvider $c) {
450  return new \Elgg\UserCapabilities($c->hooks, $c->entityTable, $c->session);
451  });
452 
453  $this->setFactory('usersTable', function(ServiceProvider $c) {
454  return new \Elgg\Database\UsersTable(
455  $c->config,
456  $c->db,
457  $c->entityTable,
458  $c->entityCache,
459  $c->events
460  );
461  });
462 
463  $this->setFactory('views', function(ServiceProvider $c) {
464  return new \Elgg\ViewsService($c->hooks, $c->logger);
465  });
466 
467  $this->setClassName('widgets', \Elgg\WidgetsService::class);
468 
469  }
470 
478  protected function resolveLoggerDependencies($service_needed) {
479  $svcs['hooks'] = new \Elgg\PluginHooksService();
480 
481  $svcs['logger'] = new \Elgg\Logger($svcs['hooks'], $this->config, $this->context);
482  $svcs['hooks']->setLogger($svcs['logger']);
483 
484  $svcs['events'] = new \Elgg\EventsService();
485  $svcs['events']->setLogger($svcs['logger']);
486  if ($this->config->getVolatile('enable_profiling')) {
487  $svcs['events']->setTimer($this->timer);
488  }
489 
490  foreach ($svcs as $key => $service) {
491  $this->setValue($key, $service);
492  }
493  return $svcs[$service_needed];
494  }
495 }
$servers
Memcache info.
Definition: memcache.php:5
The Elgg database.
Definition: Database.php:17
Access to configuration values.
Definition: Config.php:11
current_page_url()
Returns the current page&#39;s complete URL.
Definition: input.php:65
resolveLoggerDependencies($service_needed)
Returns the first requested service of the logger, events, and hooks.
$url
Definition: exceptions.php:24
$options
Elgg admin footer.
Definition: footer.php:6
$params
Definition: login.php:72
Save menu items.
$key
Definition: summary.php:34
__construct(\Elgg\Config $config)
Constructor.
$dbprefix
Definition: index.php:13
setClassName($name, $class_name, $shared=true)
Set a factory based on instantiating a class with no arguments.
setValue($name, $value)
Set a value to be returned without modification.
Definition: DiContainer.php:91
setFactory($name, $callable, $shared=true)
Set a factory to generate a value when the container is read.
class
Definition: placeholder.php:21
$handler
Definition: add.php:10
$session
Definition: login.php:9
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:5
$loader
Definition: ajax_loader.php:32