Skip to content

Commit

Permalink
automatically configure an admin session for every session
Browse files Browse the repository at this point in the history
use this admin session in all relevant commands
  • Loading branch information
lsmith77 committed Aug 3, 2015
1 parent a215676 commit f3c8c04
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 22 deletions.
6 changes: 3 additions & 3 deletions Command/DoctrineCommandHelper.php
Expand Up @@ -55,12 +55,12 @@ public static function setApplicationConnection(Application $application, $sessi
*
* @param Application $application
* @param string $sessionName
* @param Boolean $admin
*/
public static function setApplicationPHPCRSession(Application $application, $sessionName)
public static function setApplicationPHPCRSession(Application $application, $sessionName, $admin = false)
{
/** @var $registry ManagerRegistry */
$registry = $application->getKernel()->getContainer()->get('doctrine_phpcr');
$session = $registry->getConnection($sessionName);
$session = $admin ? $registry->getAdminConnection($sessionName): $registry->getConnection($sessionName);

$helperSet = $application->getHelperSet();
if (class_exists('Doctrine\ODM\PHPCR\Version')) {
Expand Down
3 changes: 2 additions & 1 deletion Command/NodeTypeListCommand.php
Expand Up @@ -52,7 +52,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommandHelper::setApplicationPHPCRSession(
$this->getApplication(),
$input->getOption('session')
$input->getOption('session'),
true
);

return parent::execute($input, $output);
Expand Down
3 changes: 2 additions & 1 deletion Command/NodeTypeRegisterCommand.php
Expand Up @@ -60,7 +60,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommandHelper::setApplicationPHPCRSession(
$this->getApplication(),
$input->getOption('session')
$input->getOption('session'),
true
);

$definitions = $input->getArgument('cnd-file');
Expand Down
3 changes: 2 additions & 1 deletion Command/WorkspaceCreateCommand.php
Expand Up @@ -51,7 +51,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommandHelper::setApplicationPHPCRSession(
$this->getApplication(),
$input->getOption('session')
$input->getOption('session'),
true
);

return parent::execute($input, $output);
Expand Down
3 changes: 2 additions & 1 deletion Command/WorkspaceDeleteCommand.php
Expand Up @@ -53,7 +53,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommandHelper::setApplicationPHPCRSession(
$this->getApplication(),
$input->getOption('session')
$input->getOption('session'),
true
);

return parent::execute($input, $output);
Expand Down
3 changes: 2 additions & 1 deletion Command/WorkspaceListCommand.php
Expand Up @@ -51,7 +51,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommandHelper::setApplicationPHPCRSession(
$this->getApplication(),
$input->getOption('session')
$input->getOption('session'),
true
);

return parent::execute($input, $output);
Expand Down
4 changes: 4 additions & 0 deletions DependencyInjection/Configuration.php
Expand Up @@ -69,6 +69,8 @@ private function addPHPCRSection(ArrayNodeDefinition $node)
'workspace',
'username',
'password',
'admin_username',
'admin_password',
'backend',
'options',
) as $key) {
Expand Down Expand Up @@ -106,6 +108,8 @@ private function getPHPCRSessionsNode()
->scalarNode('workspace')->isRequired()->cannotBeEmpty()->end()
->scalarNode('username')->defaultNull()->end()
->scalarNode('password')->defaultNull()->end()
->scalarNode('admin_username')->defaultNull()->end()
->scalarNode('admin_password')->defaultNull()->end()
->arrayNode('backend')
->addDefaultsIfNotSet()
->beforeNormalization()
Expand Down
35 changes: 24 additions & 11 deletions DependencyInjection/DoctrinePHPCRExtension.php
Expand Up @@ -20,6 +20,7 @@

namespace Doctrine\Bundle\PHPCRBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -156,14 +157,18 @@ private function sessionLoad($config, ContainerBuilder $container)
return;
}

if (empty($sessions[$config['default_session']])) {
throw new InvalidConfigurationException(sprintf("Default session is configured to '%s' which does not match any configured session name: %s", $config['default_session'], implode(', ', array_keys($sessions))));
}
$this->defaultSession = $config['default_session'];
$this->sessions = $sessions;
$container->setParameter('doctrine_phpcr.default_session', $config['default_session']);
$container->setAlias('doctrine_phpcr.session', $sessions[$config['default_session']]);
}

private function loadJackalopeSession(array $session, ContainerBuilder $container, $type)
private function loadJackalopeSession(array $session, ContainerBuilder $container, $type, $admin = false)
{
$serviceNamePrefix = $admin ? '.admin' : '';
$backendParameters = array();
switch ($type) {
case 'doctrinedbal':
Expand All @@ -176,7 +181,7 @@ private function loadJackalopeSession(array $session, ContainerBuilder $containe
: 'database_connection'
;
$connectionService = new Alias($connectionService, true);
$connectionAliasName = sprintf('doctrine_phpcr.jackalope_doctrine_dbal.%s_connection', $session['name']);
$connectionAliasName = sprintf('doctrine_phpcr.jackalope_doctrine_dbal%s.%s_connection', $serviceNamePrefix, $session['name']);
$container->setAlias($connectionAliasName, $connectionService);

$backendParameters['jackalope.doctrine_dbal_connection'] = new Reference($connectionAliasName);
Expand Down Expand Up @@ -262,20 +267,25 @@ private function loadJackalopeSession(array $session, ContainerBuilder $containe
$backendParameters['jackalope.logger'] = $logger;
}

$repositoryFactory = new DefinitionDecorator('doctrine_phpcr.jackalope.repository.factory.'.$type);
$factory = $container
->setDefinition(sprintf('doctrine_phpcr.jackalope.repository.%s', $session['name']), new DefinitionDecorator('doctrine_phpcr.jackalope.repository.factory.'.$type))
->setDefinition(sprintf('doctrine_phpcr.jackalope.repository%s.%s', $serviceNamePrefix, $session['name']), $repositoryFactory)
;
$factory->replaceArgument(0, $backendParameters);

$username = $admin && $session['admin_username'] ? $session['admin_username'] : $session['username'];
$password = $admin && $session['admin_password'] ? $session['admin_password'] : $session['password'];
$credentials = new DefinitionDecorator('doctrine_phpcr.credentials');
$credentialsServiceId = sprintf('doctrine_phpcr%s.%s_credentials', $serviceNamePrefix, $session['name']);
$container
->setDefinition(sprintf('doctrine_phpcr.%s_credentials', $session['name']), new DefinitionDecorator('doctrine_phpcr.credentials'))
->replaceArgument(0, $session['username'])
->replaceArgument(1, $session['password'])
->setDefinition($credentialsServiceId, $credentials)
->replaceArgument(0, $username)
->replaceArgument(1, $password)
;

// TODO: move the following code block back into the XML file when we drop support for symfony <2.6
$definition = new DefinitionDecorator('doctrine_phpcr.jackalope.session');
$factoryServiceId = sprintf('doctrine_phpcr.jackalope.repository.%s', $session['name']);
$factoryServiceId = sprintf('doctrine_phpcr%s.jackalope.repository.%s', $serviceNamePrefix, $session['name']);
if (method_exists($definition, 'setFactory')) {
$definition->setFactory(array(
new Reference($factoryServiceId),
Expand All @@ -286,17 +296,20 @@ private function loadJackalopeSession(array $session, ContainerBuilder $containe
$definition->setFactoryMethod('login');
}

$workspace = $admin ? null : $session['workspace'];
$definition
->replaceArgument(0, new Reference(sprintf('doctrine_phpcr.%s_credentials', $session['name'])))
->replaceArgument(1, $session['workspace'])
->replaceArgument(0, new Reference($credentialsServiceId))
->replaceArgument(1, $workspace)
;
$container->setDefinition($session['service_name'], $definition);

$serviceName = sprintf('doctrine_phpcr%s.%s_session', $serviceNamePrefix, $session['name']);
$container->setDefinition($serviceName, $definition);

foreach ($session['options'] as $key => $value) {
$definition->addMethodCall('setSessionOption', array($key, $value));
}

$eventManagerServiceId = sprintf('doctrine_phpcr.%s_session.event_manager', $session['name']);
$eventManagerServiceId = sprintf('doctrine_phpcr%s.%s_session.event_manager', $serviceNamePrefix, $session['name']);
$container->setDefinition($eventManagerServiceId, new DefinitionDecorator('doctrine_phpcr.session.event_manager'));
}

Expand Down
23 changes: 23 additions & 0 deletions ManagerRegistry.php
Expand Up @@ -45,4 +45,27 @@ public function getAliasNamespace($alias)

throw PHPCRException::unknownDocumentNamespace($alias);
}

/**
* Get the admin connection associated to the connection
*
* @param null $name
* @return object
*/
public function getAdminConnection($name = null)
{
if (null === $name) {
$name = $this->getDefaultConnectionName();
}

$serviceName = sprintf('doctrine_phpcr.admin.%s_session', $name);

$connections = $this->getConnectionNames();
if (!isset($connections[$name])) {
throw new \InvalidArgumentException(sprintf('Doctrine %s Connection named "%s" does not exist.', $this->getName(), $name));
}

return $this->getService($serviceName);
}

}
11 changes: 9 additions & 2 deletions OptionalCommand/Jackalope/InitDoctrineDbalCommand.php
Expand Up @@ -38,7 +38,7 @@ protected function configure()

$this
->setName('doctrine:phpcr:init:dbal')
->addOption('session', null, InputOption::VALUE_OPTIONAL, 'The session to use for this command', 'default')
->addOption('session', null, InputOption::VALUE_OPTIONAL, 'The session to use for this command')
;
}

Expand All @@ -47,7 +47,14 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommandHelper::setApplicationConnection($this->getApplication(), $input->getOption('session'));
$application = $this->getApplication();
$sessionName = $input->getOption('session');
if (empty($sessionName)) {
$container = $application->getKernel()->getContainer();
$sessionName = $container->getParameter('doctrine_phpcr.default_session');
}

DoctrineCommandHelper::setApplicationConnection($application, $sessionName);

parent::execute($input, $output);
}
Expand Down
2 changes: 2 additions & 0 deletions Tests/Resources/Fixtures/config/multiple.php
Expand Up @@ -21,6 +21,8 @@
'workspace' => 'website',
'username' => 'root',
'password' => 'root',
'admin_username' => 'admin',
'admin_password' => 'admin',
),
),
),
Expand Down
2 changes: 2 additions & 0 deletions Tests/Resources/Fixtures/config/multiple.xml
Expand Up @@ -13,6 +13,8 @@
workspace="website"
username="root"
password="root"
admin-username="admin"
admin-password="admin"
>
<backend type="jackrabbit" url="http://b"/>
</session>
Expand Down
3 changes: 2 additions & 1 deletion Tests/Resources/Fixtures/config/multiple.yml
Expand Up @@ -16,7 +16,8 @@ doctrine_phpcr:
workspace: website
username: "root"
password: "root"

admin_username: admin
admin_password: admin
odm:
auto_generate_proxy_classes: true
document_managers:
Expand Down
8 changes: 8 additions & 0 deletions Tests/Unit/DependencyInjection/ConfigurationTest.php
Expand Up @@ -70,6 +70,8 @@ public function configurations()
'options' => array(
'jackalope.fetch_depth' => 1,
),
'admin_username' => null,
'admin_password' => null,
),
),
),
Expand Down Expand Up @@ -142,6 +144,8 @@ public function configurations()
'password' => 'admin',
'options' => array(
),
'admin_username' => null,
'admin_password' => null,
),
'website' => array(
'backend' => array(
Expand All @@ -159,6 +163,8 @@ public function configurations()
'password' => 'root',
'options' => array(
),
'admin_username' => 'admin',
'admin_password' => 'admin',
),
),
),
Expand Down Expand Up @@ -236,6 +242,8 @@ public function configurations()
'username' => 'admin',
'password' => 'admin',
'options' => array(),
'admin_username' => null,
'admin_password' => null,
),
),
),
Expand Down

0 comments on commit f3c8c04

Please sign in to comment.