Elgg  Version master
DiContainer.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Di;
4 
5 use DI\Container;
6 use DI\ContainerBuilder;
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
static factory()
Create a DI Container.
Definition: DiContainer.php:91
__set($name, $value)
{}
Definition: DiContainer.php:47
reset(string $name)
Unsets the service to force rebuild on next request.
Definition: DiContainer.php:72
static getDefinitionSources()
Returns an array of file locations.
if($item instanceof \ElggEntity) elseif($item instanceof \ElggRiverItem) elseif($item instanceof \ElggRelationship) elseif(is_callable([ $item, 'getType']))
Definition: item.php:48
_elgg_services()
Get the global service provider.
Definition: elgglib.php:353
$value
Definition: generic.php:51
$location
Definition: member.php:29
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10