Hooky - My first open source composer package

Here’s my first composer package Hooky. This is a package that allows people that have their own libraries or that can replace core classes from existing packages to allow certain methods to be hooked into before or after. Hooks can be applied to one instance or to all instances.

It’s inspired by AOP, but it’s not exactly AOP. I wanted to get some feedback in terms of design, usefulness, any best practices I’m not following, and concerns. I’m also more than happy to answer any questions about how I went about creating this or any other questions you may have that I could help with regarding this package: things you want to learn, things I learned from this, etc.

Some things I did:

  • implemented PSR2 and used php-cs-fixer judiciously
  • implemented Semantic Versioning (still trying to wrap my head around when to bump minor versions)
  • implemented Travis CI and SensioLabs Insights
  • TDD
  • Git flow
  • Traits
  • Reflection

The whole idea came about while writing an open source scraping package. Part of it was that I wanted an easy way to hook into the http client do things before or after a GET call: things like authenticating with OAuth before each request. So that’s where the whole idea regarding hooking into the methods came into play.

Here’s an example:

 <?php
require 'vendor/autoload.php';

class HelloWorld
{
    use _2UpMedia\Hooky\HooksTrait;

    public function sayIt()
    {
        $this->callBeforeHooks($this, __METHOD__);
    }
}

HelloWorld::globalBeforeAllHook(function () {
    echo 'hello world';
});

(new HelloWorld())->sayIt();

Other possible applications:

  • Logging, triggering events, analytics
  • When you need to do something consistently before or after a method call: custom authenticating of an HTTP call, transforming data after it’s been received
  • Whatever you make of it