50 if (array_key_exists(
$name, $this->cache)) {
51 return $this->cache[
$name];
53 if (!isset($this->factories[
$name])) {
54 throw new \Elgg\Di\MissingValueException(
"Value or factory was not set for: $name");
56 $value = $this->
build($this->factories[$name][
'callable'], $name);
60 if (!empty($this->factories[$name]) && $this->factories[$name][
'shared']) {
75 if (is_callable($factory)) {
76 return call_user_func($factory, $this);
78 $msg =
"Factory for '$name' was uncallable";
79 if (is_string($factory)) {
80 $msg .=
": '$factory'";
81 } elseif (is_array($factory)) {
82 if (is_string($factory[0])) {
83 $msg .=
": '{$factory[0]}::{$factory[1]}'";
85 $msg .=
": " . get_class($factory[0]) .
"->{$factory[1]}";
88 throw new \Elgg\Di\FactoryUncallableException($msg);
100 $this->
remove(
$name);
115 if (!is_callable($callable,
true)) {
116 throw new \InvalidArgumentException(
'$factory must appear callable');
118 $this->
remove(
$name);
119 $this->factories[
$name] = array(
120 'callable' => $callable,
136 $classname_pattern = self::CLASS_NAME_PATTERN_53;
137 if (!is_string($class_name) || !preg_match($classname_pattern, $class_name)) {
138 throw new \InvalidArgumentException(
'Class names must be valid PHP class names');
140 $func =
function () use ($class_name) {
141 return new $class_name();
153 unset($this->cache[
$name]);
154 unset($this->factories[$name]);
165 return isset($this->factories[
$name]) || array_key_exists($name, $this->cache);
__get($name)
Fetch a value.
if($guid==elgg_get_logged_in_user_guid()) $name
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.
build($factory, $name)
Build a value.
setFactory($name, $callable, $shared=true)
Set a factory to generate a value when the container is read.