🏡 index : ~doyle/koselig.git

author Jordan Doyle <jordan@doyle.wf> 2016-10-03 20:13:49.0 +00:00:00
committer Jordan Doyle <jordan@doyle.wf> 2016-10-03 20:13:49.0 +00:00:00
commit
122f539aa4f9ed8e49c1e646ab1027aa5ef36a0f [patch]
tree
bd831910c39f87a91f72c37631ffa067baa9eb02
parent
75e010903e07f8c2509db90e061197e57584d272
download
122f539aa4f9ed8e49c1e646ab1027aa5ef36a0f.tar.gz

Clean up routing service provider



Diff

 README.md                                |   2 +-
 src/Providers/RoutingServiceProvider.php |  88 +---------------------
 src/Routing/Routing.php                  | 130 ++++++++++++++++++++++++++++++++-
 3 files changed, 138 insertions(+), 82 deletions(-)

diff --git a/README.md b/README.md
index 54f07a3..9b0f641 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,7 @@
# Koselig

[![StyleCI]https://styleci.io/repos/69823688/shield?style=flat]https://styleci.io/repos/69823688

Seamlessly integrate Wordpress with Laravel.

>If you're looking to install Koselig, this is probably not the repository you are looking for. You should go to [koeslig/koeslig]https://github.com/koeslig/koeslig instead.
diff --git a/src/Providers/RoutingServiceProvider.php b/src/Providers/RoutingServiceProvider.php
index 7365196..60a5f4f 100644
--- a/src/Providers/RoutingServiceProvider.php
+++ b/src/Providers/RoutingServiceProvider.php
@@ -7,6 +7,7 @@ use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Koselig\Routing\ArchiveRoute;
use Koselig\Routing\PageRoute;
use Koselig\Routing\Routing;
use Koselig\Routing\SingularRoute;
use Koselig\Routing\TemplateRoute;

@@ -24,89 +25,12 @@ class RoutingServiceProvider extends ServiceProvider
     */
    public function register()
    {
        $app = $this->app;
        $routing = new Routing;

        // Router methods
        Router::macro('template', function ($slug, $action) use ($app) {
            $action = $this->formatAction($action);

            $route = (new TemplateRoute($action['method'], $slug, $action))
                ->setRouter($app->make('router'))
                ->setContainer($app->make(Container::class));

            return Route::getRoutes()->add($route);
        });

        Router::macro('page', function ($slug, $action) use ($app) {
            $action = $this->formatAction($action);

            $route = (new PageRoute($action['method'], $slug, $action))
                ->setRouter($app->make('router'))
                ->setContainer($app->make(Container::class));

            return Route::getRoutes()->add($route);
        });

        Router::macro('archive', function ($postTypes = [], $action = []) use ($app) {
            if (empty($action)) {
                $action = $postTypes;
                $postTypes = [];
            }

            if (!is_array($postTypes)) {
                $postTypes = [$postTypes];
            }

            $action = $this->formatAction($action);

            $route = (new ArchiveRoute($action['method'], $postTypes, $action))
                ->setRouter($app->make('router'))
                ->setContainer($app->make(Container::class));

            return Route::getRoutes()->add($route);
        });

        Router::macro('singular', function ($types, $action) use ($app) {
            if (!is_array($types)) {
                $types = [$types];
            }

            $action = $this->formatAction($action);

            $route = (new SingularRoute($action['method'], $types, $action))
                ->setRouter($app->make('router'))
                ->setContainer($app->make(Container::class));

            return Route::getRoutes()->add($route);
        });

        // Router helpers
        Router::macro('formatAction', function ($action) {
            if (!($action instanceof $action) && (is_string($action) || (isset($action['uses'])
                    && is_string($action['uses'])))) {
                if (is_string($action)) {
                    $action = ['uses' => $action];
                }

                if (!empty($this->groupStack)) {
                    $group = end($this->groupStack);

                    $action['uses'] = isset($group['namespace']) && strpos($action['uses'], '\\') !== 0 ?
                        $group['namespace'] . '\\' . $action['uses'] : $action['uses'];
                }

                $action['controller'] = $action['uses'];
            }

            if (!is_array($action)) {
                $action = ['uses' => $action];
            }

            if (!isset($action['method'])) {
                $action['method'] = ['GET'];
            }

            return $action;
        });
        Router::macro('template', [$routing, 'template']);
        Router::macro('page', [$routing, 'page']);
        Router::macro('archive', [$routing, 'archive']);
        Router::macro('singular', [$routing, 'singular']);
    }
}
diff --git a/src/Routing/Routing.php b/src/Routing/Routing.php
new file mode 100644
index 0000000..3d218e1
--- /dev/null
+++ b/src/Routing/Routing.php
@@ -0,0 +1,130 @@
<?php
namespace Koselig\Routing;

use Illuminate\Container\Container;
use Illuminate\Support\Facades\Route;

class Routing
{
    /**
     * Register a new template route with the router.
     *
     * @param  string $slug slug to match
     * @param  \Closure|array|string|null $action
     * @return \Illuminate\Routing\Route
     */
    public function template($slug, $action)
    {
        $action = $this->formatAction($action);

        $route = (new TemplateRoute($action['method'], $slug, $action))
            ->setRouter(app('router'))
            ->setContainer(app(Container::class));

        return Route::getRoutes()->add($route);
    }

    /**
     * Register a new page route with the router.
     *
     * @param  string $slug slug to match
     * @param  \Closure|array|string|null $action
     * @return \Illuminate\Routing\Route
     */
    public function page($slug, $action)
    {
        $action = $this->formatAction($action);

        $route = (new PageRoute($action['method'], $slug, $action))
            ->setRouter(app('router'))
            ->setContainer(app(Container::class));

        return Route::getRoutes()->add($route);
    }

    /**
     * Register a new archive route with the router. Optionally supply
     * the post types you'd like to supply with this route.
     *
     * @param \Closure|string|array $postTypes
     * @param  \Closure|array|string|null $action
     * @return \Illuminate\Routing\Route
     */
    public function archive($postTypes = [], $action = [])
    {
        if (empty($action)) {
            $action = $postTypes;
            $postTypes = [];
        }

        if (!is_array($postTypes)) {
            $postTypes = [$postTypes];
        }

        $action = $this->formatAction($action);

        $route = (new ArchiveRoute($action['method'], $postTypes, $action))
            ->setRouter(app('router'))
            ->setContainer(app(Container::class));

        return Route::getRoutes()->add($route);
    }

    /**
     * Register a singular route with the router. This allows the user to
     * create pages for a single post type, ie. a news article.
     *
     * @param array|string $types post types to supply with this route
     * @param callable|string $action
     * @return mixed
     */
    public function singular($types, $action)
    {
        if (!is_array($types)) {
            $types = [$types];
        }

        $action = $this->formatAction($action);

        $route = (new SingularRoute($action['method'], $types, $action))
            ->setRouter(app('router'))
            ->setContainer(app(Container::class));

        return Route::getRoutes()->add($route);
    }

    /**
     * Format <pre>$action</pre> in a nice way to pass to the {@link \Illuminate\Routing\RouteCollection}.
     *
     * @param $action
     * @return array|string
     */
    private function formatAction($action)
    {
        if (!($action instanceof $action) && (is_string($action) || (isset($action['uses'])
                    && is_string($action['uses'])))) {
            if (is_string($action)) {
                $action = ['uses' => $action];
            }

            if (!empty(Route::getGroupStack())) {
                $group = end(Route::getGroupStack());

                $action['uses'] = isset($group['namespace']) && strpos($action['uses'], '\\') !== 0 ?
                    $group['namespace'] . '\\' . $action['uses'] : $action['uses'];
            }

            $action['controller'] = $action['uses'];
        }

        if (!is_array($action)) {
            $action = ['uses' => $action];
        }

        if (!isset($action['method'])) {
            $action['method'] = ['GET'];
        }

        return $action;
    }
}