33 private $factories_ = array();
45 if (!isset($this->factories_[
$name])) {
46 throw new \Elgg\Di\MissingValueException(
"Value or factory was not set for: $name");
48 $value = $this->build($this->factories_[$name][
'callable'], $name);
52 if (!empty($this->factories_[$name]) && $this->factories_[$name][
'shared']) {
66 private function build($factory,
$name) {
67 if (is_callable($factory)) {
68 return call_user_func($factory, $this);
70 $msg =
"Factory for '$name' was uncallable";
71 if (is_string($factory)) {
72 $msg .=
": '$factory'";
73 } elseif (is_array($factory)) {
74 if (is_string($factory[0])) {
75 $msg .=
": '{$factory[0]}::{$factory[1]}'";
77 $msg .=
": " . get_class($factory[0]) .
"->{$factory[1]}";
80 throw new \Elgg\Di\FactoryUncallableException($msg);
92 if (substr(
$name, -1) ===
'_') {
93 throw new \InvalidArgumentException(
'$name cannot end with "_"');
110 if (substr(
$name, -1) ===
'_') {
111 throw new \InvalidArgumentException(
'$name cannot end with "_"');
113 if (!is_callable($callable,
true)) {
114 throw new \InvalidArgumentException(
'$factory must appear callable');
116 $this->
remove(
$name);
117 $this->factories_[
$name] = array(
118 'callable' => $callable,
134 if (substr(
$name, -1) ===
'_') {
135 throw new \InvalidArgumentException(
'$name cannot end with "_"');
137 $classname_pattern = self::CLASS_NAME_PATTERN_53;
138 if (!is_string($class_name) || !preg_match($classname_pattern, $class_name)) {
139 throw new \InvalidArgumentException(
'Class names must be valid PHP class names');
141 $func =
function ()
use ($class_name) {
142 return new $class_name();
154 if (substr(
$name, -1) ===
'_') {
155 throw new \InvalidArgumentException(
'$name cannot end with "_"');
157 unset($this->{
$name});
158 unset($this->factories_[
$name]);
169 if (isset($this->factories_[
$name])) {
172 if (substr($name, -1) ===
'_') {
175 return (
bool)property_exists($this, $name);
188 $refl = new \ReflectionObject($this);
189 foreach ($refl->getProperties(\ReflectionProperty::IS_PUBLIC) as $prop) {
190 $names[] = $prop->name;
192 foreach (array_keys($this->factories_) as
$name) {
__get($name)
Fetch a value.
if($guid==elgg_get_logged_in_user_guid()) $name
getNames()
Get names for all values/factories.
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.
setFactory($name, $callable, $shared=true)
Set a factory to generate a value when the container is read.
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use