Business-Workflows in Symfony-Anwendungen modellieren

Business-Workflows in Symfony-Anwendungen modellieren

SymfonyLive Cologone 2017

B50a40d1c576ef0aa065cbc5d4d6dec9?s=128

Christian Flothmann

April 07, 2017
Tweet

Transcript

  1. SensioLabs Business-Workflows in Symfony- Anwendungen modellieren

  2. Christian Flothmann Symfony Core Team & Documentation Team Entwickler bei

    SensioLabs Deutschland github.com/xabbuh @xabbuh
  3. Die Workflow Komponente

  4. • eingeführt in Symfony 3.2 • unterstützt Workflow Nets und

    State Machines • in Twig und das Full-Stack Framework integriert • Audit Logging Die Workflow Komponente
  5. • Subject: Objekt, auf das ein Workflow angewandt wird •

    Place: Zustände, die ein Workflow annehmen kann • Transition: (benannter) Übergang zwischen zwei Places Terminologie
  6. • Marking: Zustand/Zustände, in denen sich ein Workflow befindet •

    Marking Store: Mapping des Workflow Markings auf Properties des Subjects Terminologie
  7. Beispiel: Bestellungen in einem Onlineshop

  8. AppBundle\Entity\Order class Order { /** * @var string */ private

    $state; /** * @var Customer */ private $customer; /** * @var Product[] */ private $products; }
  9. Konfiguration framework: # ... workflows: order_payment: marking_store: type: single_state arguments:

    [state] supports: - AppBundle\Entity\Order # ...
  10. Konfiguration framework: workflows: order_payment: places: - placed - paid -

    payment_failed - delivered - returned - refunded initial_place: placed
  11. Konfiguration framework: workflows: order_payment: transitions: pay: from: [placed, payment_failed] to:

    paid deliver: from: paid to: delivered # ...
  12. Verwendung des Workflows public function startDeliveryAction(Order $order) { $workflow =

    $this->get('workflow.order_payment'); if (!$workflow->can($order, 'deliver')) { // Error Handling } $workflow->apply($order, 'deliver'); // ... }
  13. Twig-Integration {# Transition auf Anwendbarkeit testen #} {% if workflow_can(order,

    'deliver', 'order_payment') %} <a href="{{ path('deliver_order') }}">Deliver</a> {% endif %} {# alle anwendbaren Transitionen #} {% for t in workflow_transitions(order, 'order_payment') %} <a href="{{ path(t.name ~ '_order') }}"> {{ ('transition.' ~ t.name)|trans }} </a> {% endfor %}
  14. Das Event-System

  15. • workflow.leave vor dem Verlassen eines Places • workflow.transition vor

    dem Anwenden einer Transition • workflow.enter vor dem Entern eines Places • workflow.<workflow>.announce.<transition> Announcement aktivierter Transitions Event-Kategorien
  16. Die Event Klasse use Symfony\Component\EventDispatcher\Event as BaseEvent; class Event extends

    BaseEvent { // ... public function getMarking() { return $this->marking; } public function getSubject() { return $this->subject; } public function getTransition() { return $this->transition; } }
  17. Beispiel: Audit Logging services: audit_trail_logger: class: Symfony\Component\Workflow\EventListener\AuditTrailListener arguments: ['@logger'] tags:

    - name: kernel.event_subscriber event: workflow.order_payment.leave method: onLeave - name: kernel.event_listener event: workflow.order_payment.transition method: onTransition - name: kernel.event_listener event: workflow.order_payment.enter method: onEnter
  18. • Möglichkeit durch Event Listener Ausführung von Transitionen zu blocken

    • Events: o workflow.guard o workflow.<workflow>.guard o workflow.<workflow>.guard.<transition> Guard Events
  19. Beispiel: rollenbasierter Access Check class RoleGuardListener { private $authorizationChecker; public

    function __construct( AuthorizationCheckerInterface $authorizationChecker ) { $this->authorizationChecker = $authorizationChecker; } public function canDeliver(GuardEvent $event) { if (!$this->authorizationChecker->isGranted('ROLE_DELIVERER')) { $event->setBlocked(true); } } }
  20. Beispiel: rollenbasierter Access Check services: role_guard_listener: class: AppBundle\Workflow\RoleGuardListener arguments: ['@security.authorization_checker']

    tags: - name: kernel.event_listener event: workflow.order_payment.guard.deliver method: canDeliver
  21. SensioLabs Neu in Symfony 3.3

  22. Neue Twig-Funktionen {# prüfen, ob ein Workflow in einem gewünschten

    Place ist #} {% if workflow_has_marked_place(order, 'paid', 'order_payment') %} bezahlt {% endif %} {# aktive Places ermitteln #} {{ workflow_marked_places(order, true, 'order_payment')|join(', ') }}
  23. • neues Event: workflow.entered • nachdem ein Place aktiviert wurde

    • Abstufungen: o workflow.entered o workflow.<workflow>.entered o workflow. <workflow>.entered.<place> • neue Methode: getWorkflowName() Änderungen am Event-System
  24. Guard Expression Support

  25. Guard Expression Support framework: workflows: order_payment: # ... transitions: deliver:

    guard: 'has_role("ROLE_DELIVERER")' from: paid to: delivered # ...
  26. • subject • token • user • roles Guard Expression

    Support, Variablen
  27. • is_granted() • is_anonymous() • is_authenticated() • is_fully_authenticated() • is_rememberme()

    • has_role() Guard Expression Support, Funktionen
  28. Verbesserte Integration des AuditTrailLogger

  29. Symfony 3.2 services: audit_trail_logger: class: Symfony\Component\Workflow\EventListener\AuditTrailListener arguments: ['@logger'] tags: -

    name: kernel.event_subscriber event: workflow.order_payment.leave method: onLeave - name: kernel.event_listener event: workflow.order_payment.transition method: onTransition - name: kernel.event_listener event: workflow.order_payment.enter method: onEnter
  30. Symfony 3.3 framework: workflows: order_payment: supports: AppBundle\Entity\Order audit_trail: true #

    ...
  31. Benutzerdefinierte Support Strategien

  32. Benutzerdefinierte Support Strategien use Symfony\Component\Workflow\SupportStrategy\SupportStrategyInterface; class EconomySupportStrategy implements SupportStrategyInterface {

    public function supports(Workflow $workflow, $subject) { if (!$subject instanceof Article) { return false; } return $subject->getCategory() === Article::CATEGORY_ECONOMY; } }
  33. Benutzerdefinierte Support Strategien services: economy_article_strategy: class: AppBundle\EconomySupportStrategy

  34. Benutzerdefinierte Support Strategien framework: workflows: economy_article_publication: support_strategy: economy_article_strategy # ...

  35. SensioLabs Vielen Dank!

  36. SensioLabs https://joind.in/talk/10c9f