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