Skip to content

Commit

Permalink
optimize lazy load command handler
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-gribanov committed Jun 22, 2017
1 parent 6dd1f3c commit 2c753bc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
25 changes: 17 additions & 8 deletions src/Command/Handler/Locator/ContainerCommandHandlerLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,7 @@ public function __construct(ContainerInterface $container)
*/
public function findHandler(Command $command)
{
$command_name = get_class($command);

if (!isset($this->command_handler_ids[$command_name])) {
return null;
}
list($service, $method) = $this->command_handler_ids[$command_name];

return $this->resolve($this->container->get($service), $method);
return $this->lazyLoad(get_class($command));
}

/**
Expand All @@ -60,6 +53,22 @@ public function registerService($command_name, $service, $method = '__invoke')
$this->command_handler_ids[$command_name] = [$service, $method];
}

/**
* @param string $command_name
*
* @return callable|null
*/
private function lazyLoad($command_name)
{
if (isset($this->command_handler_ids[$command_name])) {
list($service, $method) = $this->command_handler_ids[$command_name];

return $this->resolve($this->container->get($service), $method);
}

return null;
}

/**
* @param mixed $service
* @param string $method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,7 @@ class SymfonyContainerCommandHandlerLocator implements CommandHandlerLocator, Co
*/
public function findHandler(Command $command)
{
$command_name = get_class($command);

if (!($this->container instanceof ContainerInterface) || !isset($this->command_handler_ids[$command_name])) {
return null;
}
list($service, $method) = $this->command_handler_ids[$command_name];

return $this->resolve($this->container->get($service), $method);
return $this->lazyLoad(get_class($command));
}

/**
Expand All @@ -51,6 +44,22 @@ public function registerService($command_name, $service, $method = '__invoke')
$this->command_handler_ids[$command_name] = [$service, $method];
}

/**
* @param string $command_name
*
* @return callable|null
*/
private function lazyLoad($command_name)
{
if ($this->container instanceof ContainerInterface && isset($this->command_handler_ids[$command_name])) {
list($service, $method) = $this->command_handler_ids[$command_name];

return $this->resolve($this->container->get($service), $method);
}

return null;
}

/**
* @param mixed $service
* @param string $method
Expand Down

0 comments on commit 2c753bc

Please sign in to comment.