Skip to content

Commit 6eb1ec4

Browse files
author
Anthony Regeda
committed
added getValue method
1 parent b6bd751 commit 6eb1ec4

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ $container->share('mail', function () {
5959
return new \Zend_Mail();
6060
});
6161
$container->extend('mail', function ($mail, $container) {
62-
$mail->setFrom($container['mail.default_from']);
62+
$mail->setFrom($container->getValue('mail.default_from'));
6363
return $mail;
6464
});
6565
```

src/Castel.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,29 +74,50 @@ public function extend($id, Closure $callable)
7474
}
7575
$parent = $this->values[$id];
7676
$this->values[$id] = function ($c) use ($callable, $parent) {
77-
$isFactory = is_object($parent) && method_exists($parent, '__invoke');
78-
return $callable($isFactory ? $parent($c) : $parent, $c);
77+
return $callable(self::fabricate($parent, $c), $c);
7978
};
8079
if (property_exists($this, $id)) {
8180
$this->$id = $callable($this->$id, $this);
8281
}
8382
return $this;
8483
}
8584

85+
/**
86+
* Invokes a callable or returns the value "as is".
87+
*
88+
* @param mixed $value
89+
* @param mixed $context
90+
* @return mixed
91+
*/
92+
public static function fabricate($value, $context)
93+
{
94+
return is_object($value) && method_exists($value, '__invoke') ? $value($context) : $value;
95+
}
96+
8697
/**
8798
* Gets a parameter or an object.
8899
*
89100
* @param string $id
90101
* @return mixed
91102
* @throws InvalidArgumentException
92103
*/
93-
public function __get($id)
104+
public function getValue($id)
94105
{
95106
if (!isset($this->values[$id])) {
96107
throw new InvalidArgumentException(sprintf('Identifier "%s" is undefined.', $id));
97108
}
98-
$value = $this->values[$id];
99-
$isFactory = is_object($value) && method_exists($value, '__invoke');
100-
return $this->$id = $isFactory ? $value($this) : $value;
109+
return self::fabricate($this->values[$id], $this);
110+
}
111+
112+
/**
113+
* Gets a parameter or an object after caching it.
114+
*
115+
* @param string $id
116+
* @return mixed
117+
* @throws InvalidArgumentException
118+
*/
119+
public function __get($id)
120+
{
121+
return $this->$id = $this->getValue($id);
101122
}
102123
}

0 commit comments

Comments
 (0)