Elgg  Version 6.1
DiContainer.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Di;
4 
5 use DI\Container;
7 
13 abstract class DiContainer extends Container {
14 
18  public function __get($name) {
19  $service = $this->get($name);
20 
21  // get the traits implemented directly by the service
22  $traits = class_uses($service, true);
23 
24  // check for certain global cases
25  if (in_array(\Elgg\Traits\Debug\Profilable::class, $traits) && !$service->hasTimer()) {
26  // profiling is supported
27  if ($service instanceof \Elgg\Database) {
28  // the database uses a different config flag to enable profiling
29  if ($this->config->profiling_sql) {
30  // need to get the timer from the InternalContainer
31  // especially if the current DiContainer is the PublicContainer
32  $service->setTimer(_elgg_services()->timer);
33  }
34  } elseif ($this->config->enable_profiling) {
35  // need to get the timer from the InternalContainer
36  // especially if the current DiContainer is the PublicContainer
37  $service->setTimer(_elgg_services()->timer);
38  }
39  }
40 
41  return $service;
42  }
43 
47  public function __set($name, $value) {
48  // prevent setting of class variables using container->service
49  $this->set($name, $value);
50  }
51 
55  public function set(string $name, mixed $value): void {
56  parent::set($name, $value);
57 
58  if (is_object($value)) {
59  // need to also reset related class name as it is also stored as a reference for autowired classes
60  // this happens for example in the installer where the plugins service is autowired with 'old' config (found by classname) as config by name is set
61  $this->reset(get_class($value));
62  }
63  }
64 
72  public function reset(string $name): void {
73  if (!isset($this->resolvedEntries[$name])) {
74  return;
75  }
76 
77  $value = $this->resolvedEntries[$name];
78 
79  unset($this->resolvedEntries[$name]);
80  if (is_object($value)) {
81  // need to also reset related class name as it is also stored as a reference for autowired classes
82  unset($this->resolvedEntries[get_class($value)]);
83  }
84  }
85 
91  public static function factory() {
92  $dic_builder = new ContainerBuilder(static::class);
93  $dic_builder->useAttributes(false);
94 
95  foreach (static::getDefinitionSources() as $location) {
96  $dic_builder->addDefinitions($location);
97  }
98 
99  return $dic_builder->build();
100  }
101 
107  abstract public static function getDefinitionSources(): array;
108 }
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
The Elgg database.
Definition: Database.php:26
Base DI Container class.
Definition: DiContainer.php:13
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
$value
Definition: generic.php:51
static getDefinitionSources()
Returns an array of file locations.
reset(string $name)
Unsets the service to force rebuild on next request.
Definition: DiContainer.php:72
static factory()
Create a DI Container.
Definition: DiContainer.php:91
__set($name, $value)
{}
Definition: DiContainer.php:47
$location
Definition: member.php:29
_elgg_services()
Get the global service provider.
Definition: elgglib.php:353