Skip to content

Commit e19d8ce

Browse files
committed
Wrapped PHPCR session to allow absolute paths
1 parent 2173dee commit e19d8ce

File tree

6 files changed

+284
-33
lines changed

6 files changed

+284
-33
lines changed

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,10 @@
2929
use Symfony\Component\Console\Command\Command;
3030
use PHPCR\Shell\Console\Command\Shell\ChangePathCommand;
3131
use PHPCR\Shell\Console\Command\Shell\PwdCommand;
32+
use Symfony\Component\Console\Input\ArrayInput;
3233

3334
class ShellApplication extends Application
3435
{
35-
protected $cwd;
36-
37-
public function getCwd()
38-
{
39-
return $this->cwd;
40-
}
41-
42-
public function setCwd($cwd)
43-
{
44-
$this->cwd = $cwd;
45-
}
46-
4736
public function __construct(SessionInterface $session)
4837
{
4938
parent::__construct('PHPCR', '1.0');
@@ -56,6 +45,8 @@ public function __construct(SessionInterface $session)
5645
->setName('ls')
5746
->setDescription('Alias for dump')
5847
);
48+
$this->get('ls')
49+
->getDefinition()->getArgument('identifier')->setDefault(null);
5950
$this->add($this->wrap(new NodeMoveCommand())
6051
->setName('mv')
6152
);
@@ -97,16 +88,22 @@ public function __construct(SessionInterface $session)
9788
$this->getHelperSet()->set(new ResultFormatterHelper());
9889
$this->getHelperSet()->set(new PhpcrHelper($session));
9990
$this->getHelperSet()->set(new PhpcrCliHelper($session));
100-
101-
foreach ($this->all() as $command) {
102-
if ($command instanceof AbstractSessionCommand) {
103-
$command->setSession($session);
104-
}
105-
}
10691
}
10792

10893
public function wrap(Command $command)
10994
{
11095
return $command;
11196
}
97+
98+
public function doRun(InputInterface $input, OutputInterface $output)
99+
{
100+
$name = $this->getCommandName($input);
101+
102+
if (!$name) {
103+
$name = 'help';
104+
$input = new ArrayInput(array('command' => 'pwd'));
105+
}
106+
107+
return parent::doRun($input, $output);
108+
}
112109
}

src/PHPCR/Shell/Console/Command/Query/SelectCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function execute(InputInterface $input, OutputInterface $output)
3333
$limit = $input->getOption('limit');
3434
$offset = $input->getOption('offset');
3535

36-
$session = $this->getSession();
36+
$session = $this->getHelper('phpcr')->getSession();
3737
$qm = $session->getWorkspace()->getQueryManager();
3838

3939
$query = $qm->createQuery($sql, $language);

src/PHPCR/Shell/Console/Command/Shell/ChangePathCommand.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Symfony\Component\Console\Output\OutputInterface;
88
use Symfony\Component\Console\Input\InputOption;
99
use PHPCR\Shell\Console\Command\AbstractSessionCommand;
10+
use PHPCR\ItemNotFoundException;
11+
use PHPCR\PathNotFoundException;
1012

1113
class ChangePathCommand extends AbstractSessionCommand
1214
{
@@ -21,20 +23,12 @@ public function execute(InputInterface $input, OutputInterface $output)
2123
{
2224
$session = $this->getHelper('phpcr')->getSession();
2325
$path = $input->getArgument('path');
24-
$cwd = $this->getApplication()->getCwd();
25-
26-
// absolute path
27-
if (substr($path, 0, 1) == '/') {
28-
$newPath = $path;
29-
} elseif ($path == '..') {
30-
$newPath = dirname($cwd);
31-
} else {
32-
$newPath = sprintf('%s/%s', $cwd, $path);
26+
try {
27+
$session->chdir($path);
28+
$output->writeln('<comment>' . $session->getCwd() . '</comment>');
29+
} catch (PathNotFoundException $e) {
30+
$output->writeln('<error>' . $e->getMessage() . '</error>');
3331
}
34-
35-
$session->getNode($newPath);
36-
$this->getApplication()->setCwd($newPath);
37-
$output->writeln($newPath);
3832
}
3933
}
4034

src/PHPCR/Shell/Console/Command/Shell/PwdCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ protected function configure()
1818

1919
public function execute(InputInterface $input, OutputInterface $output)
2020
{
21-
$output->writeln($this->getApplication()->getCwd());
21+
$output->writeln(
22+
'<comment>' . $this->getHelper('phpcr')->getSession()->getCwd() . '</comment>'
23+
);
2224
}
2325
}
2426

src/PHPCR/Shell/Console/Command/ShellCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPCR\Shell\Console\Application\ShellApplication;
1010
use PHPCR\Shell\Console\Application\Shell;
1111
use PHPCR\SimpleCredentials;
12+
use PHPCR\Shell\PhpcrSession;
1213

1314
class ShellCommand extends Command
1415
{
@@ -47,6 +48,7 @@ public function execute(InputInterface $input, OutputInterface $output)
4748
);
4849

4950
$session = $repository->login($credentials);
51+
$session = new PhpcrSession($session);
5052

5153
$application = new Shell(new ShellApplication($session));
5254
$application->run($input, $output);

src/PHPCR/Shell/PhpcrSession.php

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
<?php
2+
3+
namespace PHPCR\Shell;
4+
5+
use PHPCR\SessionInterface;
6+
use PHPCR\CredentialsInterface;
7+
8+
class PhpcrSession implements SessionInterface
9+
{
10+
protected $session;
11+
protected $cwd = '/';
12+
13+
public function getCwd()
14+
{
15+
return $this->cwd;
16+
}
17+
18+
public function setCwd($cwd)
19+
{
20+
$this->cwd = $cwd;
21+
}
22+
23+
public function chdir($path)
24+
{
25+
$cwd = $this->getCwd();
26+
27+
// absolute path
28+
if (substr($path, 0, 1) == '/') {
29+
$newPath = $path;
30+
} elseif ($path == '..') {
31+
$newPath = dirname($cwd);
32+
} else {
33+
if ($this->cwd == '/') {
34+
$newPath = sprintf('/%s', $path);
35+
} else {
36+
$newPath = sprintf('%s/%s', $cwd, $path);
37+
}
38+
}
39+
40+
// check that path is valid
41+
$this->getNode($newPath);
42+
43+
$this->setCwd($newPath);
44+
}
45+
46+
public function getAbsPath($path)
47+
{
48+
if (!$path) {
49+
return $this->getCwd();
50+
}
51+
52+
if (substr($path, 0, 1) == '/') {
53+
$absPath = $path;
54+
} else {
55+
$absPath = $this->getCwd() . '/' . $path;
56+
}
57+
58+
return $absPath;
59+
}
60+
61+
public function getAbsPaths($paths)
62+
{
63+
$newPaths = array();
64+
foreach ($paths as $path) {
65+
$newPaths[] = $this->getAbsPath($path);
66+
}
67+
68+
return $newPaths;
69+
}
70+
71+
72+
public function __construct(SessionInterface $session)
73+
{
74+
$this->session = $session;
75+
}
76+
77+
public function getRepository()
78+
{
79+
return $this->session->getRepository();
80+
}
81+
82+
public function getUserID()
83+
{
84+
return $this->session->getUserID();
85+
}
86+
87+
public function getAttributeNames()
88+
{
89+
return $this->session->getAttributeNames();
90+
}
91+
92+
public function getAttribute($name)
93+
{
94+
return $this->session->getAttribute($name);
95+
}
96+
97+
public function getWorkspace()
98+
{
99+
return $this->session->getWorkspace();
100+
}
101+
102+
public function getRootNode()
103+
{
104+
return $this->session->getRootNode();
105+
}
106+
107+
public function impersonate(CredentialsInterface $credentials)
108+
{
109+
return $this->session->impersonate($credentials);
110+
}
111+
112+
public function getNodeByIdentifier($id)
113+
{
114+
return $this->session->getNodeByIdentifier($id);
115+
}
116+
117+
public function getNodesByIdentifier($ids)
118+
{
119+
return $this->session->getNodesByIdentifier($id);
120+
}
121+
122+
public function getItem($path)
123+
{
124+
return $this->session->getItem($this->getAbsPath($path));
125+
}
126+
127+
public function getNode($path, $depthHint = -1)
128+
{
129+
return $this->session->getNode($this->getAbsPath($path));
130+
}
131+
132+
public function getNodes($paths)
133+
{
134+
return $this->session->getNodes($this->getAbsPaths($paths));
135+
}
136+
137+
public function getProperty($path)
138+
{
139+
return $this->session->getProperty($this->getAbsPath($path));
140+
}
141+
142+
public function getProperties($paths)
143+
{
144+
return $this->session->getProperties($this->getAbsPaths($paths));
145+
}
146+
147+
public function itemExists($path)
148+
{
149+
return $this->session->itemExists($this->getAbsPath($path));
150+
}
151+
152+
public function nodeExists($path)
153+
{
154+
return $this->session->nodeExists($this->getAbsPath($path));
155+
}
156+
157+
public function propertyExists($path)
158+
{
159+
return $this->session->propertyExists($this->getAbsPath($path));
160+
}
161+
162+
public function move($srcAbsPath, $destAbsPath)
163+
{
164+
return $this->session->move($srcAbsPath, $destAbsPath);
165+
}
166+
167+
public function removeItem($path)
168+
{
169+
return $this->session->removeItem($this->getAbsPath($path));
170+
}
171+
172+
public function save()
173+
{
174+
return $this->session->save();
175+
}
176+
177+
public function refresh($keepChanges)
178+
{
179+
return $this->session->refresh($keepChanges);
180+
}
181+
182+
public function hasPendingChanges()
183+
{
184+
return $this->session->hasPendingChanges();
185+
}
186+
187+
public function hasPermission($path, $actions)
188+
{
189+
return $this->session->hasPermission($this->getAbsPath($path), $actions);
190+
}
191+
192+
public function checkPermission($path, $actions)
193+
{
194+
return $this->session->checkPermission($this->getAbsPath($path), $actions);
195+
}
196+
197+
public function hasCapability($methodName, $target, array $arguments)
198+
{
199+
return $this->session->hasCapability($methodNames, $target, $arguments);
200+
}
201+
202+
public function importXML($parentAbsPath, $uri, $uuidBehavior)
203+
{
204+
return $this->session->importXML($parentAbsPath, $uri, $uuidBehavior);
205+
}
206+
207+
public function exportSystemView($path, $stream, $skipBinary, $noRecurse)
208+
{
209+
return $this->session->exportSystemView($this->getAbsPath($path), $stream, $skipBinary, $noRecurse);
210+
}
211+
212+
public function exportDocumentView($path, $stream, $skipBinary, $noRecurse)
213+
{
214+
return $this->session->exportDocumentView($this->getAbsPath($path), $stream, $skipBinary, $noRecurse);
215+
}
216+
217+
public function setNamespacePrefix($prefix, $uri)
218+
{
219+
return $this->session->setNamespacePrefix($prefix, $uri);
220+
}
221+
222+
public function getNamespacePrefixes()
223+
{
224+
return $this->session->getNamespacePrefixes();
225+
}
226+
227+
public function getNamespaceURI($prefix)
228+
{
229+
return $this->session->getNamespaceURI($prefix);
230+
}
231+
232+
public function getNamespacePrefix($uri)
233+
{
234+
return $this->session->getNamespacePrefix($uri);
235+
}
236+
237+
public function logout()
238+
{
239+
return $this->session->logout();
240+
}
241+
242+
public function isLive()
243+
{
244+
return $this->session->isLive();
245+
}
246+
247+
public function getAccessControlManager()
248+
{
249+
return $this->session->getAccessControlManager();
250+
}
251+
252+
public function getRetentionManager()
253+
{
254+
return $this->session->getRetentionManager();
255+
}
256+
}

0 commit comments

Comments
 (0)