Skip to content

Commit

Permalink
Bug fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
izniburak committed May 6, 2021
1 parent 45bff94 commit 9666905
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
40 changes: 20 additions & 20 deletions src/AutoRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Class AutoRoute
*
* @package Buki\AutoRoute
* @author İzni Burak Demirtaş <info@burakdemirtasorg>
* @author İzni Burak Demirtaş <info@burakdemirtasorg>
*/
class AutoRoute
{
Expand Down Expand Up @@ -64,7 +64,22 @@ public function __construct(Container $app)
{
$this->app = $app;
$this->router = $app->get('router');
$this->init($this->app['config']->get('auto-route'));
}

/**
* Initialize for the AutoRoute
*
* @param array $config
*/
public function setConfigurations(array $config): void
{
$this->mainMethod = $config['main_method'] ?? 'index';
$this->namespace = $config['namespace'] ?? 'App\\Http\\Controllers';
$this->defaultPatterns = array_merge($this->defaultPatterns, $config['patterns'] ?? []);
$this->defaultHttpMethods = $config['http_methods'] ?? $this->availableMethods;
if (empty($this->defaultHttpMethods) || $this->defaultHttpMethods === '*') {
$this->defaultHttpMethods = $this->availableMethods;
}
}

/**
Expand Down Expand Up @@ -110,7 +125,9 @@ public function auto(string $prefix, string $controller, array $options = []): v
function () use ($endpoints, $methodName, $method, $httpMethods, $routePatterns) {
$endpoints = implode('/', $endpoints);
$this->router->addRoute(
array_map(function($method) { return strtoupper($method); }, $httpMethods),
array_map(function ($method) {
return strtoupper($method);
}, $httpMethods),
($methodName !== $this->mainMethod ? $methodName : '') . "/{$endpoints}",
[$method->class, $method->name]
)->where($routePatterns)->name(".{$methodName}");
Expand Down Expand Up @@ -180,21 +197,4 @@ private function resolveControllerName(string $controller): array
$controller,
];
}

/**
* Initialize for the AutoRoute
*
* @param array $config
*/
private function init(array $config): void
{
$this->mainMethod = $config['main_method'] ?? 'index';
$this->namespace = $config['namespace'] ?? 'App\\Http\\Controllers';
$this->defaultPatterns = array_merge($this->defaultPatterns, $config['patterns'] ?? []);
$this->defaultHttpMethods = $config['http_methods'] ?? $this->availableMethods;
if (empty($this->defaultHttpMethods) || $this->defaultHttpMethods === '*') {
$this->defaultHttpMethods = $this->availableMethods;
}
}
}

10 changes: 6 additions & 4 deletions src/AutoRouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Class AutoRouteServiceProvider
*
* @package Buki\AutoRoute
* @author İzni Burak Demirtaş <info@burakdemirtasorg>
* @author İzni Burak Demirtaş <info@burakdemirtasorg>
*/
class AutoRouteServiceProvider extends ServiceProvider
{
Expand All @@ -21,7 +21,7 @@ class AutoRouteServiceProvider extends ServiceProvider
public function boot()
{
$this->publishes([
$this->configPath() => config_path('auto-route.php')
$this->configPath() => config_path('auto-route.php'),
], 'auto-route');
}

Expand All @@ -32,9 +32,11 @@ public function boot()
*/
public function register()
{
$this->mergeConfigFrom($this->configPath(), 'auto-router');
$this->mergeConfigFrom($this->configPath(), 'auto-route');
$this->app->singleton(AutoRoute::class, function ($app) {
return new AutoRoute($app);
$autoRouter = new AutoRoute($app);
$autoRouter->setConfigurations($app['config']->get('auto-route'));
return $autoRouter;
});

/** @var Router $router */
Expand Down

0 comments on commit 9666905

Please sign in to comment.