Feature: #102935 - PSR-14 event for package initialization functionality
See forge#102935
Description
A new PSR-14 event \TYPO3\
has been introduced. It allows listeners to execute custom functionality after
a package has been activated. The event is therefore being dispatched at several
places, where packages get activated. Those are e.g. on extension installation
by the extension manager, or on calling the typo3 extension:
command. The
main component, dispatching the event however is the new
Package
. The new service is a drop-in replacement for
the Install
method, which is from now on just a wrapper
around Package
. The wrapper is used to still
pass the current instance to listeners of the After
.
TYPO3 already registers a couple of listeners to this event:
\TYPO3\
CMS\ Core\ Package\ Initialization\ Import Extension Data On Package Initialization \TYPO3\
CMS\ Core\ Package\ Initialization\ Import Static Sql Data On Package Initialization \TYPO3\
CMS\ Core\ Package\ Initialization\ Check For Import Requirements \TYPO3\
CMS\ Impexp\ Initialization\ Import Content On Package Initialization \TYPO3\
CMS\ Impexp\ Initialization\ Import Site Configurations On Package Initialization
Developers are able to listen to the new event before or after TYPO3 Core
listeners have been executed, using before
and after
in the
listener registration. All listeners are able to store arbitrary data
in the Event using the add
method. This is also used
by the core listeners to store their result, which was previously passed
to the removed
EXT:
PSR-14 events.
Listeners can access that information using corresponding get
method. Those entries are a Package
object, which
features the following methods:
get
- Returns the entry identifier, which is the listener service name for the TYPO3 Core listenersIdentifier () get
- Returns the result data, added by the corresponding listenerResult ()
Using the new Event, listeners are equipped with following methods:
get
- Returns the extension key for the activated packageExtension Key () get
- Returns thePackage () Package
object of the activated packageInterface get
- Returns theContainer () Container
, used on activating the packageInterface get
- Returns the emitter / the service, which has dispatched the eventEmitter () has
- Whether a storage entry for a given identifier existsStorage Entry () get
- Returns a storage entry for a given identifierStorage Entry () add
- Adds a storage entry (Storage Entry () Package
) to the eventInitialization Result remove
- Removes a storage entry by a given identifierStorage Entry ()
Note
In case you have previously called Install
directly, you can now just dispatch the new event.
Example
The event listener class, using the PHP attribute #
for
registration, placing the listener after a specific core listener and adding
a storage entry, using the listener class name as identifier (which is
recommended and also done by TYPO3 Core):
use TYPO3\CMS\Core\Attribute\AsEventListener;
use TYPO3\CMS\Core\Package\Event\PackageInitializationEvent;
use TYPO3\CMS\Core\Package\Initialization\ImportExtensionDataOnPackageInitialization;
final class PackageInitializationEventListener
{
#[AsEventListener(after: ImportExtensionDataOnPackageInitialization::class)]
public function __invoke(PackageInitializationEvent $event): void
{
if ($event->getExtensionKey() === 'my_ext') {
$event->addStorageEntry(__CLASS__, 'my result');
}
}
}
Impact
Using the new PSR-14 event, it's now possible to execute custom functionality when a package has been activated. Since TYPO3 Core also uses listeners to this event, custom extensions can easily place their functionality in between and fetch necessary information directly from the event's storage, instead of registering dedicated listeners.