Skip to content
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
115 changes: 75 additions & 40 deletions index.js

Large diffs are not rendered by default.

10 changes: 3 additions & 7 deletions templates/plugin-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,13 @@ function ( $class_name ) {
register_activation_hook(
__FILE__,
static function () {
$plugin = \{{NS}}\Plugin::create();
$plugin->register_all();
( new \{{NS}}\Core\Activator() )->activate( $plugin->get_container() );
( new \{{NS}}\Core\Activator() )->activate();
}
);
register_deactivation_hook(
__FILE__,
static function () {
$plugin = \{{NS}}\Plugin::create();
$plugin->register_all();
( new \{{NS}}\Core\Deactivator() )->deactivate( $plugin->get_container() );
( new \{{NS}}\Core\Deactivator() )->deactivate();
}
);
{{WOOCOMMERCE_HPOS}}
Expand All @@ -75,7 +71,7 @@ static function () {
*/
function {{PREFIX}}_boot() {
load_plugin_textdomain( '{{SLUG}}', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
\{{NS}}\Plugin::create()->boot();
\{{NS}}\Plugin::instance()->boot();
}

add_action( 'plugins_loaded', '{{PREFIX}}_boot' );
17 changes: 2 additions & 15 deletions templates/src/Admin/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,21 @@

namespace {{NS}}\Admin;

use {{NS}}\Contracts\Service_Provider;
use {{NS}}\Core\Container;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Class Assets.
*/
class Assets implements Service_Provider {

/**
* No bindings needed.
*
* @param Container $container Application container.
* @return void
*/
public function register( Container $container ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
}
class Assets {

/**
* Register asset hooks.
*
* @param Container $container Application container.
* @return void
*/
public function boot( Container $container ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
public function init_hooks(): void {
add_action( 'admin_enqueue_scripts', $this->enqueue_assets( ... ) );
}

Expand Down
37 changes: 9 additions & 28 deletions templates/src/Admin/Settings_Registrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

namespace {{NS}}\Admin;

use {{NS}}\Contracts\Service_Provider;
use {{NS}}\Core\Container;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}
Expand All @@ -22,37 +19,22 @@
* Registers the admin menu page and Settings API hooks. Data access is
* delegated to Settings_Repository; markup lives in src/Admin/views/.
*/
class Settings_Registrar implements Service_Provider {
class Settings_Registrar {

/**
* Application container, kept for on-demand Settings_Repository lookups
* from inside WordPress-invoked callbacks (add_options_page() and
* add_settings_field() call these with fixed signatures, so the
* repository can't be a constructor argument here).
* Data-access layer for the plugin's option.
*
* @var Container
* @param Settings_Repository $repository Settings repository.
*/
private Container $container;

/**
* Bind Settings_Repository as a singleton.
*
* @param Container $container Application container.
* @return void
*/
public function register( Container $container ): void {
$container->singleton( Settings_Repository::class, static fn () => new Settings_Repository() );
public function __construct( private readonly Settings_Repository $repository ) {
}

/**
* Register admin menu and settings hooks.
*
* @param Container $container Application container.
* @return void
*/
public function boot( Container $container ): void {
$this->container = $container;

public function init_hooks(): void {
add_action( 'admin_menu', $this->add_menu_page( ... ) );
add_action( 'admin_init', $this->register_settings( ... ) );
}
Expand All @@ -78,8 +60,7 @@ public function add_menu_page() {
* @return void
*/
public function register_settings() {
$repository = $this->container->get( Settings_Repository::class );
$repository->register_setting();
$this->repository->register_setting();

add_settings_section(
'{{PREFIX}}_main_section',
Expand All @@ -89,7 +70,7 @@ public function register_settings() {
);

add_settings_field(
$repository->get_option_name(),
$this->repository->get_option_name(),
__( 'Sample Setting', '{{SLUG}}' ),
$this->render_sample_field( ... ),
'{{SLUG}}',
Expand All @@ -103,7 +84,7 @@ public function register_settings() {
* @return void
*/
public function render_sample_field() {
$repository = $this->container->get( Settings_Repository::class );
$repository = $this->repository;
$name = $repository->get_option_name();
$value = $repository->get_value();

Expand All @@ -120,7 +101,7 @@ public function render_page() {
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', '{{SLUG}}' ) );
}

$repository = $this->container->get( Settings_Repository::class );
$repository = $this->repository;

include {{PREFIX_UPPER}}_PATH . 'src/Admin/views/settings-page.php';
}
Expand Down
17 changes: 2 additions & 15 deletions templates/src/Ajax/Ajax_Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@

namespace {{NS}}\Ajax;

use {{NS}}\Contracts\Service_Provider;
use {{NS}}\Core\Container;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Class Ajax_Handler.
*/
class Ajax_Handler implements Service_Provider {
class Ajax_Handler {

/**
* Whether to register unauthenticated (nopriv) AJAX action for logged-out visitors.
Expand All @@ -30,22 +27,12 @@ class Ajax_Handler implements Service_Provider {
*/
protected bool $allow_nopriv = false;

/**
* No bindings needed.
*
* @param Container $container Application container.
* @return void
*/
public function register( Container $container ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
}

/**
* Register AJAX actions and asset enqueueing.
*
* @param Container $container Application container.
* @return void
*/
public function boot( Container $container ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
public function init_hooks(): void {
add_action( 'wp_ajax_{{PREFIX}}_action', $this->handle_ajax( ... ) );
if ( $this->allow_nopriv ) {
add_action( 'wp_ajax_nopriv_{{PREFIX}}_action', $this->handle_ajax( ... ) );
Expand Down
17 changes: 2 additions & 15 deletions templates/src/Blocks/Block_Registrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

namespace {{NS}}\Blocks;

use {{NS}}\Contracts\Service_Provider;
use {{NS}}\Core\Container;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}
Expand All @@ -29,29 +26,19 @@
* (add `--variant dynamic` for a server-rendered one) — then rebuild. It is
* picked up automatically; nothing here changes.
*/
class Block_Registrar implements Service_Provider {
class Block_Registrar {

/**
* Directory (relative to the plugin root) holding compiled block metadata.
*/
private const BUILD_DIR = 'assets/build/blocks';

/**
* No bindings needed.
*
* @param Container $container Application container.
* @return void
*/
public function register( Container $container ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
}

/**
* Register block hooks.
*
* @param Container $container Application container.
* @return void
*/
public function boot( Container $container ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
public function init_hooks(): void {
add_action( 'init', $this->register_blocks( ... ) );
}

Expand Down
23 changes: 5 additions & 18 deletions templates/src/CLI/Commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,24 @@

namespace {{NS}}\CLI;

use {{NS}}\Contracts\Service_Provider;
use {{NS}}\Core\Container;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* WP-CLI Commands for {{PLUGIN_NAME}}.
*/
class Commands implements Service_Provider {

/**
* No bindings needed.
*
* @param Container $container Application container.
* @return void
*/
public function register( Container $container ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
}
class Commands {

/**
* Register WP-CLI commands.
*
* @param Container $container Application container.
* The bootloader only instantiates this class behind a WP_CLI guard; the
* repeat check here keeps the class safe to call directly (e.g. in tests).
*
* @return void
*/
public function boot( Container $container ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
// Guard here rather than with a top-level `return` in this file — that
// would stop the class from ever being declared and break PSR-4
// autoloading (and unit tests) outside a WP-CLI context.
public function init_hooks(): void {
if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
return;
}
Expand Down
27 changes: 2 additions & 25 deletions templates/src/Cache/Cache_Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

namespace {{NS}}\Cache;

use {{NS}}\Contracts\Service_Provider;
use {{NS}}\Core\Container;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}
Expand All @@ -26,10 +23,9 @@
*
* `set_transient()` already routes to the object cache when one is present,
* so writing to both would just store every value twice on a Redis site —
* this picks one. Resolve from the container:
* $container->get( Cache_Service::class ).
* this picks one. Reach the shared instance via Services::cache().
*/
class Cache_Service implements Service_Provider {
class Cache_Service {

/**
* Object cache group / transient key prefix.
Expand All @@ -38,25 +34,6 @@ class Cache_Service implements Service_Provider {
*/
private const GROUP = '{{PREFIX}}';

/**
* Bind this instance so other services can resolve it from the container.
*
* @param Container $container Application container.
* @return void
*/
public function register( Container $container ): void {
$container->instance( self::class, $this );
}

/**
* No hooks to register — this is a plain utility service, not a hook registrar.
*
* @param Container $container Application container.
* @return void
*/
public function boot( Container $container ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
}

/**
* Get a cached value, or $fallback if it isn't cached (or has expired).
*
Expand Down
5 changes: 1 addition & 4 deletions templates/src/Contracts/Activatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

namespace {{NS}}\Contracts;

use {{NS}}\Core\Container;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}
Expand All @@ -25,8 +23,7 @@ interface Activatable {
/**
* Run activation tasks.
*
* @param Container $container Application container.
* @return void
*/
public function activate( Container $container ): void;
public function activate(): void;
}
31 changes: 0 additions & 31 deletions templates/src/Contracts/Conditional.php

This file was deleted.

Loading
Loading