neosrulez/neos-essentials

本包最新版本(0.1.0)没有可用的许可证信息。

使用Neos Flow进行开发所必需的常用事物。

0.1.0 2023-10-13 09:18 UTC

This package is not auto-updated.

Last update: 2024-09-27 13:33:35 UTC


README

我使用此包来避免在几乎每个应用中都需要重新创建东西。包括UserService、MailService(基于Fusion的邮件)、一些设置和有用的抽象PHP类。

安装

运行

composer require neosrulez/neos-essentials

安装后运行

flow flow:cache:flush --force
flow flow:package:rescan

配置

NeosRulez:
  Neos:
    Essentials:
      login:
        # These things are executed if the account is logged in when calling up the app
        ifAuthenticated:
          class: Acme\Package\Domain\Service\IsAuthenticated
          redirectToUri: /dashboard
        # These things are executed when the account has successfully logged out
        afterLogout:
          class: Acme\Package\Domain\Service\Logout
          redirectToUri: /homepage
        # These things are executed when the account has successfully logged in
        onAuthenticationSuccess:
          class: Acme\Package\Domain\Service\AuthSuccess
          redirectToUri: /dashboard
        # These things are executed when the login fails
        onAuthenticationFailure:
          class: Acme\Package\Domain\Service\AuthFailure
          redirectToUri: /homepage
      account:
        # Required!
        defaultRole: NeosRulez.Neos.Essentials:User
        # These things are done after an account is created. Account and password are passed in this function
        afterCreateAccount:
          class: Acme\Package\Domain\Service\UserService
      # Required! This is required to be able to use the getLoggedInUser() function from the UserService. The getLoggedInAccount() function is also available without this.
      user:
        model:
          class: Acme\Package\Domain\Model\User
        repository:
          class: Acme\Package\Domain\Repository\UserRepository
      # Required!
      mail:
        senderMail: noreply@foo.com
        senderName: Sender name

# You can safely override these settings if you need something else
Neos:
  Flow:
    security:
      authentication:
        providers:
          'NeosRulez.Neos.Essentials:Login':
            provider: 'PersistedUsernamePasswordProvider'
            entryPoint: 'WebRedirect'
    persistence:
      doctrine:
        filters:
          soft-deletable: 'Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter'
        eventListeners:
          - events: [ 'onFlush', 'loadClassMetadata' ]
            listener: 'Gedmo\SoftDeleteable\SoftDeleteableListener'
          - events: [ 'prePersist', 'onFlush', 'loadClassMetadata' ]
            listener: 'Gedmo\Timestampable\TimestampableListener'

有用的服务

UserService.php

UserService涵盖了几乎所有应用都需要的重要功能。账户和用户创建、密码生成、检查哪个用户或账户已登录的能力。

use NeosRulez\Neos\Essentials\Service\UserService;

#[Flow\Inject]
protected UserService $userService;

$this->userService->createUser(string $email, string|null $password = null, string|null $role = null); # Create a new account and a new user. A separate user model abstracted from the AbstractUser.php model is required for this.
$this->userService->generatePassword(int $length); # Generates a secure password of any length
$this->userService->getLoggedInAccount(); # Returns the logged in account
$this->userService->getLoggedInUser(); # Returns the logged in user

MailService.php

MailService可以使用Swiftmailer发送带有附件的HTML邮件。

use NeosRulez\Neos\Essentials\Service\MailService;

#[Flow\Inject]
protected MailService $mailService;

$this->mailService()->sendMail(string $packageName, string $fusionPathAndFileName, array $variables, string $subject, string $sender, string $recipient, string|bool $replyTo = false, string|bool $cc = false, string|bool $bcc = false, array $attachments = []);

# Real life example
$this->mailService()->sendMail(
    'Acme.Package', # Package name for the Fusion Files
    'Acme/Package/Mail/FusionMail', # Component Name of the Fusion Files
    ['foo' => 'foos', 'bar' => $variable], # Variables used in the mail
    'Welcome to the Neos Flow application!', # Subject of the mail
    'noreply@foo.com', # Senders address
    $user->getEmail(), # Recipients address
    false, # In that case no reply to
    false, # In that case no cc
    false, # In that case no bcc
    ['/application/the_path_to_your_file1.pdf', '/application/the_path_to_your_file2.pdf'] # File paths to the files to be attached to the mail
);

FusionMail.fusion

include: resource://Neos.Fusion/Private/Fusion/Root.fusion

Acme.Package.Mail.FusionMail = Neos.Fusion:Join {
    
    renderer = afx`
        <p>Nice that you use the app!</p>
        <p>
            <strong>Foo</strong> {foo}
            <strong>Bar</strong> {bar}
        </p>
    `
}

有用的抽象PHP类

AbstractModel.php

此类提供了gedmo doctrine扩展的功能,并提供了持久对象标识符的getter,可用于扩展您自己的实体模型。

use NeosRulez\Neos\Essentials\Domain\Model\AbstractModel;

$entity->getIdentifier();

$entity->getCreated(); # Returns a DateTime Object
$entity->getCreated('Y-m-d'); # Returns a string

AbstractUser.php

此类从AbstractModel.php抽象出来,为您的用户模型提供了一个抽象类,其中包含账户的setter和getter以及“active”属性的setter和getter。

use NeosRulez\Neos\Essentials\Domain\Model\AbstractUser;

$userEntity->getAccount(); # Returns the Neos Flow Account referenced by the user
$userEntity->isActive(); # Returns a boolean

Fusion页面组件

一个页面原型,可以轻松获取输出。

Acme.Package.StandardController.index = NeosRulez.Neos.Essentials:Page {

    htmlTag {
        lang = 'de'
    }

    content = Neos.Fusion:Component {

        renderer = afx`
            <div class="app">
                ...
            </div>
        `
    }
}

Fusion登录表单组件

登录组件开箱即用。

prototype(Acme.Package:Component.LoginForm) < prototype(NeosRulez.Neos.Essentials:LoginForm) {

    username {
        label = 'Your username'
        placeholder = 'Enter your username ...'
    }

    loginButton {
        label = 'Login now!'
    }
}

作者