Skip to content

Commit 100f56e

Browse files
committed
Aliases work
1 parent c5fcb8f commit 100f56e

File tree

10 files changed

+217
-19
lines changed

10 files changed

+217
-19
lines changed

features/shell_alias.feature

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Feature: Default aliases
2+
In order to be more effective when using the shell
3+
As a user
4+
I want to be able to use the default command aliases
5+
6+
Background:
7+
Given that I am logged in as "testuser"
8+
And the "cms.xml" fixtures are loaded
9+
10+
Scenario Outline: Execute an alias
11+
Given I execute the "<command>" command
12+
Then the command should not fail
13+
14+
Examples:
15+
| command |
16+
| use default |
17+
| select * from [nt:unstructured] |
18+
| cd cms |
19+
| rm cms |
20+
| mv cms smc |
21+
| ls |
22+
| ls cms |

features/shell_config_init.feature

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Feature: Initialize a new local configuration
2+
In order to create a default configuration
3+
As a user
4+
I want to be able to execute a command which does that
5+
6+
Scenario: Initialize configuration
7+
Given I execute the "shell:config:init --no-ansi --no-interaction" command
8+
Then the command should not fail
9+
And I should see the following:
10+
"""
11+
alias.yml
12+
"""
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace spec\PHPCR\Shell\Subscriber;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Prophecy\Argument;
7+
use PHPCR\Shell\Console\Helper\ConfigHelper;
8+
use PHPCR\Shell\Console\Input\StringInput;
9+
use PHPCR\Shell\Event\CommandPreRunEvent;
10+
11+
class AliasSubscriberSpec extends ObjectBehavior
12+
{
13+
function it_is_initializable()
14+
{
15+
$this->shouldHaveType('PHPCR\Shell\Subscriber\AliasSubscriber');
16+
}
17+
18+
function let(
19+
ConfigHelper $config
20+
) {
21+
$this->beConstructedWith(
22+
$config
23+
);
24+
25+
$config->getConfig('alias')->willReturn(array(
26+
'ls' => 'list:command {arg1}',
27+
'mv' => 'move {arg1} {arg2}',
28+
));
29+
}
30+
31+
function it_should_convert_an_aliased_input_into_a_real_command_input(
32+
CommandPreRunEvent $event,
33+
ConfigHelper $config,
34+
StringInput $input
35+
) {
36+
$event->getInput()->willReturn($input);
37+
$input->getFirstArgument()->willReturn('ls');
38+
$input->getTokens()->willReturn(array(
39+
'ls', 'me'
40+
));
41+
$event->setInput(Argument::type('PHPCR\Shell\Console\Input\StringInput'))->shouldBeCalled();
42+
43+
$this->handleAlias($event)->shouldReturn('list:command me');
44+
}
45+
46+
function it_should_ommit_missing_arguments(
47+
CommandPreRunEvent $event,
48+
ConfigHelper $config,
49+
StringInput $input
50+
) {
51+
$event->getInput()->willReturn($input);
52+
$input->getFirstArgument()->willReturn('ls');
53+
$input->getTokens()->willReturn(array(
54+
'ls'
55+
));
56+
$event->setInput(Argument::type('PHPCR\Shell\Console\Input\StringInput'))->shouldBeCalled();
57+
58+
$this->handleAlias($event)->shouldReturn('list:command');
59+
}
60+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class ShellApplication extends Application
6161

6262
public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
6363
{
64-
parent::__construct($name, $verpion);
64+
parent::__construct($name, $version);
6565

6666
$this->dispatcher = new EventDispatcher();
6767
}
@@ -224,6 +224,7 @@ private function registerCommands()
224224
private function registerEventListeners()
225225
{
226226
$this->dispatcher->addSubscriber(new Subscriber\ExceptionSubscriber());
227+
$this->dispatcher->addSubscriber(new Subscriber\AliasSubscriber($this->getHelperSet()->get('config')));
227228
}
228229

229230
/**
@@ -365,7 +366,7 @@ public function doRun(InputInterface $input, OutputInterface $output)
365366
* @param \Exception $e $exception
366367
* @param OutputInterface $output
367368
*/
368-
public function renderException(\Exception $exception, OutputInterface $output)
369+
public function renderException($exception, $output)
369370
{
370371
$output->writeln(sprintf('<exception>%s</exception>', $exception->getMessage()));
371372
}

src/PHPCR/Shell/Console/Command/Phpcr/NodeListCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protected function configure()
2222
{
2323
$this->setName('node:list');
2424
$this->setDescription('List the children / properties of this node');
25-
$this->addArgument('path', InputArgument::OPTIONAL, 'Path of node');
25+
$this->addArgument('path', InputArgument::OPTIONAL, 'Path of node', '.');
2626
$this->addOption('children', null, InputOption::VALUE_NONE, 'List only the children of this node');
2727
$this->addOption('properties', null, InputOption::VALUE_NONE, 'List only the properties of this node');
2828
$this->addOption('filter', 'f', InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Optional filter to apply');

src/PHPCR/Shell/Console/Helper/ConfigHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private function loadConfig()
9999
public function getConfig($type)
100100
{
101101
if (null !== $this->cachedConfig) {
102-
return $this->cachedConfig;
102+
return $this->cachedConfig['alias'];
103103
}
104104

105105
$this->cachedConfig = $this->loadConfig();

src/PHPCR/Shell/Console/Input/StringInput.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class StringInput extends BaseInput
88
{
99
protected $rawCommand;
10+
protected $tokens;
1011

1112
public function __construct($command)
1213
{
@@ -38,6 +39,17 @@ protected function parse()
3839
}
3940
}
4041

42+
protected function setTokens(array $tokens)
43+
{
44+
$this->tokens = $tokens;
45+
parent::setTokens($tokens);
46+
}
47+
48+
public function getTokens()
49+
{
50+
return $this->tokens;
51+
}
52+
4153
protected function isQuery()
4254
{
4355
if (strpos(strtolower($this->rawCommand), 'select') === 0) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace PHPCR\Shell\Listener;
4+
5+
class ExceptionListener
6+
{
7+
}
Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
# MySQL commands
22
use: workspace:use
33
"show databases": workspace:list
4-
select: query:select {args}
4+
select: query:select
55

66
# Filesystem commands
7-
cd: shell:change-path {args}
8-
rm: node:delete {args}
9-
mv: node:move {args}
7+
cd: shell:path:change {arg1}
8+
rm: node:delete {arg1}
9+
mv: node:move {arg1} {arg2}
10+
pwd: shell:path:show
11+
exit: shell:exit
1012

11-
ls: node:list .
12-
sl: node:clone %arg1% %arg2% # symlink, like ln -s
13-
cat: node:property:show %arg1%
13+
ls: node:list {arg1}
14+
sl: node:clone {arg1} {arg2} # symlink, as in ln -s
15+
cp: node:copy {arg1} {arg2}
16+
cat: node:property:show {arg1}
1417

1518
# Editor commands
16-
vim: node:property:edit %arg1%
17-
nano: node:property:edit %arg1%
19+
vim: node:property:edit {arg1} {arg2}
20+
nano: node:property:edit {arg1} {arg2}
1821

1922
# GNU commands
20-
man: help %arg1%
23+
man: help {arg1}
2124

2225
# 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%
26+
checkin: version:checkin {arg1}
27+
checkout: version:checkout {arg1}
28+
checkpoint: version:checkpoint {arg1}
29+
checkpoint: version:checkpoint {arg1}
30+
lsversion: version:history {arg1}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace PHPCR\Shell\Subscriber;
4+
5+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6+
use PHPCR\Shell\Console\Helper\ConfigHelper;
7+
use PHPCR\Shell\Event\PhpcrShellEvents;
8+
use PHPCR\Shell\Event\CommandPreRunEvent;
9+
use PHPCR\Shell\Console\Input\StringInput;
10+
11+
/**
12+
* Check to see if the input references a command alias and
13+
* modify the input to represent the command which it represents.
14+
*
15+
* @author Daniel Leech <daniel@dantleech.com>
16+
*/
17+
class AliasSubscriber implements EventSubscriberInterface
18+
{
19+
protected $config;
20+
21+
public function __construct(ConfigHelper $config)
22+
{
23+
$this->config = $config;
24+
}
25+
26+
public static function getSubscribedEvents()
27+
{
28+
return array(
29+
PhpcrShellEvents::COMMAND_PRE_RUN => 'handleAlias',
30+
);
31+
}
32+
33+
/**
34+
* Check for an alias and replace the input with a new string command
35+
* if the alias exists.
36+
*
37+
* @return string New command string (for testing purposes)
38+
*/
39+
public function handleAlias(CommandPreRunEvent $event)
40+
{
41+
$input = $event->getInput();
42+
43+
$commandName = $input->getFirstArgument();
44+
45+
$aliasConfig = $this->config->getConfig('alias');
46+
47+
if (!isset($aliasConfig[$commandName])) {
48+
return;
49+
}
50+
51+
$commandTemplate = $aliasConfig[$commandName];
52+
$replaces = array();
53+
54+
preg_match_all('{\{arg[0-9]+\}}', $commandTemplate, $matches);
55+
56+
$args = array();
57+
if (isset($matches[0])) {
58+
$args = $matches[0];
59+
}
60+
61+
$tokens = $input->getTokens();
62+
63+
foreach ($tokens as $i => $token) {
64+
$replaces['{arg' . $i . '}'] = $token;
65+
}
66+
67+
$command = strtr($commandTemplate, $replaces);
68+
69+
foreach ($args as $arg) {
70+
$command = str_replace($arg, '', $command);
71+
}
72+
73+
$command = trim($command);
74+
error_log($command);
75+
76+
$newInput = new StringInput($command);
77+
$event->setInput($newInput);
78+
79+
return $command;
80+
}
81+
}

0 commit comments

Comments
 (0)