<?php
/**
 * BlockManager plugin loader.
 *
 * Picked up by core/includes/twig.php's plugins/*\/loader.php glob and
 * included by core/includes/header.inc.php after Laravel is booted. Defines
 * a process-wide constant so the discovery is observable across container
 * resets (e.g. PHPUnit's per-test Tests\TestCase::createApplication()).
 *
 * This file is also the right place to add the element-catalogue directory
 * to Twig's loader — it runs AFTER core/includes/twig.php has bound the
 * `twig` instance, so `app('twig')` is reliably resolvable here. The
 * service provider's boot() runs too early (before twig.php executes) so
 * doing it there would silently no-op on real web requests.
 */

if (!defined('BLOCK_MANAGER_LOADED')) {
    define('BLOCK_MANAGER_LOADED', true);
}

if (function_exists('app')) {
    app()->instance('block_manager.loaded', true);

    // The element catalogue lives at plugins/BlockManager/elements/ — outside
    // the conventional plugins/*/templates/ glob that core/includes/twig.php
    // scans. Register it as a Twig loader root so shipped blocks can include
    // variant partials by their short path (e.g. "button/variants/primary.twig").
    if (app()->bound('twig')) {
        $loader = app('twig')->getLoader();
        if ($loader instanceof \Twig\Loader\FilesystemLoader) {
            $catalogue = __DIR__ . '/elements';
            if (!in_array($catalogue, $loader->getPaths(), true)) {
                $loader->prependPath($catalogue);
            }
        }
    }
}
