Skip to content

Instantly share code, notes, and snippets.

@mgussekloo
Created March 13, 2018 18:44
Show Gist options
  • Save mgussekloo/54802a17b7788e851196b492a6db4d59 to your computer and use it in GitHub Desktop.
Save mgussekloo/54802a17b7788e851196b492a6db4d59 to your computer and use it in GitHub Desktop.
Proposal for dynamic operations for Timber
/**
* Dynamic operations
*/
// All the valid operations
static $registered_operations;
// Convenience method to get the valid operations.
// To register a valid operation, use the filter "timber/image/operations".
public static function getOperations() {
if (isset(self::$registered_operations) && is_array(self::$registered_operations)) { return self::$registered_operations;
}
self::$registered_operations = apply_filters( 'timber/image/operations' , []);
return self::$registered_operations;
}
// Magic method so you can call whatever method on this class.
// If the called non-existing method is a valid operation, perform it.
// If it is not, just return the source image.
// This way, missing operations (plugins) are not breaking the theme too much.
public static function __callStatic($name, $args) {
$registered_operations = self::getOperations();
if (in_array( $name, array_keys( $registered_operations ))) {
$op = call_user_func_array($registered_operations[$name], $args);
return self::_operate($args[0], $op, $force);
}
Helper::error_log('TimberImage does not support this operation: ', $name);
return $args[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment