Skip to content

Commit

Permalink
Merge pull request #21 from ghostwriter/feature/extend-aliases
Browse files Browse the repository at this point in the history
Extend services via aliases
  • Loading branch information
ghostwriter authored Apr 8, 2023
2 parents c307db9 + b01694c commit 55d3ff2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 34 deletions.
59 changes: 29 additions & 30 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,14 @@ public function __unserialize(array $data): void

public function alias(string $abstract, string $concrete): void
{
if (trim($abstract) === '') {
throw $this->throwServiceIdMustBeNonEmptyString();
}

if (trim($concrete) === '') {
if (trim($abstract) === '' || trim($concrete) === '') {
throw $this->throwServiceIdMustBeNonEmptyString();
}

if ($abstract === $concrete) {
throw $this->throwInvalidArgument('Service "%s" can not use an alias with the same name.', $concrete);
}

if (! $this->has($concrete)) {
throw $this->throwNotFoundException('Service "%s" was not found.', $concrete);
}

self::$instance->cache[self::ALIASES][$abstract] = $concrete;
}

Expand Down Expand Up @@ -216,9 +208,7 @@ public function call(callable|string $invokable, array $arguments = []): mixed

public function extend(string $class, callable $extension): void
{
if (trim($class) === '') {
throw $this->throwServiceIdMustBeNonEmptyString();
}
$class = $this->resolve($class);

$factories = self::$instance->cache[self::FACTORIES];
$extensions = self::$instance->cache[self::EXTENSIONS];
Expand Down Expand Up @@ -262,7 +252,7 @@ public function get(string $id): mixed
throw $this->throwNotFoundException('Service "%s" was not found.', $id);
}

$serviceFactory = $factories[$id] ?? static fn (Container $container): object => $container->build($id);
$serviceFactory = $factories[$id] ?? static fn (self $container): object => $container->build($id);

$extensions = self::$instance->cache[self::EXTENSIONS];

Expand All @@ -276,13 +266,15 @@ public static function getInstance(): self
return self::$instance ??= new self();
}

/**
* @throws ContainerExceptionInterface
*/
public function has(string $id): bool
{
$id = $this->resolve($id);

return array_key_exists($id, self::$instance->cache[self::SERVICES]) ||
array_key_exists($id, self::$instance->cache[self::FACTORIES]) ||
array_key_exists($id, self::$instance->cache[self::ALIASES]);
return array_key_exists($id, self::$instance->cache[self::SERVICES])
|| array_key_exists($id, self::$instance->cache[self::FACTORIES]);
}

public function register(string $serviceProvider): void
Expand Down Expand Up @@ -317,6 +309,9 @@ public function remove(string $id): void
);
}

/**
* @throws ContainerExceptionInterface
*/
public function replace(string $id, mixed $value, array $tags = []): void
{
$this->remove($id);
Expand All @@ -337,21 +332,19 @@ public function resolve(string $id): string
return $id;
}

/**
* @throws ContainerExceptionInterface
*/
public function set(string $id, mixed $value, array $tags = []): void
{
if (trim($id) === '') {
throw $this->throwServiceIdMustBeNonEmptyString();
}

if (array_key_exists($id, self::$instance->cache[self::SERVICES])) {
throw $this->throwServiceAlreadyRegisteredException($id);
}

if (array_key_exists($id, self::$instance->cache[self::FACTORIES])) {
throw $this->throwServiceAlreadyRegisteredException($id);
}

if (array_key_exists($id, self::$instance->cache[self::ALIASES])) {
if (array_key_exists($id, self::$instance->cache[self::SERVICES]) ||
array_key_exists($id, self::$instance->cache[self::FACTORIES]) ||
array_key_exists($id, self::$instance->cache[self::ALIASES])
) {
throw $this->throwServiceAlreadyRegisteredException($id);
}

Expand Down Expand Up @@ -391,10 +384,15 @@ public function tag(string $id, array $tags): void
}

/**
* @template TObject of object
* @template TMixed
*
* @param class-string<TObject> $tag
*
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*
* @return Generator<int, object, mixed, void>
* @return Generator<int, TObject, TMixed, void>
*/
public function tagged(string $tag): Generator
{
Expand All @@ -410,6 +408,7 @@ private function buildParameters(array $reflectionParameters, array $arguments):
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
function (ReflectionParameter $reflectionParameter) use (&$arguments) {
$parameterName = $reflectionParameter->getName();
Expand All @@ -425,15 +424,15 @@ function (ReflectionParameter $reflectionParameter) use (&$arguments) {
return $parameter;
}

if ($reflectionParameter->isDefaultValueAvailable()) {
return $reflectionParameter->getDefaultValue();
}

$reflectionType = $reflectionParameter->getType();
if ($reflectionType instanceof ReflectionNamedType && ! $reflectionType->isBuiltin()) {
return $this->get($reflectionType->getName());
}

if ($reflectionParameter->isDefaultValueAvailable()) {
return $reflectionParameter->getDefaultValue();
}

$name = $reflectionParameter->getDeclaringFunction()
->getName();
$isFunction = is_callable($name);
Expand Down
4 changes: 0 additions & 4 deletions tests/Unit/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,6 @@ public static function dataProviderServiceNotFoundException(): Generator
static fn (Container $container): string => $container->get('dose-not-exist'),
];

yield 'ServiceNotFoundException::missingServiceId@alias' => [
static fn (Container $container) => $container->alias('alias', 'dose-not-exist'),
];

yield 'ServiceNotFoundException::missingServiceId@extend' => [
static function (Container $container): void {
$container->extend('extend-missing-service', static fn (Container $container) => null);
Expand Down

0 comments on commit 55d3ff2

Please sign in to comment.