Skip to content

Commit

Permalink
deal with ::class notation fanboys
Browse files Browse the repository at this point in the history
  • Loading branch information
fredbradley committed Jul 27, 2021
1 parent a928b4d commit 71434ed
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace spkm\isams;


use Illuminate\Support\Str;
use spkm\isams\Contracts\Institution;
use spkm\isams\Exceptions\ControllerNotFound;
use spkm\isams\Exceptions\MethodNotFound;
Expand Down Expand Up @@ -44,15 +45,31 @@ public function institution(Institution $institution): self
*/
public function endpoint(string $controller): self
{
if (class_exists(self::CONTROLLER_NAMESPACE.$controller)) {
$controllerClass = self::CONTROLLER_NAMESPACE.$controller;
$controllerClass = $this->getController($controller);
$this->controller = new $controllerClass($this->institution);
return $this;
}

$this->controller = new $controllerClass($this->institution);
/**
* Sanatizes the controller name for us, so people can use ::class notation if they wish
*
* @param string $controllerClassName
*
* @return string
* @throws \spkm\isams\Exceptions\ControllerNotFound
*/
private function getController(string $controllerClassName): string
{
if (Str::contains($controllerClassName, self::CONTROLLER_NAMESPACE) && class_exists($controllerClassName)) {
return $controllerClassName;
}

return $this;
if (class_exists(self::CONTROLLER_NAMESPACE.$controllerClassName)) {
return self::CONTROLLER_NAMESPACE.$controllerClassName;
}

throw new ControllerNotFound("Could not find Controller: ".self::CONTROLLER_NAMESPACE.$controller, 500);
throw new ControllerNotFound("Could not find Controller: ".$controllerClassName,
500);
}

/**
Expand All @@ -71,4 +88,5 @@ public function callMethod(string $method, array $args = [])
return call_user_func_array([$this->controller, $method], $args);
}


}

0 comments on commit 71434ed

Please sign in to comment.