From 100aeb402bcc49741076b0b52c1a4b96011c9495 Mon Sep 17 00:00:00 2001 From: dantleech Date: Sun, 23 Mar 2014 13:01:07 +0100 Subject: [PATCH] Change workspace command and related --- .../Console/Application/ShellApplication.php | 45 ++++++++++++++----- .../Console/Command/WorkspaceListCommand.php | 12 +++-- .../Console/Command/WorkspaceUseCommand.php | 29 ++++++++++++ src/PHPCR/Shell/PhpcrSession.php | 21 ++++++--- 4 files changed, 87 insertions(+), 20 deletions(-) create mode 100644 src/PHPCR/Shell/Console/Command/WorkspaceUseCommand.php diff --git a/src/PHPCR/Shell/Console/Application/ShellApplication.php b/src/PHPCR/Shell/Console/Application/ShellApplication.php index 937edad9..c81e8982 100644 --- a/src/PHPCR/Shell/Console/Application/ShellApplication.php +++ b/src/PHPCR/Shell/Console/Application/ShellApplication.php @@ -57,6 +57,7 @@ use PHPCR\Shell\Console\Command\WorkspaceNamespaceListCommand; use PHPCR\Shell\Console\Command\WorkspaceNamespaceRegisterCommand; use PHPCR\Shell\Console\Command\WorkspaceNamespaceUnregisterCommand; +use PHPCR\Shell\Console\Command\WorkspaceUseCommand; use PHPCR\Shell\Console\Command\NodeTypeShowCommand; use PHPCR\Shell\Console\Helper\EditorHelper; use PHPCR\Shell\Console\Command\NodeTypeEditCommand; @@ -91,10 +92,14 @@ class ShellApplication extends Application { protected $transports; - protected $credentials; protected $initialized; protected $sessionInput; + /** + * @var SessionInterface + */ + private $session; + /** * Input from the shell command, containing the connection * parameters etc. @@ -156,6 +161,7 @@ public function init() $this->add(new WorkspaceNamespaceListCommand()); $this->add(new WorkspaceNamespaceRegisterCommand()); $this->add(new WorkspaceNamespaceUnregisterCommand()); + $this->add(new WorkspaceUseCommand()); $this->add(new NodeTypeShowCommand()); $this->add(new NodeTypeEditCommand()); $this->add(new NodeTypeUnregisterCommand()); @@ -219,31 +225,46 @@ public function init() ->setName('workspace-purge') ); - $session = $this->getSession($this->sessionInput); + $this->initSession(); - $this->getHelperSet()->set(new EditorHelper($session)); + $this->getHelperSet()->set(new EditorHelper($this->session)); $this->getHelperSet()->set(new PhpcrConsoleDumperHelper()); - $this->getHelperSet()->set(new PhpcrHelper($session)); + $this->getHelperSet()->set(new PhpcrHelper($this->session)); $this->getHelperSet()->set(new ResultFormatterHelper()); $this->getHelperSet()->set(new TextHelper()); - $this->getHelperSet()->set(new NodeHelper($session)); + $this->getHelperSet()->set(new NodeHelper($this->session)); $this->initialized = true; } - private function getSession($input) + private function initSession() { - $transport = $this->getTransport($input); + $transport = $this->getTransport($this->sessionInput); $repository = $transport->getRepository(); $credentials = new SimpleCredentials( - $input->getOption('phpcr-username'), - $input->getOption('phpcr-password') + $this->sessionInput->getOption('phpcr-username'), + $this->sessionInput->getOption('phpcr-password') ); - $session = $repository->login($credentials, $input->getOption('phpcr-workspace')); - $session = new PhpcrSession($session); + $session = $repository->login($credentials, $this->sessionInput->getOption('phpcr-workspace')); - return $session; + if (!$this->session) { + $this->session = new PhpcrSession($session); + } else { + $this->session->setPhpcrSession($session); + } + } + + /** + * Change the current workspace + * + * @param string $workspaceName + */ + public function changeWorkspace($workspaceName) + { + $this->session->logout(); + $this->sessionInput->setOption('phpcr-workspace', $workspaceName); + $this->initSession($this->sessionInput); } private function getTransport(InputInterface $input) diff --git a/src/PHPCR/Shell/Console/Command/WorkspaceListCommand.php b/src/PHPCR/Shell/Console/Command/WorkspaceListCommand.php index 3311e6a6..d0a57935 100644 --- a/src/PHPCR/Shell/Console/Command/WorkspaceListCommand.php +++ b/src/PHPCR/Shell/Console/Command/WorkspaceListCommand.php @@ -17,6 +17,8 @@ protected function configure() $this->setHelp(<<getHelper('phpcr')->getSession(); - $workspaces = $session->getWorkspace()->getAccessibleWorkspaceNames(); + $workspace = $session->getWorkspace(); + $availableWorkspaces = $workspace->getAccessibleWorkspaceNames(); $table = clone $this->getHelper('table'); $table->setHeaders(array('Name')); - foreach ($workspaces as $workspace) { - $table->addRow(array($workspace)); + foreach ($availableWorkspaces as $availableWorkspace) { + if ($availableWorkspace == $workspace->getName()) { + $availableWorkspace .= ' *'; + } + $table->addRow(array($availableWorkspace)); } $table->render($output); diff --git a/src/PHPCR/Shell/Console/Command/WorkspaceUseCommand.php b/src/PHPCR/Shell/Console/Command/WorkspaceUseCommand.php new file mode 100644 index 00000000..97444bce --- /dev/null +++ b/src/PHPCR/Shell/Console/Command/WorkspaceUseCommand.php @@ -0,0 +1,29 @@ +setName('workspace:use'); + $this->setDescription('Change the current workspace'); + $this->addArgument('name', InputArgument::REQUIRED, 'Name of workspace to use'); + $this->addArgument('srcWorkspace', InputArgument::OPTIONAL, 'If specified, clone from this workspace'); + $this->setHelp(<<getArgument('name'); + $this->getApplication()->changeWorkspace($workspaceName); + } +} diff --git a/src/PHPCR/Shell/PhpcrSession.php b/src/PHPCR/Shell/PhpcrSession.php index 935fe7f3..d24ee9cc 100644 --- a/src/PHPCR/Shell/PhpcrSession.php +++ b/src/PHPCR/Shell/PhpcrSession.php @@ -12,6 +12,22 @@ class PhpcrSession implements SessionInterface protected $session; protected $cwd = '/'; + public function __construct(SessionInterface $session) + { + $this->session = $session; + } + + /** + * Allow underlying session to be changed + * For example when changing workspaces + * + * @param SessionInterface $session + */ + public function setPhpcrSession(SessionInterface $session) + { + $this->session = $session; + } + public function getCurrentNode() { return $this->getNode($this->getCwd()); @@ -121,11 +137,6 @@ public function getAbsPaths($paths) return $newPaths; } - public function __construct(SessionInterface $session) - { - $this->session = $session; - } - public function getRepository() { return $this->session->getRepository();