Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
2 / 3
CRAP
83.93% covered (success)
83.93%
47 / 56
AppServiceProvider
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
2 / 3
19.34
83.93% covered (success)
83.93%
47 / 56
 getMeta
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 register
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 boot
0.00% covered (danger)
0.00%
0 / 1
17.19
83.33% covered (success)
83.33%
45 / 54
<?php
namespace App\Providers;
use App\Module;
use App\Setting;
use App\Language;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
    protected static $meta = null;
    /**
     * Register any application services.
     *
     * @return void
     */
    /**
     * @return null
     */
    public static function getMeta()
    {
        return self::$meta;
    }
    public function register()
    {
    }
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // Fix migrate issue associated with index key length
        Schema::defaultStringLength(191);
        // CMS Supported Languages
        $languages= Language::where('status', 1)->cursor();
        View::share('languages', $languages);
        View::composer('*', function($view) {
            $routePath= $this->app->request->getRequestUri();
            $routePath= explode('/', trim($routePath, '/'));
            $activeMenu= [];
            if (count($routePath)) {
                if ('admin' == $routePath[0]  ) {
                    // dd($routePath);
                    $activeMenu[$routePath[1] ?? ""]= 'active';
                    $view->with('activeMenu', $activeMenu);
                }elseif ( isset($routePath[1]) && 'admin' == $routePath[1] ) {
                    $activeMenu[$routePath[2] ?? ""]= 'active';
                    $view->with('activeMenu', $activeMenu);
                }
            }
            // Current User Info
            $userInfo= (object) ['photo'=> 'uploads/avatar.png'];
            if(Auth::check()){
                $userInfo= Auth::user();
                if (!is_file(public_path($userInfo->photo))) {
                    $userInfo->photo= 'uploads/avatar.png';
                }
                // Get Modules Current User can access
                if (in_array($userInfo->role, ['super_admin', 'admin']) || $userInfo->admin_token) {
                    $userInfo->modules = Module::where('deleted_at', null)->cursor();
                }
                if ($userInfo->role == 'sub_admin') {
                    $userInfo->modules = Module::where('deleted_at', null)
                        ->join('user_modules as um', 'um.module_id', 'modules.id')
                        ->where('um.user_id', $userInfo->id)
                        ->select(['modules.*', 'um.*'])->cursor();
                }
            }
            $view->with('currentUserInfo', $userInfo);
            // Share meta for modules instead of language files
            if (Schema::hasTable('modules')){
                if (preg_match('/admin/', URL::current())){
                    $path = explode('.', Route::currentRouteName());
                    if (preg_match('/_/', $path[0])){
                        $path = explode('_', $path[0]);
                        $path = $path[0].ucfirst($path[0]);
                    } else {
                        $path = $path[0];
                    }
                    $module = Module::where('path', $path)->pluck('meta')->toArray();
                    $meta = array_first($module);
                    static::$meta = $meta;
                    $view->with('meta', $meta);
                }
            }
        });
        // Values Got from settings
        if (Schema::hasTable('settings')){
            $settings = Setting::cursor();
            if(count($settings) > 0){
                foreach ($settings as $setting) {
                    $settings->{$setting['key']} = $setting['value'];
                }
                Config::set('app.name', $settings->website_name);
                Config::set('mail.name', $settings->website_name);
                Config::set('mail.driver',$settings->mail_driver);
                Config::set('mail.host',$settings->mail_host);
                Config::set('mail.port',$settings->mail_port);
                Config::set('mail.username',$settings->mail_username);
                Config::set('mail.password',$settings->mail_password);
                Config::set('mail.from', [
                    'address' => $settings->mail_from_address,
                    'name' => $settings->mail_from_name,
                ]);
            }
        }
    }
}