Elgg  Version 6.1
Invoker.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
6 
10 class Invoker {
11 
18  public function __construct(protected SessionManagerService $session_manager, protected PublicContainer $dic) {
19  }
20 
37  public function call(int $flags, \Closure $closure) {
38 
39  $access = $this->session_manager->getIgnoreAccess();
40  if ($flags & ELGG_IGNORE_ACCESS) {
41  $this->session_manager->setIgnoreAccess(true);
42  } elseif ($flags & ELGG_ENFORCE_ACCESS) {
43  $this->session_manager->setIgnoreAccess(false);
44  }
45 
46  $disabled = $this->session_manager->getDisabledEntityVisibility();
47  if ($flags & ELGG_SHOW_DISABLED_ENTITIES) {
48  $this->session_manager->setDisabledEntityVisibility(true);
49  } elseif ($flags & ELGG_HIDE_DISABLED_ENTITIES) {
50  $this->session_manager->setDisabledEntityVisibility(false);
51  }
52 
53  $deleted = $this->session_manager->getDeletedEntityVisibility();
54  if ($flags & ELGG_SHOW_DELETED_ENTITIES) {
55  $this->session_manager->setDeletedEntityVisibility(true);
56  } elseif ($flags & ELGG_HIDE_DELETED_ENTITIES) {
57  $this->session_manager->setDeletedEntityVisibility(false);
58  }
59 
60  $system_log_enabled = null;
61  $system_log_service = null;
62  if ((($flags & ELGG_ENABLE_SYSTEM_LOG) || ($flags & ELGG_DISABLE_SYSTEM_LOG)) && elgg_is_active_plugin('system_log')) {
63  try {
64  $system_log_service = \Elgg\SystemLog\SystemLog::instance();
65  $system_log_enabled = $system_log_service->isLoggingEnabled();
66 
67  if ($flags & ELGG_ENABLE_SYSTEM_LOG) {
68  $system_log_service->enableLogging();
69  } elseif ($flags & ELGG_DISABLE_SYSTEM_LOG) {
70  $system_log_service->disableLogging();
71  }
72  } catch (\DI\NotFoundException $e) {
73  // somehow the service isn't correctly registered
74  }
75  }
76 
77  $restore = function () use ($access, $disabled, $deleted, $system_log_service, $system_log_enabled) {
78  $this->session_manager->setIgnoreAccess($access);
79  $this->session_manager->setDisabledEntityVisibility($disabled);
80  $this->session_manager->setDeletedEntityVisibility($deleted);
81 
82  if (isset($system_log_service)) {
83  if ($system_log_enabled) {
84  $system_log_service->enableLogging();
85  } else {
86  $system_log_service->disableLogging();
87  }
88  }
89  };
90 
91  try {
92  $result = $this->dic->call($closure);
93  } catch (\Throwable $e) {
94  $restore();
95  throw $e;
96  }
97 
98  $restore();
99 
100  return $result;
101  }
102 }
const ELGG_DISABLE_SYSTEM_LOG
Definition: constants.php:125
$deleted
Definition: delete.php:25
const ELGG_ENFORCE_ACCESS
Definition: constants.php:122
if(elgg_view_exists("widgets/{$widget->handler}/edit")) $access
Definition: save.php:19
call(int $flags,\Closure $closure)
Calls a callable autowiring the arguments using public DI services and applying logic based on flags...
Definition: Invoker.php:37
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:124
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:121
const ELGG_SHOW_DISABLED_ENTITIES
Definition: constants.php:123
const ELGG_ENABLE_SYSTEM_LOG
Definition: constants.php:126
elgg_is_active_plugin(string $plugin_id)
Returns if a plugin is active for a current site.
Definition: plugins.php:27
__construct(protected SessionManagerService $session_manager, protected PublicContainer $dic)
Constructor.
Definition: Invoker.php:18
const ELGG_SHOW_DELETED_ENTITIES
Definition: constants.php:127
const ELGG_HIDE_DELETED_ENTITIES
Definition: constants.php:128
Invocation service.
Definition: Invoker.php:10