Skip to content

Commit

Permalink
Merge pull request #98 from symfony-cmf/cleanup-routing
Browse files Browse the repository at this point in the history
refactor common features into RoutingBundle
  • Loading branch information
lsmith77 committed Apr 3, 2014
2 parents c3384bf + 9ae1897 commit d9ece63
Show file tree
Hide file tree
Showing 36 changed files with 1,087 additions and 1,244 deletions.
55 changes: 32 additions & 23 deletions Admin/PageAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
* file that was distributed with this source code.
*/


namespace Symfony\Cmf\Bundle\SimpleCmsBundle\Admin;

use Sonata\DoctrinePHPCRAdminBundle\Admin\Admin;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\DoctrinePHPCRAdminBundle\Admin\Admin;
use Symfony\Cmf\Bundle\RoutingBundle\Admin\RouteAdmin;
use Symfony\Cmf\Bundle\SimpleCmsBundle\Doctrine\Phpcr\Page;
use Symfony\Cmf\Bundle\SimpleCmsBundle\Model\Page as ModelPage;
use Symfony\Component\Form\FormBuilder;

class PageAdmin extends Admin
class PageAdmin extends RouteAdmin
{
protected $translationDomain = 'CmfSimpleCmsBundle';

Expand All @@ -37,32 +37,41 @@ protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('path', 'text')
->add('title', 'text')
->addIdentifier('title', 'text')
->add('label', 'text')
->add('name', 'text')
->add('createDate', 'date')
->add('publishStartDate', 'date')
->add('publishEndDate', 'date')
;
}

protected function configureFormFields(FormMapper $formMapper)
{
parent::configureFormFields($formMapper);

$formMapper->remove('content');

// remap to routeOptions
$formMapper->remove('options');

$formMapper
->with('form.group_general')
->add(
'parent',
'doctrine_phpcr_odm_tree',
array('choice_list' => array(), 'select_root_node' => true, 'root_node' => $this->getRootPath())
)
->add('name', 'text')
->add('label', null, array('required' => false))
->add('title')
->add('createDate')
->add('addFormatPattern', null, array('required' => false, 'help' => 'form.help_add_format_pattern'))
->add('addTrailingSlash', null, array('required' => false, 'help' => 'form.help_add_trailing_slash'))
->add('addLocalePattern', null, array('required' => false, 'help' => 'form.help_add_locale_pattern'))
->add('body', 'textarea')
->with('form.group_general', array(
'translation_domain' => 'CmfSimpleCmsBundle',
))
->add('label', null, array('required' => false))
->add('title')
->add('body', 'textarea')
->add('createDate')
->end()
->with('form.group_advanced', array(
'translation_domain' => 'CmfRoutingBundle',
))
->add(
'routeOptions',
'sonata_type_immutable_array',
array('keys' => $this->configureFieldsForOptions($this->getSubject()->getRouteOptions()), 'label' => 'form.label_options'),
array('help' => 'form.help_options')
)
->end()
;
}

Expand Down Expand Up @@ -98,7 +107,7 @@ public function preUpdate($object)
*/
protected function ensureOrderByDate($page)
{
$items = $page->getParent()->getChildren();
$items = $page->getParentDocument()->getChildren();

$itemsByDate = array();
/** @var $item Page */
Expand Down Expand Up @@ -135,7 +144,7 @@ protected function ensureOrderByDate($page)

public function toString($object)
{
return $object instanceof ModelPage && $object->getTitle()
return $object instanceof Page && $object->getTitle()
? $object->getTitle()
: $this->trans('link_add', array(), 'SonataAdminBundle')
;
Expand Down
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
Changelog
=========

* **2014-04-01**: Refactored the RoutingBundle to provide all routing
features needed by SimpleCmsBundle.
* If you where only using the SimpleCmsBundle you now need to activate
the dynamic router. SimpleCmsBundle automatically does this for you.
* The configuration for document to route/template now all happens under
cmf_routing.dynamic and the route enhancers also apply to simplecms Pages.
You can configure additional base paths where to look for routes in the
cmf_routing.persistence.phpcr.base_routepaths field.
* The configuration for locales is not needed anymore. Configuring it on the
cmf_routing is enough.
* The options for the format pattern, trailing slash and locale pattern are
now moved into the route "options" and the Page Document now takes an
array of options in the constructor instead of boolean flags.

* **2013-11-14**: The Page now supports the menu options that make sense for a
page: display, displayChildren, attributes, children|link|labelAttributes.
Note that those only affect the menu rendering, not anything else.
Expand Down
1 change: 0 additions & 1 deletion CmfSimpleCmsBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(
DoctrinePhpcrMappingsPass::createXmlMappingDriver(
array(
realpath(__DIR__ . '/Resources/config/doctrine-model') => 'Symfony\Cmf\Bundle\SimpleCmsBundle\Model',
realpath(__DIR__ . '/Resources/config/doctrine-phpcr') => 'Symfony\Cmf\Bundle\SimpleCmsBundle\Doctrine\Phpcr',
),
array('cmf_simple_cms.persistence.phpcr.manager_name')
Expand Down
128 changes: 26 additions & 102 deletions DependencyInjection/CmfSimpleCmsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,32 @@ class CmfSimpleCmsExtension extends Extension implements PrependExtensionInterfa
*/
public function prepend(ContainerBuilder $container)
{
$prependConfig = array('persistence' => array('phpcr' => (array('enabled' => true))));
$container->prependExtensionConfig('cmf_menu', $prependConfig);
$prependConfig = array('dynamic' => $prependConfig);
// process the configuration of CmfCoreExtension
$configs = $container->getExtensionConfig($this->getAlias());
$parameterBag = $container->getParameterBag();
$configs = $parameterBag->resolveValue($configs);
$config = $this->processConfiguration(new Configuration(), $configs);

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

$prependConfig = array(
'chain' => array(
'routers_by_id' => array(
'router.default' => 0,
'cmf_routing.dynamic_router' => -100,
)
),
'dynamic' => array(
'enabled' => true,
)
);
if (isset($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);
}

Expand All @@ -43,101 +66,20 @@ public function load(array $configs, ContainerBuilder $container)
$config = $this->processConfiguration(new Configuration(), $configs);
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));

$this->loadRouting($config['routing'], $loader, $container);

if ($config['persistence']['phpcr']) {
$this->loadPhpcr($config['persistence']['phpcr'], $loader, $container);
$locales = isset($config['multilang']['locales']) ? $config['multilang']['locales'] : false;
$this->loadPhpcrRouting($config, $loader, $container, $locales);

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

protected function loadRouting($config, XmlFileLoader $loader, ContainerBuilder $container)
{
$container->setParameter($this->getAlias() . '.uri_filter_regexp', $config['uri_filter_regexp']);

$loader->load('routing.xml');

$dynamic = $container->getDefinition($this->getAlias().'.dynamic_router');

if (!empty($config['generic_controller'])) {
$definition = new DefinitionDecorator('cmf_routing.enhancer.explicit_template');
$definition->replaceArgument(2, $config['generic_controller']);
$container->setDefinition(
$this->getAlias() . '.enhancer.explicit_template',
$definition
);
$dynamic->addMethodCall('addRouteEnhancer', array(
new Reference($this->getAlias() . '.enhancer.explicit_template')
));
}

if (!empty($config['controllers_by_type'])) {
$definition = new DefinitionDecorator('cmf_routing.enhancer.controllers_by_type');
$definition->replaceArgument(2, $config['controllers_by_type']);
$container->setDefinition(
$this->getAlias() . '.enhancer.controllers_by_type',
$definition
);
$dynamic->addMethodCall('addRouteEnhancer', array(
new Reference($this->getAlias() . '.enhancer.controllers_by_type')
));
}

if (!empty($config['controllers_by_class'])) {
$definition = new DefinitionDecorator('cmf_routing.enhancer.controllers_by_class');
$definition->replaceArgument(2, $config['controllers_by_class']);
$container->setDefinition(
$this->getAlias() . '.enhancer.controllers_by_class',
$definition
);
$dynamic->addMethodCall('addRouteEnhancer', array(
new Reference($this->getAlias() . '.enhancer.controllers_by_class')
));
}

if (!empty($config['generic_controller']) && !empty($config['templates_by_class'])) {
$controllerForTemplates = array();
foreach ($config['templates_by_class'] as $key => $value) {
$controllerForTemplates[$key] = $config['generic_controller'];
}

$definition = new DefinitionDecorator('cmf_routing.enhancer.controller_for_templates_by_class');
$definition->replaceArgument(2, $controllerForTemplates);

$container->setDefinition(
$this->getAlias() . '.enhancer.controller_for_templates_by_class',
$definition
);

$definition = new DefinitionDecorator('cmf_routing.enhancer.templates_by_class');
$definition->replaceArgument(2, $config['templates_by_class']);

$container->setDefinition(
$this->getAlias() . '.enhancer.templates_by_class',
$definition
);

$dynamic->addMethodCall('addRouteEnhancer', array(
new Reference($this->getAlias() . '.enhancer.controller_for_templates_by_class')
));
$dynamic->addMethodCall('addRouteEnhancer', array(
new Reference($this->getAlias() . '.enhancer.templates_by_class')
));
}
}

protected function loadPhpcr($config, XmlFileLoader $loader, ContainerBuilder $container)
{
$loader->load('services-phpcr.xml');
// migrator is only for PHPCR
$loader->load('migrator-phpcr.xml');

// save some characters
$prefix = $this->getAlias() . '.persistence.phpcr';

$container->setParameter($prefix . '.basepath', $config['basepath']);
Expand All @@ -155,24 +97,6 @@ protected function loadPhpcr($config, XmlFileLoader $loader, ContainerBuilder $c
$container->setParameter($prefix . '.document.class', $config['document_class']);
}

protected function loadPhpcrRouting($config, XmlFileLoader $loader, ContainerBuilder $container, $locales)
{
$loader->load('routing-phpcr.xml');
$prefix = $this->getAlias() . '.persistence.phpcr';

$routeProvider = $container->getDefinition($prefix.'.route_provider');
$routeProvider->replaceArgument(0, new Reference($config['persistence']['phpcr']['manager_registry']));
if (!empty($locales)) {
$routeProvider->addMethodCall('setLocales', array($locales));
}
$container->setAlias($this->getAlias() . '.route_provider', $prefix.'.route_provider');

$generator = $container->getDefinition($this->getAlias().'.generator');
$generator->addMethodCall('setContentRepository', array(
new Reference($config['routing']['content_repository_id'])
));
}

protected function loadPhpcrMenu($config, XmlFileLoader $loader, ContainerBuilder $container)
{
$bundles = $container->getParameter('kernel.bundles');
Expand Down
36 changes: 8 additions & 28 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,37 +58,17 @@ public function getConfigTreeBuilder()
->end()

->arrayNode('routing')
->fixXmlConfig('controller_by_type', 'controllers_by_type')
->fixXmlConfig('controller_by_class', 'controllers_by_class')
->fixXmlConfig('template_by_class', 'templates_by_class')
->addDefaultsIfNotSet()
->children()
->scalarNode('generic_controller')->defaultValue('cmf_content.controller:indexAction')->end()
->scalarNode('content_repository_id')->defaultValue('cmf_routing.content_repository')->end()
->scalarNode('uri_filter_regexp')->defaultValue('')->end()
->arrayNode('controllers_by_type')
->useAttributeAsKey('type')
->prototype('scalar')->end()
->end()
->arrayNode('controllers_by_class')
->useAttributeAsKey('alias')
->prototype('scalar')->end()
->end()
->arrayNode('templates_by_class')
->useAttributeAsKey('alias')
->prototype('scalar')->end()
->end()
->info('removed')
->beforeNormalization()
->ifArray()
->thenInvalid('The SimpleCmsBundle routing configuration has moved to cmf_routing.dynamic')
->end()
->end()

->arrayNode('multilang')
->fixXmlConfig('locale')
->children()
->arrayNode('locales')
->isRequired()
->requiresAtLeastOneElement()
->prototype('scalar')->end()
->end()
->info('removed')
->beforeNormalization()
->ifArray()
->thenInvalid('The SimpleCmsBundle locale configuration is not needed anymore')
->end()
->end()
->end()
Expand Down
37 changes: 0 additions & 37 deletions Doctrine/Phpcr/MultilangRedirectRoute.php

This file was deleted.

Loading

0 comments on commit d9ece63

Please sign in to comment.