Elgg  Version 5.1
Invoker.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
6 
10 class Invoker {
11 
15  protected $session_manager;
16 
20  protected $dic;
21 
28  public function __construct(SessionManagerService $session_manager, PublicContainer $dic) {
29  $this->session_manager = $session_manager;
30  $this->dic = $dic;
31  }
32 
47  public function call(int $flags, \Closure $closure) {
48 
49  $ia = $this->session_manager->getIgnoreAccess();
50  if ($flags & ELGG_IGNORE_ACCESS) {
51  $this->session_manager->setIgnoreAccess(true);
52  } else if ($flags & ELGG_ENFORCE_ACCESS) {
53  $this->session_manager->setIgnoreAccess(false);
54  }
55 
56  $ha = $this->session_manager->getDisabledEntityVisibility();
57  if ($flags & ELGG_SHOW_DISABLED_ENTITIES) {
58  $this->session_manager->setDisabledEntityVisibility(true);
59  } else if ($flags & ELGG_HIDE_DISABLED_ENTITIES) {
60  $this->session_manager->setDisabledEntityVisibility(false);
61  }
62 
63  $system_log_enabled = null;
64  $system_log_service = null;
65  if ((($flags & ELGG_ENABLE_SYSTEM_LOG) || ($flags & ELGG_DISABLE_SYSTEM_LOG)) && elgg_is_active_plugin('system_log')) {
66  try {
67  $system_log_service = \Elgg\SystemLog\SystemLog::instance();
68  $system_log_enabled = $system_log_service->isLoggingEnabled();
69 
70  if ($flags & ELGG_ENABLE_SYSTEM_LOG) {
71  $system_log_service->enableLogging();
72  } elseif ($flags & ELGG_DISABLE_SYSTEM_LOG) {
73  $system_log_service->disableLogging();
74  }
75  } catch (\DI\NotFoundException $e) {
76  // somehow the service isn't correctly registered
77  }
78  }
79 
80  $restore = function () use ($ia, $ha, $system_log_service, $system_log_enabled) {
81  $this->session_manager->setIgnoreAccess($ia);
82  $this->session_manager->setDisabledEntityVisibility($ha);
83 
84  if (isset($system_log_service)) {
85  if ($system_log_enabled) {
86  $system_log_service->enableLogging();
87  } else {
88  $system_log_service->disableLogging();
89  }
90  }
91  };
92 
93  try {
94  $result = $this->dic->call($closure);
95  } catch (\Throwable $e) {
96  $restore();
97  throw $e;
98  }
99 
100  $restore();
101 
102  return $result;
103  }
104 }
const ELGG_DISABLE_SYSTEM_LOG
Definition: constants.php:134
const ELGG_ENFORCE_ACCESS
Definition: constants.php:131
call(int $flags,\Closure $closure)
Calls a callable autowiring the arguments using public DI services and applying logic based on flags...
Definition: Invoker.php:47
__construct(SessionManagerService $session_manager, PublicContainer $dic)
Constructor.
Definition: Invoker.php:28
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
const ELGG_HIDE_DISABLED_ENTITIES
Definition: constants.php:133
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:130
const ELGG_SHOW_DISABLED_ENTITIES
Definition: constants.php:132
const ELGG_ENABLE_SYSTEM_LOG
Definition: constants.php:135
elgg_is_active_plugin(string $plugin_id)
Returns if a plugin is active for a current site.
Definition: plugins.php:43
Invocation service.
Definition: Invoker.php:10