Skip to content
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit 6f3a032

Browse files
committed
up
1 parent fabb9c6 commit 6f3a032

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

src/DI/Container.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -333,11 +333,7 @@ public function createCallback($target, array $arguments = [], array $props = []
333333
// If there are no parameters, just return a new object.
334334
if (null === $reflectionMethod) {
335335
$callback = function () use ($class, $props) {
336-
$object = new $class;
337-
338-
Obj::smartConfigure($object, $props);
339-
340-
return $object;
336+
return Obj::init(new $class, $props);
341337
};
342338
} else {
343339
$arguments = $arguments ?: Obj::getMethodArgs($reflectionMethod);
@@ -346,9 +342,7 @@ public function createCallback($target, array $arguments = [], array $props = []
346342
$callback = function () use ($reflection, $arguments, $props) {
347343
$object = $reflection->newInstanceArgs($arguments);
348344

349-
Obj::smartConfigure($object, $props);
350-
351-
return $object;
345+
return Obj::init($object, $props);
352346
};
353347
}
354348

@@ -428,6 +422,15 @@ public function del($id)
428422
}
429423
}
430424

425+
/**
426+
* @param string|array $definition
427+
* @return mixed
428+
*/
429+
public function make($definition)
430+
{
431+
return Obj::smartCreate($definition);
432+
}
433+
431434
/*******************************************************************************
432435
* Helper
433436
******************************************************************************/

src/Helpers/ObjectHelper.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,27 @@ public static function create($class)
205205
// Create a callable for the dataStorage
206206
return $reflection->newInstanceArgs($newInstanceArgs);
207207
}
208+
209+
/**
210+
* @param string|array $config
211+
* @return mixed
212+
*/
213+
public static function smartCreate($config)
214+
{
215+
if (is_string($config)) {
216+
return new $config;
217+
}
218+
219+
if (is_array($config) && !empty($config['target'])) {
220+
$class = Arr::remove($config, 'target');
221+
$args = Arr::remove($config, '_args', []);
222+
$props = $config;
223+
224+
$obj = new $class(...$args);
225+
226+
return self::init($obj, $props);
227+
}
228+
229+
return null;
230+
}
208231
}

0 commit comments

Comments
 (0)