Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ea99e13

Browse files
committed
Fix container resolution of default values for non-scalar dependencies.
1 parent 308b710 commit ea99e13

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

src/Illuminate/Container/Container.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ protected function getDependencies($parameters)
348348
}
349349
else
350350
{
351-
$dependencies[] = $this->make($dependency->name);
351+
$dependencies[] = $this->resolveClass($parameter);
352352
}
353353
}
354354

@@ -375,6 +375,35 @@ protected function resolveNonClass(ReflectionParameter $parameter)
375375
}
376376
}
377377

378+
/**
379+
* Resolve a class based dependency from the container.
380+
*
381+
* @param \ReflectionParameter $parameter
382+
* @return mixed
383+
*/
384+
protected function resolveClass(ReflectionParameter $parameter)
385+
{
386+
try
387+
{
388+
return $this->make($parameter->getClass()->name);
389+
}
390+
391+
// If we can not resolve the class instance, we will check to see if the value
392+
// is optional, and if it is we will return the optional parameter value as
393+
// the value of the dependency, similarly to how we do this with scalars.
394+
catch (BindingResolutionException $e)
395+
{
396+
if ($parameter->isOptional())
397+
{
398+
return $parameter->getDefaultValue();
399+
}
400+
else
401+
{
402+
throw $e;
403+
}
404+
}
405+
}
406+
378407
/**
379408
* Register a new resolving callback.
380409
*

src/Illuminate/Foundation/changes.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
{"message": "Restore method on Eloquent models now fires restoring and restored events.", "backport": null},
2727
{"message": "Fixed re-population of radio buttons and checkboxes in FormBuilder.", "backport": null},
2828
{"message": "Postgres ENUMs are now more truly implemented using 'check' constraints.", "backport": null},
29-
{"message": "Added selectMonth and selectYear to FormBuilder.", "backport": null}
29+
{"message": "Added selectMonth and selectYear to FormBuilder.", "backport": null},
30+
{"message": "Fix container resolution of default values for non-scalar dependencies.", "backport": null}
3031
]
3132
}

0 commit comments

Comments
 (0)