Skip to content

Commit

Permalink
Simplify container initialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-1-anderson committed Sep 10, 2016
1 parent 551a819 commit 7ec4e7c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
9 changes: 7 additions & 2 deletions src/Robo.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static function unsetContainer()
/**
* Returns the currently active global container.
*
* @return \League\Container\ContainerInterface|null
* @return \League\Container\ContainerInterface
*
* @throws \RuntimeException
*/
Expand All @@ -74,10 +74,14 @@ public static function hasContainer()
*/
public static function createDefaultContainer($input = null, $output = null, $app)
{
// Do not allow this function to be called more than once.
if (static::hasContainer()) {
return static::getContainer();
}

// Set up our dependency injection container.
$container = new Container();
static::configureContainer($container, $input, $output, $app);
static::setContainer($container);

return $container;
}
Expand All @@ -89,6 +93,7 @@ public static function configureContainer($container, $input = null, $output = n
{
// Self-referential container refernce for the inflector
$container->add('container', $container);
static::setContainer($container);

// Create default input and output objects if they were not provided
if (!$input) {
Expand Down
12 changes: 4 additions & 8 deletions src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,15 @@ public function run($input = null, $output = null, $app = null, $commandFiles =
$this->setInput($input);
$this->setOutput($output);

// If our client gave us a container, then also set it inside
// the static Robo class.
if ($this->getContainer()) {
Robo::setContainer($this->getContainer());
}
// If we were not provided a container, then create one
if (!Robo::hasContainer()) {
Robo::createDefaultContainer($input, $output, $app);
$this->setContainer(Robo::getContainer());
if (!$this->getContainer()) {
$container = Robo::createDefaultContainer($input, $output, $app);
$this->setContainer($container);
// Automatically register a shutdown function and
// an error handler when we provide the container.
$this->installRoboHandlers();
}

if (!$app) {
$app = Robo::application();
}
Expand Down

0 comments on commit 7ec4e7c

Please sign in to comment.