<?php

namespace Dipper\Foundation\Core;

use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider;

abstract class RouteServiceProvider extends ServiceProvider
{
    /**
     * Read the routes from the "api.php" and "web.php" files of this Service
     */
    abstract public function boot();

    /**
     * @param $router
     * @param $namespace
     * @param $pathApi
     * @param $pathWeb
     */
    public function loadRoutesFiles($router, $namespace, $pathApi = null, $pathWeb = null)
    {
        if (is_file($pathApi)) {
            $this->mapApiRoutes($router, $namespace, $pathApi);
        }
        if (is_file($pathWeb)) {
            $this->mapWebRoutes($router, $namespace, $pathWeb);
        }
    }

    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @param $router
     * @param $namespace
     * @param $path
     *
     * @return void
     */
    protected function mapApiRoutes($router, $namespace, $path)
    {
        $router->group([
            'namespace' => $namespace,
            'prefix' => config('dipper.api.prefix'),
            'as' => 'api',
        ], function ($router) use ($path) {
            require $path;
        });
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @param $router
     * @param $namespace
     * @param $path
     *
     * @return void
     */
    protected function mapWebRoutes($router, $namespace, $path)
    {
        $router->group([
            'namespace' => $namespace,
            'as' => 'web',
        ], function ($router) use ($path) {
            require $path;
        });
    }
}
