‘Signals and Slots’ is a technique which allows easy implementation of the Observer Pattern. This pattern is mainly used to handle Event Management. Observer pattern in general has an Observer object and a list of dependents. The Observer notifies state changes by usually calling one of its methods. This Observer pattern plays a key part in traditional MVC systems.
Observer Prototype implementation
Upon running the above script will output as,
Using Signals in TYPO3 Flow
To define a Signal, Create a method stub (method with empty body) with ’emit’ as method prefix and annotate it with ‘Signal’ Annotation. As PHP doesn’t have language support for first class annotation’s we will place our annotation specific code in a doc-block. The Signal Annotation will be picked-up by Flows AOP Framework during compilation phase and will be filled with the implementation code.
A Signal can be defined in TYPO3 Flow by,
In the program logic we can invoke the function which is annotated with the Signal Annotation, like
Slots contain the implementation part which will be executed when a method annotated with ‘Signal’ is invoked. Slot methods doesn’t need any annotations, but care should be taken to ensure that the method signatures for the signals and slots to be same.
Sample Slot Implementation
Wiring Signals and Slots
Once we are done with defining Signals and Slot methods in out code base, we need to wire them up in order for the framework to know which slot methods should be used once a signal is invoked. When TYPO3 Flow initializes it runs the boot method in the Specific Packages ‘Package’ class. We can write our Signal Slot specific wiring specific code in there.
If we examine the above code block, the signal-slot dispatcher object has a connect method which is used to wire signals and corresponding slots. The first block [class name and method name] defines the signal, The Signal method name should be declared without the ’emit’ prefix.
All the wiring process which happens behind the scenes in managed by TYPO3 Flow’s AOP Framework. Aspect-Oriented Programming (AOP) is a programming paradigm which complements Object-Oriented Programming (OOP) by separating concerns of a software application to improve modularization.
The AOP Framework reads the Signal slot wiring block and generates specific code to connect Signal method and its corresponding slot method. We’ll see on how AOP Framework makes all of this possible on next topic.