Skip to content
This repository was archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Remove container component
Browse files Browse the repository at this point in the history
  • Loading branch information
panlatent committed Dec 26, 2017
1 parent 7f9d476 commit 296cdac
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 104 deletions.
24 changes: 1 addition & 23 deletions bin/site-cli
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* This file is the application launcher.
* ----------------------------------------------------------------------------
*/
use Panlatent\Container\Container;
use Panlatent\SiteCli\Commands\AlfredCommand;
use Panlatent\SiteCli\Commands\ClearCommand;
use Panlatent\SiteCli\Commands\CompletionCommand;
Expand All @@ -28,8 +27,6 @@ use Panlatent\SiteCli\Commands\InitCommand;
use Panlatent\SiteCli\Commands\ListCommand;
use Panlatent\SiteCli\Application;
use Panlatent\SiteCli\Commands\EditCommand;
use Panlatent\SiteCli\Configure;
use Panlatent\SiteCli\Site\Manager;

/* ----------------------------------------------------------------------------
* Check PHP version and composer autoload file.
Expand All @@ -47,32 +44,13 @@ if (version_compare(PHP_VERSION, '5.5', '<')) {
*/
require(__DIR__ . '/../vendor/autoload.php');

/* ----------------------------------------------------------------------------
* Register container services.
*
* Load configure file.
* ----------------------------------------------------------------------------
*/
$container = new Container();
$container->setService(Configure::class, function() {
return new Configure([
__DIR__ . '/../site-cli.yml',
'?~/.site-cli.yml' // optional
]);
});

$container->setService(Manager::class, function() use($container) {
return new Manager($container[Configure::class]['available'],
$container[Configure::class]['enabled']);
});

/* ----------------------------------------------------------------------------
* Build a symfony console application.
*
* The Application uses a custom command replace default command(list).
* ----------------------------------------------------------------------------
*/
$app = new Application($container);
$app = new Application();

/* ----------------------------------------------------------------------------
* The Completion command is based on stecman/symfony-console-completion.
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
"symfony/process": "^3.3",
"symfony/yaml": "~3.0",
"stecman/symfony-console-completion": "^0.7.0",
"hassankhan/config": "^0.10.0",
"panlatent/container": "~0.1"
"hassankhan/config": "^0.10.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
Expand Down
20 changes: 2 additions & 18 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,16 @@ class Application extends \Symfony\Component\Console\Application
/**
* The console version.
*/
const VERSION = '1.2.0';

/**
* @var \Panlatent\Container\Container
*/
protected $container;
const VERSION = '1.3.2';

/**
* Application constructor.
*
* Using DefaultCommand replace default list command.
*
* @param \Panlatent\Container\Container $container
*/
public function __construct($container)
public function __construct()
{
parent::__construct(static::NAME, static::VERSION);
$this->container = $container;
$this->add(new DefaultCommand());
}

Expand All @@ -62,12 +54,4 @@ public function all($namespace = null)

return $all;
}

/**
* @return \Panlatent\Container\Container
*/
public function getContainer()
{
return $this->container;
}
}
97 changes: 50 additions & 47 deletions src/Commands/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,19 @@ abstract class Command extends \Symfony\Component\Console\Command\Command implem
protected $application;

/**
* @var \Panlatent\Container\Container
* @var SymfonyStyle
*/
protected $container;
protected $io;

/**
* @var \Panlatent\SiteCli\Configure
*/
protected $configure;

/**
* @var SymfonyStyle
*/
protected $io;
private $_configure;

/**
* @var \Panlatent\SiteCli\Site\Manager
*/
private $manager;
private $_manager;

/**
* Command constructor.
Expand All @@ -67,9 +62,6 @@ public function __construct()
final protected function preInit()
{
$this->application = $this->getApplication();
$this->container = $this->application->getContainer();
$this->configure = $this->container[Configure::class];

if ($this instanceof Reloadable) {
$this->addOption(
'without-reload',
Expand All @@ -88,14 +80,14 @@ final protected function initialize(InputInterface $input, OutputInterface $outp
{
$this->preInit();
$this->io->getFormatter()->setStyle('enable', new OutputFormatterStyle('white', null, ['bold']));
$this->container->setService(SymfonyStyle::class, $this->io);
// $this->container->setService(SymfonyStyle::class, $this->io);

if ( ! method_exists($this, 'register') ||
! is_callable([$this, 'register'])) {
return;
}
$this->container->injectMethod($this, 'register');
if ($this->configure['validate']['lost-symbolic-link']) {
// $this->container->injectMethod($this, 'register');
if ($this->getConfigure()['validate']['lost-symbolic-link']) {
$this->checkLostSymbolicLink($output);
}
}
Expand All @@ -109,7 +101,7 @@ final protected function initialize(InputInterface $input, OutputInterface $outp
public function run(InputInterface $input, OutputInterface $output)
{
$this->io = new SymfonyStyle($input, $output);
if ( ! file_exists(Util::home() . '/.site-cli.yml')) {
if ( ! file_exists(Util::home() . '/.site-cli.yml') && ! $this instanceof InitCommand) {
if ($this->io->confirm('Create a .site-cli.yml file to your home?', true)) {
$this->createUserConfigure($output);
}
Expand All @@ -129,6 +121,37 @@ public function run(InputInterface $input, OutputInterface $output)
return $statusCode;
}

/**
* @return null|Manager
*/
public function getManager()
{
if ($this->_manager === null) {
$configure = $this->getConfigure();
if (isset($configure['available']) && isset($configure['enabled']) && file_exists
($configure['available']) && file_exists($configure['enabled'])) {
$this->_manager = new Manager($configure['available'], $configure['enabled']);
}
}

return $this->_manager;
}

/**
* @return Configure
*/
public function getConfigure()
{
if ($this->_configure === null) {
$this->_configure = new Configure([
__DIR__ . '/../../site-cli.yml',
'?~/.site-cli.yml' // optional
]);
}

return $this->_configure;
}

final public function completeArgumentValues($argumentName, CompletionContext $context)
{
$this->preInit();
Expand Down Expand Up @@ -163,32 +186,10 @@ protected function getOptionValues($optionName, CompletionContext $context)
return [];
}

/**
* @param bool $throwException
* @return bool|Manager
* @throws Exception
*/
protected function getManager($throwException = true)
{
if ($this->manager === null) {
try {
$this->manager = $this->container[Manager::class];
} catch (Exception $exception) {
if ($throwException) {
throw $exception;
}

return false;
}
}

return $this->manager;
}

protected function getArgumentGroup()
{
$names = [];
if ($manager = $this->getManager(false)) {
if ($manager = $this->getManager()) {
foreach ($manager as $group) {
$names[] = $group->getName();
}
Expand All @@ -201,7 +202,7 @@ protected function getArgumentSite(CompletionContext $context)
{
$command = $context->getWordAtIndex(1);
$sites = [];
if ($manager = $this->getManager(false)) {
if ($manager = $this->getManager()) {
foreach ($manager->filter()->sites() as $site) {
if ($command != 'disable' || $site->isEnable()) {
$sites[] = $site->getPrettyName();
Expand All @@ -224,13 +225,15 @@ private function createUserConfigure($output)

private function checkLostSymbolicLink(OutputInterface $output)
{
$manager = $this->container[Manager::class];
if ($lostSymbolicLinkEnables = $manager->getLostSymbolicLinkEnables()) {
foreach ($lostSymbolicLinkEnables as $enable) {
$output->writeln(sprintf("<error>Warning: Symbolic link lost in \"%s\"</error>", $enable));
/** @var Manager $manager */
if ($manager = $this->getManager()) {
if ($lostSymbolicLinkEnables = $manager->getLostSymbolicLinkEnables()) {
foreach ($lostSymbolicLinkEnables as $enable) {
$output->writeln(sprintf("<error>Warning: Symbolic link lost in \"%s\"</error>", $enable));
}
$output->writeln('<comment>Note:</comment> Run <info>disable</info> <comment>--clear-lost</comment> will remove them');
$output->writeln('');
}
$output->writeln('<comment>Note:</comment> Run <info>disable</info> <comment>--clear-lost</comment> will remove them');
$output->writeln('');
}
}

Expand All @@ -242,7 +245,7 @@ private function checkLostSymbolicLink(OutputInterface $output)
private function isReloadService($object, $input)
{
$can = $object instanceof Reloadable && ! $input->getOption('without-reload') &&
$this->configure->get('service.reload', false);
$this->getConfigure()->get('service.reload', false);

return $can && $object->canReloadService();
}
Expand Down
6 changes: 0 additions & 6 deletions src/Commands/CompletionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ class CompletionCommand extends \Stecman\Component\Symfony\Console\BashCompletio
*/
protected $application;

/**
* @var \Panlatent\Container\Container
*/
protected $container;

protected function configure()
{
parent::configure();
Expand All @@ -36,7 +31,6 @@ protected function initialize(InputInterface $input, OutputInterface $output)
{
parent::initialize($input, $output);
$this->application = $this->getApplication();
$this->container = $this->application->getContainer();
}

protected function execute(InputInterface $input, OutputInterface $output)
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected function getArgumentValues($argumentName, CompletionContext $context)
{
if ($argumentName == 'target') {
$names = [];
if ($this->getManager(false)) {
if ($this->getManager()) {
$groups = $this->getManager()->filter()->groups();
foreach ($groups as $group) {
$names[] = $group->getName() . '/';
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return true;
}

$config = $this->configure->all();
$config = $this->getConfigure()->all();
$filename = $input->getOption('output') ? $input->getOption('output') : Util::home() . '/.site-cli.yml';

$location = $this->locate();
Expand Down Expand Up @@ -91,7 +91,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
private function locate()
{
$probables = [];
foreach ($this->configure['service']['search'] as $path) {
foreach ($this->getConfigure()['service']['search'] as $path) {
$path = Util::realPath($path);
if (is_dir($path)) {
$probables[] = $path;
Expand Down
10 changes: 5 additions & 5 deletions src/Commands/ServiceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$params = $this->configure->get('service', ['root' => false]);
$params = $this->getConfigure()->get('service', ['root' => false]);
$template = $input->getOption('template');
if ($input->getOption('with-root')) {
$params['root'] = true;
Expand All @@ -83,7 +83,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$params['user'] = $input->getOption('user');
}

$service = new Control($this->configure->get("templates.$template"), $params);
$service = new Control($this->getConfigure()->get("templates.$template"), $params);

if ($input->getOption('preview')) {
$preview = $service->getShellCommand($input->getArgument('signal'));
Expand All @@ -100,8 +100,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
protected function getArgumentValues($argumentName, CompletionContext $context)
{
if ($argumentName === 'signal') {
$templateName = $this->configure->get('nginx.template', 'default');
$template = $this->configure->get("templates.$templateName", []);
$templateName = $this->getConfigure()->get('nginx.template', 'default');
$template = $this->getConfigure()->get("templates.$templateName", []);

return array_keys($template);
}
Expand All @@ -112,7 +112,7 @@ protected function getArgumentValues($argumentName, CompletionContext $context)
protected function getOptionValues($optionName, CompletionContext $context)
{
if ($optionName == 'template') {
$templates = $this->configure->get("templates", []);
$templates = $this->getConfigure()->get("templates", []);
return array_keys($templates);
} elseif ($optionName == 'user') {
$users = [];
Expand Down

0 comments on commit 296cdac

Please sign in to comment.