| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the WindPress package. |
| 5 |
* |
| 6 |
* (c) Joshua Gugun Siagian <suabahasa@gmail.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
declare (strict_types=1); |
| 12 |
namespace WindPress\WindPress\Integration; |
| 13 |
|
| 14 |
use Exception; |
| 15 |
use ReflectionClass; |
| 16 |
use WindPressDeps\Symfony\Component\Finder\Finder; |
| 17 |
use WIND_PRESS; |
| 18 |
class Loader |
| 19 |
{ |
| 20 |
/** |
| 21 |
* List of Integrations services. |
| 22 |
* |
| 23 |
* @var IntegrationInterface[] |
| 24 |
*/ |
| 25 |
private array $integrations = []; |
| 26 |
/** |
| 27 |
* Stores the instance, implementing a Singleton pattern. |
| 28 |
*/ |
| 29 |
private static self $instance; |
| 30 |
/** |
| 31 |
* The Singleton's constructor should always be private to prevent direct |
| 32 |
* construction calls with the `new` operator. |
| 33 |
*/ |
| 34 |
private function __construct() |
| 35 |
{ |
| 36 |
} |
| 37 |
/** |
| 38 |
* Singletons should not be cloneable. |
| 39 |
*/ |
| 40 |
private function __clone() |
| 41 |
{ |
| 42 |
} |
| 43 |
/** |
| 44 |
* Singletons should not be restorable from strings. |
| 45 |
* |
| 46 |
* @throws Exception Cannot unserialize a singleton. |
| 47 |
*/ |
| 48 |
public function __wakeup() |
| 49 |
{ |
| 50 |
throw new Exception('Cannot unserialize a singleton.'); |
| 51 |
} |
| 52 |
/** |
| 53 |
* This is the static method that controls the access to the singleton |
| 54 |
* instance. On the first run, it creates a singleton object and places it |
| 55 |
* into the static property. On subsequent runs, it returns the client existing |
| 56 |
* object stored in the static property. |
| 57 |
*/ |
| 58 |
public static function get_instance(): self |
| 59 |
{ |
| 60 |
if (!isset(self::$instance)) { |
| 61 |
self::$instance = new self(); |
| 62 |
self::$instance->scan_integrations(); |
| 63 |
} |
| 64 |
return self::$instance; |
| 65 |
} |
| 66 |
public function scan_integrations() |
| 67 |
{ |
| 68 |
// Get cached Integrations |
| 69 |
$transient_name = 'windpress_scanned_integrations_' . WIND_PRESS::VERSION; |
| 70 |
/** @var IntegrationInterface[]|false $cached */ |
| 71 |
$cached = get_transient($transient_name); |
| 72 |
if (!\WP_DEBUG && $cached !== \false) { |
| 73 |
$this->integrations = $cached; |
| 74 |
return; |
| 75 |
} |
| 76 |
$finder = new Finder(); |
| 77 |
$finder->files()->in(__DIR__)->name('*.php'); |
| 78 |
/** |
| 79 |
* Add additional places to scan for Integrations. |
| 80 |
* |
| 81 |
* @param Finder $finder The Finder instance. |
| 82 |
*/ |
| 83 |
do_action('a!windpress/integration/loader:scan_integrations.before_scan', $finder); |
| 84 |
foreach ($finder as $file) { |
| 85 |
$integration_file = $file->getPathname(); |
| 86 |
if (!is_readable($integration_file)) { |
| 87 |
continue; |
| 88 |
} |
| 89 |
require_once $integration_file; |
| 90 |
} |
| 91 |
// Find any Integrations that extends IntegrationInterface class |
| 92 |
$declared_classes = get_declared_classes(); |
| 93 |
foreach ($declared_classes as $declared_class) { |
| 94 |
if (!class_exists($declared_class)) { |
| 95 |
continue; |
| 96 |
} |
| 97 |
$reflector = new ReflectionClass($declared_class); |
| 98 |
if (!$reflector->isSubclassOf(\WindPress\WindPress\Integration\IntegrationInterface::class)) { |
| 99 |
continue; |
| 100 |
} |
| 101 |
// Get Integration detail and push to Router::$integrations to be register later |
| 102 |
/** @var IntegrationInterface $integration */ |
| 103 |
$integration = $reflector->newInstanceWithoutConstructor(); |
| 104 |
$this->integrations[$integration->get_name()] = ['name' => $integration->get_name(), 'file_path' => $reflector->getFileName(), 'class_name' => $reflector->getName()]; |
| 105 |
} |
| 106 |
// Cache the scanned Integrations |
| 107 |
set_transient($transient_name, $this->integrations, \HOUR_IN_SECONDS); |
| 108 |
} |
| 109 |
/** |
| 110 |
* Register Integrations. |
| 111 |
*/ |
| 112 |
public function register_integrations(): void |
| 113 |
{ |
| 114 |
/** |
| 115 |
* Filter the Integrations before register to WP Rest. |
| 116 |
* |
| 117 |
* @param IntegrationInterface[] $integrations |
| 118 |
* @return IntegrationInterface[] |
| 119 |
*/ |
| 120 |
/** @var IntegrationInterface[] $integrations */ |
| 121 |
$integrations = apply_filters('f!windpress/integration/loader:register_integrations', $this->integrations); |
| 122 |
foreach ($integrations as $integration) { |
| 123 |
// Create new instance of Integration class and register custom endpoints |
| 124 |
/** @var IntegrationInterface $integrationInstance */ |
| 125 |
$integrationInstance = new $integration['class_name'](); |
| 126 |
$this->integrations[$integration['name']]['instance'] = $integrationInstance; |
| 127 |
} |
| 128 |
} |
| 129 |
/** |
| 130 |
* Get the list of Integrations. |
| 131 |
* |
| 132 |
* @return IntegrationInterface[] |
| 133 |
*/ |
| 134 |
public function get_integrations(): array |
| 135 |
{ |
| 136 |
return $this->integrations; |
| 137 |
} |
| 138 |
} |
| 139 |
|