Skip to content

Commit

Permalink
Merge pull request #329 from alexander-schranz/feature/upgrade-symfony-6
Browse files Browse the repository at this point in the history
Add compatibility for Symfony 6
  • Loading branch information
luca-rath authored Jul 15, 2022
2 parents ba31c3e + 747b413 commit 4b52fdf
Show file tree
Hide file tree
Showing 21 changed files with 239 additions and 78 deletions.
37 changes: 28 additions & 9 deletions .github/workflows/test-application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ on:
- '[0-9]+.[0-9]+'

jobs:
test:
name: 'PHP ${{ matrix.php-version }} (${{ matrix.database }}, ${{ matrix.dependency-versions }}, Lint ${{ matrix.lint }})'
php:
name: 'PHP ${{ matrix.php-version }} (${{ matrix.database }}, ${{ matrix.dependency-versions }})'
runs-on: ubuntu-latest

strategy:
Expand All @@ -19,8 +19,7 @@ jobs:
- php-version: '7.2'
database: postgres
dependency-versions: 'lowest'
tools: 'composer:v1'
lint: false
tools: 'composer:v2'
env:
SYMFONY_DEPRECATIONS_HELPER: disabled
DATABASE_URL: postgres://postgres:postgres@127.0.0.1/sulu_form_test?serverVersion=12.5
Expand All @@ -31,7 +30,6 @@ jobs:
database: mysql
dependency-versions: 'highest'
tools: 'composer:v2'
lint: false
env:
SYMFONY_DEPRECATIONS_HELPER: weak
DATABASE_URL: mysql://root:root@127.0.0.1/sulu_form_test?serverVersion=5.7
Expand All @@ -42,7 +40,6 @@ jobs:
database: mysql
dependency-versions: 'highest'
tools: 'composer:v2'
lint: false
env:
SYMFONY_DEPRECATIONS_HELPER: weak
DATABASE_URL: mysql://root:root@127.0.0.1/sulu_form_test?serverVersion=5.7
Expand All @@ -53,7 +50,6 @@ jobs:
database: mysql
dependency-versions: 'highest'
tools: 'composer:v2'
lint: true
env:
SYMFONY_DEPRECATIONS_HELPER: weak
DATABASE_URL: mysql://root:root@127.0.0.1/sulu_form_test?serverVersion=5.7
Expand Down Expand Up @@ -104,7 +100,30 @@ jobs:
run: time composer test
env: ${{ matrix.env }}

lint:
name: 'PHP Lint'
runs-on: ubuntu-latest
env:
DATABASE_URL: mysql://root:root@127.0.0.1/sulu_form_test?serverVersion=5.7

steps:
- name: Checkout project
uses: actions/checkout@v2

- name: Install and configure PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
extensions: 'imagick'
tools: 'composer:v2'
coverage: none

- name: Install composer dependencies
uses: ramsey/composer-install@v1
with:
dependency-versions: ${{matrix.dependency-versions}}
composer-options: ${{ matrix.composer-options }}


- name: Lint code
if: ${{ matrix.lint }}
run: composer lint
env: ${{ matrix.env }}
20 changes: 10 additions & 10 deletions Controller/FormWebsiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function __construct()
);
}

public static function getSubscribedServices()
public static function getSubscribedServices(): array
{
$subscribesServices = parent::getSubscribedServices();
$subscribesServices['form.registry'] = FormRegistryInterface::class;
Expand All @@ -75,11 +75,11 @@ public function formAction(Request $request, StructureInterface $structure, bool

$typeClass = $this->getTypeClass($template);
/** @var AbstractType $type */
$type = $this->get('form.registry')->getType($typeClass)->getInnerType();
$type = $this->container->get('form.registry')->getType($typeClass)->getInnerType();
$type->setAttributes($attributes);

$this->form = $this->get('form.factory')->create($typeClass, [], [
'csrf_token_manager' => new DisabledCsrfTokenManager($this->get('security.csrf.token_manager')),
$this->form = $this->container->get('form.factory')->create($typeClass, [], [
'csrf_token_manager' => new DisabledCsrfTokenManager($this->container->get('security.csrf.token_manager')),
]);
$this->form->handleRequest($request);

Expand Down Expand Up @@ -110,8 +110,8 @@ public function onlyAction(Request $request, string $key): Response

$typeClass = $this->getTypeClass($key);
/** @var AbstractType $type */
$type = $this->get('form.registry')->getType($typeClass)->getInnerType();
$this->form = $this->get('form.factory')->create($typeClass);
$type = $this->container->get('form.registry')->getType($typeClass)->getInnerType();
$this->form = $this->container->get('form.factory')->create($typeClass);
$this->form->handleRequest($request);

if ($this->form->isSubmitted()
Expand All @@ -136,14 +136,14 @@ public function onlyAction(Request $request, string $key): Response
private function handleFormSubmit(Request $request, AbstractType $type, array $attributes): ?Response
{
// handle form submit
$configuration = $this->get('sulu_form.configuration.form_configuration_factory')->buildByType(
$configuration = $this->container->get('sulu_form.configuration.form_configuration_factory')->buildByType(
$type,
$this->form->getData(),
$request->getLocale(),
$attributes
);

$success = $this->get('sulu_form.handler')->handle($this->form, $configuration);
$success = $this->container->get('sulu_form.handler')->handle($this->form, $configuration);

if ($success) {
if ($request->isXmlHttpRequest()) {
Expand All @@ -169,14 +169,14 @@ private function handleFormSubmit(Request $request, AbstractType $type, array $a
private function handleFormOnlySubmit(Request $request, AbstractType $type): ?RedirectResponse
{
// handle form submit
$configuration = $this->get('sulu_form.configuration.form_configuration_factory')->buildByType(
$configuration = $this->container->get('sulu_form.configuration.form_configuration_factory')->buildByType(
$type,
$this->form->getData(),
$request->getLocale(),
[]
);

if ($this->get('sulu_form.handler')->handle($this->form, $configuration)) {
if ($this->container->get('sulu_form.handler')->handle($this->form, $configuration)) {
return new RedirectResponse('?send=true');
}

Expand Down
11 changes: 7 additions & 4 deletions Csrf/DisabledCsrfTokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
use Symfony\Component\Security\Csrf\CsrfToken;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;

/**
* @final
*/
class DisabledCsrfTokenManager implements CsrfTokenManagerInterface
{
/**
Expand All @@ -29,28 +32,28 @@ public function __construct(CsrfTokenManagerInterface $csrfTokenManager)
/**
* @param string $tokenId
*/
public function refreshToken($tokenId)
public function refreshToken($tokenId): CsrfToken
{
return $this->csrfTokenManager->refreshToken($tokenId);
}

/**
* @param string $tokenId
*/
public function removeToken($tokenId)
public function removeToken($tokenId): ?string
{
return $this->csrfTokenManager->removeToken($tokenId);
}

public function isTokenValid(CsrfToken $token)
public function isTokenValid(CsrfToken $token): bool
{
return $this->csrfTokenManager->isTokenValid($token);
}

/**
* @param string $tokenId
*/
public function getToken($tokenId)
public function getToken($tokenId): CsrfToken
{
return new CsrfToken('', null);
}
Expand Down
4 changes: 2 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Configuration implements ConfigurationInterface
public const SWIFT_MAILER_HELPER = 'swift_mailer';
public const MAILER_HELPER = 'mailer';

public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('sulu_form');
$rootNode = $treeBuilder->getRootNode();
Expand All @@ -47,7 +47,7 @@ public function getConfigTreeBuilder()
SuluFormExtension::MEDIA_COLLECTION_STRATEGY_TREE,
])
->defaultValue(null)
->setDeprecated()
->setDeprecated('sulu/form-bundle', '2.2.0')
->end()
->arrayNode('media')
->addDefaultsIfNotSet()
Expand Down
11 changes: 11 additions & 0 deletions DependencyInjection/SuluFormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,17 @@ public function load(array $configs, ContainerBuilder $container): void
$loader->load('types.xml');
$loader->load('title-providers.xml');

$definition = $container->getDefinition('sulu_mail.null_helper');

$reflection = new \ReflectionClass($definition);
$reflectionMethod = $reflection->getMethod('setDeprecated');

if (isset($reflectionMethod->getParameters()[1]) && 'version' === $reflectionMethod->getParameters()[1]->getName()) {
$definition->setDeprecated('sulu/form-bundle', '2.3', 'The "%service_id%" is deprecated use the mailer configuration instead.');
} else {
$definition->setDeprecated(true, 'The "%service_id%" is deprecated use the mailer configuration instead.');
}

if ($config['sendinblue_api_key']) {
if (!\class_exists(\SendinBlue\Client\Configuration::class)) {
throw new \LogicException('You need to install the "sendinblue/api-v3-sdk" package to use the sendinblue type.');
Expand Down
19 changes: 15 additions & 4 deletions Dynamic/Checksum.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Sulu\Bundle\FormBundle\Dynamic;

use Symfony\Component\PasswordHasher\Hasher\MessageDigestPasswordHasher;
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;

/**
Expand All @@ -24,14 +25,16 @@ class Checksum
private $secret;

/**
* @var MessageDigestPasswordEncoder
* @var MessageDigestPasswordEncoder|MessageDigestPasswordHasher
*/
private $encoder;

public function __construct(string $secret)
{
$this->secret = $secret;
$this->encoder = new MessageDigestPasswordEncoder();
$this->encoder = \class_exists(MessageDigestPasswordEncoder::class)
? new MessageDigestPasswordEncoder()
: new MessageDigestPasswordHasher();
}

/**
Expand All @@ -41,7 +44,11 @@ public function check(string $checksum, string $type, string $typeId, int $formI
{
$checksumRaw = $this->createKey($type, $typeId, $formId, $formName);

return $this->encoder->isPasswordValid($checksum, $checksumRaw, $this->secret);
if (\class_exists(MessageDigestPasswordEncoder::class)) {
return $this->encoder->isPasswordValid($checksum, $checksumRaw, $this->secret);
}

return $this->encoder->verify($checksum, $checksumRaw, $this->secret);
}

/**
Expand All @@ -59,6 +66,10 @@ public function get(string $type, string $typeId, int $formId, string $formName)
{
$checksumRaw = $this->createKey($type, $typeId, $formId, $formName);

return $this->encoder->encodePassword($checksumRaw, $this->secret);
if (\class_exists(MessageDigestPasswordEncoder::class)) {
return $this->encoder->encodePassword($checksumRaw, $this->secret);
}

return $this->encoder->hash($checksumRaw, $this->secret);
}
}
2 changes: 1 addition & 1 deletion Event/ProtectedMediaSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static function getSubscribedEvents()

public function onRequest(RequestEvent $event): void
{
if (!$event->isMasterRequest()) {
if (\method_exists($event, 'isMainRequest') ? !$event->isMainRequest() : !$event->isMasterRequest()) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion Event/RequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function __construct(

public function onKernelRequest(RequestEvent $event): void
{
if (!$event->isMasterRequest()) {
if (\method_exists($event, 'isMainRequest') ? !$event->isMainRequest() : !$event->isMasterRequest()) {
// do nothing if it's not the master request
return;
}
Expand Down
1 change: 0 additions & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
<!-- Mail-->
<service id="sulu_mail.null_helper" class="Sulu\Bundle\FormBundle\Mail\NullHelper">
<argument type="service" id="logger" />
<deprecated />
</service>

<!-- Admin -->
Expand Down
8 changes: 7 additions & 1 deletion Tests/Application/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Sulu\Bundle\FormBundle\SuluFormBundle;
use Sulu\Bundle\TestBundle\Kernel\SuluTestKernel;
use Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle;
use Symfony\Component\Config\Loader\LoaderInterface;

class Kernel extends SuluTestKernel
Expand All @@ -32,11 +33,16 @@ public function registerBundles(): iterable
);
}

public function registerContainerConfiguration(LoaderInterface $loader)
public function registerContainerConfiguration(LoaderInterface $loader): void
{
parent::registerContainerConfiguration($loader);

$loader->load(__DIR__ . '/config/config_' . $this->getContext() . '.yml');

$parameters = $this->getKernelParameters();
if (isset($parameters['kernel.bundles'][SwiftmailerBundle::class])) {
$loader->load(__DIR__ . '/config/swiftmailer.yml'); // @deprecated
}
}

protected function getKernelParameters(): array
Expand Down
2 changes: 1 addition & 1 deletion Tests/Application/MailerKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class MailerKernel extends Kernel
{
public function registerContainerConfiguration(LoaderInterface $loader)
public function registerContainerConfiguration(LoaderInterface $loader): void
{
parent::registerContainerConfiguration($loader);

Expand Down
3 changes: 0 additions & 3 deletions Tests/Application/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,3 @@ sulu_form:
mail:
from: "from@example.org"
to: "to@example.org"

swiftmailer:
disable_delivery: true
2 changes: 2 additions & 0 deletions Tests/Application/config/swiftmailer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
swiftmailer:
disable_delivery: true
4 changes: 2 additions & 2 deletions Tests/Functional/Controller/FormControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function testFormMetadata(): void
$this->assertHttpStatusCode(200, $this->client->getResponse());
}

public function testGetNotFound()
public function testGetNotFound(): void
{
$this->client->request(
'GET',
Expand Down Expand Up @@ -340,7 +340,7 @@ private function assertFullForm($response)
$this->assertCountFields(18, $response);
}

private function assertCountFields($expectedCount, $haystack)
private function assertCountFields($expectedCount, $haystack): void
{
$this->assertCount(
$expectedCount,
Expand Down
8 changes: 8 additions & 0 deletions Tests/Functional/Mail/HelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@
namespace Sulu\Bundle\FormBundle\Tests\Functional\Mail;

use Sulu\Bundle\FormBundle\Entity\FormTranslation;
use Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle;

/**
* @deprecated
*/
class HelperTest extends HelperTestCase
{
public function testSendsEmailUsingSwiftmailer()
{
if (!\class_exists(SwiftmailerBundle::class)) {
$this->markTestSkipped('Swiftmailer is not installed.');
}

$formTranslationRepository = $this->entityManager->getRepository(FormTranslation::class);
/** @var FormTranslation $formTranslation */
$formTranslation = $formTranslationRepository->findOneBy(['title' => 'Title', 'locale' => 'de']);
Expand Down
Loading

0 comments on commit 4b52fdf

Please sign in to comment.