Skip to content

Commit

Permalink
Remove the manager dependency from a BlockService
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Rabaix committed Sep 12, 2011
1 parent d2008a3 commit d438c67
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 73 deletions.
4 changes: 2 additions & 2 deletions Admin/BlockAdmin.php
Expand Up @@ -103,7 +103,7 @@ public function preUpdate($object)
public function postUpdate($object)
{
$service = $this->cmsManager->getBlockService($object);
$cacheElement = $service->getCacheElement($object);
$cacheElement = $service->getCacheElement($this->cmsManager, $object);

$this->cmsManager->invalidate($cacheElement);
}
Expand All @@ -119,7 +119,7 @@ public function prePersist($object)
public function postPersist($object)
{
$service = $this->cmsManager->getBlockService($object);
$cacheElement = $service->getCacheElement($object);
$cacheElement = $service->getCacheElement($this->cmsManager, $object);

$this->cmsManager->invalidate($cacheElement);
}
Expand Down
26 changes: 22 additions & 4 deletions Block/ActionBlockService.php
Expand Up @@ -20,6 +20,7 @@
use Sonata\PageBundle\Model\PageInterface;
use Sonata\PageBundle\Generator\Mustache;
use Sonata\AdminBundle\Validator\ErrorElement;
use Sonata\PageBundle\CmsManager\CmsManagerInterface;

/**
* PageExtension
Expand All @@ -31,6 +32,11 @@ class ActionBlockService extends BaseBlockService
{
private $kernel;

/**
* @param $name
* @param \Symfony\Component\Templating\EngineInterface $templating
* @param \Symfony\Component\HttpKernel\HttpKernelInterface $kernel
*/
public function __construct($name, EngineInterface $templating, HttpKernelInterface $kernel)
{
parent::__construct($name, $templating);
Expand All @@ -39,12 +45,14 @@ public function __construct($name, EngineInterface $templating, HttpKernelInterf
}

/**
* @throws \Exception
* @param \Sonata\PageBundle\CmsManager\CmsManagerInterface $manager
* @param \Sonata\PageBundle\Model\BlockInterface $block
* @param \Sonata\PageBundle\Model\PageInterface $page
* @param \Symfony\Component\HttpFoundation\Response $response
* @param null|\Symfony\Component\HttpFoundation\Response $response
* @return string
*/
public function execute(BlockInterface $block, PageInterface $page, Response $response = null)
public function execute(CmsManagerInterface $manager, BlockInterface $block, PageInterface $page, Response $response = null)
{
$parameters = (array)json_decode($block->getSetting('parameters'), true);
$parameters = array_merge($parameters, array('_block' => $block, '_page' => $page));
Expand All @@ -67,17 +75,24 @@ public function execute(BlockInterface $block, PageInterface $page, Response $re
), $response);
}

public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
/**
* @param \Sonata\PageBundle\CmsManager\CmsManagerInterface $manager
* @param \Sonata\AdminBundle\Validator\ErrorElement $errorElement
* @param \Sonata\PageBundle\Model\BlockInterface $block
* @return void
*/
public function validateBlock(CmsManagerInterface $manager, ErrorElement $errorElement, BlockInterface $block)
{
// TODO: Implement validateBlock() method.
}

/**
* @param \Sonata\PageBundle\CmsManager\CmsManagerInterface $manager
* @param \Sonata\AdminBundle\Form\FormMapper $formMapper
* @param \Sonata\PageBundle\Model\BlockInterface $block
* @return void
*/
public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
public function buildEditForm(CmsManagerInterface $manager, FormMapper $formMapper, BlockInterface $block)
{
$formMapper->add('settings', 'sonata_type_immutable_array', array(
'keys' => array(
Expand All @@ -88,6 +103,9 @@ public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
));
}

/**
* @return string
*/
public function getName()
{
return 'Action (core)';
Expand Down
55 changes: 44 additions & 11 deletions Block/BaseBlockService.php
Expand Up @@ -30,8 +30,10 @@ abstract class BaseBlockService implements BlockServiceInterface

protected $templating;

protected $manager;

/**
* @param $name
* @param \Symfony\Component\Templating\EngineInterface $templating
*/
public function __construct($name, EngineInterface $templating)
{
$this->name = $name;
Expand Down Expand Up @@ -71,12 +73,14 @@ public function getTemplating()
/**
* Returns the cache keys for the block
*
* @param \Sonata\PageBundle\CmsManager\CmsManagerInterface $manager
* @param \Sonata\PageBundle\Model\BlockInterface $block
* @return \Sonata\PageBundle\Cache\CacheElement
*/
public function getCacheElement(BlockInterface $block)
public function getCacheElement(CmsManagerInterface $manager, BlockInterface $block)
{
$baseCacheKeys = array(
'manager' => $manager->getCode(),
'block_id' => $block->getId(),
'page_id' => $block->getPage()->getId(),
'updated_at' => $block->getUpdatedAt()->format('U')
Expand All @@ -85,41 +89,70 @@ public function getCacheElement(BlockInterface $block)
return new CacheElement($baseCacheKeys, $block->getTtl());
}

public function setManager(CmsManagerInterface $manager)
{
$this->manager = $manager;
}

public function buildCreateForm(FormMapper $formMapper, BlockInterface $block)
/**
* @param \Sonata\PageBundle\CmsManager\CmsManagerInterface $manager
* @param \Sonata\AdminBundle\Form\FormMapper $formMapper
* @param \Sonata\PageBundle\Model\BlockInterface $block
* @return void
*/
public function buildCreateForm(CmsManagerInterface $manager, FormMapper $formMapper, BlockInterface $block)
{
return $this->buildEditForm($formMapper, $block);
$this->buildEditForm($manager, $formMapper, $block);
}

/**
* @param \Sonata\PageBundle\Model\BlockInterface $block
* @return void
*/
public function prePersist(BlockInterface $block)
{
}

/**
* @param \Sonata\PageBundle\Model\BlockInterface $block
* @return void
*/
public function postPersist(BlockInterface $block)
{
}

/**
* @param \Sonata\PageBundle\Model\BlockInterface $block
* @return void
*/
public function preUpdate(BlockInterface $block)
{
}

/**
* @param \Sonata\PageBundle\Model\BlockInterface $block
* @return void
*/
public function postUpdate(BlockInterface $block)
{
}

/**
* @param \Sonata\PageBundle\Model\BlockInterface $block
* @return void
*/
public function preDelete(BlockInterface $block)
{
}

/**
* @param \Sonata\PageBundle\Model\BlockInterface $block
* @return void
*/
public function postDelete(BlockInterface $block)
{
}

public function load(BlockInterface $block)
/**
* @param \Sonata\PageBundle\Model\BlockInterface $block
* @return void
*/
public function load(CmsManagerInterface $manager, BlockInterface $block)
{
}
}
28 changes: 14 additions & 14 deletions Block/BlockServiceInterface.php
Expand Up @@ -25,36 +25,40 @@ interface BlockServiceInterface
{
/**
* @abstract
* @param \Sonata\PageBundle\CmsManager\CmsManagerInterface $manager
* @param \Sonata\AdminBundle\Form\FormMapper $form
* @param \Sonata\PageBundle\Model\BlockInterface $block
* @return void
*/
function buildEditForm(FormMapper $form, BlockInterface $block);
function buildEditForm(CmsManagerInterface $manager, FormMapper $form, BlockInterface $block);

/**
* @abstract
* @param \Sonata\PageBundle\CmsManager\CmsManagerInterface $manager
* @param \Sonata\AdminBundle\Form\FormMapper $form
* @param \Sonata\PageBundle\Model\BlockInterface $block
* @return void
*/
function buildCreateForm(FormMapper $form, BlockInterface $block);
function buildCreateForm(CmsManagerInterface $manager, FormMapper $form, BlockInterface $block);

/**
* @abstract
* @param \Sonata\PageBundle\CmsManager\CmsManagerInterface $manager
* @param \Sonata\PageBundle\Model\BlockInterface $block
* @param \Sonata\PageBundle\Model\PageInterface $page
* @param \Symfony\Component\HttpFoundation\Response $response
* @param null|\Symfony\Component\HttpFoundation\Response $response
* @return void
*/
function execute(BlockInterface $block, PageInterface $page, Response $response = null);
function execute(CmsManagerInterface $manager, BlockInterface $block, PageInterface $page, Response $response = null);

/**
* @abstract
* @param \Sonata\PageBundle\CmsManager\CmsManagerInterface $manager
* @param \Sonata\AdminBundle\Validator\ErrorElement $errorElement
* @param \Sonata\PageBundle\Model\BlockInterface $block
* @return void
*/
function validateBlock(ErrorElement $errorElement, BlockInterface $block);
function validateBlock(CmsManagerInterface $manager, ErrorElement $errorElement, BlockInterface $block);

/**
* @abstract
Expand All @@ -72,21 +76,17 @@ function getDefaultSettings();

/**
* @abstract
* @param \Sonata\PageBundle\CmsManager\CmsManagerInterface $manager
* @param \Sonata\PageBundle\Model\BlockInterface $block
* @return \Sonata\PageBundle\Cache\CacheElement
*/
function getCacheElement(BlockInterface $block);

/**
*
* @param \Sonata\PageBundle\CmsManagerInterface $manager
* @return void
*/
function setManager(CmsManagerInterface $manager);
function getCacheElement(CmsManagerInterface $manager, BlockInterface $block);

/**
* @abstract
* @param \Sonata\PageBundle\CmsManager\CmsManagerInterface $manager
* @param \Sonata\PageBundle\Model\BlockInterface $block
* @return void
*/
function load(BlockInterface $block);
function load(CmsManagerInterface $manager, BlockInterface $block);
}
50 changes: 43 additions & 7 deletions Block/ChildrenPagesBlockService.php
Expand Up @@ -17,6 +17,7 @@
use Sonata\PageBundle\Model\BlockInterface;
use Sonata\PageBundle\Model\PageInterface;
use Sonata\AdminBundle\Validator\ErrorElement;
use Sonata\PageBundle\CmsManager\CmsManagerInterface;

/**
* PageExtension
Expand All @@ -26,16 +27,23 @@
*/
class ChildrenPagesBlockService extends BaseBlockService
{
public function execute(BlockInterface $block, PageInterface $page, Response $response = null)
/**
* @param \Sonata\PageBundle\CmsManager\CmsManagerInterface $manager
* @param \Sonata\PageBundle\Model\BlockInterface $block
* @param \Sonata\PageBundle\Model\PageInterface $page
* @param null|\Symfony\Component\HttpFoundation\Response $response
* @return string
*/
public function execute(CmsManagerInterface $manager, BlockInterface $block, PageInterface $page, Response $response = null)
{
$settings = array_merge($this->getDefaultSettings(), $block->getSettings());

if ($settings['current']) {
$page = $this->manager->getCurrentPage();
$page = $manager->getCurrentPage();
} else if ($settings['pageId']) {
$page = $settings['pageId'];
} else {
$page = $this->manager->getPage('/');
$page = $manager->getPage('/');
}

return $this->renderResponse('SonataPageBundle:Block:block_core_children_pages.html.twig', array(
Expand All @@ -45,12 +53,24 @@ public function execute(BlockInterface $block, PageInterface $page, Response $re
), $response);
}

public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
/**
* @param \Sonata\PageBundle\CmsManager\CmsManagerInterface $manager
* @param \Sonata\AdminBundle\Validator\ErrorElement $errorElement
* @param \Sonata\PageBundle\Model\BlockInterface $block
* @return void
*/
public function validateBlock(CmsManagerInterface $manager, ErrorElement $errorElement, BlockInterface $block)
{
// TODO: Implement validateBlock() method.
}

public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
/**
* @param \Sonata\PageBundle\CmsManager\CmsManagerInterface $manager
* @param \Sonata\AdminBundle\Form\FormMapper $formMapper
* @param \Sonata\PageBundle\Model\BlockInterface $block
* @return void
*/
public function buildEditForm(CmsManagerInterface $manager, FormMapper $formMapper, BlockInterface $block)
{
$formMapper->add('settings', 'sonata_type_immutable_array', array(
'keys' => array(
Expand All @@ -72,6 +92,9 @@ public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
));
}

/**
* @return string
*/
public function getName()
{
return 'Children Page (core)';
Expand All @@ -92,20 +115,33 @@ public function getDefaultSettings()
);
}

/**
* @param \Sonata\PageBundle\Model\BlockInterface $block
* @return void
*/
public function prePersist(BlockInterface $block)
{
$block->setSetting('pageId', is_object($block->getSetting('pageId')) ? $block->getSetting('pageId')->getId() : null);
}

/**
* @param \Sonata\PageBundle\Model\BlockInterface $block
* @return void
*/
public function preUpdate(BlockInterface $block)
{
$block->setSetting('pageId', is_object($block->getSetting('pageId')) ? $block->getSetting('pageId')->getId() : null);
}

public function load(BlockInterface $block)
/**
* @param \Sonata\PageBundle\CmsManager\CmsManagerInterface $manager
* @param \Sonata\PageBundle\Model\BlockInterface $block
* @return void
*/
public function load(CmsManagerInterface $manager, BlockInterface $block)
{
if (is_numeric($block->getSetting('pageId'))) {
$block->setSetting('pageId', $this->manager->getPage($block->getSetting('pageId')));
$block->setSetting('pageId', $manager->getPage($block->getSetting('pageId')));
}
}
}

0 comments on commit d438c67

Please sign in to comment.