feature #18547 DX: better error message if factory class is empty (dbu)
This PR was merged into the 3.1-dev branch.
Discussion
----------
DX: better error message if factory class is empty
| Q | A
| ------------- | ---
| Branch? | master
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | ?
| Fixed tickets | -
| License | MIT
| Doc PR | -
i was trying to debug this service configuration:
```xml
<service id="repository.user" class="Doctrine\ORM\EntityRepository">
<argument>%user_entity_class%</argument>
<factory id="doctrine.orm.default_entity_manager" method="getRepository"/>
</service>
```
Turns out i should have used `<factory service=...` instead of `<factory id=...` but the error message does not even tell which service is wrong. The same happens when only specifying the `method=` attribute. The current exception message tells:
Cannot dump definition because of invalid class name ('')
With this change, the message at least tells the context, hopefully helping the developer debug the issue:
Empty class for factory of service repository.user and no factory service specified
Commits
-------
0999326
better error message if factory class is empty
This commit is contained in:
commit
2824db3da8
@ -330,7 +330,7 @@ class PhpDumper extends Dumper
|
||||
throw new ServiceCircularReferenceException($id, array($id));
|
||||
}
|
||||
|
||||
$code .= $this->addNewInstance($sDefinition, '$'.$name, ' = ');
|
||||
$code .= $this->addNewInstance($sDefinition, '$'.$name, ' = ', $id);
|
||||
|
||||
if (!$this->hasReference($id, $sDefinition->getMethodCalls(), true) && !$this->hasReference($id, $sDefinition->getProperties(), true)) {
|
||||
$code .= $this->addServiceMethodCalls(null, $sDefinition, $name);
|
||||
@ -404,7 +404,7 @@ class PhpDumper extends Dumper
|
||||
$instantiation .= ' = ';
|
||||
}
|
||||
|
||||
$code = $this->addNewInstance($definition, $return, $instantiation);
|
||||
$code = $this->addNewInstance($definition, $return, $instantiation, $id);
|
||||
|
||||
if (!$simple) {
|
||||
$code .= "\n";
|
||||
@ -692,7 +692,7 @@ EOF;
|
||||
return $publicServices.$privateServices;
|
||||
}
|
||||
|
||||
private function addNewInstance(Definition $definition, $return, $instantiation)
|
||||
private function addNewInstance(Definition $definition, $return, $instantiation, $id)
|
||||
{
|
||||
$class = $this->dumpValue($definition->getClass());
|
||||
|
||||
@ -716,6 +716,10 @@ EOF;
|
||||
$class = $this->dumpValue($callable[0]);
|
||||
// If the class is a string we can optimize call_user_func away
|
||||
if (strpos($class, "'") === 0) {
|
||||
if ("''" === $class) {
|
||||
throw new RuntimeException(sprintf('Cannot dump definition: The "%s" service is defined to be created by a factory but is missing the service reference, did you forget to define the factory service id or class?', $id));
|
||||
}
|
||||
|
||||
return sprintf(" $return{$instantiation}%s::%s(%s);\n", $this->dumpLiteralClass($class), $callable[1], $arguments ? implode(', ', $arguments) : '');
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user