34 private $factories_ = [];
46 if (!isset($this->factories_[
$name])) {
47 throw new \Elgg\Di\MissingValueException(
"Value or factory was not set for: $name");
49 $value = $this->build($this->factories_[$name][
'callable'], $name);
53 if (!empty($this->factories_[$name]) && $this->factories_[$name][
'shared']) {
67 private function build($factory,
$name) {
68 if (is_callable($factory)) {
69 return call_user_func($factory, $this);
71 $msg =
"Factory for '$name' was uncallable";
72 if (is_string($factory)) {
73 $msg .=
": '$factory'";
74 }
elseif (is_array($factory)) {
75 if (is_string($factory[0])) {
76 $msg .=
": '{$factory[0]}::{$factory[1]}'";
78 $msg .=
": " . get_class($factory[0]) .
"->{$factory[1]}";
81 throw new \Elgg\Di\FactoryUncallableException($msg);
93 if (substr(
$name, -1) ===
'_') {
94 throw new \InvalidArgumentException(
'$name cannot end with "_"');
107 $this->{
$name} = null;
108 unset($this->{
$name});
121 if (substr(
$name, -1) ===
'_') {
122 throw new \InvalidArgumentException(
'$name cannot end with "_"');
124 if (!is_callable($callable,
true)) {
125 throw new \InvalidArgumentException(
'$factory must appear callable');
127 $this->
remove(
$name);
128 $this->factories_[
$name] = [
129 'callable' => $callable,
145 if (substr(
$name, -1) ===
'_') {
146 throw new \InvalidArgumentException(
'$name cannot end with "_"');
148 $classname_pattern = self::CLASS_NAME_PATTERN_53;
149 if (!is_string($class_name) || !preg_match($classname_pattern, $class_name)) {
150 throw new \InvalidArgumentException(
'Class names must be valid PHP class names');
152 $func =
function () use ($class_name) {
153 return new $class_name();
165 if (substr(
$name, -1) ===
'_') {
166 throw new \InvalidArgumentException(
'$name cannot end with "_"');
168 unset($this->{
$name});
169 unset($this->factories_[
$name]);
180 if (isset($this->factories_[
$name])) {
183 if (substr($name, -1) ===
'_') {
186 return (
bool) property_exists($this, $name);
198 $refl = new \ReflectionObject($this);
199 foreach ($refl->getProperties(\ReflectionProperty::IS_PUBLIC) as $prop) {
200 $names[] = $prop->name;
202 foreach (array_keys($this->factories_) as
$name) {
if(!$user||!$user->canDelete()) $name
__get($name)
Fetch a value.
getNames()
Get names for all values/factories.
Container holding values which can be resolved upon reading and optionally stored and shared across r...
const CLASS_NAME_PATTERN_53
has($name)
Does the container have this value.
setClassName($name, $class_name, $shared=true)
Set a factory based on instantiating a class with no arguments.
setValue($name, $value)
Set a value to be returned without modification.
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif(is_callable([$item, 'getType']))
setFactory($name, $callable, $shared=true)
Set a factory to generate a value when the container is read.
reset($name)
Remove previously built service, so that it's rebuld from factory on next call.