Created
June 14, 2020 18:09
-
-
Save ayoubbajjou/cb685beeffc63b3035ed6504b09feb11 to your computer and use it in GitHub Desktop.
The optimized PretendClassMethodIsControllerMethod class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // this is the new class | |
| namespace Livewire\Macros; | |
| class NewPretendClassMethodIsControllerMethod | |
| { | |
| protected $method; | |
| protected $router; | |
| protected $current; | |
| public function __construct($method, $router) | |
| { | |
| $this->method = $method; | |
| $this->router = $router; | |
| $this->current = $this->router->current(); | |
| } | |
| public function retrieveBindings() | |
| { | |
| $cache = $this->cache(); | |
| $this->uses(); | |
| $this->substituteImplicitBindings(); | |
| $this->uses($cache); | |
| return $this->options(); | |
| } | |
| /** | |
| * Cache the current route action (this callback actually), just to be safe. | |
| * | |
| * @return void | |
| */ | |
| public function cache() | |
| { | |
| return $this->current->getAction('uses'); | |
| } | |
| /** | |
| * We'll set the route action to be the "mount" method from the chosen | |
| * Livewire component, to get the proper implicit bindings. | |
| * | |
| * @param mixed $cache | |
| * @return void | |
| */ | |
| public function uses($cache = null) | |
| { | |
| $this->current->uses($cache ? $cache : $this->method->class.'@'.$this->method->name); | |
| } | |
| /** | |
| * This is normally handled in the "SubstituteBindings" middleware, but | |
| * because that middleware has already ran, we need to run them again. | |
| * | |
| * @return void | |
| */ | |
| public function substituteImplicitBindings() | |
| { | |
| $this->router->substituteImplicitBindings($this->current); | |
| } | |
| /** | |
| * options | |
| * | |
| * @return void | |
| */ | |
| public function options() | |
| { | |
| return $this->current->resolveMethodDependencies($this->current->parameters(), $this->method); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // this is the old class | |
| namespace Livewire\Macros; | |
| class OldPretendClassMethodIsControllerMethod | |
| { | |
| protected $method; | |
| protected $router; | |
| public function __construct($method, $router) | |
| { | |
| $this->method = $method; | |
| $this->router = $router; | |
| } | |
| public function retrieveBindings() | |
| { | |
| $route = $this->router->current(); | |
| // Cache the current route action (this callback actually), just to be safe. | |
| $cache = $route->getAction('uses'); | |
| // We'll set the route action to be the "mount" method from the chosen | |
| // Livewire component, to get the proper implicit bindings. | |
| $route->uses($this->method->class.'@'.$this->method->name); | |
| // This is normally handled in the "SubstituteBindings" middleware, but | |
| // because that middleware has already ran, we need to run them again. | |
| $this->router->substituteImplicitBindings($route); | |
| $options = $route->resolveMethodDependencies($route->parameters(), $this->method); | |
| // Restore the original route action. | |
| $route->uses($cache); | |
| return $options; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment