2017 © Pedro Peláez
 

library metis

Pimple, with some helpful tweaks for working in WordPress

image

ssnepenthe/metis

Pimple, with some helpful tweaks for working in WordPress

  • Thursday, October 19, 2017
  • by ssnepenthe
  • Repository
  • 1 Watchers
  • 2 Stars
  • 407 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 0 Forks
  • 2 Open issues
  • 11 Versions
  • 1 % Grown

The README.md

metis

Pimple, with some some helpful tweaks for working in WordPress., (*1)

Requirements

PHP 5.3 or later and Composer., (*2)

Note: Metis should continue to work down to 5.3 but is no longer tested below 5.4., (*3)

Installation

$ composer require ssnepenthe/metis

Usage

This is basically Pimple but you will use Metis\Container instead of Pimple\Container., (*4)

The following features have been added:, (*5)

Handle activation and deactivation logic within your service providers, (*6)

class Some_Provider implements Pimple\ServiceProviderInterface {
    public function activate( Pimple\Container $container ) {
        // Handle activation here.
    }

    public function deactivate( Pimple\Container $container ) {
        // Handle deactivation here.
    }

    // ...
}

And then call the corresponsing method on your container instance., (*7)

$container = new Metis\Container;
$container->register( new Some_Provider );

register_activation_hook( __FILE__, array( $container, 'activate' ) );
register_deactivation_hook( __FILE__, array( $container, 'deactivate' ) );

Handle boot logic (add_action/add_filter calls) within your service providers, (*8)

class Another_Provider implements Pimple\ServiceProviderInterface {
    public function boot( Pimple\Container $container ) {
        add_action( 'init', array( $container['service'], 'init' ) );
    }

    // ...
}

And then call the corresponding method on your container instance., (*9)

$container = new Metis\Container;
$container->register( new Another_Provider );

add_action( 'plugins_loaded', array( $container, 'boot' ) );

Service Proxies, (*10)

One of the many benefits of a dependency injection container like Pimple is that objects are created on demand as you access their container entries., (*11)

This can be especially useful for functionality that is only needed on a limited number of requests (e.g. admin, cron, etc.)., (*12)

Unfortunately this doesn't always work the way you might want in WordPress:, (*13)

class Admin_Provider implements Pimple\ServiceProviderInterface {
    public function boot( Pimple\Container $container ) {
        add_action( 'admin_init', array( $container['admin_page'], 'do_something' ) );
    }

    // ...
}

Since the boot() method is typically attached to the plugins_loaded hook, the admin page object will always be created regardless of whether admin_init has been triggered., (*14)

A sensible approach would be to verify that the current request is for an admin page before calling add_action():, (*15)

class Admin_Provider implements Pimple\ServiceProviderInterface {
    public function boot( Pimple\Container $container ) {
        if ( is_admin() ) {
            add_action( 'admin_init', array( $container['admin_page'], 'do_something' ) );
        }
    }

    // ...
}

But this results in a boot method littered with conditionals., (*16)

An alternative would be to access the admin_page entry within an anonymous function:, (*17)

class Admin_Provider implements Pimple\ServiceProviderInterface {
    public function boot( Pimple\Container $container ) {
        add_action( 'admin_init', function() use ( $container ) {
            $container['admin_page']->do_something();
        }
    }

    // ...
}

But that gets tedious quickly and can result in a large number of unnecessary Closure objects floating around., (*18)

Instead, you might choose to extend Metis\Base_Provider and use the proxy() method:, (*19)

class Admin_Provider extends Metis\Base_Provider {
    public function boot( Pimple\Container $container ) {
        add_action( 'admin_init', array( $this->proxy( $container, 'admin_page' ), 'do_something' ) );
    }

    // ...
}

This will create a Metis\Proxy object to be used in place of the admin page object. This object will correctly proxy all method calls to the underlying service from the container while holding off on creation of that service until it is actually needed., (*20)

Access WordPress globals from the container, (*21)

Use the WordPress_Provider class to get access to frequently used WordPress globals from the container., (*22)

$container = new Metis\Container;
$container->register( new Metis\WordPress_Provider );

$container['wp'] === $GLOBALS['wp']; // true

$wp, $wpdb, $wp_query, $wp_rewrite, $wp_filesystem and $wp_object_cache are all added to the container., (*23)

Be careful about timing when using these - each will return null if it has not yet been defined., (*24)

The Versions

19/10 2017

dev-master

9999999-dev https://github.com/ssnepenthe/metis

Pimple, with some helpful tweaks for working in WordPress

  Sources   Download

GPL-2.0

The Requires

 

The Development Requires

19/10 2017

0.7.0

0.7.0.0 https://github.com/ssnepenthe/metis

Pimple, with some helpful tweaks for working in WordPress

  Sources   Download

GPL-2.0

The Requires

 

The Development Requires

20/07 2017

0.6.0

0.6.0.0 https://github.com/ssnepenthe/metis

Pimple, with some helpful tweaks for working in WordPress

  Sources   Download

GPL-2.0

The Requires

 

The Development Requires

17/04 2017

0.5.0

0.5.0.0 https://github.com/ssnepenthe/metis

Just some tools for simplifying some WordPress development tasks.

  Sources   Download

GPL-2.0

The Requires

 

13/09 2016

0.4.0

0.4.0.0 https://github.com/ssnepenthe/metis

A small framework for simplifying some WordPress tasks.

  Sources   Download

GPL-2.0

The Requires

  • php >=5.4
  • ext-xml *

 

The Development Requires

01/09 2016

0.3.0

0.3.0.0 https://github.com/ssnepenthe/metis

A small framework for simplifying some WordPress tasks.

  Sources   Download

GPL-2.0

The Requires

  • php >=5.4
  • ext-xml *

 

The Development Requires

22/08 2016

0.2.0

0.2.0.0 https://github.com/ssnepenthe/metis

A small framework for simplifying some WordPress tasks.

  Sources   Download

GPL-2.0

The Requires

  • php >=5.4
  • ext-xml *

 

The Development Requires

21/08 2016

0.1.2

0.1.2.0 https://github.com/ssnepenthe/metis

A small framework for simplifying some WordPress tasks.

  Sources   Download

GPL-2.0

The Requires

  • php >=5.4
  • ext-xml *

 

The Development Requires

08/08 2016

dev-develop

dev-develop https://github.com/ssnepenthe/metis

A small framework for simplifying some common WordPress tasks.

  Sources   Download

GPL-2.0

The Requires

  • php >=5.4
  • ext-xml *

 

The Development Requires

13/05 2016

0.1.1

0.1.1.0 https://github.com/ssnepenthe/metis

A small framework for simplifying some common WordPress tasks.

  Sources   Download

GPL-2.0

The Requires

  • php >=5.4

 

The Development Requires

30/03 2016

0.1.0

0.1.0.0 https://github.com/ssnepenthe/metis

A small framework for simplifying some common WordPress tasks.

  Sources   Download

GPL-2.0

The Requires

  • php >=7.0

 

The Development Requires