Skip to content

Commit

Permalink
Update autowiring implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Stratadox committed Dec 2, 2018
1 parent cef38ce commit 60b88d4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
12 changes: 9 additions & 3 deletions src/AutoWiring.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace Stratadox\Di;

use Closure;
use Psr\Container\NotFoundExceptionInterface as NotFound;
use ReflectionClass as Reflected;
use ReflectionException;
use ReflectionType;

final class AutoWiring implements ContainerInterface
final class AutoWiring implements LinkableContainerInterface
{
private $container;
private $links;
Expand All @@ -18,12 +19,12 @@ private function __construct(ContainerInterface $container, array $links)
$this->links = $links;
}

public static function the(ContainerInterface $container): ContainerInterface
public static function the(ContainerInterface $container): LinkableContainerInterface
{
return new self($container, []);
}

public function link(string $interface, string $class) : ContainerInterface
public function link(string $interface, string $class): LinkableContainerInterface
{
if (!is_a($class, $interface, true)) {
throw InvalidServiceType::serviceIsNotOfType($class, $interface);
Expand Down Expand Up @@ -56,6 +57,7 @@ public function forget(string $service): void
$this->container->forget($service);
}

/** @throws InvalidServiceDefinition|NotFound */
private function resolve(string $service): void
{
try {
Expand All @@ -65,6 +67,7 @@ private function resolve(string $service): void
}
}

/** @throws InvalidServiceDefinition|ReflectionException */
private function resolveThe(Reflected $service): void
{
if ($service->isAbstract() || $service->isInterface()) {
Expand All @@ -74,6 +77,7 @@ private function resolveThe(Reflected $service): void
}
}

/** @throws InvalidServiceDefinition|ReflectionException */
private function resolveAbstract(Reflected $service): void
{
$name = $service->getName();
Expand All @@ -87,6 +91,7 @@ private function resolveAbstract(Reflected $service): void
});
}

/** @throws InvalidServiceDefinition */
private function resolveClass(Reflected $service): void
{
$name = $service->getName();
Expand All @@ -107,6 +112,7 @@ private function resolveClass(Reflected $service): void
});
}

/** @throws InvalidServiceDefinition */
private function handleDependency(ReflectionType $theType): string
{
if ($theType->isBuiltin()) {
Expand Down
3 changes: 2 additions & 1 deletion src/ContainerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Psr\Container\ContainerInterface as PsrContainerInterface;
use Psr\Container\NotFoundExceptionInterface as NotFound;

interface ContainerInterface extends PsrContainerInterface
{
Expand All @@ -26,7 +27,7 @@ public function set(string $service, Closure $factory, bool $cache = true): void
* @return mixed The service object
*
* @throws InvalidServiceDefinition
* @throws ServiceNotFound
* @throws NotFound
*/
public function get($service);

Expand Down
2 changes: 1 addition & 1 deletion src/DependenciesCannotBeCircular.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

final class DependenciesCannotBeCircular extends RuntimeException implements InvalidServiceDefinition
{
public static function loopDetectedIn($serviceName): DependenciesCannotBeCircular
public static function loopDetectedIn($serviceName): InvalidServiceDefinition
{
return new static(sprintf(
'Circular dependency loop detected in factory `%s`.',
Expand Down
16 changes: 16 additions & 0 deletions src/LinkableContainerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Stratadox\Di;

interface LinkableContainerInterface extends ContainerInterface
{
/**
* Links an abstraction to a concrete class.
*
* @param string $interface The interface or abstract class.
* @param string $class The concrete class to link to.
* @return LinkableContainerInterface The updated container.
* @throws InvalidServiceDefinition When the link is invalid.
*/
public function link(string $interface, string $class): LinkableContainerInterface;
}

0 comments on commit 60b88d4

Please sign in to comment.