Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add Doctrine ORM support #148

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ before_install:
- composer self-update
- if [ "${TRAVIS_PHP_VERSION}" != "hhvm" ]; then echo "memory_limit = -1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini; fi
- composer require symfony/symfony:${SYMFONY_VERSION} --no-update

install: composer update $COMPOSER_FLAGS --prefer-dist

before_script: vendor/symfony-cmf/testing/bin/travis/phpcr_odm_doctrine_dbal.sh
before_script:
- vendor/symfony-cmf/testing/bin/travis/phpcr_odm_doctrine_dbal.sh
- vendor/symfony-cmf/testing/bin/travis/doctrine_orm.sh

script: phpunit --coverage-text

Expand Down
85 changes: 75 additions & 10 deletions CmfSimpleCmsBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Cmf\Bundle\SimpleCmsBundle;

use Symfony\Cmf\Bundle\CoreBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass;
Expand All @@ -26,16 +27,80 @@ public function build(ContainerBuilder $container)
}

if (class_exists('Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass')) {
$container->addCompilerPass(
DoctrinePhpcrMappingsPass::createXmlMappingDriver(
array(
realpath(__DIR__ . '/Resources/config/doctrine-phpcr') => 'Symfony\Cmf\Bundle\SimpleCmsBundle\Doctrine\Phpcr',
),
array('cmf_simple_cms.persistence.phpcr.manager_name'),
false,
array('CmfSimpleCmsBundle' => 'Symfony\Cmf\Bundle\SimpleCmsBundle\Doctrine\Phpcr')
)
);
$this->buildPhpcrCompilerPass($container);
}

if (class_exists('Doctrine\ORM\Version')) {
$this->buildOrmCompilerPass($container);
}
}

/**
* Creates and registers compiler passes for PHPCR-ODM mapping if both the
* phpcr-odm and the phpcr-bundle are present.
*
* @param ContainerBuilder $container
*/
private function buildPhpcrCompilerPass(ContainerBuilder $container)
{
$container->addCompilerPass(
DoctrinePhpcrMappingsPass::createXmlMappingDriver(
array(
realpath(__DIR__ . '/Resources/config/doctrine-phpcr') => 'Symfony\Cmf\Bundle\SimpleCmsBundle\Doctrine\Phpcr',
),
array('cmf_simple_cms.persistence.phpcr.manager_name'),
false,
array('CmfSimpleCmsBundle' => 'Symfony\Cmf\Bundle\SimpleCmsBundle\Doctrine\Phpcr')
)
);
}

/**
* Creates and registers compiler passes for ORM mappings if both doctrine
* ORM and a suitable compiler pass implementation are available.
*
* @param ContainerBuilder $container
*/
private function buildOrmCompilerPass(ContainerBuilder $container)
{
$doctrineOrmCompiler = $this->findDoctrineOrmCompiler();

if (!$doctrineOrmCompiler) {
return;
}

$container->addCompilerPass(
$doctrineOrmCompiler::createXmlMappingDriver(
array(
realpath(__DIR__ . '/Resources/config/doctrine-orm') => 'Symfony\Cmf\Bundle\SimpleCmsBundle\Doctrine\Orm',
),
array('cmf_simple_cms.persistence.orm.manager_name'),
false,
array('CmfSimpleCmsBundle' => 'Symfony\Cmf\Bundle\SimpleCmsBundle\Doctrine\orm')
)
);
}

/**
* Looks for a mapping compiler pass. If available, use the one from
* DoctrineBundle (available only since DoctrineBundle 2.4 and Symfony 2.3)
* Otherwise use the standalone one from CmfCoreBundle.
*
* @return boolean|string the compiler pass to use or false if no suitable
* one was found
*/
private function findDoctrineOrmCompiler()
{
if (class_exists('Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterMappingsPass')
&& class_exists('Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass')
) {
return 'Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass';
}

if (class_exists('Symfony\Cmf\Bundle\CoreBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass')) {
return 'Symfony\Cmf\Bundle\CoreBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass';
}

return false;
}
}
59 changes: 49 additions & 10 deletions DependencyInjection/CmfSimpleCmsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ class CmfSimpleCmsExtension extends Extension implements PrependExtensionInterfa
public function prepend(ContainerBuilder $container)
{
// process the configuration of CmfCoreExtension
$configs = $container->getExtensionConfig($this->getAlias());
$parameterBag = $container->getParameterBag();
$configs = $parameterBag->resolveValue($configs);
$config = $this->processConfiguration(new Configuration(), $configs);
$config = $this->processConfiguration(
new Configuration(),
$container->getParameterBag()->resolveValue($container->getExtensionConfig($this->getAlias()))
);

if (empty($config['persistence']['phpcr']['enabled'])) {
if (empty($config['persistence']['phpcr']['enabled'])
&& empty($config['persistence']['orm']['enabled'])
) {
return;
}

Expand All @@ -50,12 +52,23 @@ public function prepend(ContainerBuilder $container)
'enabled' => true,
)
);

if (!empty($config['persistence']['phpcr']['enabled'])) {
$prependConfig = $this->prependPhpcr($prependConfig, $config);
}

$container->prependExtensionConfig('cmf_routing', $prependConfig);
}

private function prependPhpcr(array $prependConfig, array $config)
{
if (isset($config['persistence']['phpcr']['basepath'])
&& '/cms/simple' != $config['persistence']['phpcr']['basepath']
&& '/cms/simple' !== $config['persistence']['phpcr']['basepath']
) {
$prependConfig['dynamic']['persistence']['phpcr']['route_basepaths'] = array($config['persistence']['phpcr']['basepath']);
}
$container->prependExtensionConfig('cmf_routing', $prependConfig);

return $prependConfig;
}

public function load(array $configs, ContainerBuilder $container)
Expand All @@ -70,9 +83,17 @@ public function load(array $configs, ContainerBuilder $container)
$this->loadPhpcrMenu($config, $loader, $container);
}
}

if ($config['persistence']['orm']) {
$this->loadOrm($config['persistence']['orm'], $loader, $container);

if ($config['use_menu']) {
$this->loadOrmMenu($config, $loader, $container);
}
}
}

protected function loadPhpcr($config, XmlFileLoader $loader, ContainerBuilder $container)
private function loadPhpcr($config, XmlFileLoader $loader, ContainerBuilder $container)
{
$loader->load('services-phpcr.xml');
$loader->load('migrator-phpcr.xml');
Expand All @@ -94,7 +115,7 @@ protected function loadPhpcr($config, XmlFileLoader $loader, ContainerBuilder $c
$container->setParameter($prefix . '.document.class', $config['document_class']);
}

protected function loadPhpcrMenu($config, XmlFileLoader $loader, ContainerBuilder $container)
private function loadPhpcrMenu($config, XmlFileLoader $loader, ContainerBuilder $container)
{
$bundles = $container->getParameter('kernel.bundles');
if ('auto' === $config['use_menu'] && !isset($bundles['CmfMenuBundle'])) {
Expand All @@ -104,7 +125,25 @@ protected function loadPhpcrMenu($config, XmlFileLoader $loader, ContainerBuilde
$loader->load('menu-phpcr.xml');
}

protected function loadSonataAdmin($config, XmlFileLoader $loader, ContainerBuilder $container)
private function loadOrm($config, XmlFileLoader $loader, ContainerBuilder $container)
{
$prefix = $this->getAlias() . '.persistence.orm';

$container->setParameter($prefix . '.manager_name', $config['manager_name']);
$container->setParameter($prefix . '.entity.class', $config['entity_class']);
}

private function loadOrmMenu($config, XmlFileLoader $loader, ContainerBuilder $container)
{
$bundles = $container->getParameter('kernel.bundles');
if ('auto' === $config['use_menu'] && !isset($bundles['CmfMenuBundle'])) {
return;
}

$loader->load('menu-orm.xml');
}

private function loadSonataAdmin($config, XmlFileLoader $loader, ContainerBuilder $container)
{
$bundles = $container->getParameter('kernel.bundles');
if ('auto' === $config['use_sonata_admin'] && !isset($bundles['SonataDoctrinePHPCRAdminBundle'])) {
Expand Down
24 changes: 24 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->arrayNode('orm')
->addDefaultsIfNotSet()
->canBeEnabled()
->children()
->scalarNode('manager_registry')->defaultValue('doctrine')->end()
->scalarNode('manager_name')->defaultNull()->end()
->scalarNode('entity_class')->defaultValue('Symfony\Cmf\Bundle\SimpleCmsBundle\Doctrine\Orm\Page')->end()

/*
->enumNode('use_sonata_admin')
->values(array(true, false, 'auto'))
->defaultValue('auto')
->end()
->arrayNode('sonata_admin')
->children()
->enumNode('sort')
->values(array(false, 'asc', 'desc'))
->defaultValue(false)
->end()
->end()
->end()
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to either implement sonata or remove these commented out configuration parts

->end()
->end()
->end()
->end()

Expand Down
Loading