Elgg  Version 1.12
ServiceProvider.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Di;
3 
6 
64 
70  public function __construct(\Elgg\AutoloadManager $autoload_manager) {
71  $this->setValue('autoloadManager', $autoload_manager);
72 
73  $this->setFactory('accessCache', function(ServiceProvider $c) {
74  return new \ElggStaticVariableCache('access');
75  });
76 
77  $this->setFactory('accessCollections', function(ServiceProvider $c) {
78  return new \Elgg\Database\AccessCollections($c->config->get('site_guid'));
79  });
80 
81  $this->setClassName('actions', '\Elgg\ActionsService');
82 
83  $this->setClassName('adminNotices', '\Elgg\Database\AdminNotices');
84 
85  $this->setFactory('amdConfig', function(ServiceProvider $c) {
86  $obj = new \Elgg\Amd\Config($c->hooks);
87  $obj->setBaseUrl($c->simpleCache->getRoot() . "js/");
88  return $obj;
89  });
90 
91  $this->setClassName('annotations', '\Elgg\Database\Annotations');
92 
93  $this->setClassName('autoP', '\ElggAutoP');
94 
95  $this->setClassName('config', '\Elgg\Config');
96 
97  $this->setClassName('configTable', '\Elgg\Database\ConfigTable');
98 
99  $this->setClassName('context', '\Elgg\Context');
100 
101  $this->setClassName('crypto', '\ElggCrypto');
102 
103  $this->setFactory('datalist', function(ServiceProvider $c) {
104  // TODO(ewinslow): Add back memcached support
105  $db = $c->db;
106  $dbprefix = $db->getTablePrefix();
107  $pool = new \Elgg\Cache\MemoryPool();
108  return new \Elgg\Database\Datalist($pool, $db, $c->logger, "{$dbprefix}datalists");
109  });
110 
111  $this->setFactory('db', function(ServiceProvider $c) {
112  global $CONFIG;
113  $db_config = new \Elgg\Database\Config($CONFIG);
114  return new \Elgg\Database($db_config, $c->logger);
115  });
116 
117  $this->setFactory('deprecation', function(ServiceProvider $c) {
118  return new \Elgg\DeprecationService($c->session, $c->logger);
119  });
120 
121  $this->setClassName('entityPreloader', '\Elgg\EntityPreloader');
122 
123  $this->setClassName('entityTable', '\Elgg\Database\EntityTable');
124 
125  $this->setFactory('events', function(ServiceProvider $c) {
126  return $this->resolveLoggerDependencies('events');
127  });
128 
129  $this->setFactory('externalFiles', function(ServiceProvider $c) {
130  global $CONFIG;
131  return new \Elgg\Assets\ExternalFiles($CONFIG);
132  });
133 
134  $this->setFactory('hooks', function(ServiceProvider $c) {
135  return $this->resolveLoggerDependencies('hooks');
136  });
137 
138  $this->setClassName('input', 'Elgg\Http\Input');
139 
140  $this->setFactory('logger', function(ServiceProvider $c) {
141  return $this->resolveLoggerDependencies('logger');
142  });
143 
144  $this->setFactory('metadataCache', function (ServiceProvider $c) {
145  return new \Elgg\Cache\MetadataCache($c->session);
146  });
147 
148  $this->setFactory('metadataTable', function(ServiceProvider $c) {
149  // TODO(ewinslow): Use Elgg\Cache\Pool instead of MetadataCache
150  return new \Elgg\Database\MetadataTable(
151  $c->metadataCache, $c->db, $c->entityTable, $c->events, $c->metastringsTable, $c->session);
152  });
153 
154  $this->setFactory('metastringsTable', function(ServiceProvider $c) {
155  // TODO(ewinslow): Use memcache-based Pool if available...
156  $pool = new \Elgg\Cache\MemoryPool();
157  return new \Elgg\Database\MetastringsTable($pool, $c->db);
158  });
159 
160  $this->setFactory('notifications', function(ServiceProvider $c) {
161  // @todo move queue in service provider
163  $queue = new \Elgg\Queue\DatabaseQueue($queue_name, $c->db);
164  $sub = new \Elgg\Notifications\SubscriptionsService($c->db);
165  return new \Elgg\Notifications\NotificationsService($sub, $queue, $c->hooks, $c->session);
166  });
167 
168  $this->setFactory('persistentLogin', function(ServiceProvider $c) {
169  $global_cookies_config = _elgg_services()->config->get('cookies');
170  $cookie_config = $global_cookies_config['remember_me'];
171  $cookie_name = $cookie_config['name'];
172  $cookie_token = $c->request->cookies->get($cookie_name, '');
173  return new \Elgg\PersistentLoginService(
174  $c->db, $c->session, $c->crypto, $cookie_config, $cookie_token);
175  });
176 
177  $this->setFactory('passwords', function (ServiceProvider $c) {
178  if (!function_exists('password_hash')) {
179  $root = $c->config->getRootPath();
180  require "{$root}vendor/ircmaxell/password-compat/lib/password.php";
181  }
182  return new \Elgg\PasswordService();
183  });
184 
185  $this->setFactory('plugins', function(ServiceProvider $c) {
186  return new \Elgg\Database\Plugins($c->events, new \Elgg\Cache\MemoryPool());
187  });
188 
189  $this->setFactory('privateSettings', function(ServiceProvider $c) {
190  return new \Elgg\Database\PrivateSettingsTable($c->db, $c->entityTable);
191  });
192 
193  $this->setFactory('queryCounter', function(ServiceProvider $c) {
194  return new \Elgg\Database\QueryCounter($c->db);
195  }, false);
196 
197  $this->setClassName('relationshipsTable', '\Elgg\Database\RelationshipsTable');
198 
199  $this->setFactory('request', '\Elgg\Http\Request::createFromGlobals');
200 
201  $this->setFactory('router', function(ServiceProvider $c) {
202  // TODO(evan): Init routes from plugins or cache
203  return new \Elgg\Router($c->hooks);
204  });
205 
206  $this->setFactory('session', function(ServiceProvider $c) {
207  $params = $c->config->get('cookies')['session'];
208  $options = [
209  // session.cache_limiter is unfortunately set to "" by the NativeSessionStorage
210  // constructor, so we must capture and inject it directly.
211  'cache_limiter' => session_cache_limiter(),
212 
213  'name' => $params['name'],
214  'cookie_path' => $params['path'],
215  'cookie_domain' => $params['domain'],
216  'cookie_secure' => $params['secure'],
217  'cookie_httponly' => $params['httponly'],
218  'cookie_lifetime' => $params['lifetime'],
219  ];
220 
221  $handler = new \Elgg\Http\DatabaseSessionHandler($c->db);
222  $storage = new NativeSessionStorage($options, $handler);
223  $session = new SymfonySession($storage);
224  return new \ElggSession($session);
225  });
226 
227  $this->setClassName('simpleCache', '\Elgg\Cache\SimpleCache');
228 
229  $this->setClassName('siteSecret', '\Elgg\Database\SiteSecret');
230 
231  $this->setClassName('stickyForms', 'Elgg\Forms\StickyForms');
232 
233  $this->setClassName('subtypeTable', '\Elgg\Database\SubtypeTable');
234 
235  $this->setClassName('systemCache', '\Elgg\Cache\SystemCache');
236 
237  $this->setFactory('systemMessages', function(ServiceProvider $c) {
238  return new \Elgg\SystemMessagesService($c->session);
239  });
240 
241  $this->setClassName('translator', '\Elgg\I18n\Translator');
242 
243  $this->setClassName('usersTable', '\Elgg\Database\UsersTable');
244 
245  $this->setFactory('views', function(ServiceProvider $c) {
246  return new \Elgg\ViewsService($c->hooks, $c->logger);
247  });
248 
249  $this->setClassName('widgets', '\Elgg\WidgetsService');
250 
251  }
252 
260  protected function resolveLoggerDependencies($service_needed) {
261  $svcs['hooks'] = new \Elgg\PluginHooksService();
262  $svcs['logger'] = new \Elgg\Logger($svcs['hooks']);
263  $svcs['hooks']->setLogger($svcs['logger']);
264  $svcs['events'] = new \Elgg\EventsService();
265  $svcs['events']->setLogger($svcs['logger']);
266 
267  foreach ($svcs as $key => $service) {
268  $this->setValue($key, $service);
269  }
270  return $svcs[$service_needed];
271  }
272 }
$dbprefix
Definition: index.php:13
__construct(\Elgg\AutoloadManager $autoload_manager)
Constructor.
resolveLoggerDependencies($service_needed)
Returns the first requested service of the logger, events, and hooks.
$params
Definition: login.php:72
$options
Definition: index.php:14
Save menu items.
$key
Definition: summary.php:34
_elgg_services()
Definition: autoloader.php:14
global $CONFIG
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:99
elgg require
Throw an error if the required package isn&#39;t present.
Definition: elgglib.js:164
elgg global
Pointer to the global context.
Definition: elgglib.js:12
setFactory($name, $callable, $shared=true)
Set a factory to generate a value when the container is read.
$handler
Definition: add.php:10
$session
Definition: login.php:9