Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
webbingbrasil/core-module / Providers / CoreServiceProvider.php
Size: Mime:
<?php

/**
 * Created by PhpStorm.
 * User: danilo
 * Date: 18/12/15
 * Time: 10:58
 */
namespace Modules\Core\Providers;

use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use Maatwebsite\Sidebar\SidebarManager;
use Modules\Core\Commands\IdeHelperCommand;
use Modules\Core\Commands\InstallCommand;
use Modules\Core\Commands\PublishModuleAssetsCommand;
use Modules\Core\Commands\PublishThemeAssetsCommand;
use Modules\Core\Commands\RebuildCaches;
use Modules\Core\Composers\ControlsViewComposer;
use Modules\Core\Services\JWTGuard;
use Modules\Core\Sidebar\AdminSidebar;
use Modules\Core\Sidebar\AdminTopbar;

class CoreServiceProvider extends ServiceProvider
{
    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = false;
    /**
     * @var string
     */
    protected $prefix = 'webbing';

    /**
     * The filters base class name.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'csrf' => \App\Http\Middleware\VerifyCsrfToken::class,
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'admin.guest' => \Modules\Core\Http\Middleware\RedirectIfAuthenticated::class,
        'localize' => \Modules\Core\Http\Middleware\LocalizationRoutes::class,
        'localizationRedirect' => \Modules\Core\Http\Middleware\LocalizationRedirectFilter::class,
        'localeSessionRedirect' => \Modules\Core\Http\Middleware\LocalizationSessionRedirect::class,
        'jwt.auth' => \Tymon\JWTAuth\Middleware\GetUserFromToken::class,
        'jwt.refresh' => \Tymon\JWTAuth\Middleware\RefreshToken::class
    ];

    public function boot(SidebarManager $manager)
    {
        /*$dispatcher->mapUsing(function ($command) {
            $command = str_replace('Commands\\', 'Commands\\Handlers\\', get_class($command));
            return trim($command, '\\') . 'Handler@handle';
        });*/

        if (module_enabled('Users')) {
            $this->routeMiddleware['acl'] = 'Modules\Users\Http\Middleware\HasPermission';
        }

        $manager->register(AdminSidebar::class);
        $manager->register(AdminTopbar::class);

        $this->registerTranslations();
        $this->registerConfig();
        $this->registerViews();
        $this->registerBladeExtensions();

        $this->registerMiddleware($this->app['router']);
        $this->extendAuthGuard();
    }

    /**
     * Register translations.
     *
     * @return void
     */
    public function registerTranslations()
    {
        $langPath = base_path('resources/lang/modules/core');

        if (!is_dir($langPath)) {
            $langPath = __DIR__ . '/../Resources/lang';
        }

        $this->loadTranslationsFrom($langPath, 'core');
        $this->loadTranslationsFrom($langPath, 'upload');
        $this->loadTranslationsFrom($langPath, 'errors');
        $this->loadTranslationsFrom($langPath, 'sidebar');
        $this->loadTranslationsFrom($langPath, 'dates');
    }

    /**
     * Register config.
     *
     * @return void
     */
    protected function registerConfig()
    {
        $this->publishes([
            __DIR__ . '/../Config/config.php' => config_path('core.php'),
        ]);

        $this->mergeConfigFrom(__DIR__ . '/../Config/config.php', 'core');
    }

    /**
     * Register views.
     *
     * @return void
     */
    public function registerViews()
    {
        $viewPath = base_path('resources/views/modules/core');

        $sourcePath = __DIR__ . '/../Resources/views';

        $this->publishes([
            $sourcePath => $viewPath
        ]);

        $this->loadViewsFrom([$viewPath, $sourcePath], 'core');
    }

    /**
     * Register Blade Template Extensions.
     */
    protected function registerBladeExtensions()
    {
        Blade::directive('truncate', function ($expression) {

            list($string, $length) = explode(',', str_replace(['(', ')', ' '], '', $expression));

            return "<?php echo e(strlen({$string}) > {$length} ? substr({$string},0,{$length}).'...' : {$string}); ?>";
        });
    }

    /**
     * Register the filters.
     *
     * @param  Router $router
     * @return void
     */
    public function registerMiddleware(Router $router)
    {
        foreach ($this->routeMiddleware as $name => $class) {
            $router->middleware($name, $class);
        }
    }

    /**
     * Extend Laravel's Auth.
     *
     * @return void
     */
    protected function extendAuthGuard()
    {
        $this->app['auth']->extend('jwt', function ($app, $name, array $config) {
            $guard = new JWTGuard(
                $app['tymon.jwt'],
                $app['auth']->createUserProvider($config['provider']),
                $app['request']
            );
            $app->refresh('request', $guard, 'setRequest');
            return $guard;
        });
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->registerCommands();

        // ApiToken Repository
        $this->app->bind(
            'Modules\Core\Contracts\Repositories\ApiTokenRepository',
            'Modules\Core\Repositories\Eloquent\ApiTokenRepositoryEloquent'
        );

        $this->app->singleton('Modules\Core\Composers\ControlsViewComposer', function () {
            return new ControlsViewComposer();
        });
    }

    /**
     * Register the console commands
     */
    private function registerCommands()
    {
        $this->commands([
            InstallCommand::class,
            PublishThemeAssetsCommand::class,
            PublishModuleAssetsCommand::class,
            RebuildCaches::class,
            IdeHelperCommand::class,
        ]);
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return array();
    }
}