Skip to content

Commit

Permalink
Container: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 28, 2024
1 parent 88bed96 commit b4ad530
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 24 deletions.
9 changes: 5 additions & 4 deletions src/Bridges/DITracy/ContainerPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ public function getTab(): string
*/
public function getPanel(): string
{
$methods = (fn() => $this->methods)->bindTo($this->container, Container::class)();
$services = [];
foreach ($methods as $name => $foo) {
$name = lcfirst(str_replace('__', '.', substr($name, 13)));
$services[$name] = $this->container->getServiceType($name);
foreach (get_class_methods($this->container) as $method) {
if (preg_match('#^createService.#', $method)) {
$name = lcfirst(str_replace('__', '.', substr($method, 13)));
$services[$name] = $this->container->getServiceType($name);
}
}
ksort($services, SORT_NATURAL);

Expand Down
26 changes: 6 additions & 20 deletions src/DI/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ class Container
public function __construct(array $params = [])
{
$this->parameters = $params + $this->getStaticParameters();
$this->methods = array_flip(array_filter(
get_class_methods($this),
fn($s) => preg_match('#^createService.#', $s),
));
$this->methods = array_flip(get_class_methods($this));
}


Expand Down Expand Up @@ -257,22 +254,11 @@ public function getByType(string $type, bool $throw = true): ?object
} elseif ($throw) {
if (!class_exists($type) && !interface_exists($type)) {
throw new MissingServiceException(sprintf("Service of type '%s' not found. Check the class name because it cannot be found.", $type));
} elseif ($this->findByType($type)) {
throw new MissingServiceException(sprintf("Service of type %s is not autowired or is missing in di\u{a0}\u{a0}export\u{a0}\u{a0}types.", $type));
} else {
throw new MissingServiceException(sprintf('Service of type %s not found. Did you add it to configuration file?', $type));
}

foreach ($this->methods as $method => $foo) {
$methodType = (new \ReflectionMethod(static::class, $method))->getReturnType()->getName();
if (is_a($methodType, $type, allow_string: true)) {
throw new MissingServiceException(sprintf(
"Service of type %s is not autowired or is missing in di\u{a0}\u{a0}export\u{a0}\u{a0}types.",
$type,
));
}
}

throw new MissingServiceException(sprintf(
'Service of type %s not found. Did you add it to configuration file?',
$type,
));
}

return null;
Expand Down Expand Up @@ -383,7 +369,7 @@ private function autowireArguments(\ReflectionFunctionAbstract $function, array
/**
* Returns the method name for creating a service.
*/
public static function getMethodName(string $name): string
final public static function getMethodName(string $name): string
{
if ($name === '') {
throw new Nette\InvalidArgumentException('Service name must be a non-empty string.');
Expand Down

0 comments on commit b4ad530

Please sign in to comment.