Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
"laikait/laika-queue": "^1.0",
"laikait/laika-relay": "^1.2",
"laikait/laika-route": "^1.1",
"laikait/laika-session": "^4.0",
"laikait/laika-shield": "^1.2",
"laikait/laika-session": "^5.0",
"laikait/laika-shield": "^2.0",
"twig/twig": "^3.28",
"firebase/php-jwt": "^7.1",
"aws/aws-sdk-php": "^3.275",
Expand Down
43 changes: 25 additions & 18 deletions helpers/loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Laika\Relay\RelayRegistry;
use Laika\Relay\CoreProviders;
use Laika\Relay\ProviderRegistry;
use Laika\Relay\RelayProvider;
use Laika\Core\App\Resource;

// Define Constants
defined('APP_PATH') || define('APP_PATH', realpath(__DIR__ . '/../../../../'));
Expand All @@ -31,25 +33,29 @@
// Register Core Services
$providers->register(CoreProviders::class);

$json_file = APP_PATH.DS.'vendor'.DS.'composer'.DS.'installed.json';
// Auto Discover Relay Providers
//
// Packages and the application both declare a `relays` resource - a directory of
// RelayProvider classes. Packages are registered first so an application provider
// can still override a package binding: RelayRegistry::singleton() is
// last-write-wins, and Resource seeds framework defaults before packages.
$packageRelays = [];
$appRelays = [];

if (is_file($json_file)) {
$installed = json_decode(file_get_contents($json_file), true);

foreach ($installed['packages'] ?? $installed as $package) {
// Load Relay Classes
$services = (array) ($package['extra']['laika']['relays'] ?? []);
foreach ($services as $service) $providers->register($service);
foreach (Resource::definitions('relays') as $definition) {
if (in_array($definition->source, ['default', 'app'], true)) {
$appRelays[] = $definition;
} else {
$packageRelays[] = $definition;
}
}

// Auto Discover App Providers
$appProviderDir = APP_PATH . '/lf-app/Service';
if ($appProviderDir && is_dir($appProviderDir)) {
$appProviderFiles = glob("{$appProviderDir}/*.php");
foreach($appProviderFiles as $file) {
$className = 'App\\Relay\\' . basename($file, '.php');
if (class_exists($className)) {
foreach (array_merge($packageRelays, $appRelays) as $definition) {
foreach (Resource::entries($definition) as $className) {
// Tolerant on purpose: the shipped lf-app/Relay/Example.php stub is fully
// commented out, and this runs before Handler::register() below - a throw
// here would be an uncatchable fatal with no error page.
if (class_exists($className) && is_subclass_of($className, RelayProvider::class)) {
$providers->register($className);
}
}
Expand All @@ -61,6 +67,7 @@
// Boot Providers
$providers->boot();

// Resources are not registered here. Every package — this one included — declares
// them in its composer.json under extra.laika.resources, and they are discovered
// from vendor/composer/installed.json on first use, exactly like extra.laika.relays.
// Relay providers are the only resource read during autoload. Every other resource
// stays lazy: packages - this one included - declare them in composer.json under
// extra.laika.resources, and Resource discovers them from
// vendor/composer/installed.json on first use.
8 changes: 4 additions & 4 deletions src/App/Infra.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
namespace Laika\Core\App;

use Laika\Relay\Relay;
use Laika\Service\Directory;
use Laika\Queue\Abstracts\Job;
use Laika\Core\Helper\Directory;
use Laika\Model\Contract\SchemaAbstract;
use Laika\Core\Exceptions\SchemaException;
use Laika\Core\Exceptions\ResourceException;
use Laika\Route\Interfaces\FilterInterface;
use Laika\Route\Interfaces\PipelineInterface;
use Laika\Route\Contracts\FilterInterface;
use Laika\Route\Contracts\PipelineInterface;

// Application Infrastructure Info
class Infra
Expand Down Expand Up @@ -105,7 +105,7 @@ public function get(string $name, ?string $contract = null): array
public function getTemplateNames(): array
{
$base = realpath(APP_PATH . '/template');
$paths = Directory::scan($base, false, ['html','twig']);
$paths = (new Directory())->scan($base, false, ['html','twig']);
$list = [];
foreach ($paths as $path) {
$name = trim(str_replace($base, '', $path), DS);
Expand Down
13 changes: 10 additions & 3 deletions src/App/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
namespace Laika\Core\App;

use Laika\Queue\Abstracts\Job;
use Laika\Cli\Contracts\CommandInterface;
use Laika\Relay\RelayProvider;
use Laika\Core\Helper\Directory;
use Laika\Model\Contract\SchemaAbstract;
use Laika\Cli\Contracts\CommandInterface;
use Laika\Route\Contracts\FilterInterface;
use Laika\Core\Exceptions\ResourceException;
use Laika\Route\Interfaces\FilterInterface;
use Laika\Route\Interfaces\PipelineInterface;
use Laika\Route\Contracts\PipelineInterface;

/**
* Resource Registry.
Expand Down Expand Up @@ -612,6 +613,12 @@ private static function defaults(): array
'namespace' => 'App\\Command',
'contract' => CommandInterface::class
],
// Relay providers, not the bound accessors Infra::getRelayClasses() reports
'relays' => [
'path' => 'lf-app/Relay',
'namespace' => 'App\\Relay',
'contract' => RelayProvider::class
],
'routes' => ['path' => 'lf-routes'],
'hooks' => ['path' => 'lf-hooks']
];
Expand Down