Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clear compiled option for versions, and allow disable recompile g… #285

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cms/contents/page/translations/sfs_cms_contents.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ admin_page:
save.button: "Save changes"
cancel.button: "Cancel"
recompile.link: "Recompile"
clear_compiled.link: "Clear compiled content"
compiled:
title: "Compiled content"
error: "This content has some compilation errors, try to edit it to solve them or try to recompile"
Expand Down Expand Up @@ -366,6 +367,10 @@ admin_page:
success_flash: "Version has been recompiled"
failed_flash: "An error has been produced during the version recompilation.<br/><small>%exception%</small>"

version_clear_compiled:
success_flash: "Version compiled content has been cleared"
failed_flash: "An error has been produced during the version content clearing.<br/><small>%exception%</small>"

version_delete:
meta.title: "Delete version"
title: "%name% (v%versionNumber%)"
Expand Down
5 changes: 5 additions & 0 deletions cms/contents/page/translations/sfs_cms_contents.es.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ admin_page:
save.button: "Guardar cambios"
cancel.button: "Cancelar"
recompile.link: "Recompilar"
clear_compiled.link: "Limpiar contenido compilado"
compiled:
title: "Contenido compilado"
error: "Este contenido tiene errores en la compilación, trata de editarlo para resolverlos o de republicar"
Expand Down Expand Up @@ -366,6 +367,10 @@ admin_page:
success_flash: "La versión se ha recompilado"
failed_flash: "Se ha producido un error al recompilar la versión.<br/><small>%exception%</small>"

version_clear_compiled:
success_flash: "La versión se ha limpiado correctamente"
failed_flash: "Se ha producido un error al limpiar el contenido de la versión.<br/><small>%exception%</small>"

version_delete:
meta.title: "Eliminar versión"
title: "%name% (v%versionNumber%)"
Expand Down
8 changes: 7 additions & 1 deletion config/routing/admin_content_type.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ recompile_version:
defaults:
configKey: 'version_recompile'

clear_compiled_version:
controller: sfs_cms.admin.content_version.controller::apply
path: /{content}/{version}/clear-compiled
defaults:
configKey: 'version_clear_compiled'

version_preview:
controller: sfs_cms.admin.content_version.controller::read
path: /{content}/{version}/preview
Expand Down Expand Up @@ -142,4 +148,4 @@ delete_version:

_plugin_routes:
resource: '.'
type: sfs_cms_plugin_admin_content_type
type: sfs_cms_plugin_admin_content_type
3 changes: 2 additions & 1 deletion config/security/admin_role_hierarchy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ security:
- PERMISSION_SFS_CMS_ADMIN_CONTENT_UNPUBLISH
- PERMISSION_SFS_CMS_ADMIN_CONTENT_VERSION_INFO
- PERMISSION_SFS_CMS_ADMIN_CONTENT_RECOMPILE_VERSION
- PERMISSION_SFS_CMS_ADMIN_CONTENT_CLEAR_COMPILED_VERSION
- PERMISSION_SFS_CMS_ADMIN_CONTENT_DELETE_VERSION
ROLE_SFS_CMS_ADMIN_CONTENTS_RW:
- ROLE_SFS_CMS_ADMIN_CONTENTS_PUBLISHER
Expand Down Expand Up @@ -104,4 +105,4 @@ security:
- ROLE_SFS_CMS_ADMIN_CONTENTS_RO
ROLE_SFS_CMS_ADMIN_PAGES_RW:
- ROLE_SFS_CMS_ADMIN_PAGES_RO
- ROLE_SFS_CMS_ADMIN_CONTENTS_RW
- ROLE_SFS_CMS_ADMIN_CONTENTS_RW
2 changes: 2 additions & 0 deletions config/services/admin_services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ services:
Softspring\CmsBundle\Twig\Extension\Admin\:
resource: '../../src/Twig/Extension/Admin/*'
tags: [ 'twig.extension' ]
bind:
$recompileEnabled: '%sfs_cms.content.recompile_enabled%'

Softspring\CmsBundle\Admin\Routing\AdminRoutingLoader:
arguments:
Expand Down
2 changes: 1 addition & 1 deletion config/services/controller/admin_content.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,4 @@ services:
not_found_event_name: !php/const Softspring\CmsBundle\SfsCmsEvents::ADMIN_CONTENTS_PREVIEW_NOT_FOUND
found_event_name: !php/const Softspring\CmsBundle\SfsCmsEvents::ADMIN_CONTENTS_PREVIEW_FOUND
view_event_name: !php/const Softspring\CmsBundle\SfsCmsEvents::ADMIN_CONTENTS_PREVIEW_VIEW
exception_event_name: !php/const Softspring\CmsBundle\SfsCmsEvents::ADMIN_CONTENTS_PREVIEW_EXCEPTION
exception_event_name: !php/const Softspring\CmsBundle\SfsCmsEvents::ADMIN_CONTENTS_PREVIEW_EXCEPTION
14 changes: 13 additions & 1 deletion config/services/controller/admin_content_version.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ services:

Softspring\CmsBundle\EventListener\Admin\ContentVersion\:
resource: '../../src/EventListener/Admin/ContentVersion/*'
bind:
$recompileEnabled: '%sfs_cms.content.recompile_enabled%'

Softspring\CmsBundle\Form\Admin\ContentVersion\:
resource: '../../../src/Form/Admin/ContentVersion/*'
Expand Down Expand Up @@ -107,6 +109,16 @@ services:
success_event_name: !php/const Softspring\CmsBundle\SfsCmsEvents::ADMIN_CONTENT_VERSIONS_CLEANUP_SUCCESS
failure_event_name: !php/const Softspring\CmsBundle\SfsCmsEvents::ADMIN_CONTENT_VERSIONS_CLEANUP_FAILURE
exception_event_name: !php/const Softspring\CmsBundle\SfsCmsEvents::ADMIN_CONTENT_VERSIONS_CLEANUP_EXCEPTION
version_clear_compiled:
param_converter_key: 'id'
initialize_event_name: !php/const Softspring\CmsBundle\SfsCmsEvents::ADMIN_CONTENT_VERSIONS_CLEAR_COMPILED_INITIALIZE
load_entity_event_name: !php/const Softspring\CmsBundle\SfsCmsEvents::ADMIN_CONTENT_VERSIONS_CLEAR_COMPILED_LOAD_ENTITY
not_found_event_name: !php/const Softspring\CmsBundle\SfsCmsEvents::ADMIN_CONTENT_VERSIONS_CLEAR_COMPILED_NOT_FOUND
found_event_name: !php/const Softspring\CmsBundle\SfsCmsEvents::ADMIN_CONTENT_VERSIONS_CLEAR_COMPILED_FOUND
apply_event_name: !php/const Softspring\CmsBundle\SfsCmsEvents::ADMIN_CONTENT_VERSIONS_CLEAR_COMPILED_APPLY
success_event_name: !php/const Softspring\CmsBundle\SfsCmsEvents::ADMIN_CONTENT_VERSIONS_CLEAR_COMPILED_SUCCESS
failure_event_name: !php/const Softspring\CmsBundle\SfsCmsEvents::ADMIN_CONTENT_VERSIONS_CLEAR_COMPILED_FAILURE
exception_event_name: !php/const Softspring\CmsBundle\SfsCmsEvents::ADMIN_CONTENT_VERSIONS_CLEAR_COMPILED_EXCEPTION
version_info:
param_converter_key: 'id'
initialize_event_name: !php/const Softspring\CmsBundle\SfsCmsEvents::ADMIN_CONTENT_VERSIONS_INFO_INITIALIZE
Expand Down Expand Up @@ -148,4 +160,4 @@ services:
failure_event_name: !php/const Softspring\CmsBundle\SfsCmsEvents::ADMIN_CONTENT_VERSIONS_DELETE_FAILURE
form_invalid_event_name: !php/const Softspring\CmsBundle\SfsCmsEvents::ADMIN_CONTENT_VERSIONS_DELETE_FORM_INVALID
view_event_name: !php/const Softspring\CmsBundle\SfsCmsEvents::ADMIN_CONTENT_VERSIONS_DELETE_VIEW
exception_event_name: !php/const Softspring\CmsBundle\SfsCmsEvents::ADMIN_CONTENT_VERSIONS_DELETE_EXCEPTION
exception_event_name: !php/const Softspring\CmsBundle\SfsCmsEvents::ADMIN_CONTENT_VERSIONS_DELETE_EXCEPTION
7 changes: 6 additions & 1 deletion config/services/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ services:
Softspring\CmsBundle\Security\Voter\ContentVersionDeleteVoter:
tags: ['security.voter']

Softspring\CmsBundle\Security\Voter\ContentVersionVoter:
arguments:
$recompileEnabled: '%sfs_cms.content.recompile_enabled%'
tags: ['security.voter']

Softspring\CmsBundle\Security\Voter\DeprecatedPermissionVoter:
tags:
- { name: security.voter, priority: 1000 }
- { name: security.voter, priority: 1000 }
6 changes: 6 additions & 0 deletions src/Config/Model/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,12 @@ public function getConfigTreeBuilder(): TreeBuilder
->scalarNode('is_granted')->defaultValue('PERMISSION_SFS_CMS_ADMIN_CONTENT_RECOMPILE_VERSION')->end()
->end()
->end()
->arrayNode('version_clear_compiled')
->addDefaultsIfNotSet()
->children()
->scalarNode('is_granted')->defaultValue('PERMISSION_SFS_CMS_ADMIN_CONTENT_CLEAR_COMPILED_VERSION')->end()
->end()
->end()
->arrayNode('export_version')
->addDefaultsIfNotSet()
->children()
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->booleanNode('save_compiled')->defaultTrue()->end()
->scalarNode('prefix_compiled')->defaultValue('')->end()
->booleanNode('cache_last_modified')->defaultFalse()->end()
->booleanNode('recompile')->defaultTrue()->end()
->end()
->end()

Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/SfsCmsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function load(array $configs, ContainerBuilder $container): void
$container->setParameter('sfs_cms.content.prefix_compiled', $config['content']['prefix_compiled'] ?? null);
$container->setParameter('sfs_cms.content.page_class', $config['content']['page_class'] ?? null);
$container->setParameter('sfs_cms.content.cache_last_modified_enabled', $config['content']['cache_last_modified'] ?? false);
$container->setParameter('sfs_cms.content.recompile_enabled', $config['content']['recompile_enabled'] ?? false);

// configure menu classes
$container->setParameter('sfs_cms.menu.class', $config['menu']['class']);
Expand Down
131 changes: 131 additions & 0 deletions src/EventListener/Admin/ContentVersion/ClearCompiledListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

namespace Softspring\CmsBundle\EventListener\Admin\ContentVersion;

use Softspring\CmsBundle\Config\CmsConfig;
use Softspring\CmsBundle\Manager\ContentManagerInterface;
use Softspring\CmsBundle\Manager\ContentVersionManagerInterface;
use Softspring\CmsBundle\Manager\RouteManagerInterface;
use Softspring\CmsBundle\Model\ContentVersionInterface;
use Softspring\CmsBundle\Render\ContentVersionCompiler;
use Softspring\CmsBundle\Request\FlashNotifier;
use Softspring\CmsBundle\SfsCmsEvents;
use Softspring\Component\CrudlController\Event\ApplyEvent;
use Softspring\Component\CrudlController\Event\ExceptionEvent;
use Softspring\Component\CrudlController\Event\FailureEvent;
use Softspring\Component\CrudlController\Event\SuccessEvent;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

class ClearCompiledListener extends AbstractContentVersionListener
{
protected const ACTION_NAME = 'version_clear_compiled';

public function __construct(
ContentManagerInterface $contentManager,
ContentVersionManagerInterface $contentVersionManager,
RouteManagerInterface $routeManager,
CmsConfig $cmsConfig,
RouterInterface $router,
FlashNotifier $flashNotifier,
AuthorizationCheckerInterface $authorizationChecker,
protected ContentVersionCompiler $contentVersionCompiler,
) {
parent::__construct($contentManager, $contentVersionManager, $routeManager, $cmsConfig, $router, $flashNotifier, $authorizationChecker);
}

public static function getSubscribedEvents(): array
{
return [
SfsCmsEvents::ADMIN_CONTENT_VERSIONS_CLEAR_COMPILED_INITIALIZE => [
['onInitializeGetConfig', 20],
['onEventDispatchContentTypeEvent', 10],
['onEventLoadContentEntity', 9],
['onInitializeIsGranted', 0],
],
SfsCmsEvents::ADMIN_CONTENT_VERSIONS_CLEAR_COMPILED_LOAD_ENTITY => [
['onEventDispatchContentTypeEvent', 10],
['onLoadEntity', 0],
],
SfsCmsEvents::ADMIN_CONTENT_VERSIONS_CLEAR_COMPILED_NOT_FOUND => [
['onEventDispatchContentTypeEvent', 10],
['onNotFound', 0],
],
SfsCmsEvents::ADMIN_CONTENT_VERSIONS_CLEAR_COMPILED_FOUND => [
['onEventDispatchContentTypeEvent', 10],
],
SfsCmsEvents::ADMIN_CONTENT_VERSIONS_CLEAR_COMPILED_APPLY => [
['onEventDispatchContentTypeEvent', 10],
['onApply', 0],
],
SfsCmsEvents::ADMIN_CONTENT_VERSIONS_CLEAR_COMPILED_SUCCESS => [
['onEventDispatchContentTypeEvent', 10],
['onSuccess', 0],
],
SfsCmsEvents::ADMIN_CONTENT_VERSIONS_CLEAR_COMPILED_FAILURE => [
['onEventDispatchContentTypeEvent', 10],
['onFailure', 0],
],
SfsCmsEvents::ADMIN_CONTENT_VERSIONS_CLEAR_COMPILED_EXCEPTION => [
['onEventDispatchContentTypeEvent', 10],
['onException', 0],
],
];
}

public function onApply(ApplyEvent $event): void
{
/** @var ContentVersionInterface $entity */
$entity = $event->getEntity();

$entity->setKeep($event->getRequest()->attributes->get('recompile') ?: false);

$this->contentVersionCompiler->clearCompiled($entity);

$this->contentVersionManager->saveEntity($entity);

$event->setApplied(true);
}

public function onSuccess(SuccessEvent $event): void
{
$contentConfig = $event->getRequest()->attributes->get('_content_config');

/** @var ContentVersionInterface $version */
$version = $event->getEntity();

$this->flashNotifier->addTrans('success', "admin_{$contentConfig['_id']}.version_clear_compiled.success_flash", [], 'sfs_cms_contents');

$content = $event->getRequest()->attributes->get('content');

$event->setResponse($this->redirectBack($contentConfig['_id'], $content, $event->getRequest(), $version));
}

public function onFailure(FailureEvent $event): void
{
$contentConfig = $event->getRequest()->attributes->get('_content_config');

/** @var ContentVersionInterface $version */
$version = $event->getEntity();

$this->flashNotifier->addTrans('error', "admin_{$contentConfig['_id']}.version_clear_compiled.failed_flash", ['%exception%' => $this->extractExceptionMessage($event->getException())], 'sfs_cms_contents');

$content = $event->getRequest()->attributes->get('content');

$event->setResponse($this->redirectBack($contentConfig['_id'], $content, $event->getRequest(), $version));
}

public function onException(ExceptionEvent $event): void
{
$contentConfig = $event->getRequest()->attributes->get('_content_config');

/** @var ?ContentVersionInterface $version */
$version = $event->getRequest()->attributes->get('version');

$this->flashNotifier->addTrans('error', "admin_{$contentConfig['_id']}.version_clear_compiled.failed_flash", ['%exception%' => $this->extractExceptionMessage($event->getException())], 'sfs_cms_contents');

$content = $event->getRequest()->attributes->get('content');

$event->setResponse($this->redirectBack($contentConfig['_id'], $content, $event->getRequest(), $version));
}
}
11 changes: 11 additions & 0 deletions src/EventListener/Admin/ContentVersion/RecompileListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
use Softspring\Component\CrudlController\Event\ApplyEvent;
use Softspring\Component\CrudlController\Event\ExceptionEvent;
use Softspring\Component\CrudlController\Event\FailureEvent;
use Softspring\Component\CrudlController\Event\InitializeEvent;
use Softspring\Component\CrudlController\Event\SuccessEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

Expand All @@ -30,6 +32,7 @@ public function __construct(
FlashNotifier $flashNotifier,
AuthorizationCheckerInterface $authorizationChecker,
protected ContentVersionCompiler $contentVersionCompiler,
protected bool $recompileEnabled,
) {
parent::__construct($contentManager, $contentVersionManager, $routeManager, $cmsConfig, $router, $flashNotifier, $authorizationChecker);
}
Expand All @@ -38,6 +41,7 @@ public static function getSubscribedEvents(): array
{
return [
SfsCmsEvents::ADMIN_CONTENT_VERSIONS_RECOMPILE_INITIALIZE => [
['onInitializeCheckEnabled', 20],
['onInitializeGetConfig', 20],
['onEventDispatchContentTypeEvent', 10],
['onEventLoadContentEntity', 9],
Expand Down Expand Up @@ -73,6 +77,13 @@ public static function getSubscribedEvents(): array
];
}

public function onInitializeCheckEnabled(InitializeEvent $event): void
{
if (!$this->recompileEnabled) {
throw new NotFoundHttpException('Recompile is disabled');
}
}

public function onApply(ApplyEvent $event): void
{
/** @var ContentVersionInterface $entity */
Expand Down
13 changes: 13 additions & 0 deletions src/Render/AbstractRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Softspring\CmsBundle\Render;

use Exception;
use Softspring\CmsBundle\Model\ContentInterface;
use Softspring\CmsBundle\Model\SiteInterface;
use Softspring\CmsBundle\Render\Exception\RenderException;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -81,6 +82,18 @@ protected function encapsulateRequestRender(Request $request, callable $renderFu
return $result;
}

public static function generateRequestForContent(ContentInterface $content, string $locale, SiteInterface $site, bool $preview = false): Request
{
$request = self::generateRequest($locale, $site, $preview);

if ($routePath = $content->getCanonicalRoutePath($locale)) {
$request->attributes->set('routePath', $routePath);
$request->attributes->set('_route', $routePath->getRoute()->getId());
}

return $request;
}

public static function generateRequest(string $locale, SiteInterface $site, bool $preview = false): Request
{
$parameters = [];
Expand Down
12 changes: 7 additions & 5 deletions src/Render/ContentVersionCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public function __construct(
) {
}

public function clearCompiled(ContentVersionInterface $contentVersion): void
{
$contentVersion->setCompiled([]);
$contentVersion->setCompiledModules([]);
}

/**
* @throws InvalidLayoutException
* @throws CompileException
Expand All @@ -42,11 +48,7 @@ public function compileAll(ContentVersionInterface $contentVersion): array
foreach ($contentVersion->getContent()->getLocales() as $locale) {
$this->cmsLogger && $this->cmsLogger->debug(sprintf('Compiling "%s" content version for "%s" in "%s"', $contentVersion->getContent()->getName(), "$site", $locale));

$request = ContentVersionRenderer::generateRequest($locale, $site);

if ($routePath = $contentVersion->getContent()->getCanonicalRoutePath($locale)) {
$request->attributes->set('routePath', $routePath);
}
$request = ContentVersionRenderer::generateRequestForContent($contentVersion->getContent(), $locale, $site);

$compileKey = $this->getCompileKeyFromRequest($contentVersion, $request);

Expand Down
Loading