Permalink
Browse files

first commit (port from the PageBundle)

  • Loading branch information...
0 parents commit 1fb1d4a88443bd6c42700b1028049fbb3510437a @rande rande committed Feb 10, 2012
@@ -0,0 +1,157 @@
+<?php
+/*
+ * This file is part of the Sonata project.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+
+namespace Sonata\BlockBundle\Block;
+
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\Templating\EngineInterface;
+use Sonata\BlockBundle\Model\BlockInterface;
+use Sonata\AdminBundle\Form\FormMapper;
+
+/**
+ * BaseBlockService
+ *
+ *
+ * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ */
+abstract class BaseBlockService implements BlockServiceInterface
+{
+ protected $name;
+
+ protected $templating;
+
+ /**
+ * @param $name
+ * @param \Symfony\Component\Templating\EngineInterface $templating
+ */
+ public function __construct($name, EngineInterface $templating)
+ {
+ $this->name = $name;
+ $this->templating = $templating;
+ }
+
+ /**
+ * Render a view
+ *
+ * @param string $view
+ * @param array $parameters
+ * @param \Symfony\Component\HttpFoundation\Response $response
+ * @return string
+ */
+ public function renderResponse($view, array $parameters = array(), Response $response = null)
+ {
+ return $this->getTemplating()->renderResponse($view, $parameters, $response);
+ }
+
+ /**
+ * Get name
+ *
+ * @return string name
+ */
+ public function getName()
+ {
+ return $this->name;
+ }
+
+ /**
+ * Get templating
+ *
+ * @return \Symfony\Component\Templating\EngineInterface
+ */
+ public function getTemplating()
+ {
+ return $this->templating;
+ }
+
+ /**
+ * Build form
+ *
+ * @param \Sonata\AdminBundle\Form\FormMapper $formMapper
+ * @param \Sonata\BlockBundle\Model\BlockInterface $block
+ * @return void
+ */
+ public function buildCreateForm(FormMapper $formMapper, BlockInterface $block)
+ {
+ $this->buildEditForm($formMapper, $block);
+ }
+
+ /**
+ * @param \Sonata\BlockBundle\Model\BlockInterface $block
+ * @return void
+ */
+ public function prePersist(BlockInterface $block)
+ {
+ }
+
+ /**
+ * @param \Sonata\BlockBundle\Model\BlockInterface $block
+ * @return void
+ */
+ public function postPersist(BlockInterface $block)
+ {
+ }
+
+ /**
+ * @param \Sonata\BlockBundle\Model\BlockInterface $block
+ * @return void
+ */
+ public function preUpdate(BlockInterface $block)
+ {
+ }
+
+ /**
+ * @param \Sonata\BlockBundle\Model\BlockInterface $block
+ * @return void
+ */
+ public function postUpdate(BlockInterface $block)
+ {
+ }
+
+ /**
+ * @param \Sonata\BlockBundle\Model\BlockInterface $block
+ * @return void
+ */
+ public function preDelete(BlockInterface $block)
+ {
+ }
+
+ /**
+ * @param \Sonata\BlockBundle\Model\BlockInterface $block
+ * @return void
+ */
+ public function postDelete(BlockInterface $block)
+ {
+ }
+
+ /**
+ * @param \Sonata\BlockBundle\Model\BlockInterface $block
+ * @return void
+ */
+ public function load(BlockInterface $block)
+ {
+ }
+
+ /**
+ * @return array
+ */
+ function getJavacripts($media)
+ {
+ return array();
+ }
+
+ /**
+ * @return array
+ */
+ function getStylesheets($media)
+ {
+ return array();
+ }
+}
@@ -0,0 +1,22 @@
+<?php
+/*
+ * This file is part of the Sonata project.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+
+namespace Sonata\BlockBundle\Block;
+
+interface BlockManagerAwareInterface
+{
+ /**
+ * @abstract
+ * @param BlockManagerServiceInterface $service
+ * @return void
+ */
+ function setBlockManager(BlockManagerServiceInterface $service);
+}
@@ -0,0 +1,144 @@
+<?php
+/*
+ * This file is part of the Sonata project.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+
+namespace Sonata\BlockBundle\Block;
+
+use Sonata\BlockBundle\Model\BlockInterface;
+use Sonata\BlockBundle\Block\BlockManagerAwareInterface;
+
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpKernel\Log\LoggerInterface;
+
+class BlockManagerService implements BlockManagerServiceInterface
+{
+ protected $logger;
+
+ protected $blockServices;
+
+ protected $debug;
+
+ /**
+ * @param $debug
+ * @param null|\Symfony\Component\HttpKernel\Log\LoggerInterface $logger
+ */
+ public function __construct($debug, LoggerInterface $logger = null)
+ {
+ $this->debug = $debug;
+ $this->logger = $logger;
+ $this->blockServices = array();
+ }
+
+ /**
+ * Render a specialize block
+ *
+ * @throws \Exception|\RuntimeException
+ * @param \Sonata\BlockBundle\Model\BlockInterface $block
+ * @return \Symfony\Component\HttpFoundation\Response|void
+ */
+ public function renderBlock(BlockInterface $block)
+ {
+ if ($this->logger) {
+ $this->logger->info(sprintf('[cms::renderBlock] block.id=%d, block.type=%s ', $block->getId(), $block->getType()));
+ }
+
+ $response = new Response;
+
+ try {
+ $service = $this->getBlockService($block);
+
+ if ($service instanceof BlockManagerAwareInterface) {
+ $service->setBlockManager($this);
+ }
+
+ $service->load($block); // load the block
+
+ $response = $service->execute($block, $response);
+
+ if (!$response instanceof Response) {
+ throw new \RuntimeException('A block service must return a Response object');
+ }
+
+ } catch (\Exception $e) {
+ if ($this->logger) {
+ $this->logger->crit(sprintf('[cms::renderBlock] block.id=%d - error while rendering block - %s', $block->getId(), $e->getMessage()));
+ }
+
+ if ($this->debug) {
+ throw $e;
+ }
+
+ $response->setPrivate();
+ }
+
+ return $response;
+ }
+
+ /**
+ * Return the block service linked to the link
+ *
+ * @throws \RuntimeException
+ * @param \Sonata\BlockBundle\Model\BlockInterface $block
+ * @return \Sonata\BlockBundle\Block\BlockServiceInterface
+ */
+ public function getBlockService(BlockInterface $block)
+ {
+ if (!$this->hasBlockService($block->getType())) {
+ if ($this->debug) {
+ throw new \RuntimeException(sprintf('The block service `%s` referenced in the block `%s` does not exists', $block->getType(), $block->getId()));
+ }
+
+ if ($this->logger){
+ $this->logger->crit(sprintf('[cms::getBlockService] block.id=%d - service:%s does not exists', $block->getId(), $block->getType()));
+ }
+
+ return false;
+ }
+
+ return $this->blockServices[$block->getType()];
+ }
+
+ /**
+ *
+ * @param sring $id
+ * @return boolean
+ */
+ public function hasBlockService($id)
+ {
+ return isset($this->blockServices[$id]) ? true : false;
+ }
+
+ /**
+ * @param $name
+ * @param \Sonata\BlockBundle\Block\BlockServiceInterface $service
+ * @return void
+ */
+ public function addBlockService($name, BlockServiceInterface $service)
+ {
+ $this->blockServices[$name] = $service;
+ }
+
+ /**
+ * @param array $blockServices
+ * @return void
+ */
+ public function setBlockServices(array $blockServices)
+ {
+ $this->blockServices = $blockServices;
+ }
+
+ /**
+ * @return array
+ */
+ public function getBlockServices()
+ {
+ return $this->blockServices;
+ }
+}
@@ -0,0 +1,60 @@
+<?php
+/*
+ * This file is part of the Sonata project.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+
+namespace Sonata\BlockBundle\Block;
+
+use Sonata\BlockBundle\Model\BlockInterface;
+
+interface BlockManagerServiceInterface
+{
+
+ /**
+ * @abstract
+ * @param $name
+ * @param BlockServiceInterface $service
+ * @return void
+ */
+ function addBlockService($name, BlockServiceInterface $service);
+
+ /**
+ * Render a specialize block
+ *
+ * @param \Sonata\BlockBundle\Model\BlockInterface $block
+ * @return void
+ */
+ function renderBlock(BlockInterface $block);
+
+ /**
+ * Return the block service linked to the link
+ *
+ * @param \Sonata\BlockBundle\Model\BlockInterface $block
+ * @return void
+ */
+ function getBlockService(BlockInterface $block);
+
+ /**
+ * @param array $blockServices
+ * @return void
+ */
+ function setBlockServices(array $blockServices);
+
+ /**
+ * @return array
+ */
+ function getBlockServices();
+
+ /**
+ *
+ * @param string $name
+ * @return boolean
+ */
+ function hasBlockService($name);
+}
Oops, something went wrong.

0 comments on commit 1fb1d4a

Please sign in to comment.