Skip to content

Commit 7704c87

Browse files
committed
Added ConfigInitCommand
todo: Load the alias configuration file somehow
1 parent 3d2d7d8 commit 7704c87

File tree

5 files changed

+164
-0
lines changed

5 files changed

+164
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace spec\PHPCR\Shell\Console\Helper;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Prophecy\Argument;
7+
8+
class ConfigHelperSpec extends ObjectBehavior
9+
{
10+
function it_is_initializable()
11+
{
12+
$this->shouldHaveType('PHPCR\Shell\Console\Helper\ConfigHelper');
13+
}
14+
15+
function it_should_have_a_method_to_get_the_users_config_directory()
16+
{
17+
putenv('PHPCRSH_HOME=/home/foobar');
18+
$this->getConfigDir()->shouldReturn('/home/foobar');
19+
}
20+
}

src/PHPCR/Shell/Console/Application/ShellApplication.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@
102102
use Symfony\Component\Console\Formatter\OutputFormatter;
103103
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
104104
use Symfony\Component\Console\Input\ArrayInput;
105+
use PHPCR\Shell\Console\Command\Shell\ConfigInitCommand;
106+
use PHPCR\Shell\Console\Helper\ConfigHelper;
105107

106108
class ShellApplication extends Application
107109
{
@@ -153,6 +155,7 @@ public function init()
153155
$this->getHelperSet()->set(new NodeHelper($this->session));
154156
$this->getHelperSet()->set(new PathHelper($this->session));
155157
$this->getHelperSet()->set(new RepositoryHelper($this->session->getRepository()));
158+
$this->getHelperSet()->set(new ConfigHelper());
156159

157160
// add new commands
158161
$this->add(new AccessControlPrivilegeListCommand());
@@ -225,6 +228,7 @@ public function init()
225228
$this->add(new LockUnlockCommand());
226229

227230
// add shell-specific commands
231+
$this->add(new ConfigInitCommand());
228232
$this->add(new PathChangeCommand());
229233
$this->add(new PathShowCommand());
230234
$this->add(new ExitCommand());
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace PHPCR\Shell\Console\Command\Shell;
4+
5+
use Symfony\Component\Filesystem\Filesystem;
6+
use Symfony\Component\Console\Command\Command;
7+
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Output\OutputInterface;
9+
10+
class ConfigInitCommand extends Command
11+
{
12+
protected $output;
13+
14+
public function configure()
15+
{
16+
$this->setName('shell:config:init');
17+
$this->setDescription('Initialize a local configuration with default values');
18+
$this->setHelp(<<<EOT
19+
Initialize a new configuration folder, <info>.phpcrsh</info> in the users HOME directory.
20+
EOT
21+
);
22+
}
23+
24+
public function execute(InputInterface $input, OutputInterface $output)
25+
{
26+
$this->output = $output;
27+
$this->configHelper = $this->getHelper('config');
28+
29+
$fs = new Filesystem();
30+
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
31+
$output->writeln('<error>This feature is currently only supported on Linux and OSX (maybe). Please submit a PR to support it on windows.</error>');
32+
return 1;
33+
}
34+
35+
$configDir = $this->configHelper->getConfigDir();
36+
$distDir = __DIR__ . '/../../../Resources/config.dist';
37+
38+
if (!file_exists($configDir)) {
39+
$this->logCreation($configDir);
40+
$fs->mkdir($configDir);
41+
}
42+
43+
$configFilenames = array(
44+
'alias.yml',
45+
);
46+
47+
foreach ($configFilenames as $configFilename) {
48+
$srcFile = $distDir . '/' . $configFilename;
49+
$destFile = $configDir . '/' . $configFilename;
50+
51+
if (!file_exists($srcFile)) {
52+
throw new \Exception('Dist (source) file "' . $srcFile . '" does not exist.');
53+
}
54+
55+
$fs->copy($srcFile, $destFile);
56+
$this->logCreation($destFile);
57+
}
58+
}
59+
60+
private function logCreation($path)
61+
{
62+
$this->output->writeln('<info>[+]</info> ' . $path);
63+
}
64+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace PHPCR\Shell\Console\Helper;
4+
5+
use Symfony\Component\Console\Helper\Helper;
6+
7+
class ConfigHelper extends Helper
8+
{
9+
public function getName()
10+
{
11+
return 'config';
12+
}
13+
14+
/**
15+
* Return the configuration directory
16+
*
17+
* @return string
18+
*/
19+
public function getConfigDir()
20+
{
21+
$home = getenv('PHPCRSH_HOME');
22+
23+
if ($home) {
24+
return $home;
25+
}
26+
27+
// handle windows ..
28+
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
29+
if (!getenv('APPDATA')) {
30+
throw new \RuntimeException(
31+
'The APPDATA or phpcrsh_HOME environment variable must be set for phpcrsh to run correctly'
32+
);
33+
}
34+
$home = strtr(getenv('APPDATA'), '\\', '/').'/phpcrsh';
35+
36+
return $home;
37+
}
38+
39+
if (!getenv('HOME')) {
40+
throw new \RuntimeException(
41+
'The HOME or phpcrsh_HOME environment variable must be set for phpcrsh to run correctly'
42+
);
43+
}
44+
45+
$home = rtrim(getenv('HOME'), '/').'/.phpcrsh';
46+
47+
return $home;
48+
}
49+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# MySQL commands
2+
use: workspace:use
3+
"show databases": workspace:list
4+
select: query:select {args}
5+
6+
# Filesystem commands
7+
cd: shell:change-path {args}
8+
rm: node:delete {args}
9+
mv: node:move {args}
10+
11+
ls: node:list .
12+
sl: node:clone %arg1% %arg2% # symlink, like ln -s
13+
cat: node:property:show %arg1%
14+
15+
# Editor commands
16+
vim: node:property:edit %arg1%
17+
nano: node:property:edit %arg1%
18+
19+
# GNU commands
20+
man: help %arg1%
21+
22+
# Version commands
23+
checkin: node:version:checkin
24+
checkout: node:version:checkout %arg1%
25+
checkpoint: node:version:checkpoint %arg1%
26+
checkpoint: node:version:checkpoint %arg1%
27+
lsversion: node:version:list %arg1%

0 commit comments

Comments
 (0)