Skip to content

Commit

Permalink
change to static and rearrange
Browse files Browse the repository at this point in the history
  • Loading branch information
mogmarsh committed Dec 21, 2023
1 parent 83bc11e commit 3bd416e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 93 deletions.
49 changes: 3 additions & 46 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,53 +64,10 @@ function () {
* Instantiate the plugin.
*/
function main(): void {
$lyrics = "Hello, Dolly
Well, hello, Dolly
It's so nice to have you back where you belong
You're lookin' swell, Dolly
I can tell, Dolly
You're still glowin', you're still crowin'
You're still goin' strong
I feel the room swayin'
While the band's playin'
One of our old favorite songs from way back when
So, take her wrap, fellas
Dolly, never go away again
Hello, Dolly
Well, hello, Dolly
It's so nice to have you back where you belong
You're lookin' swell, Dolly
I can tell, Dolly
You're still glowin', you're still crowin'
You're still goin' strong
I feel the room swayin'
While the band's playin'
One of our old favorite songs from way back when
So, golly, gee, fellas
Have a little faith in me, fellas
Dolly, never go away
Promise, you'll never go away
Dolly'll never go away again";

// Add a feature using the filter.
// add_filter( 'create_wordpress_plugin_features', function ( array $features ) use ( $lyrics ): array {
// $features[ 'Create_WordPress_Plugin\Features\Hello' ] = [ 'lyrics' => $lyrics ];
// return $features;
// } );

// Add a feature using the array on construct.
$features =[
// 'Create_WordPress_Plugin\Features\Hello' => [ 'lyrics' => $lyrics ],
$features = [
// Add initial features here.
];
$features = apply_filters( 'create_wordpress_plugin_features', $features );

$feature_manager = new Feature_Manager( $features );
// Add a feature using the add_feature method.
$feature_manager->add_feature( 'Create_WordPress_Plugin\Features\Hello', [ 'lyrics' => $lyrics ] );
$feature_manager->boot();
// Get the instance of the feature.
$hello_feature = $feature_manager->get_feature( 'Create_WordPress_Plugin\Features\Hello' );
// Once we have the instance, we can remove hooks from inside the instance.
// remove_action( 'admin_head', [ $hello_feature, 'dolly_css' ] );
Feature_Manager::add_features( $features );
}
main();
59 changes: 12 additions & 47 deletions src/class-feature-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,32 @@

use \Alley\WP\Types\Feature;

class Feature_Manager implements Feature {
class Feature_Manager {
/**
* Collected features.
*
* @var Feature[]
*/
private array $features = [];

/**
* Have the features been booted?
*
* @var bool $booted
*/
private bool $booted = false;
private static array $features = [];

/**
* Set up.
*
* @param Feature ...$features Features.
* @param array ...$features_to_create Array of feature classnames and arguments.
*/
public function __construct( array $features_to_create = [] ) {
$this->autoload_features( CREATE_WORDPRESS_PLUGIN_DIR . '/features' );

public static function add_features( array $features_to_create = [] ) {

Check failure on line 25 in src/class-feature-manager.php

View workflow job for this annotation

GitHub Actions / code-quality / phpstan PHP 8.2

Method Create_WordPress_Plugin\Feature_Manager::add_features() has no return type specified.

Check failure on line 25 in src/class-feature-manager.php

View workflow job for this annotation

GitHub Actions / code-quality / phpstan PHP 8.2

Method Create_WordPress_Plugin\Feature_Manager::add_features() has parameter $features_to_create with no value type specified in iterable type array.
foreach ( $features_to_create as $feature_class => $args ) {
$this->features[] = new $feature_class( ...$args );
self::add_feature( $feature_class, $args );
}
}

/**
* Boot the feature.
*/
public function boot(): void {
foreach ( $this->features as $feature ) {
$feature->boot();
}
$this->booted = true;
}

/**
* Return the plugin features.
*
* @return Feature[] The plugin features.
*/
public function get_features() {
return $this->features;
public static function get_features() {
return self::$features;
}

/**
Expand All @@ -63,12 +44,10 @@ public function get_features() {
* @param array $args The arguments to pass to the feature constructor.
* @return Feature The feature that was added.
*/
public function add_feature( string $feature_class, array $args = [] ) {
public static function add_feature( string $feature_class, array $args = [] ) {

Check failure on line 47 in src/class-feature-manager.php

View workflow job for this annotation

GitHub Actions / code-quality / phpstan PHP 8.2

Method Create_WordPress_Plugin\Feature_Manager::add_feature() has parameter $args with no value type specified in iterable type array.
$feature = new $feature_class( ...$args );
$this->features[] = $feature;
if ( $this->booted ) {
$feature->boot();
}
self::$features[] = $feature;

Check failure on line 49 in src/class-feature-manager.php

View workflow job for this annotation

GitHub Actions / code-quality / phpstan PHP 8.2

Static property Create_WordPress_Plugin\Feature_Manager::$features (array<Alley\WP\Types\Feature>) does not accept array<object>.
$feature->boot();

Check failure on line 50 in src/class-feature-manager.php

View workflow job for this annotation

GitHub Actions / code-quality / phpstan PHP 8.2

Call to an undefined method object::boot().
return $feature;

Check failure on line 51 in src/class-feature-manager.php

View workflow job for this annotation

GitHub Actions / code-quality / phpstan PHP 8.2

Method Create_WordPress_Plugin\Feature_Manager::add_feature() should return Alley\WP\Types\Feature but returns object.
}

Expand All @@ -78,26 +57,12 @@ public function add_feature( string $feature_class, array $args = [] ) {
* @param string $feature_name The name of the feature to get.
* @return Feature|null The feature or null if it doesn't exist.
*/
public function get_feature( string $feature_name ) {
foreach ( $this->features as $feature ) {
public static function get_feature( string $feature_name ) {
foreach ( self::$features as $feature ) {
if ( get_class( $feature ) === $feature_name ) {
return $feature;
}
}
return null;
}

/**
* Autoload features from a directory.
* This only includes the files. It does not boot the features.
*
* @param string $path The directory path.
* @return void
*/
public function autoload_features( string $path ) {
$files = glob( $path . '/*.php' );
foreach ( $files as $file ) {
require_once $file;
}
}
}
53 changes: 53 additions & 0 deletions src/features/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Features
Features should be PHP classes that implement the [Alley\WP\Types\Feature interface](https://github.com/alleyinteractive/wp-type-extensions/blob/main/src/alley/wp/types/interface-feature.php).

Features should be located in the `src/features` directory of the plugin and have namespace `Create_WordPress_Plugin\Features;`

Files in the features directory will be autoloaded, but features will not be instantiated. Features are instantiated via the `Feature_Manager` static class.

The following variable is passed to the `Class_Hello` feature in each of the following examples. This shows how we can remove any business logic from the feature and pass it in when the feature is added.

```
$lyrics = "Hello, Dolly
Well, hello, Dolly
It's so nice to have you back where you belong
You're lookin' swell, Dolly
I can tell, Dolly
You're still glowin', you're still crowin'
You're still goin' strong
I feel the room swayin'
While the band's playin'
One of our old favorite songs from way back when
So, take her wrap, fellas
Dolly, never go away again";
```

## There are three ways to add a feature:
### Add a feature using the `create_wordpress_plugin_features` filter.
```
add_filter( 'create_wordpress_plugin_features', function ( array $features ) use ( $lyrics ): array {
$features[ 'Create_WordPress_Plugin\Features\Hello' ] = [ 'lyrics' => $lyrics ];
return $features;
} );
```
### Add a feature using the add_features method
```
$features = [
'Create_WordPress_Plugin\Features\Hello' => [ 'lyrics' => $lyrics ],
];
$features = apply_filters( 'create_wordpress_plugin_features', $features );
Feature_Manager::add_features( $features );
```
### Add a feature using the add_feature method
```
Feature_Manager::add_feature( 'Create_WordPress_Plugin\Features\Hello', [ 'lyrics' => $lyrics ] );
```
## Get the instance of an added feature
```
$hello_feature = Feature_Manager::get_feature( 'Create_WordPress_Plugin\Features\Hello' );
```
## Once we have the instance, we can remove hooks from inside the instance
```
remove_action( 'admin_head', [ $hello_feature, 'dolly_css' ] );
```
File renamed without changes.

0 comments on commit 3bd416e

Please sign in to comment.