Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error message for Unbound exception #238

Open
wants to merge 5 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/di/Exception/Unbound.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private function buildMessage(self $e, array $msg): string
private function getMainMessage(self $e): string
{
return sprintf(
"exception '%s' with message '%s'\n",
"'%s': Unresolvable dependency: %s\n",
get_class($e),
$e->getMessage()
);
Expand Down
9 changes: 9 additions & 0 deletions src/di/Injector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

use Doctrine\Common\Annotations\AnnotationException;
use Ray\Aop\Compiler;
use Ray\Di\Exception\Unbound;
use Ray\Di\Exception\Untargeted;

use function assert;
use function file_exists;
use function file_put_contents;
use function is_dir;
use function spl_autoload_register;
use function sprintf;
Expand Down Expand Up @@ -65,6 +67,13 @@ public function getInstance($interface, $name = Name::ANY)
$this->bind($interface);
/** @psalm-suppress MixedAssignment */
$instance = $this->getInstance($interface, $name);
} catch (Unbound $e) {
$bindings = (new ModuleString())($this->container, $this->container->getPointcuts());
$logFile = sprintf('%s/module.log', $this->classDir);
file_put_contents($logFile, $bindings);
$message = sprintf("%s\nSee the binding log: %s", $e->getMessage(), $logFile);

throw new Unbound($message);
}

/** @psalm-suppress MixedReturnStatement */
Expand Down
9 changes: 6 additions & 3 deletions src/di/Instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,20 @@ public function __toString(): string
{
if (is_scalar($this->value)) {
return sprintf(
'(%s) %s',
'(instance: %s) %s',
gettype($this->value),
(string) $this->value
);
}

if (is_object($this->value)) {
return '(object) ' . get_class($this->value);
return '(instance: object) ' . get_class($this->value);
}

return '(' . gettype($this->value) . ')';
return sprintf(
'(instance: %s)',
gettype($this->value)
);
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/di/ModuleString.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@ public function __invoke(Container $container, array $pointcuts): string
}

$log[] = sprintf(
'%s => %s',
'%s -> %s',
$dependencyIndex,
(string) $dependency
);
}

sort($log);
$numLog = [];
foreach ($log as $key => $value) {
$numLog[] = sprintf('%d: %s', $key, $value);
}

return implode(PHP_EOL, $log);
return implode(PHP_EOL, $numLog);
}
}
18 changes: 9 additions & 9 deletions tests/di/ModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ public function testActivate(): void
public function testtoString(): void
{
$string = (string) new FakeLogStringModule();
$this->assertSame('-array => (array)
-bool => (boolean) 1
-int => (integer) 1
-null => (NULL)
-object => (object) stdClass
-string => (string) 1
Ray\Di\FakeAopInterface- => (dependency) Ray\Di\FakeAop (aop) +returnSame(Ray\Di\FakeDoubleInterceptor)
Ray\Di\FakeDoubleInterceptor- => (dependency) Ray\Di\FakeDoubleInterceptor
Ray\Di\FakeRobotInterface- => (provider) (dependency) Ray\Di\FakeRobotProvider', $string);
$this->assertSame('0: -array -> (instance: array)
1: -bool -> (instance: boolean) 1
2: -int -> (instance: integer) 1
3: -null -> (instance: NULL)
4: -object -> (instance: object) stdClass
5: -string -> (instance: string) 1
6: Ray\Di\FakeAopInterface- -> (dependency) Ray\Di\FakeAop (aop) +returnSame(Ray\Di\FakeDoubleInterceptor)
7: Ray\Di\FakeDoubleInterceptor- -> (dependency) Ray\Di\FakeDoubleInterceptor
8: Ray\Di\FakeRobotInterface- -> (provider) (dependency) Ray\Di\FakeRobotProvider', $string);
}
}