| 1 |
<?php |
| 2 |
|
| 3 |
namespace Bookit\Contracts\Provider; |
| 4 |
|
| 5 |
use Bookit\Contracts\Service_Provider as Service_Provider; |
| 6 |
|
| 7 |
|
| 8 |
/** |
| 9 |
* Class Controller. |
| 10 |
* |
| 11 |
* @since 2.5.0 |
| 12 |
* |
| 13 |
* @package Bookit\Provider; |
| 14 |
* |
| 15 |
* @property ContainerInterface $container |
| 16 |
*/ |
| 17 |
abstract class Controller extends Service_Provider { |
| 18 |
/** |
| 19 |
* Registers the filters and actions hooks added by the controller if the controller has not registered yet. |
| 20 |
* |
| 21 |
* @since 2.5.0 |
| 22 |
* |
| 23 |
* @return void |
| 24 |
*/ |
| 25 |
public function register() { |
| 26 |
/* |
| 27 |
* Look up and set the value in the container request cache to allow building the same Controller |
| 28 |
* with a **different** container. (e.g. in tests). |
| 29 |
*/ |
| 30 |
if ( static::is_registered() ) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
// Register the controller as a singleton in the container. |
| 35 |
// @todo remove when the Container is updated to bind Providers as singletons by default. |
| 36 |
$this->container->singleton( static::class, $this ); |
| 37 |
|
| 38 |
if ( ! $this->is_active() ) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
$this->container->setVar( static::class . '_registered', true ); |
| 43 |
|
| 44 |
$this->do_register(); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Registers the filters and actions hooks added by the controller. |
| 49 |
* |
| 50 |
* @since 2.5.0 |
| 51 |
* |
| 52 |
* @return void |
| 53 |
*/ |
| 54 |
abstract protected function do_register(): void; |
| 55 |
|
| 56 |
/** |
| 57 |
* Removes the filters and actions hooks added by the controller. |
| 58 |
* |
| 59 |
* Bound implementations should not be removed in this method! |
| 60 |
* |
| 61 |
* @since 2.5.0 |
| 62 |
* |
| 63 |
* @return void Filters and actions hooks added by the controller are be removed. |
| 64 |
*/ |
| 65 |
abstract public function unregister(): void; |
| 66 |
|
| 67 |
/** |
| 68 |
* Whether the controller is active or not. |
| 69 |
* |
| 70 |
* Controllers will be active by default, if that is not the case, the controller should override this method. |
| 71 |
* |
| 72 |
* @since 2.5.0 |
| 73 |
* |
| 74 |
* @return bool Whether the controller is active or not. |
| 75 |
*/ |
| 76 |
public function is_active(): bool { |
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Logs a message at the `debug` level. |
| 82 |
* |
| 83 |
* @since 2.5.0 |
| 84 |
* |
| 85 |
* @param string $message The message to log. |
| 86 |
* @param array $context An array of context to log with the message. |
| 87 |
* |
| 88 |
* @return void The message is logged. |
| 89 |
*/ |
| 90 |
protected function debug( string $message, array $context = [] ): void { |
| 91 |
do_action( 'bookit_log', 'debug', $message, array_merge( [ |
| 92 |
'controller' => static::class, |
| 93 |
], $context ) ); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Logs a message at the `warning` level. |
| 98 |
* |
| 99 |
* @since 2.5.0 |
| 100 |
* |
| 101 |
* @param string $message The message to log. |
| 102 |
* @param array $context An array of context to log with the message. |
| 103 |
* |
| 104 |
* @return void The message is logged. |
| 105 |
*/ |
| 106 |
protected function warning( string $message, array $context = [] ): void { |
| 107 |
do_action( 'bookit_log', 'warning', $message, array_merge( [ |
| 108 |
'controller' => static::class, |
| 109 |
], $context ) ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Logs a message at the `error` level. |
| 114 |
* |
| 115 |
* @since 2.5.0 |
| 116 |
* |
| 117 |
* @param string $message The message to log. |
| 118 |
* @param array $context An array of context to log with the message. |
| 119 |
* |
| 120 |
* @return void The message is logged. |
| 121 |
*/ |
| 122 |
protected function error( string $message, array $context = [] ): void { |
| 123 |
do_action( 'bookit_log', 'error', $message, array_merge( [ |
| 124 |
'controller' => static::class, |
| 125 |
], $context ) ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Returns whether any instance of this controller has been registered or not. |
| 130 |
* |
| 131 |
* @since 2.5.0 |
| 132 |
* |
| 133 |
* @return bool Whether any instance of this controller has been registered or not. |
| 134 |
*/ |
| 135 |
public static function is_registered(): bool { |
| 136 |
return (bool) bookit()->getVar( static::class . '_registered' ); |
| 137 |
} |
| 138 |
} |
| 139 |
|