<commit_before><?php

namespace Module;

use Config\Repository as Config;
use Foundation\Application;
use Support\Collection;
use Support\Str;


class ModuleManager
{
    /**
     * @var \Foundation\Application
     */
    protected $app;

    /**
     * @var \Config\Repository
     */
    protected $config;


    /**
     * Create a new ModuleRepository instance.
     *
     * @param Application         $app
     * @param RepositoryInterface $repository
     */
    public function __construct(Application $app, Config $config)
    {
        $this->app = $app;

        $this->config = $config;
    }

    /**
     * Register the module service provider file from all modules.
     *
     * @return mixed
     */
    public function register()
    {
        $modules = $this->getModules();

        $modules->each(function($config)
        {
            if ($config['enabled'] == false) continue;

            //
            $this->registerServiceProvider($config);

            $this->autoloadFiles($config);
        });
    }

    /**
     * Register the Module Service Provider.
     *
     * @param string $config
     *
     * @return string
     */
    protected function registerServiceProvider($config)
    {
        $name = $config['name'];

        // Calculate the name of Service Provider, including the namespace.
        $serviceProvider = $this->getNamespace() ."\\{$name}\\Providers\\{$name}ServiceProvider";

        if (class_exists($serviceProvider)) {
            $this->app->register($serviceProvider);
        }
    }

    /**
     * Autoload custom module files.
     *
     * @param array $config
     */
    protected function autoloadFiles($config)
    {
        $autoload = array('config', 'events', 'filters', 'routes', 'bootstrap');

        // Calculate the names of the files to be autoloaded.
        if (isset($config['autoload']) && is_array($config['autoload'])) {
            $autoload = array_values(array_intersect($config['autoload'], $autoload));
        }

        // Calculate the Modules path.
        $module = $config['name'];

        $basePath = $this->getPath() .DS .$module .DS;

        foreach ($autoload as $name) {
            $path = $basePath .ucfirst($name) .'.php';

            if (is_readable($path)) require $path;
        }
    }

    public function getPath()
    {
        return $this->config->get('modules.path');
    }

    public function getNamespace()
    {
        return $this->config->get('modules.namespace');
    }

    public function getModules()
    {
        $modules = $this->config->get('modules.repository');

        $modules = array_map(function($name, $config)
        {
            return array_merge(array(
                'name'      => $name,
                'slug'      => isset($config['slug'])    ? $config['slug']    : Str::slug($name),
                'enabled'   => isset($config['enabled']) ? $config['enabled'] : true,
                'order'     => isset($config['order'])   ? $config['order']   : 9001,
            ), $config);
        }, array_keys($modules), $modules);

        return Collection::make($modules)->sortBy('order');
    }

}
<commit_msg>Improve Module\ModuleManager<commit_after><?php

namespace Module;

use Config\Repository as Config;
use Foundation\Application;
use Support\Collection;
use Support\Str;


class ModuleManager
{
    /**
     * @var \Foundation\Application
     */
    protected $app;

    /**
     * @var \Config\Repository
     */
    protected $config;


    /**
     * Create a new ModuleRepository instance.
     *
     * @param Application         $app
     * @param RepositoryInterface $repository
     */
    public function __construct(Application $app, Config $config)
    {
        $this->app = $app;

        $this->config = $config;
    }

    /**
     * Register the module service provider file from all modules.
     *
     * @return mixed
     */
    public function register()
    {
        $modules = $this->getModules();

        $modules->each(function($properties)
        {
            $enabled = array_get($properties,'enabled', true);

            if ($enabled) {
                $this->registerServiceProvider($properties);

                $this->autoloadFiles($properties);
            }
        });
    }

    /**
     * Register the Module Service Provider.
     *
     * @param string $config
     *
     * @return string
     */
    protected function registerServiceProvider($properties)
    {
        $name = array_get($properties, 'name');

        // Calculate the name of Service Provider, including the namespace.
        $serviceProvider = $this->getNamespace() ."\\{$name}\\Providers\\{$name}ServiceProvider";

        if (class_exists($serviceProvider)) {
            $this->app->register($serviceProvider);
        }
    }

    /**
     * Autoload custom module files.
     *
     * @param array $config
     */
    protected function autoloadFiles($properties)
    {
        $names = array('config', 'events', 'filters', 'routes');

        // Calculate the names of the files to be autoloaded.
        $autoload = array_get($properties, 'autoload');

        if (is_array($autoload) && ! empty($autoload)) {
            $names = array_values(array_intersect($names, $autoload));
        }

        array_push($names, 'bootstrap');

        // Calculate the Modules path.
        $module = array_get($properties, 'name');

        $basePath = $this->getPath() .DS .$module .DS;

        foreach ($names as $name) {
            $path = $basePath .ucfirst($name) .'.php';

            if (is_readable($path)) require $path;
        }
    }

    public function getPath()
    {
        return $this->config->get('modules.path');
    }

    public function getNamespace()
    {
        return $this->config->get('modules.namespace');
    }

    public function getModules()
    {
        $modules = $this->config->get('modules.repository');

        $modules = array_map(function($name, $config)
        {
            return array_merge(array(
                'name'      => $name,
                'slug'      => isset($config['slug'])    ? $config['slug']    : Str::slug($name),
                'enabled'   => isset($config['enabled']) ? $config['enabled'] : true,
                'order'     => isset($config['order'])   ? $config['order']   : 9001,
            ), $config);
        }, array_keys($modules), $modules);

        return Collection::make($modules)->sortBy('order');
    }

}

{'fraction_non_alphanumeric': 0.1471861471861472, 'fraction_numerical': 0.0012368583797155227, 'mean_word_length': 2.0557392536608408, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5301924', 'n_tokens_mistral': 1831, 'n_tokens_neox': 1775, 'n_words': 388}