This commit is contained in:
2026-07-02 15:54:39 -06:00
commit 9883323161
17470 changed files with 4470592 additions and 0 deletions
@@ -0,0 +1,103 @@
<?php
namespace Vrts\Core\Traits;
use Closure;
use ReflectionClass;
use ReflectionMethod;
use BadMethodCallException;
trait Macroable {
/**
* Macros.
*
* @var array
*/
protected static $macros = [];
/**
* Registerd macro.
*
* @param string $name Name of macro function.
* @param callable $macro Callback function.
*/
public static function macro( $name, $macro ) {
static::$macros[ $name ] = $macro;
}
/**
* Register mixins.
*
* @param class $mixin Class with mixins.
*/
public static function mixin( $mixin ) {
$methods = ( new ReflectionClass( $mixin ) )->getMethods(
ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED
);
foreach ( $methods as $method ) {
$method->setAccessible( true );
static::macro( $method->name, $method->invoke( $mixin ) );
}
}
/**
* Return true if has macro.
*
* @param string $name Name of macro function.
*
* @return bool
*/
public static function has_macro( $name ) {
return isset( static::$macros[ $name ] );
}
/**
* Call static method.
*
* @param string $method The function to be called.
* @param array $parameters The parameters to be passed to the function, as an indexed array.
* @return callable The function to be called.
*
* @throws BadMethodCallException When method doesn't exist.
*/
public static function __callStatic( $method, $parameters ) {
if ( ! static::has_macro( $method ) ) {
throw new BadMethodCallException( esc_html( "Method {$method} does not exist." ) );
}
$macro = static::$macros[ $method ];
if ( $macro instanceof Closure ) {
return call_user_func_array( Closure::bind( $macro, null, static::class ), $parameters );
}
return call_user_func_array( $macro, $parameters );
}
/**
* Call method.
*
* @param string $method The function to be called.
* @param array $parameters The parameters to be passed to the function, as an indexed array.
* @return callable The function to be called.
*
* @throws BadMethodCallException When method doesn't exist.
*/
public function __call( $method, $parameters ) {
if ( ! static::has_macro( $method ) ) {
throw new BadMethodCallException( esc_html( "Method {$method} does not exist." ) );
}
$macro = static::$macros[ $method ];
if ( $macro instanceof Closure ) {
return call_user_func_array( $macro->bindTo( $this, static::class ), $parameters );
}
return call_user_func_array( $macro, $parameters );
}
}
@@ -0,0 +1,43 @@
<?php
namespace Vrts\Core\Traits;
trait Singleton {
/**
* The reference to Singleton instance of this class.
*
* @var Singleton
*/
protected static $instance;
/**
* Returns the Singleton instance of this class.
*
* @return Singleton The Singleton instance.
*/
public static function get_instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Protected constructor to prevent creating a new instance of the
* Singleton via the `new` operator from outside of this class.
*/
protected function __construct() {}
/**
* Private clone method to prevent cloning of the instance of the
* Singleton instance.
*/
private function __clone() {}
/**
* Public unserialize method to prevent unserializing of the Singleton
* instance.
*/
public function __wakeup() {}
}