Skip to content

Commit 96cc880

Browse files
committed
Refactoring
PhpcrSessionHelper -> write spec
1 parent e974c27 commit 96cc880

File tree

2 files changed

+143
-95
lines changed

2 files changed

+143
-95
lines changed

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

Lines changed: 5 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -107,34 +107,19 @@ public function init()
107107
$this->initialized = true;
108108
}
109109

110-
/**
111-
* Initialize the supported transports.
112-
*/
113-
private function initializeTransports()
114-
{
115-
$transports = array(
116-
new \PHPCR\Shell\Transport\DoctrineDbal($this->sessionInput),
117-
new \PHPCR\Shell\Transport\Jackrabbit($this->sessionInput),
118-
);
119-
120-
foreach ($transports as $transport) {
121-
$this->transports[$transport->getName()] = $transport;;
122-
}
123-
}
124-
125110
/**
126111
* Register the helpers required by the application
127112
*/
128113
private function registerHelpers()
129114
{
130115
$helpers = array(
131116
new ConfigHelper(),
132-
new EditorHelper($this->session),
133-
new NodeHelper($this->session),
134-
new PathHelper($this->session),
117+
new EditorHelper($this->getSession()),
118+
new NodeHelper($this->getSession()),
119+
new PathHelper($this->getSession()),
135120
new PhpcrConsoleDumperHelper(),
136-
new PhpcrHelper($this->session),
137-
new RepositoryHelper($this->session->getRepository()),
121+
new PhpcrHelper($this->getSession()),
122+
new RepositoryHelper($this->getSession()->getRepository()),
138123
new ResultFormatterHelper(),
139124
new TextHelper(),
140125
);
@@ -235,81 +220,6 @@ private function registerEventListeners()
235220
$this->dispatcher->addSubscriber(new Subscriber\AliasSubscriber($this->getHelperSet()->get('config')));
236221
}
237222

238-
/**
239-
* Initialize the PHPCR session
240-
*/
241-
private function initSession()
242-
{
243-
$transport = $this->getTransport();
244-
$repository = $transport->getRepository();
245-
$credentials = new SimpleCredentials(
246-
$this->sessionInput->getOption('phpcr-username'),
247-
$this->sessionInput->getOption('phpcr-password')
248-
);
249-
250-
$session = $repository->login($credentials, $this->sessionInput->getOption('phpcr-workspace'));
251-
252-
if (!$this->session) {
253-
$this->session = new PhpcrSession($session);
254-
} else {
255-
$this->session->setPhpcrSession($session);
256-
}
257-
}
258-
259-
/**
260-
* Change the current workspace
261-
*
262-
* @todo: Move to session helper?
263-
*
264-
* @param string $workspaceName
265-
*/
266-
public function changeWorkspace($workspaceName)
267-
{
268-
$this->session->logout();
269-
$this->sessionInput->setOption('phpcr-workspace', $workspaceName);
270-
$this->initSession($this->sessionInput);
271-
}
272-
273-
/**
274-
* Login (again)
275-
*
276-
* @todo: Move to session helper
277-
*
278-
* @param string $username
279-
* @param string $password
280-
* @param string $workspaceName
281-
*/
282-
public function relogin($username, $password, $workspaceName = null)
283-
{
284-
$this->session->logout();
285-
$this->sessionInput->setOption('phpcr-username', $username);
286-
$this->sessionInput->setOption('phpcr-password', $password);
287-
288-
if ($workspaceName) {
289-
$this->sessionInput->setOption('phpcr-workspace', $workspaceName);
290-
}
291-
$this->initSession($this->sessionInput);
292-
}
293-
294-
/**
295-
* Return the transport as defined in the sessionInput
296-
*/
297-
private function getTransport()
298-
{
299-
$transportName = $this->sessionInput->getOption('transport');
300-
301-
if (!isset($this->transports[$transportName])) {
302-
throw new \InvalidArgumentException(sprintf(
303-
'Unknown transport "%s", I have "%s"',
304-
$transportName, implode(', ', array_keys($this->transports))
305-
));
306-
}
307-
308-
$transport = $this->transports[$transportName];
309-
310-
return $transport;
311-
}
312-
313223
/**
314224
* Configure the output formatter
315225
*/
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
namespace PHPCR\Shell\Console\Helper;
4+
5+
use PHPCR\Shell\PhpcrSession;
6+
7+
/**
8+
* Helper for managing PHPCR sessions
9+
*
10+
* @author Daniel Leech <daniel@dantleech.com>
11+
*/
12+
class PhpcrHelper extends Helper
13+
{
14+
/**
15+
* Initial input which was used to initialize the shell.
16+
*
17+
* @var \Symfony\Component\Console\Input\InputInterface
18+
*/
19+
protected $sessionInput;
20+
21+
/**
22+
* Active PHPCR session
23+
*
24+
* @var \PHPCR\SessionInterface
25+
*/
26+
protected $session;
27+
28+
/**
29+
* Available transports
30+
*
31+
* @var \Jackalope\Transport\TransportInterface[]
32+
*/
33+
protected $transports = array();
34+
35+
/**
36+
* @param InputInterface $sessionInput
37+
*/
38+
public function __construct(InputInterface $sessionInput)
39+
{
40+
$this->sessionInput = $sessionInput;
41+
}
42+
43+
/**
44+
* {@inheritDoc}
45+
*/
46+
public function getName()
47+
{
48+
return 'phpcr';
49+
}
50+
51+
/**
52+
* Initialize the PHPCR session
53+
*/
54+
private function initSession()
55+
{
56+
$transport = $this->getTransport();
57+
$repository = $transport->getRepository();
58+
$credentials = new SimpleCredentials(
59+
$this->sessionInput->getOption('phpcr-username'),
60+
$this->sessionInput->getOption('phpcr-password')
61+
);
62+
63+
$session = $repository->login($credentials, $this->sessionInput->getOption('phpcr-workspace'));
64+
65+
if (!$this->session) {
66+
$this->session = new PhpcrSession($session);
67+
} else {
68+
$this->session->setPhpcrSession($session);
69+
}
70+
}
71+
72+
/**
73+
* Change the current workspace
74+
*
75+
* @param string $workspaceName
76+
*/
77+
public function changeWorkspace($workspaceName)
78+
{
79+
$this->session->logout();
80+
$this->sessionInput->setOption('phpcr-workspace', $workspaceName);
81+
$this->initSession($this->sessionInput);
82+
}
83+
84+
/**
85+
* Login (again)
86+
*
87+
* @param string $username
88+
* @param string $password
89+
* @param string $workspaceName
90+
*/
91+
public function relogin($username, $password, $workspaceName = null)
92+
{
93+
$this->session->logout();
94+
$this->sessionInput->setOption('phpcr-username', $username);
95+
$this->sessionInput->setOption('phpcr-password', $password);
96+
97+
if ($workspaceName) {
98+
$this->sessionInput->setOption('phpcr-workspace', $workspaceName);
99+
}
100+
$this->initSession($this->sessionInput);
101+
}
102+
103+
/**
104+
* Return the transport as defined in the sessionInput
105+
*/
106+
private function getTransport()
107+
{
108+
$transportName = $this->sessionInput->getOption('transport');
109+
110+
if (!isset($this->transports[$transportName])) {
111+
throw new \InvalidArgumentException(sprintf(
112+
'Unknown transport "%s", I have "%s"',
113+
$transportName, implode(', ', array_keys($this->transports))
114+
));
115+
}
116+
117+
$transport = $this->transports[$transportName];
118+
119+
return $transport;
120+
}
121+
122+
/**
123+
* Initialize the supported transports.
124+
*
125+
* @todo Do this in a lazy way
126+
*/
127+
private function initializeTransports()
128+
{
129+
$transports = array(
130+
new \PHPCR\Shell\Transport\DoctrineDbal($this->sessionInput),
131+
new \PHPCR\Shell\Transport\Jackrabbit($this->sessionInput),
132+
);
133+
134+
foreach ($transports as $transport) {
135+
$this->transports[$transport->getName()] = $transport;;
136+
}
137+
}
138+
}

0 commit comments

Comments
 (0)