Skip to content

Commit

Permalink
refactor PagePartReader and PageTemplateReader allowing using sym…
Browse files Browse the repository at this point in the history
…fony configuration for storage (#1109)
  • Loading branch information
mlebkowski authored and Kristof Jochmans committed May 4, 2016
1 parent b3dba2d commit 62dc60f
Show file tree
Hide file tree
Showing 42 changed files with 1,387 additions and 839 deletions.
12 changes: 12 additions & 0 deletions UPGRADE-X.X.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,15 @@ The service definitions for both `FileHandler` and `ImageHandler` have changed.
If you have changed their classes using the `%kunstmaan_media.media_handler.[image|file].class%` parameter, make sure they implement the new method.

The `MEDIA_PATH` constant on `FileHandler` has been removed in favor of a property that can be set using the `setMediaPath` method.


## [PagePartBundle] `PagePartConfigurationReader` and `PageTemplateConfigurationReader` removed

If you relied on those classes use new interfaces and services instead:

* `kunstmaan_page_part.page_part_configuration_reader` implementing `PagePartConfigurationReaderInterface`
* `kunstmaan_page_part.page_template_configuration_reader` implementing `PageTemplateConfigurationReaderInterface`

Classes using those services has changed as well and now take them as a constructor dependency instead of creating an instance in place.

The `PageTemplateConfigurationRepostiory::findOrCreateFor` has been moved to `PageTemplateConfigurationService::findOrCreateFor`. It can be found via the `kunstmaan_page_part.page_template.page_template_configuration_service` service.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace Kunstmaan\PagePartBundle\Controller;

use Doctrine\Common\Util\ClassUtils;
use Kunstmaan\PagePartBundle\Helper\PagePartConfigurationReader;
use Kunstmaan\PagePartBundle\Helper\HasPagePartsInterface;
use Kunstmaan\PagePartBundle\Helper\PagePartInterface;
use Kunstmaan\PagePartBundle\PagePartAdmin\PagePartAdmin;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
Expand All @@ -26,16 +27,21 @@ class PagePartAdminController extends Controller
*/
public function newPagePartAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$em = $this->get('doctrine.orm.entity_manager');

$pageId = $request->get('pageid');
$pageClassName = $request->get('pageclassname');
$context = $request->get('context');
$pagePartClass = $request->get('type');

$page = $em->getRepository($pageClassName)->findOneById($pageId);
/** @var HasPagePartsInterface $page */
$page = $em->getRepository($pageClassName)->find($pageId);

$pagePartConfigurationReader = new PagePartConfigurationReader($this->container->get('kernel'));
if (false === $page instanceof HasPagePartsInterface) {
throw new \RuntimeException(sprintf('Given page (%s:%d) has no pageparts', $pageClassName, $pageId));
}

$pagePartConfigurationReader = $this->container->get('kunstmaan_page_part.page_part_configuration_reader');
$pagePartAdminConfigurators = $pagePartConfigurationReader->getPagePartAdminConfigurators($page);

$pagePartAdminConfigurator = null;
Expand All @@ -50,8 +56,16 @@ public function newPagePartAction(Request $request)
}

$pagePartAdmin = new PagePartAdmin($pagePartAdminConfigurator, $em, $page, $context, $this->container);
/** @var PagePartInterface $pagePart */
$pagePart = new $pagePartClass();

if (false === $pagePart instanceof PagePartInterface) {
throw new \RuntimeException(sprintf(
'Given pagepart expected to implement PagePartInterface, %s given',
$pagePartClass
));
}

$formFactory = $this->container->get('form.factory');
$formBuilder = $formFactory->createBuilder(FormType::class);
$pagePartAdmin->adaptForm($formBuilder);
Expand All @@ -61,23 +75,23 @@ public function newPagePartAction(Request $request)
$data['pagepartadmin_' . $id] = $pagePart;
$adminType = $pagePart->getDefaultAdminType();

if (!is_object($adminType) && is_string($adminType)) {
if (is_string($adminType)) {
$adminType = $this->container->get($adminType);
}

$adminTypeFqn = ClassUtils::getClass($adminType);
$adminTypeFqn = ClassUtils::getClass($adminType);

$formBuilder->add('pagepartadmin_' . $id, $adminTypeFqn);
$formBuilder->setData($data);
$form = $formBuilder->getForm();
$formview = $form->createView();

return array(
return [
'id' => $id,
'form' => $formview,
'pagepart' => $pagePart,
'pagepartadmin' => $pagePartAdmin,
'editmode' => true
);
];
}
}
39 changes: 35 additions & 4 deletions src/Kunstmaan/PagePartBundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace Kunstmaan\PagePartBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

Expand All @@ -17,11 +19,40 @@ class Configuration implements ConfigurationInterface
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$treeBuilder->root('kunstmaan_page_part');
$root = $treeBuilder->root('kunstmaan_page_part');

/** @var ArrayNodeDefinition $pageparts */
$pageparts = $root->children()->arrayNode('pageparts')->prototype('array');
$pageparts->children()->scalarNode('name')->isRequired();
$pageparts->children()->scalarNode('context')->isRequired();
$pageparts->children()->scalarNode('extends');
$pageparts->children()->scalarNode('widget_template');

/** @var ArrayNodeDefinition $types */
$types = $pageparts->children()->arrayNode('types')->defaultValue([])->prototype('array');
$types->children()->scalarNode('name')->isRequired();
$types->children()->scalarNode('class')->isRequired();
$types->children()->scalarNode('pagelimit');

// *************************************************************************************************************

/** @var ArrayNodeDefinition $pagetemplates */
$pagetemplates = $root->children()->arrayNode('pagetemplates')->defaultValue([])->prototype('array');

$pagetemplates->children()->scalarNode('template')->isRequired();
$pagetemplates->children()->scalarNode('name')->isRequired();

/** @var ArrayNodeDefinition $rows */
$rows = $pagetemplates->children()->arrayNode('rows')->prototype('array');

/** @var ArrayNodeDefinition $regions */
$regions = $rows->children()->arrayNode('regions')->prototype('array');

// no subregions this way, sorry. feel free to implement it: https://gist.github.com/Lumbendil/3249173
$regions->children()->scalarNode('name');
$regions->children()->scalarNode('span')->defaultValue(12);
$regions->children()->scalarNode('template');

// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.
return $treeBuilder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ class KunstmaanPagePartExtension extends Extension
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$this->processConfiguration($configuration, $configs);
$configs = $this->processConfiguration(new Configuration(), $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');

$container->setParameter('kunstmaan_page_part.page_parts_presets', $configs['pageparts']);
$container->setParameter('kunstmaan_page_part.page_templates_presets', $configs['pagetemplates']);
}
}
33 changes: 19 additions & 14 deletions src/Kunstmaan/PagePartBundle/EventListener/CloneListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace Kunstmaan\PagePartBundle\EventListener;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Kunstmaan\PagePartBundle\Helper\HasPagePartsInterface;
use Kunstmaan\PagePartBundle\Helper\HasPageTemplateInterface;
use Kunstmaan\AdminBundle\Event\DeepCloneAndSaveEvent;
use Symfony\Component\HttpKernel\KernelInterface;
use Kunstmaan\PagePartBundle\Helper\PagePartConfigurationReader;
use Kunstmaan\PagePartBundle\PagePartConfigurationReader\PagePartConfigurationReaderInterface;
use Kunstmaan\PagePartBundle\PageTemplate\PageTemplateConfigurationService;

/**
* This event will make sure pageparts are being copied when deepClone is done on an entity implementing hasPagePartsInterface
Expand All @@ -16,23 +17,29 @@ class CloneListener
{

/**
* @var EntityManager
* @var EntityManager|EntityManagerInterface
*/
private $em;

/**
* @var KernelInterface
* @var PagePartConfigurationReaderInterface
*/
private $kernel;
private $pagePartReader;

/**
* @param EntityManager $em The entity manager
* @param KernelInterface $kernel The kernel
* @var PageTemplateConfigurationService
*/
public function __construct(EntityManager $em, KernelInterface $kernel)
private $pageTemplateConfiguratiorService;

public function __construct(
EntityManagerInterface $em,
PagePartConfigurationReaderInterface $pagePartReader,
PageTemplateConfigurationService $pageTemplateConfiguratiorService
)
{
$this->em = $em;
$this->kernel = $kernel;
$this->pagePartReader = $pagePartReader;
$this->pageTemplateConfiguratiorService = $pageTemplateConfiguratiorService;
}

/**
Expand All @@ -45,17 +52,15 @@ public function postDeepCloneAndSave(DeepCloneAndSaveEvent $event)
if ($originalEntity instanceof HasPagePartsInterface) {
$clonedEntity = $event->getClonedEntity();

$pagePartConfigurationReader = new PagePartConfigurationReader($this->kernel);
$contexts = $pagePartConfigurationReader->getPagePartContexts($originalEntity);
$contexts = $this->pagePartReader->getPagePartContexts($originalEntity);
foreach ($contexts as $context) {
$this->em->getRepository('KunstmaanPagePartBundle:PagePartRef')->copyPageParts($this->em, $originalEntity, $clonedEntity, $context);
}
}

if ($originalEntity instanceof HasPageTemplateInterface) {
$clonedEntity = $event->getClonedEntity();
$PageTemplateConfigurationRepo = $this->em->getRepository('KunstmaanPagePartBundle:PageTemplateConfiguration');
$PageTemplateConfigurationRepo->setContainer($this->kernel->getContainer());
$newPageTemplateConfiguration = clone $PageTemplateConfigurationRepo->findOrCreateFor($originalEntity);
$newPageTemplateConfiguration = clone $this->pageTemplateConfiguratiorService->findOrCreateFor($originalEntity);
$newPageTemplateConfiguration->setId(null);
$newPageTemplateConfiguration->setPageId($clonedEntity->getId());
$this->em->persist($newPageTemplateConfiguration);
Expand Down
50 changes: 28 additions & 22 deletions src/Kunstmaan/PagePartBundle/EventListener/NodeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Kunstmaan\PagePartBundle\EventListener;

use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\FormFactoryInterface;
use Doctrine\ORM\EntityManagerInterface;
use Kunstmaan\PagePartBundle\PagePartConfigurationReader\PagePartConfigurationReaderInterface;
use Kunstmaan\PagePartBundle\PageTemplate\PageTemplateConfigurationReaderInterface;
use Kunstmaan\PagePartBundle\PageTemplate\PageTemplateConfigurationService;
use Kunstmaan\NodeBundle\Event\AdaptFormEvent;
use Kunstmaan\AdminBundle\Helper\FormWidgets\Tabs\Tab;
use Kunstmaan\AdminBundle\Helper\FormWidgets\ListWidget;
Expand All @@ -12,8 +14,6 @@
use Kunstmaan\PagePartBundle\Helper\HasPageTemplateInterface;
use Kunstmaan\PagePartBundle\Helper\FormWidgets\PageTemplateWidget;
use Kunstmaan\PagePartBundle\Helper\FormWidgets\PagePartWidget;
use Symfony\Component\HttpKernel\KernelInterface;
use Kunstmaan\PagePartBundle\Helper\PagePartConfigurationReader;

/**
* NodeListener
Expand All @@ -22,37 +22,43 @@ class NodeListener
{

/**
* @var EntityManager
* @var EntityManagerInterface
*/
private $em;

/**
* @var FormFactoryInterface
* @var PagePartAdminFactory
*/
private $formFactory;
private $pagePartAdminFactory;

/**
* @var KernelInterface
* @var PageTemplateConfigurationReaderInterface
*/
private $kernel;
private $templateReader;

/**
* @var PagePartAdminFactory
* @var PagePartConfigurationReaderInterface
*/
private $pagePartAdminFactory;
private $pagePartReader;

/**
* @param EntityManager $em The entity manager
* @param KernelInterface $kernel The kernel
* @param FormFactoryInterface $formFactory The form factory
* @param PagePartAdminFactory $pagePartAdminFactory The page part admin factory
* @var PageTemplateConfigurationService
*/
public function __construct(EntityManager $em, KernelInterface $kernel, FormFactoryInterface $formFactory, PagePartAdminFactory $pagePartAdminFactory)
private $pageTemplateConfiguratiorService;

public function __construct(
EntityManagerInterface $em,
PagePartAdminFactory $pagePartAdminFactory,
PageTemplateConfigurationReaderInterface $templateReader,
PagePartConfigurationReaderInterface $pagePartReader,
PageTemplateConfigurationService $pageTemplateConfiguratiorService
)
{
$this->em = $em;
$this->formFactory = $formFactory;
$this->kernel = $kernel;
$this->pagePartAdminFactory = $pagePartAdminFactory;
$this->templateReader = $templateReader;
$this->pagePartReader = $pagePartReader;
$this->pageTemplateConfiguratiorService = $pageTemplateConfiguratiorService;
}

/**
Expand All @@ -64,7 +70,8 @@ public function adaptForm(AdaptFormEvent $event)
$tabPane = $event->getTabPane();

if ($page instanceof HasPageTemplateInterface) {
$pageTemplateWidget = new PageTemplateWidget($page, $event->getRequest(), $this->em, $this->kernel, $this->formFactory, $this->pagePartAdminFactory);
$pageTemplateWidget = new PageTemplateWidget($page, $event->getRequest(), $this->em, $this->pagePartAdminFactory, $this->templateReader, $this->pagePartReader, $this->pageTemplateConfiguratiorService);

/* @var Tab $propertiesTab */
$propertiesTab = $tabPane->getTabByTitle('kuma_node.tab.properties.title');
if (!is_null($propertiesTab)) {
Expand All @@ -76,11 +83,10 @@ public function adaptForm(AdaptFormEvent $event)
}
} else if ($page instanceof HasPagePartsInterface) {
/* @var HasPagePartsInterface $page */
$pagePartConfigurationReader = new PagePartConfigurationReader($this->kernel);
$pagePartAdminConfigurators = $pagePartConfigurationReader->getPagePartAdminConfigurators($page);
$pagePartAdminConfigurators = $this->pagePartReader->getPagePartAdminConfigurators($page);

foreach ($pagePartAdminConfigurators as $index => $pagePartAdminConfiguration) {
$pagePartWidget = new PagePartWidget($page, $event->getRequest(), $this->em, $pagePartAdminConfiguration, $this->formFactory, $this->pagePartAdminFactory);
$pagePartWidget = new PagePartWidget($page, $event->getRequest(), $this->em, $pagePartAdminConfiguration, $this->pagePartAdminFactory);
if ($index == 0) {
/* @var Tab $propertiesTab */
$propertiesTab = $tabPane->getTabByTitle('kuma_node.tab.properties.title');
Expand Down
Loading

0 comments on commit 62dc60f

Please sign in to comment.