<commit_before><?php

namespace Milax\Mconsole\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Milax\Mconsole\Models\MconsoleModule;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Cache;
use Milax\Mconsole\Contracts\Repository;

class ModulesController extends Controller
{
    use \UseLayout;
    
    public function __construct(Repository $repository)
    {
        $this->setCaption(trans('mconsole::modules.menu.name'));
        $this->repository = $repository;
    }
    
    /**
     * Manage modules
     * @return Response
     */
    public function index()
    {
        $modules = app('API')->modules->get('all');
        $cached = $this->repository->get();
        
        $modules->each(function ($module) use (&$cached) {
            $module->installed = false;
            if ($dbModule = $cached->where('identifier', $module->identifier)->first()) {
                $module->installed = $dbModule->installed;
            } else {
                $this->repository->create([
                    'identifier' => $module->identifier,
                    'installed' => false,
                ]);
            }
        });
        
        return view('mconsole::modules.list', [
            'items' => app('API')->modules->get('all')->sortByDesc('name')->sortByDesc('installed'),
            'suggested' => $this->getSuggested($modules),
        ]);
    }
    
    /**
     * Install module
     * @param  string $identifier [Module identifier]
     * @return Response
     */
    public function install($identifier)
    {
        $module = app('API')->modules->get('all')->where('identifier', $identifier)->first();
        app('API')->modules->install($module);
        app('API')->translations->load();
        
        return ['status' => 'ok'];
    }
    
    /**
     * Uninstall module
     * @param  string $identifier [Module identifier]
     * @return Response
     */
    public function uninstall($identifier)
    {
        $module = app('API')->modules->get('all')->where('identifier', $identifier)->first();
        app('API')->modules->uninstall($module);
        app('API')->translations->load();
        
        return ['status' => 'ok'];
    }
    
    /**
     * Extend module
     * 
     * @param  string $identifier [Module identifier]
     * @return Response
     */
    public function extend($identifier)
    {
        $module = app('API')->modules->get('all')->where('identifier', $identifier)->first();
        app('API')->modules->extend($module);
        
        return ['status' => 'ok'];
    }
    
    /**
     * Get packages list from packagist.org
     * Cache response for 10 minutes
     * 
     * @param Collection $modules [System modules list]
     * @return array
     */
    protected function getSuggested($modules)
    {
        if (!Cache::has('modules.suggested')) {
            $client = new Client();
            $suggested = [];
            $response = $client->request('GET', 'https://packagist.org/search.json?q=mconsole-');
            $results = json_decode($response->getBody()->getContents());
            
            foreach ($results->results as $package) {
                if ($package->name != 'milax/mconsole' && $modules->where('package.name', $package->name)->count() == 0) {
                    array_push($suggested, $package);
                }
            }
            
            Cache::put('modules.suggested', $suggested, \Carbon\Carbon::now()->addMinutes(10));
        }
        
        return Cache::get('modules.suggested');
    }
}
<commit_msg>Avoid slow first-time modules scan<commit_after><?php

namespace Milax\Mconsole\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Milax\Mconsole\Models\MconsoleModule;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Cache;
use Milax\Mconsole\Contracts\Repository;

class ModulesController extends Controller
{
    use \UseLayout;
    
    public function __construct(Repository $repository)
    {
        $this->setCaption(trans('mconsole::modules.menu.name'));
        $this->repository = $repository;
    }
    
    /**
     * Manage modules
     * @return Response
     */
    public function index()
    {
        $modules = app('API')->modules->get('all');
        $cached = $this->repository->get();
        
        $modules->each(function ($module) use (&$cached) {
            $module->installed = false;
            if ($dbModule = $cached->where('identifier', $module->identifier)->first()) {
                $module->installed = $dbModule->installed;
            } else {
                \DB::table('mconsole_modules')->insert([
                    'identifier' => $module->identifier,
                    'installed' => false,
                ]);
            }
        });
        
        return view('mconsole::modules.list', [
            'items' => app('API')->modules->get('all')->sortByDesc('name')->sortByDesc('installed'),
            'suggested' => $this->getSuggested($modules),
        ]);
    }
    
    /**
     * Install module
     * @param  string $identifier [Module identifier]
     * @return Response
     */
    public function install($identifier)
    {
        $module = app('API')->modules->get('all')->where('identifier', $identifier)->first();
        app('API')->modules->install($module);
        app('API')->translations->load();
        
        return ['status' => 'ok'];
    }
    
    /**
     * Uninstall module
     * @param  string $identifier [Module identifier]
     * @return Response
     */
    public function uninstall($identifier)
    {
        $module = app('API')->modules->get('all')->where('identifier', $identifier)->first();
        app('API')->modules->uninstall($module);
        app('API')->translations->load();
        
        return ['status' => 'ok'];
    }
    
    /**
     * Extend module
     * 
     * @param  string $identifier [Module identifier]
     * @return Response
     */
    public function extend($identifier)
    {
        $module = app('API')->modules->get('all')->where('identifier', $identifier)->first();
        app('API')->modules->extend($module);
        
        return ['status' => 'ok'];
    }
    
    /**
     * Get packages list from packagist.org
     * Cache response for 10 minutes
     * 
     * @param Collection $modules [System modules list]
     * @return array
     */
    protected function getSuggested($modules)
    {
        if (!Cache::has('modules.suggested')) {
            $client = new Client();
            $suggested = [];
            $response = $client->request('GET', 'https://packagist.org/search.json?q=mconsole-');
            $results = json_decode($response->getBody()->getContents());
            
            foreach ($results->results as $package) {
                if ($package->name != 'milax/mconsole' && $modules->where('package.name', $package->name)->count() == 0) {
                    array_push($suggested, $package);
                }
            }
            
            Cache::put('modules.suggested', $suggested, \Carbon\Carbon::now()->addMinutes(10));
        }
        
        return Cache::get('modules.suggested');
    }
}

{'fraction_non_alphanumeric': 0.1653771760154739, 'fraction_numerical': 0.0013815971262779773, 'mean_word_length': 2.150130548302872, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 120, 'https://': 2, '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': '5448621', 'n_tokens_mistral': 1989, 'n_tokens_neox': 1940, 'n_words': 350}