Skip to content

Commit 39a0e57

Browse files
committed
Initialize ChannelLocale scopes
1 parent 6ec0be4 commit 39a0e57

File tree

12 files changed

+524
-0
lines changed

12 files changed

+524
-0
lines changed

src/Command/ScopeInitCommand.php

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace Sherlockode\SyliusAdvancedContentPlugin\Command;
4+
5+
use Sherlockode\AdvancedContentBundle\Manager\ConfigurationManager;
6+
use Sherlockode\SyliusAdvancedContentPlugin\Scope\ScopeInitializer;
7+
use Symfony\Component\Console\Command\Command;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
use Symfony\Component\Console\Style\SymfonyStyle;
11+
use Symfony\Contracts\Translation\TranslatorInterface;
12+
13+
class ScopeInitCommand extends Command
14+
{
15+
/**
16+
* @var ConfigurationManager
17+
*/
18+
private $configurationManager;
19+
20+
/**
21+
* @var TranslatorInterface
22+
*/
23+
private $translator;
24+
25+
/**
26+
* @var ScopeInitializer
27+
*/
28+
private $scopeInitializer;
29+
30+
/**
31+
* @param ConfigurationManager $configurationManager
32+
* @param TranslatorInterface $translator
33+
* @param ScopeInitializer $scopeInitializer
34+
* @param $name
35+
*/
36+
public function __construct(
37+
ConfigurationManager $configurationManager,
38+
TranslatorInterface $translator,
39+
ScopeInitializer $scopeInitializer,
40+
$name = null
41+
) {
42+
parent::__construct($name);
43+
$this->configurationManager = $configurationManager;
44+
$this->translator = $translator;
45+
$this->scopeInitializer = $scopeInitializer;
46+
}
47+
48+
protected function configure()
49+
{
50+
$this
51+
->setName('sherlockode:sylius-acb:init-scope')
52+
->setDescription('Initialize sylius scopes for ACB')
53+
;
54+
}
55+
56+
/**
57+
* @param InputInterface $input
58+
* @param OutputInterface $output
59+
*
60+
* @return int|void
61+
*/
62+
protected function execute(InputInterface $input, OutputInterface $output)
63+
{
64+
$io = new SymfonyStyle($input, $output);
65+
66+
if (!$this->configurationManager->isScopesEnabled()) {
67+
$io->info($this->translator->trans('sherlockode_sylius_acb.scopes.disabled'));
68+
69+
if (defined(sprintf('%s::SUCCESS', get_class($this)))) {
70+
return self::SUCCESS;
71+
}
72+
return;
73+
}
74+
if (!$this->scopeInitializer->hasMissingScopes()) {
75+
$io->info($this->translator->trans('sherlockode_sylius_acb.scopes.up_to_date'));
76+
77+
if (defined(sprintf('%s::SUCCESS', get_class($this)))) {
78+
return self::SUCCESS;
79+
}
80+
return;
81+
}
82+
83+
try {
84+
$this->scopeInitializer->init();
85+
$io->success($this->translator->trans('sherlockode_sylius_acb.scopes.init_success'));
86+
if (defined(sprintf('%s::SUCCESS', get_class($this)))) {
87+
return self::SUCCESS;
88+
}
89+
return;
90+
} catch (\Exception $e) {
91+
$io->error($e->getMessage());
92+
93+
if (defined(sprintf('%s::FAILURE', get_class($this)))) {
94+
return self::FAILURE;
95+
}
96+
}
97+
}
98+
}

src/Controller/ScopeController.php

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Sherlockode\SyliusAdvancedContentPlugin\Controller;
4+
5+
use Sherlockode\AdvancedContentBundle\Manager\ConfigurationManager;
6+
use Sherlockode\SyliusAdvancedContentPlugin\Scope\ScopeInitializer;
7+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
8+
use Symfony\Component\HttpFoundation\Request;
9+
use Symfony\Component\HttpFoundation\Response;
10+
use Symfony\Contracts\Translation\TranslatorInterface;
11+
12+
class ScopeController extends AbstractController
13+
{
14+
/**
15+
* @var ConfigurationManager
16+
*/
17+
private $configurationManager;
18+
19+
/**
20+
* @var ScopeInitializer
21+
*/
22+
private $scopeInitializer;
23+
24+
/**
25+
* @var TranslatorInterface
26+
*/
27+
private $translator;
28+
29+
/**
30+
* @param ConfigurationManager $configurationManager
31+
* @param ScopeInitializer $scopeInitializer
32+
* @param TranslatorInterface $translator
33+
*/
34+
public function __construct(
35+
ConfigurationManager $configurationManager,
36+
ScopeInitializer $scopeInitializer,
37+
TranslatorInterface $translator
38+
) {
39+
$this->configurationManager = $configurationManager;
40+
$this->scopeInitializer = $scopeInitializer;
41+
$this->translator = $translator;
42+
}
43+
44+
/**
45+
* @return Response
46+
*/
47+
public function updateScopesAction()
48+
{
49+
if (!$this->configurationManager->isScopesEnabled()) {
50+
$this->addFlash('error', $this->translator->trans('sherlockode_sylius_acb.scopes.disabled'));
51+
52+
return $this->redirectToRoute('sherlockode_acb_tools_index');
53+
}
54+
55+
if (!$this->scopeInitializer->hasMissingScopes()) {
56+
$this->addFlash('info', $this->translator->trans('sherlockode_sylius_acb.scopes.up_to_date'));
57+
58+
return $this->redirectToRoute('sherlockode_acb_tools_index');
59+
}
60+
61+
try {
62+
$this->scopeInitializer->init();
63+
$this->addFlash('success', $this->translator->trans('sherlockode_sylius_acb.scopes.init_success'));
64+
} catch (\Exception $e) {
65+
$this->addFlash('error', $e->getMessage());
66+
}
67+
68+
return $this->redirectToRoute('sherlockode_acb_tools_index');
69+
}
70+
}
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Sherlockode\SyliusAdvancedContentPlugin\EventListener;
4+
5+
use Sherlockode\AdvancedContentBundle\Manager\ConfigurationManager;
6+
use Sherlockode\SyliusAdvancedContentPlugin\Scope\ScopeInitializer;
7+
use Symfony\Component\EventDispatcher\GenericEvent;
8+
use Symfony\Component\HttpFoundation\Session\Session;
9+
use Symfony\Contracts\Translation\TranslatorInterface;
10+
11+
class AdminGridListener
12+
{
13+
/**
14+
* @var Session
15+
*/
16+
private $session;
17+
18+
/**
19+
* @var TranslatorInterface
20+
*/
21+
private $translator;
22+
23+
/**
24+
* @var ScopeInitializer
25+
*/
26+
private $scopeInitializer;
27+
28+
/**
29+
* @var ConfigurationManager
30+
*/
31+
private $configurationManager;
32+
33+
/**
34+
* @param Session $session
35+
* @param TranslatorInterface $translator
36+
* @param ScopeInitializer $scopeInitializer
37+
* @param ConfigurationManager $configurationManager
38+
*/
39+
public function __construct(
40+
Session $session,
41+
TranslatorInterface $translator,
42+
ScopeInitializer $scopeInitializer,
43+
ConfigurationManager $configurationManager
44+
) {
45+
$this->session = $session;
46+
$this->translator = $translator;
47+
$this->scopeInitializer = $scopeInitializer;
48+
$this->configurationManager = $configurationManager;
49+
}
50+
51+
/**
52+
* @param GenericEvent $event
53+
*/
54+
public function checkScopeInitialization(GenericEvent $event): void
55+
{
56+
if (!$this->configurationManager->isScopesEnabled()) {
57+
return;
58+
}
59+
if (!$this->scopeInitializer->hasMissingScopes()) {
60+
return;
61+
}
62+
63+
$this->session->getFlashBag()->add('info', $this->translator->trans('sherlockode_sylius_acb.scopes.missing_scopes'));
64+
}
65+
}

src/EventListener/ChannelListener.php

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Sherlockode\SyliusAdvancedContentPlugin\EventListener;
4+
5+
use Doctrine\ORM\Event\OnFlushEventArgs;
6+
use Sherlockode\AdvancedContentBundle\Manager\ConfigurationManager;
7+
use Sylius\Component\Core\Model\ChannelInterface;
8+
9+
class ChannelListener
10+
{
11+
/**
12+
* @var ConfigurationManager
13+
*/
14+
private $configurationManager;
15+
16+
/**
17+
* @param ConfigurationManager $configurationManager
18+
*/
19+
public function __construct(ConfigurationManager $configurationManager)
20+
{
21+
$this->configurationManager = $configurationManager;
22+
}
23+
24+
/**
25+
* @param OnFlushEventArgs $args
26+
*/
27+
public function onFlush(OnFlushEventArgs $args)
28+
{
29+
$em = $args->getEntityManager();
30+
$uow = $em->getUnitOfWork();
31+
32+
$entities = [
33+
...$uow->getScheduledEntityInsertions(),
34+
...$uow->getScheduledEntityUpdates()
35+
];
36+
37+
$scopeClass = $this->configurationManager->getEntityClass('scope');
38+
$scopeClassMetadata = $em->getClassMetadata($scopeClass);
39+
$scopeRepository = $em->getRepository($scopeClass);
40+
foreach ($entities as $entity) {
41+
if (!$entity instanceof ChannelInterface) {
42+
continue;
43+
}
44+
45+
$existingScopes = $scopeRepository->findBy([
46+
'channel' => $entity,
47+
]);
48+
49+
foreach ($entity->getLocales() as $locale) {
50+
foreach ($existingScopes as $key => $existingScope) {
51+
if ($locale->getId() === $existingScope->getLocale()->getId()) {
52+
unset($existingScopes[$key]);
53+
continue 2;
54+
}
55+
}
56+
57+
$scope = new $scopeClass;
58+
$scope->setChannel($entity);
59+
$scope->setLocale($locale);
60+
$em->persist($scope);
61+
$uow->computeChangeSet($scopeClassMetadata, $scope);
62+
}
63+
64+
foreach ($existingScopes as $existingScope) {
65+
$em->remove($existingScope);
66+
$uow->computeChangeSet($scopeClassMetadata, $existingScope);
67+
}
68+
}
69+
}
70+
}

src/Resources/config/admin_routing.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@ sherlockode_sylius_acb_admin_content:
2626
templates:
2727
form: "@SherlockodeSyliusAdvancedContentPlugin/admin/Content/_form.html.twig"
2828
type: sylius.resource
29+
30+
sherlockode_sylius_acb_admin_scope_init:
31+
path: "/scope/init"
32+
defaults:
33+
_controller: sherlockode_sylius_acb.controller.scope::updateScopesAction

src/Resources/config/events.yaml

+18
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,21 @@ sylius_ui:
99
blocks:
1010
sylius_acb:
1111
template: "@SherlockodeSyliusAdvancedContentPlugin/admin/stylesheets.html.twig"
12+
13+
services:
14+
sherlockode_sylius_acb.listener.grid:
15+
class: Sherlockode\SyliusAdvancedContentPlugin\EventListener\AdminGridListener
16+
arguments:
17+
- '@session'
18+
- '@translator'
19+
- '@sherlockode_sylius_acb.scope_initializer'
20+
- '@sherlockode_advanced_content.configuration_manager'
21+
tags:
22+
- { name: kernel.event_listener, event: sherlockode_sylius_acb.content.index, method: checkScopeInitialization }
23+
- { name: kernel.event_listener, event: sherlockode_sylius_acb.page.index, method: checkScopeInitialization }
24+
sherlockode_sylius_acb.listener.channel:
25+
class: Sherlockode\SyliusAdvancedContentPlugin\EventListener\ChannelListener
26+
arguments:
27+
- '@sherlockode_advanced_content.configuration_manager'
28+
tags:
29+
- { name: doctrine.event_listener, event: onFlush, lazy: true }

src/Resources/config/services.yaml

+33
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,36 @@ services:
1616
arguments:
1717
- '@doctrine.orm.entity_manager'
1818
- '@sherlockode_advanced_content.configuration_manager'
19+
20+
sherlockode_sylius_acb.scope_initializer:
21+
class: Sherlockode\SyliusAdvancedContentPlugin\Scope\ScopeInitializer
22+
arguments:
23+
- '@doctrine.orm.entity_manager'
24+
- '@sherlockode_advanced_content.configuration_manager'
25+
sherlockode_sylius_acb.scope_init_command:
26+
class: Sherlockode\SyliusAdvancedContentPlugin\Command\ScopeInitCommand
27+
arguments:
28+
- '@sherlockode_advanced_content.configuration_manager'
29+
- '@translator'
30+
- '@sherlockode_sylius_acb.scope_initializer'
31+
tags:
32+
- { name: console.command }
33+
sherlockode_sylius_acb.controller.scope:
34+
class: Sherlockode\SyliusAdvancedContentPlugin\Controller\ScopeController
35+
public: true
36+
arguments:
37+
- '@sherlockode_advanced_content.configuration_manager'
38+
- '@sherlockode_sylius_acb.scope_initializer'
39+
- '@translator'
40+
calls:
41+
- [ 'setContainer', [ '@Psr\Container\ContainerInterface' ] ]
42+
tags:
43+
- { name: container.service_subscriber }
44+
sherlockode_sylius_acb.scope_extension:
45+
class: Sherlockode\SyliusAdvancedContentPlugin\Twig\Extension\ScopeExtension
46+
arguments:
47+
- '@sherlockode_advanced_content.configuration_manager'
48+
- '@sherlockode_sylius_acb.scope_initializer'
49+
tags:
50+
- { name: twig.extension }
51+

src/Resources/translations/messages.en.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,11 @@ sherlockode_sylius_acb:
1313
content: Content
1414
history: Version history
1515
scope: Scope
16+
scopes:
17+
missing_scopes: Scope management is enabled but scopes have not been initialized. Please go to the Tools page to learn more about it.
18+
disabled: Scope management is disabled.
19+
up_to_date: Scopes are already up to date.
20+
init_success: Scopes have been initialized.
21+
segment_title: Scopes management
22+
details: Scopes have not been initialized. Either update them or disable scopes management.
23+
button: Update scopes

src/Resources/translations/messages.fr.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,11 @@ sherlockode_sylius_acb:
1313
content: Contenu
1414
history: Historique des versions
1515
scope: Scope
16+
scopes:
17+
missing_scopes: La gestion des scopes est activée, mais les scopes n'ont pas été initialisés. Veuillez vous rendre sur la page Outils pour en savoir plus.
18+
disabled: La gestion des scopes est désactivée.
19+
up_to_date: Les scopes sont déjà à jour.
20+
init_success: Les scopes ont bien été initialisés.
21+
segment_title: Gestion des scopes
22+
details: Les scopes n'ont pas été initialisés. Lancez l'initialisation ou désactivez la gestion des scopes.
23+
button: Mettre à jour les scopes

0 commit comments

Comments
 (0)