Skip to content

Commit 1a37dd1

Browse files
committed
Merge pull request #14 from phpcr/workspace_commands
Change workspace command and related
2 parents d99f3ea + 100aeb4 commit 1a37dd1

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;
@@ -98,10 +99,14 @@
9899
class ShellApplication extends Application
99100
{
100101
protected $transports;
101-
protected $credentials;
102102
protected $initialized;
103103
protected $sessionInput;
104104

105+
/**
106+
* @var SessionInterface
107+
*/
108+
private $session;
109+
105110
/**
106111
* Input from the shell command, containing the connection
107112
* parameters etc.
@@ -163,6 +168,7 @@ public function init()
163168
$this->add(new WorkspaceNamespaceListCommand());
164169
$this->add(new WorkspaceNamespaceRegisterCommand());
165170
$this->add(new WorkspaceNamespaceUnregisterCommand());
171+
$this->add(new WorkspaceUseCommand());
166172
$this->add(new NodeTypeShowCommand());
167173
$this->add(new NodeTypeEditCommand());
168174
$this->add(new NodeTypeUnregisterCommand());
@@ -233,31 +239,46 @@ public function init()
233239
->setName('workspace-purge')
234240
);
235241

236-
$session = $this->getSession($this->sessionInput);
242+
$this->initSession();
237243

238-
$this->getHelperSet()->set(new EditorHelper($session));
244+
$this->getHelperSet()->set(new EditorHelper($this->session));
239245
$this->getHelperSet()->set(new PhpcrConsoleDumperHelper());
240-
$this->getHelperSet()->set(new PhpcrHelper($session));
246+
$this->getHelperSet()->set(new PhpcrHelper($this->session));
241247
$this->getHelperSet()->set(new ResultFormatterHelper());
242248
$this->getHelperSet()->set(new TextHelper());
243-
$this->getHelperSet()->set(new NodeHelper($session));
249+
$this->getHelperSet()->set(new NodeHelper($this->session));
244250

245251
$this->initialized = true;
246252
}
247253

248-
private function getSession($input)
254+
private function initSession()
249255
{
250-
$transport = $this->getTransport($input);
256+
$transport = $this->getTransport($this->sessionInput);
251257
$repository = $transport->getRepository();
252258
$credentials = new SimpleCredentials(
253-
$input->getOption('phpcr-username'),
254-
$input->getOption('phpcr-password')
259+
$this->sessionInput->getOption('phpcr-username'),
260+
$this->sessionInput->getOption('phpcr-password')
255261
);
256262

257-
$session = $repository->login($credentials, $input->getOption('phpcr-workspace'));
258-
$session = new PhpcrSession($session);
263+
$session = $repository->login($credentials, $this->sessionInput->getOption('phpcr-workspace'));
259264

260-
return $session;
265+
if (!$this->session) {
266+
$this->session = new PhpcrSession($session);
267+
} else {
268+
$this->session->setPhpcrSession($session);
269+
}
270+
}
271+
272+
/**
273+
* Change the current workspace
274+
*
275+
* @param string $workspaceName
276+
*/
277+
public function changeWorkspace($workspaceName)
278+
{
279+
$this->session->logout();
280+
$this->sessionInput->setOption('phpcr-workspace', $workspaceName);
281+
$this->initSession($this->sessionInput);
261282
}
262283

263284
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)