Skip to content

Commit aed79e0

Browse files
author
Alexander Obuhovich
committed
Extract reusable code into "ConsoleHelpers\ConsoleKit" namespace
1 parent 0780988 commit aed79e0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+483
-320
lines changed

bin/svn-buddy

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,8 @@
99
* @link https://github.com/console-helpers/svn-buddy
1010
*/
1111

12-
class DetectAutoloader
13-
{
12+
require_once dirname(__DIR__) . '/vendor/autoload.php';
1413

15-
/**
16-
* Detects autoloader.
17-
*
18-
* @return void
19-
*/
20-
public static function detect()
21-
{
22-
$autoloaded = false;
23-
$autoload_locations = array(
24-
// When executed directly.
25-
__DIR__ . '/../vendor/autoload.php',
26-
27-
// When executed from PHAR.
28-
__DIR__ . '/../../../autoload.php',
29-
);
30-
31-
foreach ( $autoload_locations as $autoload_location ) {
32-
if ( file_exists($autoload_location) ) {
33-
$autoloaded = true;
34-
require_once $autoload_location;
35-
break;
36-
}
37-
}
38-
39-
if ( !$autoloaded ) {
40-
throw new \RunTimeException('Cannot find an autoload.php file, have you executed composer install command?');
41-
}
42-
}
43-
44-
}
45-
46-
DetectAutoloader::detect();
47-
48-
$container = new \ConsoleHelpers\SVNBuddy\DIContainer();
14+
$container = new \ConsoleHelpers\SVNBuddy\Container();
4915
$app = new \ConsoleHelpers\SVNBuddy\Application($container);
50-
$app->run($container['input'], $container['output']);
16+
$app->run();

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
},
2121
"autoload": {
2222
"psr-4": {
23-
"ConsoleHelpers\\SVNBuddy\\": "src/SVNBuddy/"
23+
"ConsoleHelpers\\SVNBuddy\\": "src/SVNBuddy/",
24+
"ConsoleHelpers\\ConsoleKit\\": "src/ConsoleKit/"
2425
}
2526
},
2627
"autoload-dev": {
2728
"psr-4": {
28-
"Tests\\ConsoleHelpers\\SVNBuddy\\": "tests/SVNBuddy/"
29+
"Tests\\ConsoleHelpers\\SVNBuddy\\": "tests/SVNBuddy/",
30+
"Tests\\ConsoleHelpers\\ConsoleKit\\": "tests/ConsoleKit/"
2931
}
3032
},
3133
"bin": ["bin/svn-buddy"]

src/ConsoleKit/Application.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* This file is part of the SVN-Buddy library.
4+
* For the full copyright and license information, please view
5+
* the LICENSE file that was distributed with this source code.
6+
*
7+
* @copyright Alexander Obuhovich <aik.bold@gmail.com>
8+
* @link https://github.com/console-helpers/svn-buddy
9+
*/
10+
11+
namespace ConsoleHelpers\ConsoleKit;
12+
13+
14+
use Stecman\Component\Symfony\Console\BashCompletion\CompletionCommand;
15+
use Symfony\Component\Console\Application as BaseApplication;
16+
use Symfony\Component\Console\Input\InputInterface;
17+
use Symfony\Component\Console\Output\OutputInterface;
18+
19+
class Application extends BaseApplication
20+
{
21+
22+
/**
23+
* Dependency injection container.
24+
*
25+
* @var Container
26+
*/
27+
protected $dic;
28+
29+
/**
30+
* Creates application.
31+
*
32+
* @param Container $container Container.
33+
*/
34+
public function __construct(Container $container)
35+
{
36+
$this->dic = $container;
37+
38+
parent::__construct($this->dic['app_name'], $this->dic['app_version']);
39+
40+
$helper_set = $this->getHelperSet();
41+
$helper_set->set($this->dic['container_helper']);
42+
43+
$that = $this;
44+
$this->dic['helper_set'] = function () use ($that) {
45+
return $that->getHelperSet();
46+
};
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
protected function getDefaultCommands()
53+
{
54+
$default_commands = parent::getDefaultCommands();
55+
$default_commands[] = new CompletionCommand();
56+
57+
return $default_commands;
58+
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function run(InputInterface $input = null, OutputInterface $output = null)
64+
{
65+
if ( !isset($input) ) {
66+
$input = $this->dic['input'];
67+
}
68+
69+
if ( !isset($output) ) {
70+
$output = $this->dic['output'];
71+
}
72+
73+
return parent::run($input, $output);
74+
}
75+
76+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
/**
3+
* This file is part of the SVN-Buddy library.
4+
* For the full copyright and license information, please view
5+
* the LICENSE file that was distributed with this source code.
6+
*
7+
* @copyright Alexander Obuhovich <aik.bold@gmail.com>
8+
* @link https://github.com/console-helpers/svn-buddy
9+
*/
10+
11+
namespace ConsoleHelpers\ConsoleKit\Command;
12+
13+
14+
use ConsoleHelpers\ConsoleKit\ConsoleIO;
15+
use ConsoleHelpers\ConsoleKit\Helper\ContainerHelper;
16+
use ConsoleHelpers\ConsoleKit\Container;
17+
use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
18+
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
19+
use Symfony\Component\Console\Command\Command;
20+
use Symfony\Component\Console\Input\ArrayInput;
21+
use Symfony\Component\Console\Input\InputInterface;
22+
use Symfony\Component\Console\Output\OutputInterface;
23+
24+
/**
25+
* Base command class.
26+
*/
27+
abstract class AbstractCommand extends Command implements CompletionAwareInterface
28+
{
29+
30+
/**
31+
* Console IO.
32+
*
33+
* @var ConsoleIO
34+
*/
35+
protected $io;
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
protected function initialize(InputInterface $input, OutputInterface $output)
41+
{
42+
parent::initialize($input, $output);
43+
44+
// Don't use IO from container, because it contains outer IO which doesn't reflect sub-command calls.
45+
$this->io = new ConsoleIO($input, $output, $this->getHelperSet());
46+
47+
$this->prepareDependencies();
48+
}
49+
50+
/**
51+
* Return possible values for the named option
52+
*
53+
* @param string $optionName Option name.
54+
* @param CompletionContext $context Completion context.
55+
*
56+
* @return array
57+
*/
58+
public function completeOptionValues($optionName, CompletionContext $context)
59+
{
60+
$this->prepareDependencies();
61+
62+
return array();
63+
}
64+
65+
/**
66+
* Return possible values for the named argument
67+
*
68+
* @param string $argumentName Argument name.
69+
* @param CompletionContext $context Completion context.
70+
*
71+
* @return array
72+
*/
73+
public function completeArgumentValues($argumentName, CompletionContext $context)
74+
{
75+
$this->prepareDependencies();
76+
77+
return array();
78+
}
79+
80+
/**
81+
* Prepare dependencies.
82+
*
83+
* @return void
84+
*/
85+
protected function prepareDependencies()
86+
{
87+
88+
}
89+
90+
/**
91+
* Runs another command.
92+
*
93+
* @param string $name Command name.
94+
* @param array $arguments Arguments.
95+
*
96+
* @return integer
97+
*/
98+
protected function runOtherCommand($name, array $arguments = array())
99+
{
100+
$arguments['command'] = $name;
101+
$cleanup_command = $this->getApplication()->find($name);
102+
103+
$input = new ArrayInput($arguments);
104+
105+
return $cleanup_command->run($input, $this->io->getOutput());
106+
}
107+
108+
/**
109+
* Returns container.
110+
*
111+
* @return Container
112+
*/
113+
protected function getContainer()
114+
{
115+
static $container;
116+
117+
if ( !isset($container) ) {
118+
/** @var ContainerHelper $container_helper */
119+
$container_helper = $this->getHelper('container');
120+
121+
$container = $container_helper->getContainer();
122+
}
123+
124+
return $container;
125+
}
126+
127+
}

src/SVNBuddy/Config/ConfigEditor.php renamed to src/ConsoleKit/Config/ConfigEditor.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @link https://github.com/console-helpers/svn-buddy
99
*/
1010

11-
namespace ConsoleHelpers\SVNBuddy\Config;
11+
namespace ConsoleHelpers\ConsoleKit\Config;
1212

1313

1414
class ConfigEditor
@@ -21,16 +21,6 @@ class ConfigEditor
2121
*/
2222
protected $filename;
2323

24-
/**
25-
* Default settings.
26-
*
27-
* @var array
28-
*/
29-
protected static $defaultSettings = array(
30-
'repository-connector.username' => '',
31-
'repository-connector.password' => '',
32-
);
33-
3424
/**
3525
* Settings.
3626
*
@@ -42,11 +32,12 @@ class ConfigEditor
4232
* Creates config instance.
4333
*
4434
* @param string $filename Filename.
35+
* @param array $defaults Defaults.
4536
*/
46-
public function __construct($filename)
37+
public function __construct($filename, array $defaults = array())
4738
{
4839
$this->filename = $filename;
49-
$this->load();
40+
$this->load($defaults);
5041
}
5142

5243
/**
@@ -97,17 +88,19 @@ public function set($name, $value)
9788
/**
9889
* Loads config contents from disk.
9990
*
91+
* @param array $defaults Defaults.
92+
*
10093
* @return void
10194
*/
102-
protected function load()
95+
protected function load(array $defaults)
10396
{
10497
if ( file_exists($this->filename) ) {
10598
$this->settings = json_decode(file_get_contents($this->filename), true);
10699

107100
return;
108101
}
109102

110-
$this->settings = self::$defaultSettings;
103+
$this->settings = $defaults;
111104
$this->store();
112105
}
113106

src/SVNBuddy/ConsoleIO.php renamed to src/ConsoleKit/ConsoleIO.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @link https://github.com/console-helpers/svn-buddy
99
*/
1010

11-
namespace ConsoleHelpers\SVNBuddy;
11+
namespace ConsoleHelpers\ConsoleKit;
1212

1313

1414
use Symfony\Component\Console\Helper\HelperSet;

0 commit comments

Comments
 (0)