Skip to content

Commit 100aeb4

Browse files
committed
Change workspace command and related
1 parent e72689d commit 100aeb4

File tree

4 files changed

+87
-20
lines changed

4 files changed

+87
-20
lines changed

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

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
use PHPCR\Shell\Console\Command\WorkspaceNamespaceListCommand;
5858
use PHPCR\Shell\Console\Command\WorkspaceNamespaceRegisterCommand;
5959
use PHPCR\Shell\Console\Command\WorkspaceNamespaceUnregisterCommand;
60+
use PHPCR\Shell\Console\Command\WorkspaceUseCommand;
6061
use PHPCR\Shell\Console\Command\NodeTypeShowCommand;
6162
use PHPCR\Shell\Console\Helper\EditorHelper;
6263
use PHPCR\Shell\Console\Command\NodeTypeEditCommand;
@@ -91,10 +92,14 @@
9192
class ShellApplication extends Application
9293
{
9394
protected $transports;
94-
protected $credentials;
9595
protected $initialized;
9696
protected $sessionInput;
9797

98+
/**
99+
* @var SessionInterface
100+
*/
101+
private $session;
102+
98103
/**
99104
* Input from the shell command, containing the connection
100105
* parameters etc.
@@ -156,6 +161,7 @@ public function init()
156161
$this->add(new WorkspaceNamespaceListCommand());
157162
$this->add(new WorkspaceNamespaceRegisterCommand());
158163
$this->add(new WorkspaceNamespaceUnregisterCommand());
164+
$this->add(new WorkspaceUseCommand());
159165
$this->add(new NodeTypeShowCommand());
160166
$this->add(new NodeTypeEditCommand());
161167
$this->add(new NodeTypeUnregisterCommand());
@@ -219,31 +225,46 @@ public function init()
219225
->setName('workspace-purge')
220226
);
221227

222-
$session = $this->getSession($this->sessionInput);
228+
$this->initSession();
223229

224-
$this->getHelperSet()->set(new EditorHelper($session));
230+
$this->getHelperSet()->set(new EditorHelper($this->session));
225231
$this->getHelperSet()->set(new PhpcrConsoleDumperHelper());
226-
$this->getHelperSet()->set(new PhpcrHelper($session));
232+
$this->getHelperSet()->set(new PhpcrHelper($this->session));
227233
$this->getHelperSet()->set(new ResultFormatterHelper());
228234
$this->getHelperSet()->set(new TextHelper());
229-
$this->getHelperSet()->set(new NodeHelper($session));
235+
$this->getHelperSet()->set(new NodeHelper($this->session));
230236

231237
$this->initialized = true;
232238
}
233239

234-
private function getSession($input)
240+
private function initSession()
235241
{
236-
$transport = $this->getTransport($input);
242+
$transport = $this->getTransport($this->sessionInput);
237243
$repository = $transport->getRepository();
238244
$credentials = new SimpleCredentials(
239-
$input->getOption('phpcr-username'),
240-
$input->getOption('phpcr-password')
245+
$this->sessionInput->getOption('phpcr-username'),
246+
$this->sessionInput->getOption('phpcr-password')
241247
);
242248

243-
$session = $repository->login($credentials, $input->getOption('phpcr-workspace'));
244-
$session = new PhpcrSession($session);
249+
$session = $repository->login($credentials, $this->sessionInput->getOption('phpcr-workspace'));
245250

246-
return $session;
251+
if (!$this->session) {
252+
$this->session = new PhpcrSession($session);
253+
} else {
254+
$this->session->setPhpcrSession($session);
255+
}
256+
}
257+
258+
/**
259+
* Change the current workspace
260+
*
261+
* @param string $workspaceName
262+
*/
263+
public function changeWorkspace($workspaceName)
264+
{
265+
$this->session->logout();
266+
$this->sessionInput->setOption('phpcr-workspace', $workspaceName);
267+
$this->initSession($this->sessionInput);
247268
}
248269

249270
private function getTransport(InputInterface $input)

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ protected function configure()
1717
$this->setHelp(<<<HERE
1818
Lists the workspaces accessible to the current user.
1919
20+
The current workspace is indicated by an asterix (*).
21+
2022
Lists the names of all workspaces in this
2123
repository that are accessible to this user, given the Credentials that
2224
were used to get the Session to which this Workspace is tied.
@@ -31,12 +33,16 @@ public function execute(InputInterface $input, OutputInterface $output)
3133
{
3234
$session = $this->getHelper('phpcr')->getSession();
3335

34-
$workspaces = $session->getWorkspace()->getAccessibleWorkspaceNames();
36+
$workspace = $session->getWorkspace();
37+
$availableWorkspaces = $workspace->getAccessibleWorkspaceNames();
3538

3639
$table = clone $this->getHelper('table');
3740
$table->setHeaders(array('Name'));
38-
foreach ($workspaces as $workspace) {
39-
$table->addRow(array($workspace));
41+
foreach ($availableWorkspaces as $availableWorkspace) {
42+
if ($availableWorkspace == $workspace->getName()) {
43+
$availableWorkspace .= ' *';
44+
}
45+
$table->addRow(array($availableWorkspace));
4046
}
4147

4248
$table->render($output);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace PHPCR\Shell\Console\Command;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
use Symfony\Component\Console\Input\InputArgument;
9+
10+
class WorkspaceUseCommand extends Command
11+
{
12+
protected function configure()
13+
{
14+
$this->setName('workspace:use');
15+
$this->setDescription('Change the current workspace');
16+
$this->addArgument('name', InputArgument::REQUIRED, 'Name of workspace to use');
17+
$this->addArgument('srcWorkspace', InputArgument::OPTIONAL, 'If specified, clone from this workspace');
18+
$this->setHelp(<<<HERE
19+
Change the workspace.
20+
HERE
21+
);
22+
}
23+
24+
public function execute(InputInterface $input, OutputInterface $output)
25+
{
26+
$workspaceName = $input->getArgument('name');
27+
$this->getApplication()->changeWorkspace($workspaceName);
28+
}
29+
}

src/PHPCR/Shell/PhpcrSession.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,22 @@ class PhpcrSession implements SessionInterface
1212
protected $session;
1313
protected $cwd = '/';
1414

15+
public function __construct(SessionInterface $session)
16+
{
17+
$this->session = $session;
18+
}
19+
20+
/**
21+
* Allow underlying session to be changed
22+
* For example when changing workspaces
23+
*
24+
* @param SessionInterface $session
25+
*/
26+
public function setPhpcrSession(SessionInterface $session)
27+
{
28+
$this->session = $session;
29+
}
30+
1531
public function getCurrentNode()
1632
{
1733
return $this->getNode($this->getCwd());
@@ -121,11 +137,6 @@ public function getAbsPaths($paths)
121137
return $newPaths;
122138
}
123139

124-
public function __construct(SessionInterface $session)
125-
{
126-
$this->session = $session;
127-
}
128-
129140
public function getRepository()
130141
{
131142
return $this->session->getRepository();

0 commit comments

Comments
 (0)