Skip to content

Commit

Permalink
Fix bug container for services
Browse files Browse the repository at this point in the history
  • Loading branch information
Davide Cesarano authored and Davide Cesarano committed Nov 15, 2020
1 parent 895d358 commit 657d99c
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions Embryo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Embryo\Http\Emitter\Emitter;
use Embryo\Http\Factory\ResponseFactory;
use Embryo\Routing\Middleware\{MethodOverrideMiddleware, RoutingMiddleware, RequestHandlerMiddleware};
use Embryo\Routing\Interfaces\RouteInterface;
use Embryo\Routing\Interfaces\{RouteInterface, RouterInterface};
use Psr\Container\ContainerInterface;
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
use Psr\Http\Server\MiddlewareInterface;
Expand Down Expand Up @@ -89,23 +89,22 @@ public function service($service)
if(!is_string($service) && !is_callable($service) && !is_array($service)) {
throw new \InvalidArgumentException('Service must be a string, array or callable');
}
$container = $this->containerBuilder->build();

if (is_string($service)) {
$service = new $service($container);
$service = new $service($this->containerBuilder);
$service->register();
}

if (is_callable($service)) {
call_user_func($service, $container);
call_user_func($service, $this->containerBuilder);
}

if (is_array($service)) {
foreach ($service as $s) {
if (is_callable($s)) {
call_user_func($s, $container);
call_user_func($s, $this->containerBuilder);
} else if (is_string($s)) {
$s = new $s($container);
$s = new $s($this->containerBuilder);
$s->register();
} else {
throw new \InvalidArgumentException('Service must be a string or callable in array services');
Expand Down Expand Up @@ -238,9 +237,9 @@ public function all($pattern, $callback): RouteInterface
* PREFIX
*
* @param string $prefix
* @return RouteInterface
* @return RouterInterface
*/
public function prefix($prefix): RouteInterface
public function prefix($prefix): RouterInterface
{
return $this->containerBuilder->get('router')->prefix($prefix);
}
Expand All @@ -249,9 +248,9 @@ public function prefix($prefix): RouteInterface
* MIDDLEWARE
*
* @param string|array|MiddlewareInterface $middleware
* @return RouteInterface
* @return RouterInterface
*/
public function middleware($middleware): RouteInterface
public function middleware($middleware): RouterInterface
{
return $this->containerBuilder->get('router')->middleware($middleware);
}
Expand All @@ -260,9 +259,9 @@ public function middleware($middleware): RouteInterface
* GROUP
*
* @param callable $callback
* @return RouteInterface
* @return RouterInterface
*/
public function group($callback): RouteInterface
public function group($callback): RouterInterface
{
return $this->containerBuilder->get('router')->group($callback);
}
Expand Down

0 comments on commit 657d99c

Please sign in to comment.