# life cycle hook
Blessing Skin provides three lifecycle hooks for the plugin, which you
can use to prepare or clean up the plugin. All code that utilizes hooks
must be defined in the callbacks.php
file in the root directory of the
plugin in the form of an array .
# callbacks.php
callbacks.php
file should return an array of key-value pairs. The key
is the class of the lifecycle event and the value is a callback
function. The parameters of the callback function can have parameters
with type annotations. When Blessing Skin executes these hooks, it will
use Laravel's service container to resolve dependencies and inject them
into the parameters. callbacks.php
looks something like this (leading
PHP tags omitted):
return [
A pp\ E vents\ P luginWasEnabled:: class => function ( A pp\ Services \ P luginManager $manager ) {
// Here $manager is an instance of App\Services\PluginManager
}
] ;
# plugin is enabled
When the plugin is enabled, the App\Events\PluginWasEnabled
event will
be fired. With this hook, you can perform some initialization tasks,
such as checking the environment and creating a data table in the
database (but remember to check whether the data table already exists
before creating it, and an error will be reported if the table is
created repeatedly), etc.
# plugin is turned off
When the plugin is disabled, the App\Events\PluginWasDisabled
event
will be fired.
# Plugins are about to be removed
After the administrator deletes the plugin on the "Plugin Management"
page, and the plugin has not been deleted at this time, the
App\Events\PluginWasDeleted
event will be triggered first. The name of
the event here is misleading. From the name of the event, it means "the
plug-in has been deleted", but in fact it just sends a "delete" signal
to the plug-in. At this time, the plug-in has not been deleted. Wait
until the event is triggered. , the plugin is actually removed.
With this hook, you can do some cleanup. However, from a practical point of view, what you create when the plugin is enabled doesn't mean you have to clean up when the plugin is removed. For example, suppose we now have a plug-in that verifies user information. This plug-in creates a data table in the database when it is enabled, and then records the user verification information. If the data table is deleted when the plug-in is deleted, the authenticated user will have to re-authenticate the next time the plug-in is used.