Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
ServiceProvider.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Di;
3 
5 use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
6 use Symfony\Component\HttpFoundation\Session\Session as SymfonySession;
7 use Zend\Mail\Transport\TransportInterface as Mailer;
8 
81 
87  public function __construct(\Elgg\Config $config) {
88 
89  $this->setFactory('classLoader', function(ServiceProvider $c) {
90  $loader = new \Elgg\ClassLoader(new \Elgg\ClassMap());
91  $loader->register();
92  return $loader;
93  });
94 
95  $this->setFactory('autoloadManager', function(ServiceProvider $c) {
96  $manager = new \Elgg\AutoloadManager($c->classLoader);
97  if (!$c->config->get('AutoloaderManager_skip_storage')) {
98  $manager->setStorage($c->fileCache);
99  $manager->loadCache();
100  }
101  return $manager;
102  });
103 
104  $this->setFactory('accessCache', function(ServiceProvider $c) {
105  return new \ElggStaticVariableCache('access');
106  });
107 
108  $this->setFactory('accessCollections', function(ServiceProvider $c) {
109  return new \Elgg\Database\AccessCollections($c->config->get('site_guid'));
110  });
111 
112  $this->setClassName('actions', \Elgg\ActionsService::class);
113 
114  $this->setClassName('adminNotices', \Elgg\Database\AdminNotices::class);
115 
116  $this->setFactory('ajax', function(ServiceProvider $c) {
117  return new \Elgg\Ajax\Service($c->hooks, $c->systemMessages, $c->input, $c->amdConfig);
118  });
119 
120  $this->setFactory('amdConfig', function(ServiceProvider $c) {
121  $obj = new \Elgg\Amd\Config($c->hooks);
122  $obj->setBaseUrl($c->simpleCache->getRoot());
123  return $obj;
124  });
125 
126  $this->setClassName('annotations', \Elgg\Database\Annotations::class);
127 
128  $this->setClassName('autoP', \ElggAutoP::class);
129 
130  $this->setFactory('boot', function(ServiceProvider $c) {
131  $boot = new \Elgg\BootService();
132  if ($c->config->getVolatile('enable_profiling')) {
133  $boot->setTimer($c->timer);
134  }
135  return $boot;
136  });
137 
138  $this->setValue('config', $config);
139 
140  $this->setClassName('configTable', \Elgg\Database\ConfigTable::class);
141 
142  $this->setClassName('context', \Elgg\Context::class);
143 
144  $this->setFactory('crypto', function(ServiceProvider $c) {
145  return new \ElggCrypto($c->siteSecret);
146  });
147 
148  $this->setFactory('datalist', function(ServiceProvider $c) {
149  // TODO(ewinslow): Add back memcached support
150  $db = $c->db;
151  $dbprefix = $db->getTablePrefix();
152  $pool = new Pool\InMemory();
153  return new \Elgg\Database\Datalist($pool, $db, $c->logger, "{$dbprefix}datalists");
154  });
155 
156  $this->setFactory('db', function(ServiceProvider $c) {
157  // gonna need dbprefix from settings
158  $c->config->loadSettingsFile();
159  $db_config = new \Elgg\Database\Config($c->config->getStorageObject());
160 
161  // we inject the logger in _elgg_engine_boot()
162  $db = new \Elgg\Database($db_config);
163 
164  if ($c->config->getVolatile('profiling_sql')) {
165  $db->setTimer($c->timer);
166  }
167 
168  return $db;
169  });
170 
171  $this->setFactory('deprecation', function(ServiceProvider $c) {
172  return new \Elgg\DeprecationService($c->logger);
173  });
174 
175  $this->setFactory('entityCache', function(ServiceProvider $c) {
176  return new \Elgg\Cache\EntityCache($c->session, $c->metadataCache);
177  });
178 
179  $this->setFactory('entityPreloader', function(ServiceProvider $c) {
180  return new \Elgg\EntityPreloader($c->entityCache, $c->entityTable);
181  });
182 
183  $this->setClassName('entityTable', \Elgg\Database\EntityTable::class);
184 
185  $this->setFactory('events', function(ServiceProvider $c) {
186  return $this->resolveLoggerDependencies('events');
187  });
188 
189  $this->setFactory('externalFiles', function(ServiceProvider $c) {
190  return new \Elgg\Assets\ExternalFiles($c->config->getStorageObject());
191  });
192 
193  $this->setFactory('fileCache', function(ServiceProvider $c) {
194  return new \ElggFileCache($c->config->getCachePath() . 'system_cache/');
195  });
196 
197  $this->setFactory('hooks', function(ServiceProvider $c) {
198  return $this->resolveLoggerDependencies('hooks');
199  });
200 
201  $this->setFactory('iconService', function(ServiceProvider $c) {
202  return new \Elgg\EntityIconService($c->config, $c->hooks, $c->request, $c->logger, $c->entityTable);
203  });
204 
205  $this->setClassName('input', \Elgg\Http\Input::class);
206 
207  $this->setFactory('logger', function(ServiceProvider $c) {
208  return $this->resolveLoggerDependencies('logger');
209  });
210 
211  // TODO(evan): Support configurable transports...
212  $this->setClassName('mailer', 'Zend\Mail\Transport\Sendmail');
213 
214  $this->setFactory('menus', function(ServiceProvider $c) {
215  return new \Elgg\Menu\Service($c->hooks, $c->config);
216  });
217 
218  $this->setFactory('metadataCache', function (ServiceProvider $c) {
219  return new \Elgg\Cache\MetadataCache($c->session);
220  });
221 
222  $this->setFactory('metadataTable', function(ServiceProvider $c) {
223  // TODO(ewinslow): Use Pool instead of MetadataCache for caching
224  return new \Elgg\Database\MetadataTable(
225  $c->metadataCache, $c->db, $c->entityTable, $c->events, $c->metastringsTable, $c->session);
226  });
227 
228  $this->setFactory('metastringsTable', function(ServiceProvider $c) {
229  // TODO(ewinslow): Use memcache-based Pool if available...
230  $pool = new Pool\InMemory();
231  return new \Elgg\Database\MetastringsTable($pool, $c->db);
232  });
233 
234  $this->setFactory('mutex', function(ServiceProvider $c) {
235  return new \Elgg\Database\Mutex(
236  $c->db,
237  $c->logger
238  );
239  });
240 
241  $this->setFactory('notifications', function(ServiceProvider $c) {
242  // @todo move queue in service provider
244  $queue = new \Elgg\Queue\DatabaseQueue($queue_name, $c->db);
245  $sub = new \Elgg\Notifications\SubscriptionsService($c->db);
246  return new \Elgg\Notifications\NotificationsService($sub, $queue, $c->hooks, $c->session, $c->translator, $c->entityTable);
247  });
248 
249  $this->setFactory('persistentLogin', function(ServiceProvider $c) {
250  $global_cookies_config = $c->config->getCookieConfig();
251  $cookie_config = $global_cookies_config['remember_me'];
252  $cookie_name = $cookie_config['name'];
253  $cookie_token = $c->request->cookies->get($cookie_name, '');
254  return new \Elgg\PersistentLoginService(
255  $c->db, $c->session, $c->crypto, $cookie_config, $cookie_token);
256  });
257 
258  $this->setClassName('passwords', \Elgg\PasswordService::class);
259 
260  $this->setFactory('plugins', function(ServiceProvider $c) {
261  $pool = new Pool\InMemory();
262  $plugins = new \Elgg\Database\Plugins($pool, $c->pluginSettingsCache);
263  if ($c->config->getVolatile('enable_profiling')) {
264  $plugins->setTimer($c->timer);
265  }
266  return $plugins;
267  });
268 
269  $this->setClassName('pluginSettingsCache', \Elgg\Cache\PluginSettingsCache::class);
270 
271  $this->setFactory('privateSettings', function(ServiceProvider $c) {
272  return new \Elgg\Database\PrivateSettingsTable($c->db, $c->entityTable, $c->pluginSettingsCache);
273  });
274 
275  $this->setFactory('publicDb', function(ServiceProvider $c) {
276  return new \Elgg\Application\Database($c->db);
277  });
278 
279  $this->setFactory('queryCounter', function(ServiceProvider $c) {
280  return new \Elgg\Database\QueryCounter($c->db);
281  }, false);
282 
283  $this->setFactory('relationshipsTable', function(ServiceProvider $c) {
284  return new \Elgg\Database\RelationshipsTable($c->db, $c->entityTable, $c->metadataTable, $c->events);
285  });
286 
287  $this->setFactory('request', [\Elgg\Http\Request::class, 'createFromGlobals']);
288 
289  $this->setFactory('router', function(ServiceProvider $c) {
290  // TODO(evan): Init routes from plugins or cache
291  $router = new \Elgg\Router($c->hooks);
292  if ($c->config->getVolatile('enable_profiling')) {
293  $router->setTimer($c->timer);
294  }
295  return $router;
296  });
297 
298  $this->setFactory('serveFileHandler', function(ServiceProvider $c) {
299  return new \Elgg\Application\ServeFileHandler($c->crypto, $c->config);
300  });
301 
302  $this->setFactory('session', function(ServiceProvider $c) {
303  $params = $c->config->getCookieConfig()['session'];
304  $options = [
305  // session.cache_limiter is unfortunately set to "" by the NativeSessionStorage
306  // constructor, so we must capture and inject it directly.
307  'cache_limiter' => session_cache_limiter(),
308 
309  'name' => $params['name'],
310  'cookie_path' => $params['path'],
311  'cookie_domain' => $params['domain'],
312  'cookie_secure' => $params['secure'],
313  'cookie_httponly' => $params['httponly'],
314  'cookie_lifetime' => $params['lifetime'],
315  ];
316 
317  $handler = new \Elgg\Http\DatabaseSessionHandler($c->db);
318  $storage = new NativeSessionStorage($options, $handler);
319  $session = new SymfonySession($storage);
320  return new \ElggSession($session);
321  });
322 
323  $this->setFactory('simpleCache', function(ServiceProvider $c) {
324  return new \Elgg\Cache\SimpleCache($c->config, $c->datalist, $c->views);
325  });
326 
327  $this->setFactory('siteSecret', function(ServiceProvider $c) {
328  return new \Elgg\Database\SiteSecret($c->datalist);
329  });
330 
331  $this->setClassName('stickyForms', \Elgg\Forms\StickyForms::class);
332 
333  $this->setFactory('subtypeTable', function(ServiceProvider $c) {
334  return new \Elgg\Database\SubtypeTable($c->db);
335  });
336 
337  $this->setFactory('systemCache', function (ServiceProvider $c) {
338  $cache = new \Elgg\Cache\SystemCache($c->fileCache, $c->config, $c->datalist);
339  if ($c->config->getVolatile('enable_profiling')) {
340  $cache->setTimer($c->timer);
341  }
342  return $cache;
343  });
344 
345  $this->setFactory('systemMessages', function(ServiceProvider $c) {
346  return new \Elgg\SystemMessagesService($c->session);
347  });
348 
349  $this->setClassName('timer', \Elgg\Timer::class);
350 
351  $this->setClassName('translator', \Elgg\I18n\Translator::class);
352 
353  $this->setFactory('upgrades', function(ServiceProvider $c) {
354  return new \Elgg\UpgradeService(
355  $c->translator,
356  $c->events,
357  $c->hooks,
358  $c->datalist,
359  $c->logger,
360  $c->mutex
361  );
362  });
363 
364  $this->setFactory('userCapabilities', function(ServiceProvider $c) {
365  return new \Elgg\UserCapabilities($c->hooks, $c->entityTable, $c->session);
366  });
367 
368  $this->setClassName('usersTable', \Elgg\Database\UsersTable::class);
369 
370  $this->setFactory('views', function(ServiceProvider $c) {
371  return new \Elgg\ViewsService($c->hooks, $c->logger);
372  });
373 
374  $this->setClassName('widgets', \Elgg\WidgetsService::class);
375 
376  }
377 
385  protected function resolveLoggerDependencies($service_needed) {
386  $svcs['hooks'] = new \Elgg\PluginHooksService();
387 
388  $svcs['logger'] = new \Elgg\Logger($svcs['hooks'], $this->config, $this->context);
389  $svcs['hooks']->setLogger($svcs['logger']);
390 
391  $svcs['events'] = new \Elgg\EventsService();
392  $svcs['events']->setLogger($svcs['logger']);
393  if ($this->config->getVolatile('enable_profiling')) {
394  $svcs['events']->setTimer($this->timer);
395  }
396 
397  foreach ($svcs as $key => $service) {
398  $this->setValue($key, $service);
399  }
400  return $svcs[$service_needed];
401  }
402 }
Access to configuration values.
Definition: Config.php:11
resolveLoggerDependencies($service_needed)
Returns the first requested service of the logger, events, and hooks.
$options
Elgg admin footer.
Definition: footer.php:6
$params
Definition: login.php:72
$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
$loader
Definition: ajax_loader.php:32