diff --git a/app/Helpers/activeHelpers.php b/app/Helpers/activeHelpers.php index a55e48a1..1da8e615 100644 --- a/app/Helpers/activeHelpers.php +++ b/app/Helpers/activeHelpers.php @@ -1,5 +1,4 @@ getClassIf($condition, $activeClass, $inactiveClass); } } - if (!function_exists('if_uri')) { /** * Check if the URI of the current request matches one of the specific URIs. @@ -29,10 +27,9 @@ function if_uri($uris) return app('active')->checkUri($uris); } } - if (!function_exists('if_uri_pattern')) { /** - * Check if the current URI matches one of specific patterns (using `Str::is`). + * Check if the current URI matches one of specific patterns (using `str_is`). * * @param array|string $patterns * @@ -43,7 +40,6 @@ function if_uri_pattern($patterns) return app('active')->checkUriPattern($patterns); } } - if (!function_exists('if_query')) { /** * Check if one of the following condition is true: @@ -62,7 +58,6 @@ function if_query($key, $value) return app('active')->checkQuery($key, $value); } } - if (!function_exists('if_route')) { /** * Check if the name of the current route matches one of specific values. @@ -76,7 +71,6 @@ function if_route($routeNames) return app('active')->checkRoute($routeNames); } } - if (!function_exists('if_route_pattern')) { /** * Check the current route name with one or some patterns. @@ -90,7 +84,6 @@ function if_route_pattern($patterns) return app('active')->checkRoutePattern($patterns); } } - if (!function_exists('if_route_param')) { /** * Check if the parameter of the current route has the correct value. @@ -105,7 +98,6 @@ function if_route_param($param, $value) return app('active')->checkRouteParam($param, $value); } } - if (!function_exists('if_action')) { /** * Return 'active' class if current route action match one of provided action names. @@ -119,7 +111,6 @@ function if_action($actions) return app('active')->checkAction($actions); } } - if (!function_exists('if_controller')) { /** * Check if the current controller class matches one of specific values. @@ -133,10 +124,9 @@ function if_controller($controllers) return app('active')->checkController($controllers); } } - if (!function_exists('current_controller')) { /** - * Get the current controller class. + * Get the current controller class * * @return string */ @@ -145,10 +135,9 @@ function current_controller() return app('active')->getController(); } } - if (!function_exists('current_method')) { /** - * Get the current controller method. + * Get the current controller method * * @return string */ @@ -157,10 +146,9 @@ function current_method() return app('active')->getMethod(); } } - if (!function_exists('current_action')) { /** - * Get the current action string. + * Get the current action string * * @return string */ @@ -168,4 +156,4 @@ function current_action() { return app('active')->getAction(); } -} +} \ No newline at end of file diff --git a/app/Providers/ActiveServiceProvider.php b/app/Providers/ActiveServiceProvider.php index 952d0713..327db743 100644 --- a/app/Providers/ActiveServiceProvider.php +++ b/app/Providers/ActiveServiceProvider.php @@ -20,7 +20,7 @@ class ActiveServiceProvider extends ServiceProvider public function boot() { // Update the instances each time a request is resolved and a route is matched - $instance = new Active(request()); + $instance = app('active'); if (version_compare(Application::VERSION, '5.2.0', '>=')) { app('router')->matched( @@ -44,42 +44,13 @@ function ($route, $request) use ($instance) { */ public function register() { - $this->registerActive(); - $this->registerFacade(); - - // $this->app->singleton( - // 'active', - // function ($app) { - - // $instance = new Active($app['router']->getCurrentRequest()); - - // return $instance; - // } - // ); - } - - /** - * Register the application bindings. - * - * @return void - */ - private function registerActive() - { - $this->app->bind('active', function ($app) { - return new Active($app['router']->getCurrentRequest()); - }); - } - - /** - * Register the vault facade without the user having to add it to the app.php file. - * - * @return void - */ - public function registerFacade() - { - $this->app->booting(function () { - $loader = \Illuminate\Foundation\AliasLoader::getInstance(); - $loader->alias('Active', \App\Services\Access\Facades\Active::class); - }); + $this->app->singleton( + 'active', + function ($app) { + $instance = new Active($app['router']->getCurrentRequest()); + + return $instance; + } + ); } } diff --git a/app/Services/Active/Active.php b/app/Services/Active/Active.php index 3ae3c224..6f0ed08b 100644 --- a/app/Services/Active/Active.php +++ b/app/Services/Active/Active.php @@ -21,6 +21,7 @@ * @author Hieu Le * * @version 3.2.0 + * */ class Active { @@ -30,42 +31,36 @@ class Active * @var Request */ protected $request; - /** * Current matched route. * * @var Route */ protected $route; - /** * Current action string. * * @var string */ protected $action; - /** * Current controller class. * * @var string */ protected $controller; - /** * Current controller method. * * @var string */ protected $method; - /** * Current URI. * * @var string */ protected $uri; - /** * Active constructor. * @@ -75,7 +70,6 @@ public function __construct($request) { $this->updateInstances(null, $request); } - /** * Update the route and request instances. * @@ -88,17 +82,14 @@ public function updateInstances($route, $request) if ($request) { $this->uri = urldecode($request->path()); } - $this->route = $route; if ($route) { $this->action = $route->getActionName(); - $actionSegments = Str::parseCallback($this->action, null); $this->controller = head($actionSegments); $this->method = last($actionSegments); } } - /** * Get the active class if the condition is not falsy. * @@ -112,7 +103,6 @@ public function getClassIf($condition, $activeClass = 'active', $inactiveClass = { return $condition ? $activeClass : $inactiveClass; } - /** * Check if the URI of the current request matches one of the specific URIs. * @@ -125,16 +115,13 @@ public function checkUri($uris) if (!$this->request) { return false; } - - foreach ((array) $uris as $uri) { + foreach ((array)$uris as $uri) { if ($this->uri == $uri) { return true; } } - return false; } - /** * Check if the current URI matches one of specific patterns (using `Str::is`). * @@ -147,7 +134,6 @@ public function checkUriPattern($patterns) if (!$this->request) { return false; } - foreach ((array) $patterns as $p) { if (Str::is($p, $this->uri)) { return true; @@ -174,21 +160,19 @@ public function checkQuery($key, $value) if (!$this->request) { return false; } - $queryValue = $this->request->query($key); - // if the `key` exists in the query string with the correct value // OR it exists with any value // OR its value is an array that contains the specific value - if (($queryValue == $value) || ($queryValue !== null && $value === false) || (is_array($queryValue) && in_array($value, - $queryValue)) + if (($queryValue == $value) || ($queryValue !== null && $value === false) || (is_array($queryValue) && in_array( + $value, + $queryValue + )) ) { return true; } - return false; } - /** * Check if the name of the current route matches one of specific values. * @@ -201,16 +185,12 @@ public function checkRoute($routeNames) if (!$this->route) { return false; } - $routeName = $this->route->getName(); - if (in_array($routeName, (array) $routeNames)) { return true; } - return false; } - /** * Check the current route name with one or some patterns. * @@ -223,22 +203,17 @@ public function checkRoutePattern($patterns) if (!$this->route) { return false; } - $routeName = $this->route->getName(); - if ($routeName == null) { return in_array(null, $patterns); } - foreach ((array) $patterns as $p) { if (Str::is($p, $routeName)) { return true; } } - return false; } - /** * Check if the parameter of the current route has the correct value. * @@ -252,18 +227,14 @@ public function checkRouteParam($param, $value) if (!$this->route) { return false; } - $paramValue = $this->route->parameter($param); - // If the parameter value is an instance of Model class, we compare $value with the value of // its primary key. if (is_a($paramValue, Model::class)) { return $paramValue->{$paramValue->getKeyName()} == $value; } - return $paramValue == $value; } - /** * Return 'active' class if current route action match one of provided action names. * @@ -276,14 +247,11 @@ public function checkAction($actions) if (!$this->action) { return false; } - - if (in_array($this->action, (array) $actions)) { + if (in_array($this->action, (array)$actions)) { return true; } - return false; } - /** * Check if the current controller class matches one of specific values. * @@ -296,14 +264,11 @@ public function checkController($controllers) if (!$this->controller) { return false; } - - if (in_array($this->controller, (array) $controllers)) { + if (in_array($this->controller, (array)$controllers)) { return true; } - return false; } - /** * Get the current controller method. * @@ -313,7 +278,6 @@ public function getMethod() { return $this->method ?: ''; } - /** * Get the current action string. * @@ -323,7 +287,6 @@ public function getAction() { return $this->action ?: ''; } - /** * Get the current controller class. * @@ -333,4 +296,4 @@ public function getController() { return $this->controller ?: ''; } -} +} \ No newline at end of file diff --git a/app/Services/Active/Facades/Active.php b/app/Services/Active/Facades/Active.php index 25ee2bb1..27370f6c 100644 --- a/app/Services/Active/Facades/Active.php +++ b/app/Services/Active/Facades/Active.php @@ -1,6 +1,6 @@ App\Services\Active\Facades\Active::class, 'Breadcrumbs' => DaveJamesMiller\Breadcrumbs\Facade::class, 'Captcha' => Arcanedev\NoCaptcha\Facades\NoCaptcha::class, 'Form' => Collective\Html\FormFacade::class, 'Gravatar' => Creativeorange\Gravatar\Facades\Gravatar::class, 'Html' => Collective\Html\HtmlFacade::class, 'Socialite' => Laravel\Socialite\Facades\Socialite::class, - //'Datatables' => Yajra\DataTables\Facades\DataTables::class + //'Datatables' => Yajra\DataTables\Facades\DataTables::class, ], ];