Skip to content

Commit b679491

Browse files
committed
Cleaning up
1 parent 96cc880 commit b679491

File tree

7 files changed

+108
-70
lines changed

7 files changed

+108
-70
lines changed

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

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@
1010
use Symfony\Component\Console\Input\InputInterface;
1111
use Symfony\Component\Console\Input\StringInput;
1212
use Symfony\Component\Console\Output\OutputInterface;
13-
14-
use PHPCR\SimpleCredentials;
15-
16-
use PHPCR\Util\Console\Helper\PhpcrConsoleDumperHelper;
17-
use PHPCR\Util\Console\Helper\PhpcrHelper;
13+
use Symfony\Component\EventDispatcher\EventDispatcher;
1814

1915
use PHPCR\Shell\Console\Command\Phpcr as CommandPhpcr;
2016
use PHPCR\Shell\Console\Command\Shell as CommandShell;
@@ -25,13 +21,13 @@
2521
use PHPCR\Shell\Console\Helper\RepositoryHelper;
2622
use PHPCR\Shell\Console\Helper\ResultFormatterHelper;
2723
use PHPCR\Shell\Console\Helper\TextHelper;
24+
use PHPCR\Shell\Console\Helper\PhpcrHelper;
2825

29-
use PHPCR\Shell\Subscriber;
3026
use PHPCR\Shell\Event;
31-
use PHPCR\Shell\PhpcrSession;
32-
use Symfony\Component\EventDispatcher\EventDispatcher;
33-
use PHPCR\Shell\Event\PhpcrShellEvents;
3427
use PHPCR\Shell\Event\ApplicationInitEvent;
28+
use PHPCR\Shell\Event\PhpcrShellEvents;
29+
use PHPCR\Shell\PhpcrSession;
30+
use PHPCR\Shell\Subscriber;
3531

3632
/**
3733
* Main application for PHPCRSH
@@ -41,11 +37,8 @@
4137
class ShellApplication extends Application
4238
{
4339
/**
44-
* @var \PHPCR\TransportInterface[]
45-
*/
46-
protected $transports;
47-
48-
/**
40+
* True when application has been initialized once
41+
*
4942
* @var boolean
5043
*/
5144
protected $initialized;
@@ -56,18 +49,16 @@ class ShellApplication extends Application
5649
protected $sessionInput;
5750

5851
/**
59-
* @var SessionInterface
52+
* Constructor - name and version inherited from SessionApplication
53+
*
54+
* {@inheritDoc}
6055
*/
61-
private $session;
62-
6356
public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
6457
{
6558
parent::__construct($name, $version);
66-
6759
$this->dispatcher = new EventDispatcher();
6860
}
6961

70-
7162
/**
7263
* The SessionInput is the input used to intialize the shell.
7364
* It contains the connection parameters.
@@ -94,9 +85,6 @@ public function init()
9485
);
9586
}
9687

97-
98-
$this->initializeTransports();
99-
$this->initSession();
10088
$this->registerHelpers();
10189
$this->registerCommands();
10290
$this->registerEventListeners();
@@ -112,16 +100,18 @@ public function init()
112100
*/
113101
private function registerHelpers()
114102
{
103+
$phpcrHelper = new PhpcrHelper($this->sessionInput);
104+
115105
$helpers = array(
116106
new ConfigHelper(),
117-
new EditorHelper($this->getSession()),
118-
new NodeHelper($this->getSession()),
119-
new PathHelper($this->getSession()),
107+
new EditorHelper(),
108+
new NodeHelper(),
109+
new PathHelper(),
120110
new PhpcrConsoleDumperHelper(),
121-
new PhpcrHelper($this->getSession()),
122-
new RepositoryHelper($this->getSession()->getRepository()),
111+
new RepositoryHelper($phpcrHelper),
123112
new ResultFormatterHelper(),
124113
new TextHelper(),
114+
$phpcrHelper,
125115
);
126116

127117
foreach ($helpers as $helper) {

src/PHPCR/Shell/Console/Command/Phpcr/NodeRemoveCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected function configure()
1919
$this->setDescription('Remove the node at path');
2020
$this->addArgument('path', InputArgument::REQUIRED, 'Path of node');
2121
$this->setHelp(<<<HERE
22-
Remove the current node
22+
Remove the node at the given path.
2323
HERE
2424
);
2525
}

src/PHPCR/Shell/Console/Command/Phpcr/SessionLoginCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ public function execute(InputInterface $input, OutputInterface $output)
2626
$username = $input->getArgument('userId');
2727
$password = $input->getArgument('password');
2828
$workspaceName = $input->getArgument('workspaceName');
29-
$this->getApplication()->relogin($username, $password, $workspaceName);
29+
$this->getHelper('phpcr')->relogin($username, $password, $workspaceName);
3030
}
3131
}

src/PHPCR/Shell/Console/Command/Phpcr/WorkspaceUseCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ protected function configure()
2424
public function execute(InputInterface $input, OutputInterface $output)
2525
{
2626
$workspaceName = $input->getArgument('name');
27-
$this->getApplication()->changeWorkspace($workspaceName);
27+
$this->getHelper('phpcr')->changeWorkspace($workspaceName);
2828
}
2929
}

src/PHPCR/Shell/Console/Helper/NodeHelper.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Symfony\Component\Filesystem\Filesystem;
66
use Symfony\Component\Console\Helper\Helper;
77
use PHPCR\NodeInterface;
8-
use PHPCR\SessionInterface;
98

109
/**
1110
* Helper for nodes
@@ -14,13 +13,6 @@
1413
*/
1514
class NodeHelper extends Helper
1615
{
17-
protected $session;
18-
19-
public function __construct(SessionInterface $session)
20-
{
21-
$this->session = $session;
22-
}
23-
2416
public function nodeHasMixinType($node, $mixinTypeName)
2517
{
2618
$mixinTypes = $node->getMixinNodeTypes();

src/PHPCR/Shell/Console/Helper/PhpcrHelper.php

Lines changed: 79 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace PHPCR\Shell\Console\Helper;
44

55
use PHPCR\Shell\PhpcrSession;
6+
use Symfony\Component\Console\Helper\Helper;
7+
use Symfony\Component\Console\Input\InputInterface;
8+
use PHPCR\SimpleCredentials;
69

710
/**
811
* Helper for managing PHPCR sessions
@@ -32,6 +35,13 @@ class PhpcrHelper extends Helper
3235
*/
3336
protected $transports = array();
3437

38+
/**
39+
* Lazy initialize PHPCR session
40+
*
41+
* @var boolean
42+
*/
43+
protected $initialized = false;
44+
3545
/**
3646
* @param InputInterface $sessionInput
3747
*/
@@ -48,8 +58,19 @@ public function getName()
4858
return 'phpcr';
4959
}
5060

61+
private function init()
62+
{
63+
if (false === $this->initialized) {
64+
$this->initializeTransports();
65+
$this->initSession();
66+
$this->initialized = true;
67+
}
68+
}
69+
5170
/**
5271
* Initialize the PHPCR session
72+
*
73+
* @access private
5374
*/
5475
private function initSession()
5576
{
@@ -62,20 +83,60 @@ private function initSession()
6283

6384
$session = $repository->login($credentials, $this->sessionInput->getOption('phpcr-workspace'));
6485

86+
// if you are wondering wtf here -- we wrap the PhpcrSession
6587
if (!$this->session) {
6688
$this->session = new PhpcrSession($session);
6789
} else {
6890
$this->session->setPhpcrSession($session);
6991
}
7092
}
7193

94+
/**
95+
* Return the transport as defined in the sessionInput
96+
*
97+
* @access private
98+
*/
99+
private function getTransport()
100+
{
101+
$transportName = $this->sessionInput->getOption('transport');
102+
103+
if (!isset($this->transports[$transportName])) {
104+
throw new \InvalidArgumentException(sprintf(
105+
'Unknown transport "%s", I have "%s"',
106+
$transportName, implode(', ', array_keys($this->transports))
107+
));
108+
}
109+
110+
$transport = $this->transports[$transportName];
111+
112+
return $transport;
113+
}
114+
115+
/**
116+
* Initialize the supported transports.
117+
*
118+
* @access private
119+
*/
120+
private function initializeTransports()
121+
{
122+
$transports = array(
123+
new \PHPCR\Shell\Transport\DoctrineDbal($this->sessionInput),
124+
new \PHPCR\Shell\Transport\Jackrabbit($this->sessionInput),
125+
);
126+
127+
foreach ($transports as $transport) {
128+
$this->transports[$transport->getName()] = $transport;;
129+
}
130+
}
131+
72132
/**
73133
* Change the current workspace
74134
*
75135
* @param string $workspaceName
76136
*/
77137
public function changeWorkspace($workspaceName)
78138
{
139+
$this->init();
79140
$this->session->logout();
80141
$this->sessionInput->setOption('phpcr-workspace', $workspaceName);
81142
$this->initSession($this->sessionInput);
@@ -90,49 +151,42 @@ public function changeWorkspace($workspaceName)
90151
*/
91152
public function relogin($username, $password, $workspaceName = null)
92153
{
93-
$this->session->logout();
154+
if ($this->session) {
155+
$this->session->logout();
156+
}
157+
94158
$this->sessionInput->setOption('phpcr-username', $username);
95159
$this->sessionInput->setOption('phpcr-password', $password);
96160

97161
if ($workspaceName) {
98162
$this->sessionInput->setOption('phpcr-workspace', $workspaceName);
99163
}
100-
$this->initSession($this->sessionInput);
164+
165+
$this->init();
101166
}
102167

103168
/**
104-
* Return the transport as defined in the sessionInput
169+
* Return the current PHPCR session. We lazy call
170+
* initialize.
171+
*
172+
* @return \PHPCR\SessionInterface
105173
*/
106-
private function getTransport()
174+
public function getSession()
107175
{
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];
176+
$this->init();
118177

119-
return $transport;
178+
return $this->session;
120179
}
121180

122181
/**
123-
* Initialize the supported transports.
182+
* Proxy for getting the repository (make mocking easier)
124183
*
125-
* @todo Do this in a lazy way
184+
* @return \PHPCR\RepositoryInterface
126185
*/
127-
private function initializeTransports()
186+
public function getRepository()
128187
{
129-
$transports = array(
130-
new \PHPCR\Shell\Transport\DoctrineDbal($this->sessionInput),
131-
new \PHPCR\Shell\Transport\Jackrabbit($this->sessionInput),
132-
);
188+
$this->init();
133189

134-
foreach ($transports as $transport) {
135-
$this->transports[$transport->getName()] = $transport;;
136-
}
190+
return $this->session->getRepository();
137191
}
138192
}

src/PHPCR/Shell/Console/Helper/RepositoryHelper.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@
88
class RepositoryHelper extends Helper
99
{
1010
/**
11-
* @var RepositoryInterface
11+
* @var PhpcrHelper
1212
*/
13-
protected $repository;
13+
protected $phpcrHelper;
1414

1515
/**
1616
* @var array
1717
*/
1818
protected $descriptors;
1919

20-
public function __construct(RepositoryInterface $repository)
20+
public function __construct(PhpcrHelper $phpcrHelper)
2121
{
22-
$this->repository = $repository;
22+
$this->phpcrHelper = $phpcrHelper;
2323
}
2424

2525
/**
26-
* Return true if the repository supports the given descriptor
26+
* Return true if the phpcrHelper supports the given descriptor
2727
* which relates to a descriptor key
2828
*
2929
* @param string $descriptor
@@ -62,8 +62,10 @@ public function hasDescriptor($descriptor, $value = null)
6262
private function loadDescriptors()
6363
{
6464
if (null === $this->descriptors) {
65-
foreach ($this->repository->getDescriptorKeys() as $key) {
66-
$this->descriptors[$key] = $this->repository->getDescriptor($key);
65+
$repository = $this->phpcrHelper->getRepository();
66+
67+
foreach ($repository->getDescriptorKeys() as $key) {
68+
$this->descriptors[$key] = $repository->getDescriptor($key);
6769
}
6870
}
6971
}

0 commit comments

Comments
 (0)