Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow attributes to register hooks #273

Merged
merged 4 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/coding-standards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [7.4]
php: [8.0]

name: Coding Standards

Expand Down
26 changes: 26 additions & 0 deletions src/mantle/support/attributes/class-action.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Action class file
*
* @package Mantle
*/

namespace Mantle\Support\Attributes;

use Attribute;

/**
* Hook Action Attribute
*
* Used to hook a method to an WordPress action at a specific priority.
*/
#[Attribute]
class Action {
/**
* Constructor.
*
* @param string $action Action name.
* @param int $priority Priority, defaults to 10.
*/
public function __construct( public string $action, public int $priority = 10 ) {}
}
30 changes: 30 additions & 0 deletions src/mantle/support/class-service-provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

use Mantle\Console\Command;
use Mantle\Contracts\Application;
use Mantle\Support\Attributes\Action;
use Mantle\Support\Str;
use Psr\Log\{LoggerAwareInterface, LoggerAwareTrait};
use ReflectionClass;

use function Mantle\Support\Helpers\add_action;
use function Mantle\Support\Helpers\collect;
Expand Down Expand Up @@ -64,6 +66,7 @@ public function boot_provider() {
}

$this->boot_action_hooks();
$this->boot_attribute_hooks();
$this->boot();
}

Expand Down Expand Up @@ -96,6 +99,33 @@ function( string $method ) {
);
}

/**
* Boot all attribute actions on the service provider.
*/
protected function boot_attribute_hooks() {
// Abandon if we're not running PHP 8.
if ( version_compare( phpversion(), '8.0.0', '<' ) ) {
return;
}

$class = new ReflectionClass( static::class );

foreach ( $class->getMethods() as $method ) {
$action_attributes = $method->getAttributes( Action::class );

if ( empty( $action_attributes ) ) {
continue;
}

foreach ( $action_attributes as $attribute ) {
$instance = $attribute->newInstance();

add_action( $instance->action, [ $this, $method->name ], $instance->priority );
}
}

}

/**
* Register a wp-cli command.
*
Expand Down
26 changes: 26 additions & 0 deletions tests/support/test-service-provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Mantle\Console\Command;
use Mantle\Contracts\Providers as ProviderContracts;
use Mantle\Support\Service_Provider;
use Mantle\Support\Attributes\Action;
use Mockery as m;

class Test_Service_Provider extends \Mockery\Adapter\Phpunit\MockeryTestCase {
Expand Down Expand Up @@ -70,6 +71,22 @@ public function test_hook_method_filter() {

$this->assertEquals( 15, $value );
}

public function test_hook_attribute() {
// Abandon if we're not running PHP 8.
if ( version_compare( phpversion(), '8.0.0', '<' ) ) {
$this->markTestSkipped( 'Requires PHP 8.0.0 or greater.' );
return;
}

$app = m::mock( Application::class )->makePartial();
$app->register( Provider_Test_Hook::class );
$app->boot();

do_action( 'testable-attribute-hook' );

$this->assertTrue( $_SERVER['__custom_hook_fired'] ?? false );
}
}

class Provider_Test_Hook extends Service_Provider {
Expand All @@ -80,4 +97,13 @@ public function on_custom_hook() {
public function on_custom_filter( $value ) {
return $value + 10;
}

#[Action('testable-attribute-hook', 20)]
public function handle_custom_hook() {
$_SERVER['__custom_hook_fired'] = true;
}

public function handle_custom_filter( $value ) {
return $value + 100;
}
}