Skip to content

Commit

Permalink
reference container itself
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Apr 25, 2017
1 parent 475dcc3 commit e23d547
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use DI\Container as DIContainer;
use DI\ContainerBuilder as DIContainerBuilder;
use Psr\Container\ContainerInterface;

/**
* Helper to create and configure a Container.
Expand Down Expand Up @@ -50,7 +51,12 @@ public static function build(Configuration $configuration = null): DIContainer
// Custom definitions
$containerBuilder->addDefinitions(self::parseDefinitions($configuration->getDefinitions()));

return $containerBuilder->build();
$container = $containerBuilder->build();

// Add container itself
$container->set(ContainerInterface::class, $container);

return $container;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/definitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@

'router' => \DI\create(Router::class)
->method('setCacheFile', \DI\get('settings.routerCacheFile')),
Router::class => \DI\get('router'),

'phpErrorHandler' => \DI\create(PhpError::class)
->constructor(\DI\get('settings.displayErrorDetails')),
Expand All @@ -89,9 +90,9 @@
return new CallableResolver(new InvokerResolver($container));
},

// Aliases
Router::class => \DI\get('router'),

// Replaced by used configuration on container build
Configuration::class => null,

// Replaced by generated container
ContainerInterface::class => null,
];
4 changes: 4 additions & 0 deletions tests/PHPDI/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Jgut\Slim\PHPDI\ContainerBuilder;
use Jgut\Slim\PHPDI\FoundHandler;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Handlers\Error;
Expand Down Expand Up @@ -122,5 +123,8 @@ public function testDefaultServices()

self::assertTrue($this->container->has(Configuration::class));
self::assertInstanceOf(Configuration::class, $this->container->get(Configuration::class));

self::assertTrue($this->container->has(ContainerInterface::class));
self::assertEquals($this->container, $this->container->get(ContainerInterface::class));
}
}

0 comments on commit e23d547

Please sign in to comment.