-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cec90ce
commit 0052e5c
Showing
3 changed files
with
214 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,13 +13,13 @@ | |
* For example: | ||
* | ||
* ```php | ||
* class PhpDiContainerProxy extends ContainerProxy | ||
* { | ||
* public function has(string $id): bool | ||
* { | ||
* return in_array($id, $this->container->getKnownEntryNames(), true); | ||
* } | ||
* } | ||
* use DI\Container; | ||
* use yii1tech\di\external\ContainerProxy; | ||
* | ||
* $container = ContainerProxy::new(new Container()) | ||
* ->setCallbackForHas(function (Container $container, string $id) { | ||
* return in_array($id, $container->getKnownEntryNames(), true); | ||
* }); | ||
* ``` | ||
* | ||
* @author Paul Klimov <[email protected]> | ||
|
@@ -31,6 +31,14 @@ class ContainerProxy implements ContainerInterface | |
* @var \Psr\Container\ContainerInterface wrapped container instance. | ||
*/ | ||
protected $container; | ||
/** | ||
* @var callable|null PHP callback, which should be used to implement `get()` method. | ||
*/ | ||
protected $callbackForGet; | ||
/** | ||
* @var callable|null PHP callback, which should be used to implement `has()` method. | ||
*/ | ||
protected $callbackForHas; | ||
|
||
/** | ||
* Constructor. | ||
|
@@ -47,15 +55,59 @@ public function __construct(ContainerInterface $container) | |
*/ | ||
public function get(string $id) | ||
{ | ||
return $this->container->get($id); | ||
if ($this->callbackForGet === null) { | ||
return $this->container->get($id); | ||
} | ||
|
||
return call_user_func($this->callbackForGet, $this->container, $id); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function has(string $id): bool | ||
{ | ||
return $this->container->has($id); | ||
if ($this->callbackForHas == null) { | ||
return $this->container->has($id); | ||
} | ||
|
||
return call_user_func($this->callbackForHas, $this->container, $id); | ||
} | ||
|
||
/** | ||
* Specifies a PHP callback, which should be invoked to implement method `get()`. | ||
* The callback signature: | ||
* | ||
* ``` | ||
* function (\Psr\Container\ContainerInterface $container, string $id): mixed | ||
* ``` | ||
* | ||
* @param callable|null $callback PHP callback. | ||
* @return static self reference. | ||
*/ | ||
public function setCallbackForGet(?callable $callback): self | ||
{ | ||
$this->callbackForGet = $callback; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Specifies a PHP callback, which should be invoked to implement method `has()`. | ||
* The callback signature: | ||
* | ||
* ``` | ||
* function (\Psr\Container\ContainerInterface $container, string $id): bool | ||
* ``` | ||
* | ||
* @param callable|null $callback PHP callback. | ||
* @return static self reference. | ||
*/ | ||
public function setCallbackForHas(?callable $callback): self | ||
{ | ||
$this->callbackForHas = $callback; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters