Skip to content

Commit

Permalink
Merge pull request #6 from baraja-core/task-with-container
Browse files Browse the repository at this point in the history
BaseTask: Add "magic" method getContainer().
  • Loading branch information
janbarasek authored Apr 2, 2020
2 parents c8c30c4 + 919173c commit 9832be8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"contributte/console": "^0.6.0"
},
"require-dev": {
"nette/bootstrap": "^3.0",
"phpstan/phpstan": "^0.12.18",
"tracy/tracy": "^2.7",
"phpstan/phpstan-nette": "^0.12.6"
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ parameters:
- '#Instantiated class PackageDescriptorEntity not found\.#'
- '#Method Baraja\\PackageManager\\Storage::load\(\) should return Baraja\\PackageManager\\PackageDescriptorEntity but returns PackageDescriptorEntity\.#'
- '#Negated boolean expression is always true\.#'
- '#Class App\\Bootstrap not found\.#'
- '#Call to static method boot\(\) on an unknown class App\\Bootstrap\.#'
43 changes: 42 additions & 1 deletion src/InteractiveComposer/BaseTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
namespace Baraja\PackageManager\Composer;


use App\Bootstrap;
use Baraja\PackageManager\Helpers;
use Baraja\PackageManager\PackageRegistrator;
use Nette\Configurator;
use Nette\DI\Container;

abstract class BaseTask implements ITask
{
Expand All @@ -30,4 +33,42 @@ public function ask(string $question, array $possibilities = []): ?string
{
return Helpers::terminalInteractiveAsk($question, $possibilities);
}
}


/**
* Try boot Nette application and create DIC.
* This container is same for all tasks.
*
* Warning: When you boot application, you can not modify configuration neon data.
*
* @return Container
*/
final public function getContainer(): Container
{
/** @var Container|null $container */
static $container;

if ($container === null) {
$container = $this->bootApplication()->createContainer();
}

return $container;
}


/**
* Try find Nette application and boot.
*
* @return Configurator
*/
private function bootApplication(): Configurator
{
if (\class_exists(Bootstrap::class) === false) {
throw new \RuntimeException(
'Nette application does not exist, because class "\App\Bootstrap" does not found.'
);
}

return Bootstrap::boot();
}
}

0 comments on commit 9832be8

Please sign in to comment.