Skip to content

Commit 5003537

Browse files
committed
Encapsualte instantiation in PhpcrShell class
1 parent c48d0c2 commit 5003537

File tree

6 files changed

+91
-36
lines changed

6 files changed

+91
-36
lines changed

bin/phpcrsh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ if (file_exists($file)) {
1212
exit(1);
1313
}
1414

15-
$shell = new PHPCR\Shell\Console\Application\SessionApplication();
15+
$shell = \PHPCR\Shell\PhpcrShell::createShell();
1616
$shell->run();

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

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPCR\Shell\DependencyInjection\Container;
66
use PHPCR\Shell\Console\Helper\PhpcrHelper;
7+
use PHPCR\Shell\PhpcrShell;
78

89
/**
910
* Subclass of the full ShellApplication for running as an EmbeddedApplication
@@ -13,35 +14,20 @@
1314
*/
1415
class EmbeddedApplication extends ShellApplication
1516
{
16-
/**
17-
* @deprecated remove after DoctrinePhpcrBundle is upgraded
18-
*/
19-
const MODE_COMMAND = Container::MODE_EMBEDDED_COMMAND;
20-
21-
/**
22-
* @deprecated remove after DoctrinePhpcrBundle is upgraded
23-
*/
24-
const MODE_SHELL = Container::MODE_EMBEDDED_SHELL;
25-
2617
protected $mode;
2718

2819
/**
29-
* The $mode can be one of EmbeddedApplication::MODE_SHELL or EmbeddedApplication::MODE_COMMAND.
20+
* The $mode can be one of PhpcrShell::MODE_SHELL or PhpcrShell::MODE_COMMAND.
3021
*
3122
* - Shell mode initializes the whole environement
3223
* - Command mode initailizes only enough to run commands
3324
*
3425
* @param string $mode
3526
*/
36-
public function __construct($mode)
27+
public function __construct(Container $container)
3728
{
38-
$this->mode = $mode;
39-
$container = new Container($this->mode);
40-
parent::__construct($container, SessionApplication::APP_NAME, SessionApplication::APP_VERSION);
29+
parent::__construct($container);
4130
$this->setAutoExit(false);
42-
43-
// @deprecated This will be removed in 1.0
44-
$this->getHelperSet()->set(new PhpcrHelper($container->get('phpcr.session_manager')));
4531
}
4632

4733
/**
@@ -51,7 +37,7 @@ public function init()
5137
{
5238
$this->registerPhpcrCommands();
5339

54-
if ($this->container->getMode() === self::MODE_SHELL) {
40+
if ($this->container->getMode() === PhpcrShell::MODE_EMBEDDED_SHELL) {
5541
$this->registerShellCommands();
5642
}
5743
}
@@ -61,6 +47,6 @@ public function init()
6147
*/
6248
protected function getDefaultCommand()
6349
{
64-
return $this->mode === self::MODE_SHELL ? 'shell:path:show' : 'list';
50+
return $this->mode === PhpcrShell::MODE_EMBEDDED_SHELL ? 'shell:path:show' : 'list';
6551
}
6652
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Symfony\Component\Console\Input\InputInterface;
88
use PHPCR\Shell\Console\Command\ShellCommand;
99
use PHPCR\Shell\DependencyInjection\Container;
10+
use PHPCR\Shell\PhpcrShell;
1011

1112
/**
1213
* This application wraps a single command which accepts
@@ -16,9 +17,6 @@
1617
*/
1718
class SessionApplication extends BaseApplication
1819
{
19-
const APP_NAME = 'PHPCRSH';
20-
const APP_VERSION = '1.0.0-alpha6';
21-
2220
protected $shellApplication;
2321

2422
/**
@@ -27,10 +25,10 @@ class SessionApplication extends BaseApplication
2725
*/
2826
public function __construct()
2927
{
30-
parent::__construct(self::APP_NAME, self::APP_VERSION);
28+
parent::__construct(PhpcrShell::APP_NAME, PhpcrShell::APP_VERSION);
3129

3230
$container = new Container();
33-
$this->shellApplication = $container->get('console.application.shell');
31+
$this->shellApplication = $container->get('application');
3432

3533
$command = new ShellCommand($this->shellApplication);
3634
$command->setApplication($this);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use PHPCR\Shell\Event\PhpcrShellEvents;
2121
use PHPCR\Shell\Console\Command\Phpcr\PhpcrShellCommand;
2222
use PHPCR\Shell\Config\Profile;
23+
use PHPCR\Shell\PhpcrShell;
2324

2425
/**
2526
* Main application for PHPCRSH
@@ -50,7 +51,7 @@ class ShellApplication extends Application
5051
*/
5152
public function __construct($container)
5253
{
53-
parent::__construct(SessionApplication::APP_NAME, SessionApplication::APP_VERSION);
54+
parent::__construct(PhpcrShell::APP_NAME, PhpcrShell::APP_VERSION);
5455
$this->dispatcher = $container->get('event.dispatcher') ? : new EventDispatcher();
5556
$this->setDispatcher($this->dispatcher);
5657
$this->container = $container;

src/PHPCR/Shell/DependencyInjection/Container.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@
44

55
use Symfony\Component\DependencyInjection\Reference;
66
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use PHPCR\Shell\PhpcrShell;
78

89
class Container extends ContainerBuilder
910
{
10-
const MODE_EMBEDDED_SHELL = 'shell';
11-
const MODE_EMBEDDED_COMMAND = 'command';
12-
const MODE_STANDALONE = 'standalon';
13-
1411
protected $mode;
1512

1613
/**
@@ -22,7 +19,7 @@ class Container extends ContainerBuilder
2219
'transport.transport.fs' => 'PHPCR\Shell\Transport\Transport\JackalopeFs',
2320
);
2421

25-
public function __construct($mode = self::MODE_STANDALONE)
22+
public function __construct($mode = PhpcrShell::MODE_STANDALONE)
2623
{
2724
parent::__construct();
2825
$this->mode = $mode;
@@ -97,7 +94,7 @@ public function registerPhpcr()
9794

9895
public function registerEvent()
9996
{
100-
if ($this->mode === self::MODE_STANDALONE) {
97+
if ($this->mode === PhpcrShell::MODE_STANDALONE) {
10198
$this->register(
10299
'event.subscriber.profile_loader',
103100
'PHPCR\Shell\Subscriber\ProfileLoaderSubscriber'
@@ -148,10 +145,16 @@ public function registerEvent()
148145

149146
public function registerConsole()
150147
{
151-
$this->register('console.application.shell', 'PHPCR\Shell\Console\Application\ShellApplication')
152-
->addArgument(new Reference('container'));
148+
if ($this->mode === PhpcrShell::MODE_STANDALONE) {
149+
$this->register('application', 'PHPCR\Shell\Console\Application\ShellApplication')
150+
->addArgument(new Reference('container'));
151+
} else {
152+
$this->register('application', 'PHPCR\Shell\Console\Application\EmbeddedApplication')
153+
->addArgument(new Reference('container'));
154+
}
155+
153156
$this->register('console.input.autocomplete', 'PHPCR\Shell\Console\Input\AutoComplete')
154-
->addArgument(new Reference('console.application.shell'))
157+
->addArgument(new Reference('application'))
155158
->addArgument(new Reference('phpcr.session'));
156159
}
157160

src/PHPCR/Shell/PhpcrShell.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace PHPCR\Shell;
4+
5+
use Symfony\Component\Console\Input\InputInterface;
6+
use Symfony\Component\Console\Output\OutputInterface;
7+
use PHPCR\Shell\Console\Application\SessionApplication;
8+
use PHPCR\Shell\PhpcrShell;
9+
use PHPCR\SessionInterface;
10+
use PHPCR\Shell\DependencyInjection\Container;
11+
use PHPCR\Shell\Console\Application\Shell;
12+
13+
/**
14+
* PHPCRShell entry point
15+
*
16+
* @author Daniel Leech <daniel@dantleech.com>
17+
*/
18+
class PhpcrShell
19+
{
20+
const APP_NAME = 'PHPCRSH';
21+
const APP_VERSION = 'dev-master';
22+
23+
const MODE_EMBEDDED_SHELL = 'shell';
24+
const MODE_EMBEDDED_COMMAND = 'command';
25+
const MODE_STANDALONE = 'standalon';
26+
27+
/**
28+
* Create a new embedded shell
29+
*
30+
* @param SessionInterface $session
31+
* @return Shell
32+
*/
33+
public static function createEmbeddedShell(SessionInterface $session)
34+
{
35+
$container = new Container(self::MODE_EMBEDDED_SHELL);
36+
$container->get('phpcr.session_manager')->setSession(new PhpcrSession($session));
37+
$application = $container->get('application');
38+
39+
return new Shell($application);
40+
}
41+
42+
/**
43+
* Create a new (non-interactive) embedded application (e.g. for running
44+
* single commands)
45+
*
46+
* @param SessionInterface $session
47+
* @return EmbeddedApplication
48+
*/
49+
public static function createEmbeddedApplication(SessionInterface $session)
50+
{
51+
$container = new Container(self::MODE_EMBEDDED_COMMAND);
52+
$container->get('phpcr.session_manager')->setSession(new PhpcrSession($session));
53+
$application = $container->get('application');
54+
55+
return $application;
56+
}
57+
58+
/**
59+
* Create a new standalone shell application
60+
*
61+
* @return SessionApplication
62+
*/
63+
public static function createShell()
64+
{
65+
return new SessionApplication();
66+
}
67+
}

0 commit comments

Comments
 (0)