Add "events" for performing user-defined actions before or after a build #189
Conversation
This was referenced Mar 5, 2018
# Conflicts: # jigsaw-core.php # src/DataLoader.php # src/Jigsaw.php # src/SiteBuilder.php # tests/CollectionItemTest.php
Closed
Is this event bus mechanism available in latest Jigsaw? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
This PR allows users to hook into "events" in order to run custom code before and after a build is processed. It combines ideas from issues #135 and #175, and PRs #136 and #165. It can also serve as the basis for addressing #120.
There are three events a user can listen to:
beforeBuild
is fired before anysource
files have been processed. This gives the user an opportunity to programmatically modify config variables, fetch data from external sources, or modify files in thesource
folder.afterCollections
is fired after any collections have been processed, but before any output files are built. This gives the user access to the parsed contents of collection items.afterBuild
is fired after the build is complete, and all output files have been written to thebuild
directory. This allows the user to obtain a list of the output file paths (to use, for example, when creating asitemap.xml
file), programmatically create output files, or take care of any other post-processing tasks.Registering event listeners (as closures)
To add an event listener, head over to
bootstrap.php
. There, you can access the event bus with the$events
variable, adding listeners by calling the name of the event:At its simplest, you can define your event listeners as closures that accept an instance of
Jigsaw
. TheJigsaw
instance contains a number of helper methods (see below) to allow you to access information about the site and interact with files and config settings.For example, the following listener will fetch the current weather from an external API, and add it as a variable to
config.php
, where it can be referenced in your templates:Registering event listeners (as classes)
For more complex event listeners, you can specify the name of a class, or an array of class names, instead of a closure. These classes can either live in
bootstrap.php
or in a separate directory. Listener classes should countain ahandle()
method with accepts an instance ofJigsaw
:If there are multiple listeners defined for a single event, they will be fired in the order in which they were defined.
To call a listener class that lives in a separate directory, the class namespace should be added to a
composer.json
file:Helper methods in
$jigsaw
The instance of
Jigsaw
available to each event listener includes the following helper methods:getEnvironment()
local
orproduction
getCollections()
beforeBuild
, returns an array of collection names; inafterCollections
andafterBuild
, returns a collection of collection items, keyed by collection name.getCollection($collection)
afterCollections
andafterBuild
only) Returns the items in a particular collection, keyed by theirsource
filenames. Each item contains the variables defined for the collection item, as well as access to all collection item methods likegetContent()
.getConfig()
config.php
getConfig($key)
config.php
setConfig($key, $value)
config.php
getSourcePath()
source
directorysetSourcePath($path)
source
directorygetDestinationPath()
build
directorysetDestinationPath($path)
build
directorygetOutputPaths()
afterBuild
only) Returns an array of paths to the output files that were generated, relative to thebuild
directoryreadSourceFile($fileName)
source
directorywriteSourceFile($fileName, $contents)
source
directoryreadOutputFile($fileName)
build
directorywriteOutputFile($fileName, $contents)
build
directory