Skip to content

Commit

Permalink
feat(providers): preloading all catpaw providers.
Browse files Browse the repository at this point in the history
This is technically not necessary, but it helps with the user experience because otherwise the programmer would have to include vendor/catpaw/catpaw/src/lib into the launcher libraries.
  • Loading branch information
razshare committed Jun 15, 2024
1 parent a6dd7df commit ccdf3d1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/lib/Core/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ public static function start(
bool $dieOnChange = false,
): void {
try {
Container::requireLibraries($libraries)->unwrap($requireError);
if ($requireError) {
self::kill((string)$requireError);
foreach (explode(',', $libraries) as $library) {
Container::requireLibraries($library)->unwrap($requireError);
if ($requireError) {
self::kill((string)$requireError);
}
}

Container::loadDefaultProviders($name)->unwrap($initializeError);
if ($initializeError) {
self::kill((string)$initializeError);
Expand Down Expand Up @@ -208,10 +211,13 @@ public static function spawn(
EventLoop::onSignal(SIGQUIT, static fn () => self::kill("Killing application..."));
EventLoop::onSignal(SIGTERM, static fn () => self::kill("Killing application..."));

Container::requireLibraries($libraries)->unwrap($requireError);
if ($requireError) {
self::kill((string)$requireError);
foreach (explode(',', $libraries) as $library) {
Container::requireLibraries($library)->unwrap($requireError);
if ($requireError) {
self::kill((string)$requireError);
}
}

Container::loadDefaultProviders("Watcher")->unwrap($initializeError);
if ($initializeError) {
self::kill((string)$initializeError);
Expand Down
27 changes: 27 additions & 0 deletions src/lib/Core/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,33 @@ public static function requireLibraries(string $librariesPath):Unsafe {
* @return Unsafe<None>
*/
public static function loadDefaultProviders(string $applicationName):Unsafe {
// Making sure providers load correctly.
$providersLoaded = match (false) {
class_exists($className = \CatPaw\Core\Implementations\Environment\SimpleEnvironment::class) => error("Could not load class $className"),
class_exists($className = \CatPaw\Queue\Implementations\Queue\SimpleQueue::class) => error("Could not load class $className"),
class_exists($className = \CatPaw\RaspberryPi\Implementations\Gpio\SimpleGpio::class) => error("Could not load class $className"),
class_exists($className = \CatPaw\Schedule\Implementations\Schedule\SimpleSchedule::class) => error("Could not load class $className"),
class_exists($className = \CatPaw\Store\Implementations\Store\SimpleState::class) => error("Could not load class $className"),
class_exists($className = \CatPaw\Web\Implementations\ByteRange\SimpleByteRange::class) => error("Could not load class $className"),
class_exists($className = \CatPaw\Web\Implementations\FileServer\SimpleFileServer::class) => error("Could not load class $className"),
class_exists($className = \CatPaw\Web\Implementations\FileServer\SpaFileServer::class) => error("Could not load class $className"),
class_exists($className = \CatPaw\Web\Implementations\HttpInvoker\SimpleHttpInvoker::class) => error("Could not load class $className"),
class_exists($className = \CatPaw\Web\Implementations\OpenApi\SimpleOpenApi::class) => error("Could not load class $className"),
class_exists($className = \CatPaw\Web\Implementations\OpenApiState\SimpleOpenApiState::class) => error("Could not load class $className"),
class_exists($className = \CatPaw\Web\Implementations\RequestHandler\SimpleRequestHandler::class) => error("Could not load class $className"),
class_exists($className = \CatPaw\Web\Implementations\RouteResolver\SimpleRouteResolver::class) => error("Could not load class $className"),
class_exists($className = \CatPaw\Web\Implementations\Router\SimpleRouter::class) => error("Could not load class $className"),
class_exists($className = \CatPaw\Web\Implementations\Server\SimpleServer::class) => error("Could not load class $className"),
class_exists($className = \CatPaw\Web\Implementations\ViewEngine\LatteViewEngine::class) => error("Could not load class $className"),
class_exists($className = \CatPaw\Web\Implementations\Websocket\SimpleWebsocket::class) => error("Could not load class $className"),
default => ok()
};

$providersLoaded->unwrap($error);
if ($error) {
return error($error);
}

/** @var LoggerInterface $logger */
$logger = LoggerFactory::create(loggerName: $applicationName)->unwrap($error);
if ($error) {
Expand Down

0 comments on commit ccdf3d1

Please sign in to comment.