PHPackages                             brain/striatum - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. brain/striatum

AbandonedLibrary[Utility &amp; Helpers](/categories/utility)

brain/striatum
==============

Observer pattern package and API for WordPress hooks.

0.1.0(10y ago)192.0k23GPL-2.0+PHPPHP &gt;=5.4

Since Feb 23Pushed 10y ago2 watchersCompare

[ Source](https://github.com/gmazzap/Striatum)[ Packagist](https://packagist.org/packages/brain/striatum)[ RSS](/packages/brain-striatum/feed)WikiDiscussions master Synced 4d ago

READMEChangelog (1)Dependencies (5)Versions (2)Used By (3)

Striatum
========

[](#striatum)

Striatum is a package (not full plugin) to handle WordPress hooks OOP way.

It makes use of [composer](https://getcomposer.org/) to be embedded in larger projects and implements [Observer pattern](http://en.wikipedia.org/wiki/Observer_pattern) to deal with WordPress hooks. It takes the two pattern interfaces, [Subject](http://www.php.net/manual/en/class.splsubject.php) and [Observer](http://www.php.net/manual/en/class.splobserver.php), from PHP SPL library.

It is a [Brain Project](http://giuseppe-mazzapica.github.io/Brain) module.

\##Table of contents##

- [Quick Start](#quick-start)
- [Features](#features)
- [More Info](#more-info)
- [WordPress core compatible](#wordpress-core-compatible)
- [The key "hook id" concept](#the-key-hook-id-concept)
- [API](#api)
- [Embed in OOP projects](#embed-in-oop-projects)
- [Gotchas!](#gotchas)
- [Requirements](#requirements)
- [Installation](#installation)
- [Codename: Striatum](#codename-striatum)
- [Developers and Contributors](#developers-and-contributors)
- [License](#license)

\##Read also##

- [API documentation](https://github.com/Giuseppe-Mazzapica/Striatum/blob/master/API.md)

\##Quick Start## Striatum is very easy to use, e.g. adding an action is as easy as:

```
Brain\Hooks::addAction( 'myplugin.init', 'init', [ new MyPlugin, 'init' ] );
```

or fire a custom action or filter

```
// action
Brain\Hooks::trigger( 'foo_action', $optional_1, $optional_2, $and_so_on );

 // filter
$filtered = Brain\Hooks::filter( 'bar_filter', $unfiltered, $optional_1 );
```

All the functions available through the `Brain\Hooks` API facade are documented in the API doc page. (Before teasing the *static* approach, read [here](#api)).

\##Features##

1. Add action and filters to core hooks, in absolutely core-compatible way
2. Remove (or edit) any type of callback added, even objects methods or closures
3. Auto-removing hooks: add callback that auto-remove themselves after a specific number of times they have been performed
4. Hooks *freezing* and *unfreezing*: temporarily disable callbacks added to one or more hooks, and then enabling them again
5. Add same callback to more than one hook using one function
6. Advanced debug for hooks
7. Fluent (chained) coding style

\##More info##

In WordPress there are **hooks** and **callbacks**. An hook is identified by a tag (e.g. `'init'`, `'the_content'`) and for every tag is possible to have multiple callbacks attached. Every callback has two additional properties: priority and accepted arguments. In Striatum, a tag has its own subject object, and rather than callbacks, Striatum uses observer objects, and every observer has properties that coincides with WordPress ones: callback, priority and accepted arguments. Additionally, Striatum observers have some additional properties, main one is **id**, but there are also 'times' and 'debug'. Another important property is 'is\_filter', that when true makes a subject object be a filter rather than an action.

\###WordPress core compatible###

Striatum is fully compatible with WordPress core hooks: in the concrete implementations of observer pattern interfaces, the subject methods `attach`, `detach` and `notify` are internally implemented using [WordPress Plugin API](http://codex.wordpress.org/Plugin_API) functions.

\###The key "hook id" concept###

To identify a specific callback, WordPress builds an unique id. When the callback is a string (plain functions) or an array of two strings (object static methods) that unique id is predictable, but when dynamic object methods are used, or worse, anonymous functions (closures), than WordPress uses [`spl_object_hash`](http://www.php.net/manual/en/function.spl-object-hash.php) and to identify a specific callback becomes a pain. For further info worth reading these two *WordPress Development* (Stack Exchange) answers, [this one](http://wordpress.stackexchange.com/a/57088/35541) by @toscho (Thomas Scholz), and [this one](http://wordpress.stackexchange.com/a/140989/35541) by myself. Striatum force to **set an id property for every hook** (observer) added, in this way is possible to use that id to retrieve, remove, edit or debug the hook object.

\###API###

Striatum package comes with an API that ease its usage, without having to get, instantiate or digging into package objects. API is defined in a class, stored in the Brain (Pimple) container with the id: `"hooks.api"`. So is possible to get it using Brain instance, something like: `$api = Brain\Container::instance()->get("hooks.api")`, and then call all API function on the instance got in that way. However that's not very easy to use, especially for people used to just use a plain function to add and trigger hooks. This is the reason why package also comes with a **facade class**. The term is not referred to [façade pattern](http://en.wikipedia.org/wiki/Facade_pattern), but more to [Laravel facades](http://laravel.com/docs/facades), whence the approach (not actual code) comes from: no *real* static method is present in the class, but a single `__callstatic` method that *proxy* API methods to proper instantiated objects.

The facade class is named `Hooks` inside Brain namespace. Using it, add an hook is as easy as:

```
Brain\Hooks::addAction( 'plugin.init', 'init', [ new MyPlugin, 'init' ] );
```

`addAction` method is designed using almost same signature of core `add_action` function, so take almost same arguments. It differs for first argument that is the hook id: using `'plugin.init'`, is possible to retrieve, to edit, to remove and to debug the hook added.

All the functions available through the `Brain\Hooks` facade are documented in the [API documentation](https://github.com/Giuseppe-Mazzapica/Striatum/blob/master/API.md).

\###Embed in OOP projects###

The static facade class is easy to use, however using in that way inside other classes, create there hardcoded dependency to Striatum. In addition, unit testing other classes in isolation becomes pratically impossible. To solve these problems, the easiest way is to use composition via dependency injection. In facts, the `Brain\Hooks` facade class can be used in dynamic way, like so:

```
$hooks = new Brain\Hooks;
$hooks->addAction( 'plugin.init', 'init', [ new MyPlugin, 'init' ] );
```

Looking at `Brain\Hooks` class code, you'll see there is absolutely no difference in the two methods, but using the latter is possible to inject an instance of the class inside other classes. See the following example:

```
class A_Plugin_Class {

  function __construct( \Brain\Hooks $hooks ) {
    $this->hooks = $hooks;
  }

  function get_a_filtered_value( $a_value ) {
    return $this->hooks->filter( 'a_filter', $a_value, $this );
  }

}
```

The method `get_a_filtered_value` makes use of `$this->hooks` property to call the Striatum API method. Testing the method in isolation is very simple too, an example using PHPUnit and Mockery:

```
class A_Plugin_Class_Test () {

  test_get_a_filtered_value() {
    $hooks = \Mockery::mock('\Brain\Hooks');
    $hooks->shouldReceive( 'filter' )
      ->once()
      ->with( 'a_filter', 'foo' )
      ->andReturn( 'bar' );
    $class = new A_Plugin_Class( $hooks );
    $this->assertEquals( 'bar', $class->get_a_filtered_value( 'foo' ) );
  }
}
```

So the method is tested in isolation, mocking the behavior of a filter: easy and straightforward.

If the classes had used the core `apply_filters` this simple test would be very hard and had required a testing package like the awesome [wp\_mock by 10up](https://github.com/10up/wp_mock) or the simpler, but less powerful [`HooksMock`](https://github.com/Giuseppe-Mazzapica/HooksMock) by myself.

\###Gotchas!###

Striatum is a Brain module. As you can read in [Brain readme](https://github.com/Giuseppe-Mazzapica/Brain/blob/master/README.md), it bootstrap itself and its modules on `after_setup_theme` with priority 0, this mean that you **can't use Striatum to attach callbacks to hooks that are triggered before `after_setup_theme`**.

There are not so many hooks fired before `after_setup_theme`, and all are available only for plugins and not themes, so if you use Striatum in themes you will not miss anything. Main hooks **not** available in Striatum are: `muplugins_loaded` (only for mu-plugins), `plugins_loaded`, `sanitize_comment_cookies`, `setup_theme` and `load_textdomain`.

\###Requirements###

- PHP 5.4+
- Composer (to install)
- WordPress 3.9 (it *maybe* works with earlier versions, but it's not tested and versions &lt; 3.9 will never supported).

\###Installation###

You need [Composer](https://getcomposer.org/) to install the package. It is hosted on [Packagist](https://packagist.org/), so the only thing needed is insert `"brain/striatum": "dev-master"` in your `composer.json` `require` object

```
{
    "require": {
        "php": ">=5.4",
        "brain/striatum": "dev-master"
    }
}

```

See [Composer documentation](https://getcomposer.org/doc/) on how to install Composer itself, and packages.

\###Codename: Striatum###

The *Striatum*, also known as the *neostriatum* or *striate nucleus*, is a subcortical part of the forebrain. Seems that human perception of timing resides in that part of brain.

Striatum is so called because is a [Brain](https://github.com/Giuseppe-Mazzapica/Brain) module, and it's function regards the *timing* aspect of WordPress: **hooks**.

\###Developers &amp; Contributors###

Package is open to contributors and pull requests. It comes with a set of unit tests written for [PHPUnit](http://phpunit.de/) suite. Please be sure all tests pass before submit a PR. To run tests, please install package in stand-alone mode (i.e 'vendor' folder is inside package folder). When installed in *dev* mode Striatum also install [Mockery](https://github.com/padraic/mockery), a powerful mocking test utility, and [HooksMock](https://github.com/Giuseppe-Mazzapica/HooksMock) a package that was written expressly to test Striatum.

\###License###

Striatum own code is licensed under GPLv2+. Through Composer, it install code from:

- [Composer](https://getcomposer.org/) (MIT)
- [Brain](https://github.com/Giuseppe-Mazzapica/Brain) (GPLv2+)
- [Pimple](http://pimple.sensiolabs.org/) (MIT) - required by Brain -
- [PHPUnit](http://phpunit.de/) (BSD-3-Clause) - only dev install -
- [Mockery](https://github.com/padraic/mockery) (BSD-3-Clause) - only dev install -
- [HooksMock](https://github.com/Giuseppe-Mazzapica/HooksMock) (GPLv2+) - only dev install -

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3734d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2208282?v=4)[Giuseppe Mazzapica](/maintainers/gmazzap)[@gmazzap](https://github.com/gmazzap)

---

Top Contributors

[![gmazzap](https://avatars.githubusercontent.com/u/2208282?v=4)](https://github.com/gmazzap "gmazzap (56 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/brain-striatum/health.svg)

```
[![Health](https://phpackages.com/badges/brain-striatum/health.svg)](https://phpackages.com/packages/brain-striatum)
```

###  Alternatives

[fidry/cpu-core-counter

Tiny utility to get the number of CPU cores.

238148.2M23](/packages/fidry-cpu-core-counter)[kiboit/phast

A toolbox for optimizing web page performance

3615.4k1](/packages/kiboit-phast)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
