hooks

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 1, 2022 License: MIT Imports: 1 Imported by: 0

README

Golang Hooks

An Event Manager, Plugin System, Middleware Manager, Extendability System, Plugin API for Go apps. Have you ever imagined if your Go app has WordPress extendability features? The goal is to make the powerful & simple plugin API of WordPress available in Go!

Installation

Go 1.18+ is required.

go get github.com/Golang-Hooks/Golang-Hooks@v1.0.1

Examples

The test file hooks_test.go covers a wide range of examples.

Actions Example
package main

import (
	"github.com/Golang-Hooks/Golang-Hooks"
)

func main() {
	h := hooks.CreateHooks()

	h.AddAction("test", "vendor/plugin/function", func(i ...interface{}) interface{} {
		println("just doing something...")
		return nil
	}, 10)

	h.AddAction("test", "vendor/plugin/function", func(i ...interface{}) interface{} {
		arg1 := i[0]
		arg2 := i[1]
		if p, ok := arg1.(int); ok {
			println("arg1", p)
		}
		if p, ok := arg2.(string); ok {
			println("arg2", p)
		}
		return nil
	}, 9)

	h.DoAction("test", 33, "awesome")
}
Filters Example
package main

import (
	"fmt"

	"github.com/Golang-Hooks/Golang-Hooks"
)

func main() {
	h := hooks.CreateHooks()

	h.AddFilter("MyFilter", "vendor/plugin/function", func(i ...interface{}) interface{} {
		arg1 := i[0]
		if p, ok := arg1.(int); ok {
			println("f1", p)
			return p + 1
		}
		return nil
	}, 10)

	h.AddFilter("MyFilter", "vendor/plugin/function", func(i ...interface{}) interface{} {
		arg1 := i[0]
		arg2 := i[1]
		p1, ok1 := arg1.(int)
		p2, ok2 := arg2.(int)
		if !ok1 || !ok2 {
			println("not ok")
			return nil
		}
		println("f2", p1, p2)
		return p1 + p2 + 1
	}, 9)

	v := h.ApplyFilters("MyFilter", 3, 2)
	fmt.Println(v)
}

API Usage

  • CreateHooks()
  • AddAction("HookName", "namespace", callback, priority)
  • AddFilter("HookName", "namespace", callback, priority)
  • RemoveAction("HookName", "namespace")
  • RemoveFilter("HookName", "namespace")
  • RemoveAllActions("HookName", "")
  • RemoveAllFilters("HookName", "")
  • DoAction("HookName", arg1, arg2, moreArgs, finalArg)
  • ApplyFilters("HookName", content, arg1, arg2, moreArgs, finalArg)
  • DoingAction("HookName")
  • DoingFilter("HookName")
  • DidAction("HookName")
  • DidFilter("HookName")
  • HasAction("HookName")
  • HasFilter("HookName")
  • Actions
  • Filters

The namespace is a unique string used to identify the callback, the best practice to make it in the form vendor/plugin/function

Events on action/filter add or remove

Whenever an action or filter is added or removed, a matching HookAdded or HookRemoved action is triggered.

  • HookAdded action is triggered when AddFilter() or AddAction() method is called, passing values for HookName, functionName, callback and priority.
  • HookRemoved action is triggered when RemoveFilter() or RemoveAction() method is called, passing values for HookName and functionName.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Core

type Core struct {
	AddAction        func(string, string, func(...interface{}) interface{}, int)
	DoAction         func(string, ...interface{}) interface{}
	AddFilter        func(string, string, func(...interface{}) interface{}, int)
	ApplyFilters     func(string, ...interface{}) interface{}
	CurrentAction    func() (HookInfo, error)
	CurrentFilter    func() (HookInfo, error)
	DidAction        func(string) int
	DidFilter        func(string) int
	DoingAction      func(string) bool
	DoingFilter      func(string) bool
	HasAction        func(string) bool
	HasFilter        func(string) bool
	RemoveAction     func(string, string) int
	RemoveFilter     func(string, string) int
	RemoveAllActions func(string, string) int
	RemoveAllFilters func(string, string) int
	Actions          Hooks
	Filters          Hooks
}

func CreateHooks

func CreateHooks() Core

type Handler

type Handler struct {
	Namespace string
	Callback  func(...interface{}) interface{}
	Priority  int
}

type Handlers

type Handlers struct {
	Handlers []Handler
	Runs     int
}

type HookInfo

type HookInfo struct {
	Name         string
	CurrentIndex int
}

type Hooks

type Hooks struct {
	Hooks   map[string]Handlers
	Current []*HookInfo
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL