Skip to content

Commit f4bb3bd

Browse files
committed
Better aliasing
1 parent 7e45f81 commit f4bb3bd

File tree

3 files changed

+33
-73
lines changed

3 files changed

+33
-73
lines changed

spec/PHPCR/Shell/Subscriber/AliasSubscriberSpec.php

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public function let(
3232
);
3333

3434
$config->getConfig('alias')->willReturn(array(
35-
'ls' => 'list:command {arg1}',
36-
'mv' => 'move {arg1} {arg2}',
35+
'ls' => 'list:command',
36+
'mv' => 'move',
3737
));
3838
}
3939

@@ -42,26 +42,10 @@ public function it_should_convert_an_aliased_input_into_a_real_command_input(
4242
StringInput $input
4343
) {
4444
$event->getInput()->willReturn($input);
45+
$input->getRawCommand()->willReturn('ls -L5 --children');
4546
$input->getFirstArgument()->willReturn('ls');
46-
$input->getTokens()->willReturn(array(
47-
'ls', 'me'
48-
));
49-
$event->setInput(Argument::type('PHPCR\Shell\Console\Input\StringInput'))->shouldBeCalled();
50-
51-
$this->handleAlias($event)->shouldReturn('list:command me');
52-
}
53-
54-
public function it_should_ommit_missing_arguments(
55-
CommandPreRunEvent $event,
56-
StringInput $input
57-
) {
58-
$event->getInput()->willReturn($input);
59-
$input->getFirstArgument()->willReturn('ls');
60-
$input->getTokens()->willReturn(array(
61-
'ls'
62-
));
6347
$event->setInput(Argument::type('PHPCR\Shell\Console\Input\StringInput'))->shouldBeCalled();
6448

65-
$this->handleAlias($event)->shouldReturn('list:command');
49+
$this->handleAlias($event)->shouldReturn('list:command -L5 --children');
6650
}
6751
}

src/PHPCR/Shell/Resources/config.dist/alias.yml

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,54 @@ cinit: shell:config:init
55
clear: shell:clear
66

77
# MySQL commands
8-
use: workspace:use {arg1}
9-
explain: node-type:show {arg1}
8+
use: workspace:use
9+
explain: node-type:show
1010

1111
# Filesystem commands
12-
cd: shell:path:change {arg1}
13-
rm: node:remove {arg1}
14-
mv: node:move {arg1} {arg2}
12+
cd: shell:path:change
13+
rm: node:remove
14+
mv: node:move
1515
pwd: shell:path:show
1616
exit: shell:exit
1717

1818
# Node commands
19-
ls: node:list {arg1}
20-
ln: node:clone {arg1} {arg2} # symlink, as in ln -s
21-
cp: node:copy {arg1} {arg2}
22-
cat: node:property:show {arg1}
23-
touch: node:property:set {arg1} {arg2} {arg3}
24-
mkdir: node:create {arg1} {arg2}
19+
ls: node:list
20+
ln: node:clone # symlink, as in ln -s
21+
cp: node:copy
22+
cat: node:property:show
23+
touch: node:property:set
24+
mkdir: node:create
2525

2626
# Node type commands
2727
mixins: node-type:list "^mix:"
28-
nodetypes: node-type:list {arg1}
29-
ntedit: node-type:edit {arg1}
30-
ntshow: node-type:show {arg1}
28+
nodetypes: node-type:list
29+
ntedit: node-type:edit
30+
ntshow: node-type:show
3131

3232
# Workspace commands
3333
workspaces: workspace:list
3434

3535
# Namespsce commands
36-
namespaces: workspace:namespace:list {arg1}
36+
namespaces: workspace:namespace:list
3737
nsset: workspace:namespace:register
3838

3939
# Editor commands
40-
vi: node:edit {arg1} {arg2}
41-
vim: node:edit {arg1} {arg2}
42-
nano: node:edit {arg1} {arg2}
40+
vi: node:edit
41+
vim: node:edit
42+
nano: node:edit
4343

4444
# GNU commands
45-
man: help {arg1}
45+
man: help
4646

4747
# Version commands
48-
checkin: version:checkin {arg1}
49-
ci: version:checkin {arg1}
50-
co: version:checkout {arg1}
51-
checkout: version:checkout {arg1}
52-
cp: version:checkpoint {arg1}
53-
checkpoint: version:checkpoint {arg1}
54-
vhist: version:history {arg1}
55-
versions: version:history {arg1}
48+
checkin: version:checkin
49+
ci: version:checkin
50+
co: version:checkout
51+
checkout: version:checkout
52+
cp: version:checkpoint
53+
checkpoint: version:checkpoint
54+
vhist: version:history
55+
versions: version:history
5656

5757
# Session commands
5858
save: session:save

src/PHPCR/Shell/Subscriber/AliasSubscriber.php

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,32 +60,8 @@ public function handleAlias(CommandPreRunEvent $event)
6060
return;
6161
}
6262

63-
$commandTemplate = $aliasConfig[$commandName];
64-
$replaces = array();
65-
66-
preg_match_all('{\{arg[0-9]+\}}', $commandTemplate, $matches);
67-
68-
$args = array();
69-
if (isset($matches[0])) {
70-
$args = $matches[0];
71-
}
72-
73-
$tokens = $input->getTokens();
74-
75-
foreach ($tokens as $i => $token) {
76-
if (strstr($token, ' ')) {
77-
$token = escapeshellarg($token);
78-
}
79-
$replaces['{arg' . $i . '}'] = $token;
80-
}
81-
82-
$command = strtr($commandTemplate, $replaces);
83-
84-
foreach ($args as $arg) {
85-
$command = str_replace($arg, '', $command);
86-
}
87-
88-
$command = trim($command);
63+
$command = $aliasConfig[$commandName];
64+
$command = $command .= substr($input->getRawCommand(), strlen($commandName));
8965

9066
$newInput = new StringInput($command);
9167
$event->setInput($newInput);

0 commit comments

Comments
 (0)