<?php
/**
 * Helper Class
 *
 * @todo        Caching von "app.env" abhängig machen und einen Mechanismus für die automatische
 *              Aktualisierung des Cache entwickeln.
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @package     bplan-base/globals
 * @subpackage  Helpers
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 * @copyright   Copyright (C) 2025 bplan-solutions GmbH & Co. KG <https://www.bplan-solutions.de/>
 * /Δ\
 */

 namespace BplanBase\Globals\Support;


use BplanBase\Globals\Registries\MixinRegistry;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\File;
use Livewire\Livewire;


/**
 * Helper Class
 *
 * @version     1.0.0 / 2025-04-22
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 */
class MixinAutoDiscovery
{


/* +++ TRAITS +++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ CLASS CONSTANTS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ OBJECT MEMBERS +++ +++++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ CLASS MEMBERS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ OBJECT METHODS +++ +++++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ CLASS METHODS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++++ */


    /**
     * Discovers all Mixin classes under the given path
     *
     * @param       string $baseNamespace
     *
     * @param       string $basePath
     *
     * @param       null|string $prefix
     *
     * @return      void
     *
     * @version     1.0.0 / 2025-04-22
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public static function discover(string $baseNamespace, string $basePath, null|string $prefix = null): void
    {
        $cacheFile = base_path('bootstrap/cache/livewire-'.$prefix.'-components.php');
        $basePath = str_replace('\\', '/', realpath($basePath));
        $namespace = rtrim($baseNamespace, '\\');

        foreach (File::allFiles($basePath) as $File) {
            $fileName = $File->getFilename();
            /*
            **  Ignore files whose name begins with a dot or a diamond symbol. */
            if ($fileName[0] === '.' || $fileName[0] === '#') {
                continue;
            }
            $mixinClass = $namespace.'\\'.Str::replaceLast('.php', '', $fileName);

            MixinRegistry::register($mixinClass);
        }
    } // discover()


} // class MixinAutoDiscovery {}
