ServiceProvider.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. namespace Elgg\Di;
  3. use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
  4. use Symfony\Component\HttpFoundation\Session\Session as SymfonySession;
  5. /**
  6. * Provides common Elgg services.
  7. *
  8. * We extend the container because it allows us to document properties in the PhpDoc, which assists
  9. * IDEs to auto-complete properties and understand the types returned. Extension allows us to keep
  10. * the container generic.
  11. *
  12. * @property-read \Elgg\Database\AccessCollections $accessCollections
  13. * @property-read \ElggStaticVariableCache $accessCache
  14. * @property-read \Elgg\ActionsService $actions
  15. * @property-read \Elgg\Database\AdminNotices $adminNotices
  16. * @property-read \Elgg\Amd\Config $amdConfig
  17. * @property-read \Elgg\Database\Annotations $annotations
  18. * @property-read \ElggAutoP $autoP
  19. * @property-read \Elgg\AutoloadManager $autoloadManager
  20. * @property-read \ElggCrypto $crypto
  21. * @property-read \Elgg\Config $config
  22. * @property-read \Elgg\Database\ConfigTable $configTable
  23. * @property-read \Elgg\Context $context
  24. * @property-read \Elgg\Database\Datalist $datalist
  25. * @property-read \Elgg\Database $db
  26. * @property-read \Elgg\DeprecationService $deprecation
  27. * @property-read \Elgg\EntityPreloader $entityPreloader
  28. * @property-read \Elgg\Database\EntityTable $entityTable
  29. * @property-read \Elgg\EventsService $events
  30. * @property-read \Elgg\Assets\ExternalFiles $externalFiles
  31. * @property-read \Elgg\PluginHooksService $hooks
  32. * @property-read \Elgg\Http\Input $input
  33. * @property-read \Elgg\Logger $logger
  34. * @property-read \Elgg\Cache\MetadataCache $metadataCache
  35. * @property-read \Elgg\Database\MetadataTable $metadataTable
  36. * @property-read \Elgg\Database\MetastringsTable $metastringsTable
  37. * @property-read \Elgg\Notifications\NotificationsService $notifications
  38. * @property-read \Elgg\PasswordService $passwords
  39. * @property-read \Elgg\PersistentLoginService $persistentLogin
  40. * @property-read \Elgg\Database\Plugins $plugins
  41. * @property-read \Elgg\Database\PrivateSettingsTable $privateSettings
  42. * @property-read \Elgg\Database\QueryCounter $queryCounter
  43. * @property-read \Elgg\Http\Request $request
  44. * @property-read \Elgg\Database\RelationshipsTable $relationshipsTable
  45. * @property-read \Elgg\Router $router
  46. * @property-read \ElggSession $session
  47. * @property-read \Elgg\Cache\SimpleCache $simpleCache
  48. * @property-read \Elgg\Database\SiteSecret $siteSecret
  49. * @property-read \Elgg\Forms\StickyForms $stickyForms
  50. * @property-read \Elgg\Database\SubtypeTable $subtypeTable
  51. * @property-read \Elgg\Cache\SystemCache $systemCache
  52. * @property-read \Elgg\SystemMessagesService $systemMessages
  53. * @property-read \Elgg\I18n\Translator $translator
  54. * @property-read \Elgg\Database\UsersTable $usersTable
  55. * @property-read \Elgg\ViewsService $views
  56. * @property-read \Elgg\WidgetsService $widgets
  57. *
  58. * @package Elgg.Core
  59. * @access private
  60. */
  61. class ServiceProvider extends \Elgg\Di\DiContainer {
  62. /**
  63. * Constructor
  64. *
  65. * @param \Elgg\AutoloadManager $autoload_manager Class autoloader
  66. */
  67. public function __construct(\Elgg\AutoloadManager $autoload_manager) {
  68. $this->setValue('autoloadManager', $autoload_manager);
  69. $this->setFactory('accessCache', function(ServiceProvider $c) {
  70. return new \ElggStaticVariableCache('access');
  71. });
  72. $this->setFactory('accessCollections', function(ServiceProvider $c) {
  73. return new \Elgg\Database\AccessCollections($c->config->get('site_guid'));
  74. });
  75. $this->setClassName('actions', '\Elgg\ActionsService');
  76. $this->setClassName('adminNotices', '\Elgg\Database\AdminNotices');
  77. $this->setFactory('amdConfig', function(ServiceProvider $c) {
  78. $obj = new \Elgg\Amd\Config($c->hooks);
  79. $obj->setBaseUrl($c->simpleCache->getRoot() . "js/");
  80. return $obj;
  81. });
  82. $this->setClassName('annotations', '\Elgg\Database\Annotations');
  83. $this->setClassName('autoP', '\ElggAutoP');
  84. $this->setClassName('config', '\Elgg\Config');
  85. $this->setClassName('configTable', '\Elgg\Database\ConfigTable');
  86. $this->setClassName('context', '\Elgg\Context');
  87. $this->setClassName('crypto', '\ElggCrypto');
  88. $this->setFactory('datalist', function(ServiceProvider $c) {
  89. // TODO(ewinslow): Add back memcached support
  90. $db = $c->db;
  91. $dbprefix = $db->getTablePrefix();
  92. $pool = new \Elgg\Cache\MemoryPool();
  93. return new \Elgg\Database\Datalist($pool, $db, $c->logger, "{$dbprefix}datalists");
  94. });
  95. $this->setFactory('db', function(ServiceProvider $c) {
  96. global $CONFIG;
  97. $db_config = new \Elgg\Database\Config($CONFIG);
  98. return new \Elgg\Database($db_config, $c->logger);
  99. });
  100. $this->setFactory('deprecation', function(ServiceProvider $c) {
  101. return new \Elgg\DeprecationService($c->session, $c->logger);
  102. });
  103. $this->setClassName('entityPreloader', '\Elgg\EntityPreloader');
  104. $this->setClassName('entityTable', '\Elgg\Database\EntityTable');
  105. $this->setFactory('events', function(ServiceProvider $c) {
  106. return $this->resolveLoggerDependencies('events');
  107. });
  108. $this->setFactory('externalFiles', function(ServiceProvider $c) {
  109. global $CONFIG;
  110. return new \Elgg\Assets\ExternalFiles($CONFIG);
  111. });
  112. $this->setFactory('hooks', function(ServiceProvider $c) {
  113. return $this->resolveLoggerDependencies('hooks');
  114. });
  115. $this->setClassName('input', 'Elgg\Http\Input');
  116. $this->setFactory('logger', function(ServiceProvider $c) {
  117. return $this->resolveLoggerDependencies('logger');
  118. });
  119. $this->setFactory('metadataCache', function (ServiceProvider $c) {
  120. return new \Elgg\Cache\MetadataCache($c->session);
  121. });
  122. $this->setFactory('metadataTable', function(ServiceProvider $c) {
  123. // TODO(ewinslow): Use Elgg\Cache\Pool instead of MetadataCache
  124. return new \Elgg\Database\MetadataTable(
  125. $c->metadataCache, $c->db, $c->entityTable, $c->events, $c->metastringsTable, $c->session);
  126. });
  127. $this->setFactory('metastringsTable', function(ServiceProvider $c) {
  128. // TODO(ewinslow): Use memcache-based Pool if available...
  129. $pool = new \Elgg\Cache\MemoryPool();
  130. return new \Elgg\Database\MetastringsTable($pool, $c->db);
  131. });
  132. $this->setFactory('notifications', function(ServiceProvider $c) {
  133. // @todo move queue in service provider
  134. $queue_name = \Elgg\Notifications\NotificationsService::QUEUE_NAME;
  135. $queue = new \Elgg\Queue\DatabaseQueue($queue_name, $c->db);
  136. $sub = new \Elgg\Notifications\SubscriptionsService($c->db);
  137. return new \Elgg\Notifications\NotificationsService($sub, $queue, $c->hooks, $c->session);
  138. });
  139. $this->setFactory('persistentLogin', function(ServiceProvider $c) {
  140. $global_cookies_config = _elgg_services()->config->get('cookies');
  141. $cookie_config = $global_cookies_config['remember_me'];
  142. $cookie_name = $cookie_config['name'];
  143. $cookie_token = $c->request->cookies->get($cookie_name, '');
  144. return new \Elgg\PersistentLoginService(
  145. $c->db, $c->session, $c->crypto, $cookie_config, $cookie_token);
  146. });
  147. $this->setFactory('passwords', function (ServiceProvider $c) {
  148. if (!function_exists('password_hash')) {
  149. $root = $c->config->getRootPath();
  150. require "{$root}vendor/ircmaxell/password-compat/lib/password.php";
  151. }
  152. return new \Elgg\PasswordService();
  153. });
  154. $this->setFactory('plugins', function(ServiceProvider $c) {
  155. return new \Elgg\Database\Plugins($c->events, new \Elgg\Cache\MemoryPool());
  156. });
  157. $this->setFactory('privateSettings', function(ServiceProvider $c) {
  158. return new \Elgg\Database\PrivateSettingsTable($c->db, $c->entityTable);
  159. });
  160. $this->setFactory('queryCounter', function(ServiceProvider $c) {
  161. return new \Elgg\Database\QueryCounter($c->db);
  162. }, false);
  163. $this->setClassName('relationshipsTable', '\Elgg\Database\RelationshipsTable');
  164. $this->setFactory('request', '\Elgg\Http\Request::createFromGlobals');
  165. $this->setFactory('router', function(ServiceProvider $c) {
  166. // TODO(evan): Init routes from plugins or cache
  167. return new \Elgg\Router($c->hooks);
  168. });
  169. $this->setFactory('session', function(ServiceProvider $c) {
  170. $params = $c->config->get('cookies')['session'];
  171. $options = [
  172. // session.cache_limiter is unfortunately set to "" by the NativeSessionStorage
  173. // constructor, so we must capture and inject it directly.
  174. 'cache_limiter' => session_cache_limiter(),
  175. 'name' => $params['name'],
  176. 'cookie_path' => $params['path'],
  177. 'cookie_domain' => $params['domain'],
  178. 'cookie_secure' => $params['secure'],
  179. 'cookie_httponly' => $params['httponly'],
  180. 'cookie_lifetime' => $params['lifetime'],
  181. ];
  182. $handler = new \Elgg\Http\DatabaseSessionHandler($c->db);
  183. $storage = new NativeSessionStorage($options, $handler);
  184. $session = new SymfonySession($storage);
  185. return new \ElggSession($session);
  186. });
  187. $this->setClassName('simpleCache', '\Elgg\Cache\SimpleCache');
  188. $this->setClassName('siteSecret', '\Elgg\Database\SiteSecret');
  189. $this->setClassName('stickyForms', 'Elgg\Forms\StickyForms');
  190. $this->setClassName('subtypeTable', '\Elgg\Database\SubtypeTable');
  191. $this->setClassName('systemCache', '\Elgg\Cache\SystemCache');
  192. $this->setFactory('systemMessages', function(ServiceProvider $c) {
  193. return new \Elgg\SystemMessagesService($c->session);
  194. });
  195. $this->setClassName('translator', '\Elgg\I18n\Translator');
  196. $this->setClassName('usersTable', '\Elgg\Database\UsersTable');
  197. $this->setFactory('views', function(ServiceProvider $c) {
  198. return new \Elgg\ViewsService($c->hooks, $c->logger);
  199. });
  200. $this->setClassName('widgets', '\Elgg\WidgetsService');
  201. }
  202. /**
  203. * Returns the first requested service of the logger, events, and hooks. It sets the
  204. * hooks and events up in the right order to prevent circular dependency.
  205. *
  206. * @param string $service_needed The service requested first
  207. * @return mixed
  208. */
  209. protected function resolveLoggerDependencies($service_needed) {
  210. $svcs['hooks'] = new \Elgg\PluginHooksService();
  211. $svcs['logger'] = new \Elgg\Logger($svcs['hooks']);
  212. $svcs['hooks']->setLogger($svcs['logger']);
  213. $svcs['events'] = new \Elgg\EventsService();
  214. $svcs['events']->setLogger($svcs['logger']);
  215. foreach ($svcs as $key => $service) {
  216. $this->setValue($key, $service);
  217. }
  218. return $svcs[$service_needed];
  219. }
  220. }