Skip to content

Commit

Permalink
Add compatibility with Sulu 2.5 - Symfony 6 (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-newvisibility committed Apr 25, 2024
1 parent 64db8bd commit 723b4b4
Show file tree
Hide file tree
Showing 31 changed files with 360 additions and 181 deletions.
20 changes: 19 additions & 1 deletion .github/workflows/test-application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,25 @@ jobs:
SYMFONY_DEPRECATIONS_HELPER: weak

- php-version: '8.1'
lint: true
lint: false
dependency-versions: 'highest'
tools: 'composer:v2'
env:
SYMFONY_DEPRECATIONS_HELPER: weak

- php-version: '8.2'
lint: false
dependency-versions: 'highest'
tools: 'composer:v2'
env:
SYMFONY_DEPRECATIONS_HELPER: weak

- php-version: '8.3'
lint: true
dependency-versions: 'highest'
tools: 'composer:v2'
env:
SYMFONY_DEPRECATIONS_HELPER: weak
services:
mysql:
image: mysql:5.7
Expand All @@ -71,6 +84,11 @@ jobs:
if: ${{ matrix.php-version == '7.2' }}
run: composer remove php-cs-fixer/shim --dev --no-interaction

- name: Install additional lowest dependencies
if: ${{ matrix.dependency-versions == 'lowest' }}
run: |
composer require symfony/swiftmailer-bundle --no-interaction --no-update
- name: Install composer dependencies
uses: ramsey/composer-install@v1
with:
Expand Down
41 changes: 18 additions & 23 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,33 @@
$config->setRiskyAllowed(true)
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'ordered_imports' => true,
'concat_space' => ['spacing' => 'one'],
'array_syntax' => ['syntax' => 'short'],
'phpdoc_align' => ['align' => 'left'],
'class_definition' => [
'multi_line_extends_each_single_line' => true,
] ,
'linebreak_after_opening_tag' => true,
// 'declare_strict_types' => true,
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'],
'class_definition' => false,
'concat_space' => ['spacing' => 'one'],
'function_declaration' => ['closure_function_spacing' => 'none'],
'header_comment' => ['header' => $header],
'native_constant_invocation' => true,
'native_function_casing' => true,
'native_function_invocation' => ['include' => ['@internal']],
'no_php4_constructor' => true,
'global_namespace_import' => ['import_classes' => false, 'import_constants' => false, 'import_functions' => false],
'no_superfluous_phpdoc_tags' => ['allow_mixed' => true, 'remove_inheritdoc' => true],
'no_unreachable_default_argument_value' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'php_unit_strict' => true,
'phpdoc_order' => true,
'semicolon_after_instruction' => true,
'strict_comparison' => true,
'strict_param' => true,
'array_indentation' => true,
'multiline_whitespace_before_semicolons' => true,
'ordered_imports' => true,
'phpdoc_align' => ['align' => 'left'],
'phpdoc_types_order' => false,
'single_line_throw' => false,
'visibility_required' => ['elements' => ['property', 'method', 'const']],
'single_line_comment_spacing' => false,
'phpdoc_to_comment' => [
'ignored_tags' => ['todo', 'var'],
],
'trailing_comma_in_multiline' => ['elements' => ['arrays', /*'arguments', 'parameters' */]],
'phpdoc_separation' => [
'groups' => [
['Serializer\\*', 'VirtualProperty', 'Accessor', 'Type', 'Groups', 'Expose', 'Exclude', 'SerializedName', 'Inline', 'ExclusionPolicy'],
],
],
'get_class_to_class_keyword' => false, // should be enabled as soon as support for php < 8 is dropped
'nullable_type_declaration_for_default_null_value' => true,
'no_null_property_initialization' => false,
'fully_qualified_strict_types' => false,
])
->setFinder($finder);

Expand Down
34 changes: 22 additions & 12 deletions Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;

/**
* Contains helper function for all controllers.
Expand Down Expand Up @@ -75,12 +75,26 @@ protected function setUserPasswordAndSalt(User $user, FormInterface $form): User
}

$user->setSalt($salt);
$password = $this->getUserPasswordEncoder()->encodePassword($user, $plainPassword);
$password = $this->encodePassword($user, $plainPassword);
$user->setPassword($password);

return $user;
}

protected function encodePassword(User $user, string $plainPassword): string
{
if ($this->container->has('?security.password_hasher')) {
/** @var UserPasswordHasherInterface $hasher */
$hasher = $this->container->get('?security.password_hasher');

return $hasher->hashPassword($user, $plainPassword);
}
/** @var \Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface $encoder */
$encoder = $this->container->get('?security.password_encoder');

return $encoder->encodePassword($user, $plainPassword);
}

/**
* Check if user should be logged in.
*/
Expand Down Expand Up @@ -132,9 +146,6 @@ private function getTemplateAttributes(array $custom = []): array
return $this->getTemplateAttributeResolver()->resolve($custom);
}

/**
* @return User
*/
public function getUser(): ?User
{
$user = parent::getUser();
Expand Down Expand Up @@ -175,7 +186,7 @@ private function addAddress(User $user): void
/**
* @param mixed[] $parameters
*/
public function render(string $view, array $parameters = [], Response $response = null): Response
public function render(string $view, array $parameters = [], ?Response $response = null): Response
{
return parent::render(
$view,
Expand Down Expand Up @@ -207,11 +218,6 @@ protected function getSaltGenerator(): SaltGenerator
return $this->container->get('sulu_security.salt_generator');
}

protected function getUserPasswordEncoder(): UserPasswordEncoderInterface
{
return $this->container->get('security.password_encoder');
}

protected function getTemplateAttributeResolver(): TemplateAttributeResolverInterface
{
return $this->container->get('sulu_website.resolver.template_attribute');
Expand All @@ -232,9 +238,13 @@ public static function getSubscribedServices(): array
$subscribedServices['sulu_community.community_manager.registry'] = CommunityManagerRegistryInterface::class;
$subscribedServices['sulu_core.webspace.request_analyzer'] = RequestAnalyzerInterface::class;
$subscribedServices['sulu_security.salt_generator'] = SaltGenerator::class;
$subscribedServices['security.password_encoder'] = UserPasswordEncoderInterface::class;
$subscribedServices['sulu_website.resolver.template_attribute'] = TemplateAttributeResolverInterface::class;
$subscribedServices['doctrine.orm.entity_manager'] = EntityManagerInterface::class;
$subscribedServices['?security.password_hasher'] = UserPasswordHasherInterface::class;

if (\class_exists('Symfony\\Component\\Security\\Core\\Encoder\\UserPasswordEncoderInterface')) {
$subscribedServices['?security.password_encoder'] = 'Symfony\\Component\\Security\\Core\\Encoder\\UserPasswordEncoderInterface';
}

return $subscribedServices;
}
Expand Down
3 changes: 2 additions & 1 deletion Controller/BlacklistItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* Provides admin-api for blacklist-items.
*
* @NamePrefix("sulu_community.")
*
* @RouteResource("blacklist-item")
*/
class BlacklistItemController extends AbstractRestController implements ClassResourceInterface
Expand Down Expand Up @@ -148,7 +149,7 @@ public function deleteAction(int $id): Response
*/
public function cdeleteAction(Request $request): Response
{
$ids = \array_map(function ($id) {
$ids = \array_map(function($id) {
return (int) $id;
}, \array_filter(\explode(',', (string) $request->query->get('ids', ''))));

Expand Down
4 changes: 2 additions & 2 deletions Controller/SaveMediaTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ private function saveMedia(UploadedFile $uploadedFile, ?int $id, string $locale,
*/
private function getSystemCollectionManager(): SystemCollectionManagerInterface
{
return $this->get('sulu_media.system_collections.manager');
return $this->container->get('sulu_media.system_collections.manager');
}

/**
* Get media manager.
*/
private function getMediaManager(): MediaManagerInterface
{
return $this->get('sulu_media.media_manager');
return $this->container->get('sulu_media.media_manager');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
* },
* delete_user: bool,
* }
*
* @phpstan-type Config array{
* from: string|string[],
* to: string|string[],
Expand Down
4 changes: 2 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function getConfigTreeBuilder()
->end()
->beforeNormalization()
->ifString()
->then(function ($value) {
->then(function($value) {
return [
self::EMAIL_FROM_NAME => $value,
self::EMAIL_FROM_EMAIL => $value,
Expand All @@ -151,7 +151,7 @@ public function getConfigTreeBuilder()
->end()
->beforeNormalization()
->ifString()
->then(function ($value) {
->then(function($value) {
return [
self::EMAIL_TO_NAME => $value,
self::EMAIL_TO_EMAIL => $value,
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/SuluCommunityExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function prepend(ContainerBuilder $container): void
'orm' => [
'dql' => [
'string_functions' => [
'regexp' => RegExp::class,
'regexp' => Regexp::class,
],
],
],
Expand Down
4 changes: 0 additions & 4 deletions Entity/BlacklistItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ class BlacklistItem
*/
private $type;

/**
* @param string $pattern
* @param string $type
*/
public function __construct(?string $pattern = null, ?string $type = null)
{
$this->type = $type;
Expand Down
2 changes: 1 addition & 1 deletion EventListener/CompletionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function onRequest(RequestEvent $event): void
$request = $event->getRequest();
$completionUrl = $this->router->generate('sulu_community.completion');

if (!$event->isMasterRequest()
if (!$event->isMainRequest()
|| !$request->isMethodSafe()
|| $request->isXmlHttpRequest()
|| $request->getPathInfo() === $completionUrl
Expand Down
2 changes: 1 addition & 1 deletion EventListener/LastLoginListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static function getSubscribedEvents()
*/
public function onRequest(RequestEvent $event): void
{
if (!$event->isMasterRequest()) {
if (!$event->isMainRequest()) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion Form/Type/RegistrationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function configureOptions(OptionsResolver $resolver): void
[
'data_class' => User::class,
'validation_groups' => ['registration'],
'empty_data' => function (FormInterface $form) {
'empty_data' => function(FormInterface $form) {
$user = new User();
$user->setContact(new Contact());

Expand Down
2 changes: 0 additions & 2 deletions Mail/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ class Mail
* user_template: string|null,
* admin_template: string|null,
* } $config
*
* @return Mail
*/
public static function create($from, $to, array $config): self
{
Expand Down
57 changes: 49 additions & 8 deletions Mail/MailFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
namespace Sulu\Bundle\CommunityBundle\Mail;

use Sulu\Bundle\SecurityBundle\Entity\User;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Contracts\Translation\LocaleAwareInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
Expand All @@ -22,7 +26,7 @@
class MailFactory implements MailFactoryInterface
{
/**
* @var \Swift_Mailer
* @var MailerInterface|\Swift_Mailer
*/
protected $mailer;

Expand All @@ -36,7 +40,7 @@ class MailFactory implements MailFactoryInterface
*/
protected $translator;

public function __construct(\Swift_Mailer $mailer, Environment $twig, TranslatorInterface $translator)
public function __construct($mailer, Environment $twig, TranslatorInterface $translator)
{
$this->mailer = $mailer;
$this->twig = $twig;
Expand Down Expand Up @@ -80,12 +84,49 @@ protected function sendEmail($from, $to, string $subject, string $template, arra
{
$body = $this->twig->render($template, $data);

$message = new \Swift_Message();
$message->setSubject($this->translator->trans($subject));
$message->setFrom($from);
$message->setTo($to);
$message->setBody($body, 'text/html');
if ($this->mailer instanceof \Swift_Mailer) {
$email = $this->mailer->createMessage()
->setSubject($this->translator->trans($subject))
->setFrom($from)
->setTo($to)
->setBody($body, 'text/html');
} else {
if (!$this->getAddress($from) || !$this->getAddress($to)) {
return;
}

$this->mailer->send($message);
$email = (new Email())
->subject($this->translator->trans($subject))
->from($this->getAddress($from))
->to($this->getAddress($to))
->html($body);
}

$this->mailer->send($email);
}

/**
* Convert string/array email address to an Address object.
*
* @param mixed $address
*/
protected function getAddress($address): ?Address
{
$name = '';

if (\is_array($address)) {
if (empty($address)) {
return null;
} elseif (!isset($address['email'])) {
$email = $address[\array_keys($address)[0]];
} else {
$email = $address['email'];
$name = $address['name'] ?? '';
}
} else {
$email = $address;
}

return new Address($email, $name);
}
}
1 change: 0 additions & 1 deletion Manager/CommunityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ public function login(User $user, Request $request): void

$token = new UsernamePasswordToken(
$user,
null,
$this->getConfigProperty(Configuration::FIREWALL),
$user->getRoles()
);
Expand Down
Loading

0 comments on commit 723b4b4

Please sign in to comment.