Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
25 changes: 20 additions & 5 deletions src/Abstracts/Table_Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

use StellarWP\Schema\Tables\Contracts\Table;
use StellarWP\DB\DB;
use StellarWP\Pigeon\Provider as Pigeon_Main_Controller;
use StellarWP\Pigeon\Config;
use StellarWP\Pigeon\Tables\Utility\Safe_Dynamic_Prefix;
use StellarWP\Pigeon\Traits\Custom_Table_Query_Methods;
use DateTimeInterface;

Expand Down Expand Up @@ -140,11 +140,10 @@ abstract class Table_Abstract extends Table {
*/
public function __construct() {
$this->db = DB::class;
$this->container = Pigeon_Main_Controller::get_container();
$this->container = Config::get_container();

// Modify table names to use the hook prefix.
self::$base_table_name = sprintf( self::$base_table_name, Config::get_safe_hook_prefix() );
self::$schema_slug = sprintf( self::$schema_slug, Config::get_hook_prefix() );
self::$schema_slug = sprintf( self::$schema_slug, Config::get_hook_prefix() );
Comment thread
borkweb marked this conversation as resolved.
Outdated
}

/**
Expand All @@ -157,7 +156,12 @@ public function __construct() {
* @return string The base table name.
*/
public static function base_table_name(): string {
return sprintf( static::$base_table_name, Config::get_safe_hook_prefix() );
$container = Config::get_container();

return sprintf(
static::$base_table_name,
$container->get( Safe_Dynamic_Prefix::class )->get()
);
}

/**
Expand Down Expand Up @@ -295,4 +299,15 @@ protected function after_update( array $results ) {

return $results;
}

/**
* Returns the base table name without the dynamic prefix.
*
* @since TBD
*
* @return string The base table name without the dynamic prefix.
*/
public static function raw_base_table_name(): string {
return static::$base_table_name;
}
}
4 changes: 2 additions & 2 deletions src/Abstracts/Task_Model_Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use StellarWP\Pigeon\Contracts\Task_Model;
use RuntimeException;
use StellarWP\Pigeon\Tables\Tasks as Tasks_Table;
use StellarWP\Pigeon\Provider;
use StellarWP\Pigeon\Config;
use StellarWP\Pigeon\Contracts\Task;
use StellarWP\Pigeon\Action_Scheduler_Methods;
use StellarWP\Pigeon\Exceptions\PigeonTaskAlreadyExistsException;
Expand Down Expand Up @@ -240,7 +240,7 @@ public function get_args(): array {
* @return Table_Abstract The table interface.
*/
public function get_table_interface(): Table_Abstract {
return Provider::get_container()->get( static::TABLE_INTERFACE );
return Config::get_container()->get( static::TABLE_INTERFACE );
}

/**
Expand Down
123 changes: 59 additions & 64 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace StellarWP\Pigeon;

use RuntimeException;
use StellarWP\ContainerContract\ContainerInterface;
use StellarWP\Pigeon\Contracts\Logger;
use StellarWP\Pigeon\Loggers\ActionScheduler_DB_Logger;

Expand All @@ -21,6 +22,13 @@
* @package StellarWP\Pigeon\Config
*/
class Config {
/**
* Container object.
*
* @var ContainerInterface
Comment thread
borkweb marked this conversation as resolved.
Outdated
*/
protected static ?ContainerInterface $container = null;

/**
* The hook prefix.
*
Expand All @@ -39,57 +47,30 @@ class Config {
*/
protected static ?Logger $logger = null;

/**
* The maximum safe hook prefix length.
*
* @since TBD
*
* @var ?int
*/
protected static ?int $max_hook_prefix_length = null;

/**
* The maximum table name length.
*
* @since TBD
*
* @var int
*/
protected const MAX_TABLE_NAME_LENGTH = 64;
protected static int $max_table_name_length = 64;

/**
* The longest table name.
* Get the container.
*
* @since TBD
*
* @var string
*/
protected const LONGEST_TABLE_NAME = 'pigeon_%s_task_logs';

/**
* Gets the maximum safe hook prefix length.
*
* Calculates the maximum length a hook prefix can be while ensuring
* table names don't exceed MySQL's 64-character limit.
* @throws RuntimeException If the container is not set.
*
* @since TBD
*
* @return int The maximum safe hook prefix length.
* @return ContainerInterface
*/
public static function get_max_hook_prefix_length(): int {
if ( null !== static::$max_hook_prefix_length ) {
return static::$max_hook_prefix_length;
public static function get_container(): ContainerInterface {
if ( self::$container === null ) {
throw new \RuntimeException( 'You must provide a container via StellarWP\Pigeon\Config::set_container() before attempting to fetch it.' );
}

global $wpdb;

$wp_prefix_length = strlen( $wpdb->prefix );

$hook_prefix = static::get_hook_prefix();

$base_name_length = strlen( sprintf( self::LONGEST_TABLE_NAME, $hook_prefix ) );

return strlen( $hook_prefix ) + ( self::MAX_TABLE_NAME_LENGTH - $base_name_length - $wp_prefix_length );
return self::$container;
}

/**
Expand All @@ -111,45 +92,45 @@ public static function get_hook_prefix(): string {
}

/**
* Gets the safe hook prefix.
*
* Returns the hook prefix trimmed to the maximum safe length
* to ensure table names don't exceed MySQL's limit.
* Gets the logger.
*
* @since TBD
*
* @throws RuntimeException If the hook prefix is not set or the max hook prefix length could not be determined.
*
* @return string The safe hook prefix.
* @return Logger
*/
public static function get_safe_hook_prefix(): string {
$prefix = static::get_hook_prefix();
$max_length = static::get_max_hook_prefix_length();

if ( ! $max_length ) {
throw new RuntimeException( 'The max hook prefix could not be determined.' );
}

if ( strlen( $prefix ) > $max_length ) {
return substr( $prefix, 0, $max_length );
public static function get_logger(): Logger {
if ( ! static::$logger ) {
static::$logger = new ActionScheduler_DB_Logger();
}

return $prefix;
return static::$logger;
}

/**
* Gets the logger.
* Gets the maximum table name length.
*
* @since TBD
* @return int
*/
public static function get_max_table_name_length(): int {
return static::$max_table_name_length;
}

/**
* Returns whether the container has been set.
*
* @return Logger
* @return bool
*/
public static function get_logger(): Logger {
if ( ! static::$logger ) {
static::$logger = new ActionScheduler_DB_Logger();
}
public static function has_container(): bool {
return self::$container !== null;
}

return static::$logger;
/**
* Set the container object.
*
* @param ContainerInterface $container Container object.
*/
public static function set_container( ContainerInterface $container ) {
self::$container = $container;
}

/**
Expand Down Expand Up @@ -182,14 +163,28 @@ public static function set_logger( ?Logger $logger ): void {
static::$logger = $logger;
}

/**
* Sets the maximum table name length.
*
* @since TBD
*
* @param int $length The maximum table name length.
*
* @return void
*/
public static function set_max_table_name_length( int $length ): void {
static::$max_table_name_length = $length;
}

/**
* Resets the config.
*
* @return void
*/
public static function reset(): void {
static::$hook_prefix = '';
static::$logger = null;
static::$max_hook_prefix_length = null;
static::$container = null;
static::$hook_prefix = '';
static::$logger = null;
static::$max_table_name_length = 64;
}
}
6 changes: 3 additions & 3 deletions src/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

namespace StellarWP\Pigeon;

use StellarWP\Pigeon\Config;
use StellarWP\Pigeon\Contracts\Log_Model;
use StellarWP\Pigeon\Tables\Task_Logs as Task_Logs_Table;
use StellarWP\Pigeon\Tables\AS_Logs as AS_Logs_Table;
use StellarWP\Pigeon\Loggers\ActionScheduler_DB_Logger;
use StellarWP\Pigeon\Loggers\DB_Logger;
use StellarWP\Pigeon\Contracts\Logger;
use StellarWP\Pigeon\Provider;
use StellarWP\Pigeon\Abstracts\Model_Abstract;
use DateTimeInterface;
use StellarWP\Pigeon\Abstracts\Table_Abstract;
Expand Down Expand Up @@ -287,7 +287,7 @@ public function get_entry(): string {
* @throws RuntimeException If the log table interface is invalid.
*/
public function get_table_interface(): Table_Abstract {
$logger = Provider::get_container()->get( Logger::class );
$logger = Config::get_container()->get( Logger::class );

$table = null;

Expand All @@ -305,7 +305,7 @@ public function get_table_interface(): Table_Abstract {
throw new RuntimeException( 'Invalid log table interface.' );
}

return Provider::get_container()->get( $table );
return Config::get_container()->get( $table );
}

/**
Expand Down
32 changes: 2 additions & 30 deletions src/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,6 @@ class Provider extends Provider_Abstract {
*/
protected static string $hook_prefix;

/**
* The container.
*
* @since TBD
*
* @var ?Container
*/
private static ?Container $static_container = null;

/**
* Whether the provider has been registered.
*
Expand All @@ -77,9 +68,7 @@ public function register(): void {

$this->require_action_scheduler();

self::$static_container = $this->container;

Schema_Config::set_container( $this->container );
Schema_Config::set_container( Config::get_container() );
Schema_Config::set_db( DB::class );
$this->container->singleton( Logger::class, Config::get_logger() );
$this->container->singleton( Tables_Provider::class );
Expand All @@ -101,19 +90,6 @@ private function require_action_scheduler(): void {
require_once __DIR__ . '/../vendor/woocommerce/action-scheduler/action-scheduler.php';
}

/**
* Sets the container.
*
* @since TBD
*
* @param ?Container $container The container.
*
* @return void
*/
public static function set_container( ?Container $container ): void {
self::$static_container = $container;
}

/**
* Resets the registered state.
*
Expand All @@ -135,11 +111,7 @@ public static function reset(): void {
* @throws RuntimeException If the container has not been set.
*/
public static function get_container(): Container {
if ( ! self::$static_container ) {
throw new RuntimeException( 'The container has not been set.' );
}

return self::$static_container;
return Config::get_container();
Comment thread
borkweb marked this conversation as resolved.
Outdated
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/Tables/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
* @package StellarWP\Pigeon\Tables;
*/
class Provider extends Provider_Abstract {
/**
* Tables to register.
*
* @var array<int, class-string>
*/
private array $tables = [
Tasks::class,
Task_Logs::class,
];

/**
* Registers the service provider bindings.
*
Expand All @@ -32,6 +42,10 @@ class Provider extends Provider_Abstract {
* @return void The method does not return any value.
*/
public function register(): void {
// Bind after all tables are registered.
$this->container->singleton( Utility\Safe_Dynamic_Prefix::class );
$this->container->get( Utility\Safe_Dynamic_Prefix::class )->calculate_longest_table_name( $this->tables );

Register::table( Tasks::class );

if ( $this->container->get( Logger::class ) instanceof DB_Logger ) {
Expand Down
Loading
Loading