Skip to content

Commit 9330dab

Browse files
committed
Refactored connection logic to use event
1 parent abb898d commit 9330dab

23 files changed

+388
-173
lines changed

spec/PHPCR/Shell/Config/ProfileSpec.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,22 @@ function it_is_initializable()
2020
$this->shouldHaveType('PHPCR\Shell\Config\Profile');
2121
}
2222

23-
function it_has_a_method_to_set_the_transport_config(
24-
TransportConfig $transportConfig
23+
function it_has_a_method_to_set_config(
2524
)
2625
{
27-
$this->setTransportConfig($transportConfig);
26+
$this->set('transport', array());
27+
}
28+
29+
function it_has_a_method_to_get_config()
30+
{
31+
$this->set('transport', array(
32+
'foo' => 'bar'
33+
));
34+
35+
$this->get('transport')->shouldReturn(array(
36+
'foo' => 'bar'
37+
));
38+
39+
$this->get('transport', 'foo')->shouldReturn('bar');
2840
}
2941
}

spec/PHPCR/Shell/Console/Helper/PhpcrHelperSpec.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
use PhpSpec\ObjectBehavior;
66
use Prophecy\Argument;
7-
use Symfony\Component\Console\Input\InputInterface;
7+
use PHPCR\Shell\Config\Profile;
8+
use PHPCR\Shell\Transport\TransportRegistryInterface;
89

910
class PhpcrHelperSpec extends ObjectBehavior
1011
{
1112
function let(
12-
InputInterface $input
13+
Profile $profile,
14+
TransportRegistryInterface $transportRegistry
1315
) {
14-
$this->beConstructedWith($input);
16+
$this->beConstructedWith($transportRegistry, $profile);
1517
}
1618

1719
function it_is_initializable()

spec/PHPCR/Shell/Console/Wizard/ProfileWizardSpec.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Symfony\Component\Console\Output\OutputInterface;
1212
use PHPCR\Shell\Transport\SessionConfig;
1313
use PHPCR\Shell\Config\Profile;
14-
use PHPCR\Shell\Transport\TransportConfig;
1514

1615
class ProfileWizardSpec extends ObjectBehavior
1716
{
@@ -66,7 +65,6 @@ function it_should_create_a_new_profile(
6665
Profile $profile,
6766
ProfileHelper $profileHelper,
6867
DialogHelper $dialogHelper,
69-
TransportConfig $transportConfig,
7068
WizardInterface $connectionWizard
7169
)
7270
{
@@ -77,6 +75,7 @@ function it_should_create_a_new_profile(
7775
$dialogHelper->ask($output, 'Enter name for new profile: ')->willReturn('test');
7876
$connectionWizard->run($input, $output)->willReturn(array());
7977
$profileHelper->createNewProfile('test')->willReturn($profile);
78+
$profile->set('transport', Argument::type('array'))->shouldBeCalled();
8079

8180
$this->run($input, $output)->shouldReturn($profile);
8281
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace spec\PHPCR\Shell\Event;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Prophecy\Argument;
7+
use PHPCR\Shell\Config\Profile;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
11+
class ProfileInitEventSpec extends ObjectBehavior
12+
{
13+
function it_is_initializable()
14+
{
15+
$this->shouldHaveType('PHPCR\Shell\Event\ProfileInitEvent');
16+
}
17+
18+
function let(
19+
Profile $profile,
20+
InputInterface $input,
21+
OutputInterface $output
22+
)
23+
{
24+
$this->beConstructedWith(
25+
$profile, $input, $output
26+
);
27+
}
28+
29+
function it_should_have_getters(
30+
Profile $profile,
31+
InputInterface $input,
32+
OutputInterface $output
33+
)
34+
{
35+
$this->getProfile()->shouldReturn($profile);
36+
$this->getInput()->shouldReturn($input);
37+
$this->getOutput()->shouldReturn($output);
38+
}
39+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace spec\PHPCR\Shell\Transport\Transport;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Prophecy\Argument;
7+
8+
class DoctrineDbalSpec extends ObjectBehavior
9+
{
10+
function it_is_initializable()
11+
{
12+
$this->shouldHaveType('PHPCR\Shell\Transport\Transport\DoctrineDbal');
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace spec\PHPCR\Shell\Transport\Transport;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Prophecy\Argument;
7+
8+
class JackrabbitSpec extends ObjectBehavior
9+
{
10+
function it_is_initializable()
11+
{
12+
$this->shouldHaveType('PHPCR\Shell\Transport\Transport\Jackrabbit');
13+
}
14+
}

spec/PHPCR/Shell/Transport/TransportConfigSpec.php

Lines changed: 0 additions & 26 deletions
This file was deleted.

spec/PHPCR/Shell/Transport/TransportFactorySpec.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use PhpSpec\ObjectBehavior;
66
use Prophecy\Argument;
7-
use PHPCR\Shell\Transport\Transport\TransportInterface;
7+
use PHPCR\Shell\Transport\TransportInterface;
88

99
class TransportFactorySpec extends ObjectBehavior
1010
{

src/PHPCR/Shell/Config/Profile.php

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,83 @@
99
*/
1010
class Profile
1111
{
12-
protected $profile = array();
12+
protected $profile = array(
13+
'transport' => array(),
14+
'phpcr' => array(),
15+
);
16+
1317
protected $name;
1418

15-
public function __construct($name)
19+
public function __construct($name = null)
1620
{
1721
$this->name = $name;
1822
}
1923

20-
public function setTransportConfig($transportConfig)
24+
protected function validateDomain($domain)
25+
{
26+
if (!array_key_exists($domain, $this->profile)) {
27+
throw new \InvalidArgumentException(sprintf(
28+
'Unknown profile domain "%s", can only use one of: %s',
29+
$domain, implode(', ', array_keys($this->profile))
30+
));
31+
}
32+
}
33+
34+
/**
35+
* Set a domain configuration
36+
*
37+
* @param string $domain
38+
* @param array $value
39+
*
40+
* @throws \InvalidArgumentException
41+
*/
42+
public function set($domain, $key, $value = null)
43+
{
44+
$this->validateDomain($domain);
45+
if (null !== $value) {
46+
$this->profile[$domain][$key] = $value;
47+
} else {
48+
$this->profile[$domain] = $key;
49+
}
50+
}
51+
52+
/**
53+
* Get a domain configuration
54+
*
55+
* @param string $domain
56+
*
57+
* @return array
58+
*/
59+
public function get($domain, $key = null)
2160
{
22-
$this->profile['transport'] = $transportConfig;
61+
$this->validateDomain($domain);
62+
63+
if (null === $key) {
64+
return $this->profile[$domain];
65+
}
66+
67+
if (!isset($this->profile[$domain][$key])) {
68+
throw new \InvalidArgumentException(sprintf(
69+
'Unknown key "%s" for profile domain "%s"',
70+
$key, $domain
71+
));
72+
}
73+
74+
return $this->profile[$domain][$key];
2375
}
2476

77+
/**
78+
* Return the name of this profile
79+
*
80+
* @return string $name
81+
*/
2582
public function getName()
2683
{
2784
return $this->name;
2885
}
86+
87+
public function setName($name)
88+
{
89+
$this->name = $name;
90+
}
2991
}

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

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
use PHPCR\Shell\Event\PhpcrShellEvents;
2828
use PHPCR\Shell\Subscriber;
2929
use PHPCR\Shell\Console\Command\Phpcr\PhpcrShellCommand;
30+
use PHPCR\Shell\Config\Profile;
31+
use PHPCR\Shell\Transport\TransportRegistry;
3032

3133
/**
3234
* Main application for PHPCRSH
@@ -43,9 +45,9 @@ class ShellApplication extends Application
4345
protected $initialized;
4446

4547
/**
46-
* @var \Symfony\Component\Console\Input\InputInterface
48+
* @var boolean
4749
*/
48-
protected $sessionInput;
50+
protected $showUnsupported = false;
4951

5052
/**
5153
* Constructor - name and version inherited from SessionApplication
@@ -55,18 +57,39 @@ class ShellApplication extends Application
5557
public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
5658
{
5759
parent::__construct($name, $version);
60+
$this->profile = new Profile();
5861
$this->dispatcher = new EventDispatcher();
62+
$this->transportRegistry = new TransportRegistry();
63+
$this->registerTransports();
64+
$this->registerEventListeners();
5965
}
6066

6167
/**
62-
* The SessionInput is the input used to intialize the shell.
63-
* It contains the connection parameters.
68+
* Initialize the supported transports.
6469
*
65-
* @param \Symfony\Component\Console\Input\InputInterface $input
70+
* @access private
6671
*/
67-
public function setSessionInput(InputInterface $input)
72+
protected function registerTransports()
6873
{
69-
$this->sessionInput = $input;
74+
$transports = array(
75+
new \PHPCR\Shell\Transport\Transport\DoctrineDbal($this->profile),
76+
new \PHPCR\Shell\Transport\Transport\Jackrabbit($this->profile),
77+
);
78+
79+
foreach ($transports as $transport) {
80+
$this->transportRegistry->register($transport);
81+
}
82+
}
83+
84+
/**
85+
* If true, show all commands, even if they are unsupported by the
86+
* transport.
87+
*
88+
* @param boolean $boolean
89+
*/
90+
public function setShowUnsupported($boolean)
91+
{
92+
$this->showUnsupported = $boolean;
7093
}
7194

7295
/**
@@ -82,15 +105,8 @@ public function init()
82105
return;
83106
}
84107

85-
if (null === $this->sessionInput) {
86-
throw new \RuntimeException(
87-
'sessionInput has not been set.'
88-
);
89-
}
90-
91108
$this->registerHelpers();
92109
$this->registerCommands();
93-
$this->registerEventListeners();
94110

95111
$event = new ApplicationInitEvent($this);
96112
$this->dispatcher->dispatch(PhpcrShellEvents::APPLICATION_INIT, $event);
@@ -103,7 +119,7 @@ public function init()
103119
*/
104120
private function registerHelpers()
105121
{
106-
$phpcrHelper = new PhpcrHelper($this->sessionInput);
122+
$phpcrHelper = new PhpcrHelper($this->transportRegistry, $this->profile);
107123

108124
$helpers = array(
109125
new ConfigHelper(),
@@ -207,9 +223,11 @@ private function registerCommands()
207223

208224
private function registerEventListeners()
209225
{
226+
$this->dispatcher->addSubscriber(new Subscriber\ProfileFromSessionInputSubscriber());
227+
210228
$this->dispatcher->addSubscriber(new Subscriber\ConfigInitSubscriber());
211229
$this->dispatcher->addSubscriber(new Subscriber\ExceptionSubscriber());
212-
$this->dispatcher->addSubscriber(new Subscriber\AliasSubscriber($this->getHelperSet()->get('config')));
230+
$this->dispatcher->addSubscriber(new Subscriber\AliasSubscriber($this->getHelperSet()));
213231
}
214232

215233
/**
@@ -286,13 +304,17 @@ public function renderException($exception, $output)
286304
public function add(Command $command)
287305
{
288306
if ($command instanceof PhpcrShellCommand) {
289-
$showUnsupported = $this->sessionInput->getOption('unsupported');
290-
291-
if ($showUnsupported || $command->isSupported($this->getHelperSet()->get('repository'))) {
307+
if ($this->showUnsupported || $command->isSupported($this->getHelperSet()->get('repository'))) {
292308
parent::add($command);
293309
}
294310
} else {
295311
parent::add($command);
296312
}
297313
}
314+
315+
public function dispatchProfileInitEvent(InputInterface $sessionInput, OutputInterface $output)
316+
{
317+
$event = new Event\ProfileInitEvent($this->profile, $sessionInput, $output);
318+
$this->dispatcher->dispatch(PhpcrShellEvents::PROFILE_INIT, $event);
319+
}
298320
}

0 commit comments

Comments
 (0)