Matthias Lantsch(4 years, 5 months ago)
Fix di namespacing and implement a set method
Browse Filesdiff --git a/f6d575b1776440e237974e3ad9f582508660e5bf b/b0c63d594edc6cffa57e65712d2210879b9abaad
index f6d575b..b0c63d5 100644
--- a/f6d575b1776440e237974e3ad9f582508660e5bf
+++ b/b0c63d594edc6cffa57e65712d2210879b9abaad
@@ -3,3 +3,6 @@
/.php_cs
/.php_cs.cache
/.phpunit.result.cache
+/composer.lock
+.idea/
+
diff --git a/4aaccb065f5c9a7d06ff99ba49465cf757706ab9 b/a1e91bee8291c7ffa894b9726f420ecc707a1192
index 4aaccb0..a1e91be 100644
--- a/4aaccb065f5c9a7d06ff99ba49465cf757706ab9
+++ b/a1e91bee8291c7ffa894b9726f420ecc707a1192
@@ -77,7 +77,7 @@ class Registry implements ArrayAccess {
* Offset to retrieve.
* @see http://php.net/manual/en/arrayaccess.offsetget.php
* @param string $offset
- * @return mixed can return all value types
+ * @return mixed|null can return all value types or null if not found
*/
public function offsetGet($offset) {
$parts = explode('.', $offset);
@@ -85,7 +85,7 @@ class Registry implements ArrayAccess {
foreach ($parts as $sublevel) {
if (!isset($position[$sublevel])) {
- return;
+ return null;
}
$position = $position[$sublevel];
}
diff --git a/d2ed559b8bce86fd64da6867e2b1c09baf1d77b1 b/05f3cf714848b6d3df6e0e5b7b2cb714c4e576d3
index d2ed559..05f3cf7 100644
--- a/d2ed559b8bce86fd64da6867e2b1c09baf1d77b1
+++ b/05f3cf714848b6d3df6e0e5b7b2cb714c4e576d3
@@ -9,8 +9,9 @@
* @author Matthias Lantsch <[email protected]>
*/
-namespace holonet\common;
+namespace holonet\common\di;
+use TypeError;
use Psr\Container\ContainerInterface;
/**
@@ -61,4 +62,34 @@ class Container implements ContainerInterface {
}
}
}
+
+ /**
+ * Method used to set a dependency in this class.
+ * If the given value is an object, it will get injected and saved under the key
+ * If the given value is a string a class name is assumed and a new object will be created and automatically get injected.
+ * @param string $id The key to save the dependency under
+ * @param object|string $value The dependency to save
+ * @param array ...$constructorArgs Arguments for the class instantiation
+ */
+ public function set(string $id, $value, ...$constructorArgs): void {
+ if (is_string($value) && class_exists($value)) {
+ try {
+ $value = new $value(...$constructorArgs);
+ } catch (TypeError $e) {
+ throw new DependencyInjectionException(
+ "Cannot create dependency '{$id}' on Dependency Container: '{$e->getMessage()}'",
+ $e->getCode(), $e
+ );
+ }
+ }
+
+ if (!is_object($value)) {
+ throw new DependencyInjectionException(
+ "Cannot set dependency '{$id}' on Dependency Container, value must be object or class string"
+ );
+ }
+
+ $this->inject($value);
+ $this->dependencies[$id] = $value;
+ }
}
<?php
/**
* This file is part of the holonet common library
* (c) Matthias Lantsch.
*
* Class file for the Dependency Injection DependencyInjectionException class
*
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @author Matthias Lantsch <[email protected]>
*/
namespace holonet\common\di;
use RuntimeException;
use Psr\Container\ContainerExceptionInterface;
/**
* Dependency Injection general error exception conforming with PSR-11.
*/
class DependencyInjectionException extends RuntimeException implements ContainerExceptionInterface {
}
diff --git a/e42629de6b44c6d0c1215509d73de8eaddd7b029 b/981b6805f6b4b8c613c596d05937617dd49534d4
index e42629d..981b680 100644
--- a/e42629de6b44c6d0c1215509d73de8eaddd7b029
+++ b/981b6805f6b4b8c613c596d05937617dd49534d4
@@ -9,7 +9,7 @@
* @author Matthias Lantsch <[email protected]>
*/
-namespace holonet\common;
+namespace holonet\common\di;
use RuntimeException;
use Psr\Container\NotFoundExceptionInterface;