Skip to content

Commit

Permalink
Upgrade Sonatablock and SonataSeo
Browse files Browse the repository at this point in the history
  • Loading branch information
Daric971 committed Jul 13, 2022
1 parent bc84b13 commit 541fa4b
Show file tree
Hide file tree
Showing 24 changed files with 293 additions and 564 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
"doctrine/doctrine-bundle": "^1.12.3 || ^2.0",
"doctrine/persistence": "^1.3.4 || ^2.0",
"sonata-project/admin-bundle": "^3.99",
"sonata-project/block-bundle": "^3.20",
"sonata-project/block-bundle": "^4.0",
"sonata-project/doctrine-extensions": "^1.8",
"sonata-project/doctrine-orm-admin-bundle": "^3.19",
"sonata-project/form-extensions": "^0.1.1 || ^1.4",
"sonata-project/notification-bundle": "^3.8",
"sonata-project/seo-bundle": "^2.11",
"sonata-project/seo-bundle": "^3.0",
"sonata-project/twig-extensions": "^0.1.1 || ^1.3",
"symfony-cmf/routing-bundle": "^2.1",
"symfony/config": "^4.4",
Expand Down
17 changes: 3 additions & 14 deletions src/Admin/BaseBlockAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,9 @@ abstract class BaseBlockAdmin extends AbstractAdmin
*/
protected $blockManager;

/**
* @var bool
*/
protected $inValidate = false;
protected bool $inValidate = false;

/**
* @var array
*/
protected $containerBlockTypes = [];
protected array $containerBlockTypes = [];

/**
* @param BaseBlock $object
Expand Down Expand Up @@ -255,12 +249,7 @@ private function loadBlockDefaults(BlockInterface $block)

$resolver = new OptionsResolver();
// use new interface method whenever possible
// NEXT_MAJOR: Remove this check and legacy setDefaultSettings method call
if (method_exists($service, 'configureSettings')) {
$service->configureSettings($resolver);
} else {
$service->setDefaultSettings($resolver);
}
$service->configureSettings($resolver);

try {
$block->setSettings($resolver->resolve($block->getSettings()));
Expand Down
43 changes: 11 additions & 32 deletions src/Admin/BlockAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Doctrine\ORM\EntityRepository;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\BlockBundle\Block\BlockServiceInterface;
use Sonata\BlockBundle\Block\Service\BlockServiceInterface;
use Sonata\BlockBundle\Block\Service\EditableBlockService;
use Sonata\BlockBundle\Form\Type\ServiceListType;
use Sonata\BlockBundle\Model\BlockInterface;
Expand All @@ -32,16 +32,9 @@
* Admin class for the Block model.
*
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* @final since sonata-project/page-bundle 3.26
*/
class BlockAdmin extends BaseBlockAdmin
final class BlockAdmin extends BaseBlockAdmin
{
/**
* @var array
*/
protected $blocks;

protected $classnameLabel = 'Block';

protected $accessMapping = [
Expand All @@ -50,12 +43,9 @@ class BlockAdmin extends BaseBlockAdmin
'composePreview' => 'EDIT',
];

/**
* @param string $code
* @param string $class
* @param string $baseControllerName
*/
public function __construct($code, $class, $baseControllerName, array $blocks = [])
private array $blocks;

public function __construct(string $code, string $class, string $baseControllerName, array $blocks = [])
{
parent::__construct($code, $class, $baseControllerName);

Expand Down Expand Up @@ -150,9 +140,9 @@ protected function configureFormFields(FormMapper $form): void
return $repository->createQueryBuilder('a')
->andWhere('a.page = :page AND a.type IN (:types)')
->setParameters([
'page' => $page,
'types' => $containerBlockTypes,
]);
'page' => $page,
'types' => $containerBlockTypes,
]);
},
], [
'admin_code' => $this->getCode(),
Expand Down Expand Up @@ -186,24 +176,13 @@ protected function configureFormFields(FormMapper $form): void
}
}

/**
* @return string|null
*/
private function getDefaultTemplate(BlockServiceInterface $blockService)
private function getDefaultTemplate(BlockServiceInterface $blockService): ?string
{
$resolver = new OptionsResolver();
// use new interface method whenever possible
// NEXT_MAJOR: Remove this check and legacy setDefaultSettings method call
if (method_exists($blockService, 'configureSettings')) {
$blockService->configureSettings($resolver);
} else {
$blockService->setDefaultSettings($resolver);
}
$blockService->configureSettings($resolver);
$options = $resolver->resolve();

if (isset($options['template'])) {
return $options['template'];
}
return $options['template'] ?? null;
}

private function configureBlockFields(FormMapper $form, BlockInterface $block): void
Expand Down
10 changes: 4 additions & 6 deletions src/Admin/SharedBlockAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\BlockBundle\Block\BlockServiceInterface;
use Sonata\BlockBundle\Block\Service\BlockServiceInterface;
use Sonata\BlockBundle\Block\Service\EditableBlockService;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\PageBundle\Entity\BaseBlock;
Expand All @@ -26,22 +26,20 @@
* Admin class for shared Block model.
*
* @author Romain Mouillard <romain.mouillard@gmail.com>
*
* @final since sonata-project/page-bundle 3.26
*/
class SharedBlockAdmin extends BaseBlockAdmin
final class SharedBlockAdmin extends BaseBlockAdmin
{
/**
* @var string
*/
protected $classnameLabel = 'shared_block';

public function getBaseRoutePattern()
public function getBaseRoutePattern(): string
{
return sprintf('%s/%s', parent::getBaseRoutePattern(), 'shared');
}

public function getBaseRouteName()
public function getBaseRouteName(): string
{
return sprintf('%s/%s', parent::getBaseRouteName(), 'shared');
}
Expand Down
65 changes: 52 additions & 13 deletions src/Block/BlockContextManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,71 @@

namespace Sonata\PageBundle\Block;

use Sonata\BlockBundle\Block\BlockContextInterface;
use Sonata\BlockBundle\Block\BlockContextManager as BaseBlockContextManager;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\BlockBundle\Block\BlockContextManagerInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* NEXT_MAJOR: Do not extend from `BlockContextManager` since it will be final.
*
* @psalm-suppress InvalidExtendClass
* @phpstan-ignore-next-line
*
* @final since sonata-project/page-bundle 3.26
*/
class BlockContextManager extends BaseBlockContextManager
final class BlockContextManager implements BlockContextManagerInterface
{
protected function configureSettings(OptionsResolver $optionsResolver, BlockInterface $block): void
private BaseBlockContextManager $blockContextManager;

private OptionsResolver $optionsResolver;

public function __construct(BaseBlockContextManager $blockContextManager)
{
$this->blockContextManager = $blockContextManager;
$this->optionsResolver = new OptionsResolver();
}

public function getOptionsResolver(): OptionsResolver
{
return $this->optionsResolver;
}

/**
* @return BaseBlockContextManager|BlockContextManagerInterface
*/
public function getBlockContextManager()
{
return $this->blockContextManager;
}

public function addSettingsByType(string $type, array $settings, bool $replace = false): void
{
parent::configureSettings($optionsResolver, $block);
$this->blockContextManager->addSettingsByType($type, $settings, $replace);
}

public function addSettingsByClass(string $class, array $settings, bool $replace = false): void
{
$this->blockContextManager->addSettingsByClass($class, $settings, $replace);
}

public function get($meta, array $settings = []): BlockContextInterface
{
return $this->blockContextManager->get($meta, [
'manager' => false,
'page_id' => false,
]);
}

public function exists(string $type): bool
{
return $this->blockContextManager->exists($type);
}

private function configureSettings(OptionsResolver $optionsResolver): void
{
$optionsResolver->setDefaults([
'manager' => false,
'page_id' => false,
'attr' => [],
]);

$optionsResolver
->addAllowedTypes('manager', ['string', 'bool'])
->addAllowedTypes('page_id', ['int', 'string', 'bool']);
->addAllowedTypes('page_id', ['int', 'string', 'bool'])
->addAllowedTypes('attr', 'array');

$optionsResolver->setRequired([
'manager',
Expand Down
42 changes: 18 additions & 24 deletions src/Block/BreadcrumbBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,56 +15,52 @@

use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Knp\Menu\Provider\MenuProviderInterface;
use Sonata\BlockBundle\Block\BlockContextInterface;
use Sonata\BlockBundle\Meta\Metadata;
use Sonata\BlockBundle\Meta\MetadataInterface;
use Sonata\PageBundle\CmsManager\CmsManagerSelectorInterface;
use Sonata\PageBundle\Model\PageInterface;
use Sonata\SeoBundle\Block\Breadcrumb\BaseBreadcrumbMenuBlockService;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Twig\Environment;

/**
* BlockService for homepage breadcrumb.
*
* @author Sylvain Deloux <sylvain.deloux@ekino.com>
*
* @final since sonata-project/page-bundle 3.26
*/
class BreadcrumbBlockService extends BaseBreadcrumbMenuBlockService
final class BreadcrumbBlockService extends BaseBreadcrumbMenuBlockService
{
/**
* @var CmsManagerSelectorInterface
*/
protected $cmsSelector;
private CmsManagerSelectorInterface $cmsSelector;

/**
* @param string $context
* @param string $name
*/
public function __construct($context, $name, EngineInterface $templating, MenuProviderInterface $menuProvider, FactoryInterface $factory, CmsManagerSelectorInterface $cmsSelector)
public function __construct(Environment $twig, FactoryInterface $factory, CmsManagerSelectorInterface $cmsSelector)
{
$this->cmsSelector = $cmsSelector;
parent::__construct($twig, $factory);

parent::__construct($context, $name, $templating, $menuProvider, $factory);
$this->cmsSelector = $cmsSelector;
}

public function getName()
public function getName(): string
{
return 'sonata.page.block.breadcrumb';
}

public function getBlockMetadata($code = null)
public function getMetadata($code = null): MetadataInterface
{
return new Metadata($this->getName(), (null !== $code ? $code : $this->getName()), false, 'SonataPageBundle', [
return new Metadata($this->getName(), (null !== $code ? $code : $this->getName()), null, 'SonataPageBundle', [
'class' => 'fa fa-bars',
]);
}

protected function getMenu(BlockContextInterface $blockContext)
public function handleContext(string $context): bool
{
return $this->getName() === $context;
}

protected function getMenu(BlockContextInterface $blockContext): ItemInterface
{
$blockContext->setSetting('include_homepage_link', false);

$menu = $this->getRootMenu($blockContext);
$menu = parent::getMenu($blockContext);

$page = $this->getCurrentPage();

Expand All @@ -91,10 +87,8 @@ protected function getMenu(BlockContextInterface $blockContext)

/**
* Return the current Page.
*
* @return PageInterface
*/
protected function getCurrentPage()
protected function getCurrentPage(): PageInterface
{
$cms = $this->cmsSelector->retrieve();

Expand Down
2 changes: 1 addition & 1 deletion src/Block/ChildrenPagesBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __construct(Environment $twig, SiteSelectorInterface $siteSelect
$this->cmsManagerSelector = $cmsManagerSelector;
}

public function execute(BlockContextInterface $blockContext, ?Response $response = null)
public function execute(BlockContextInterface $blockContext, ?Response $response = null): Response
{
$settings = $blockContext->getSettings();

Expand Down
Loading

0 comments on commit 541fa4b

Please sign in to comment.