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