Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

58 linhas
1.3KB

  1. <?php
  2. declare(strict_types=1);
  3. namespace Core;
  4. use Exception;
  5. use ReflectionFunction;
  6. use ReflectionMethod;
  7. class App
  8. {
  9. protected array $bindings = [];
  10. public function bind(string $name, mixed $value): void
  11. {
  12. $this->bindings[$name] = $value;
  13. }
  14. public function get(string $name): mixed
  15. {
  16. return $this->bindings[$name] ?? null;
  17. }
  18. public function call(callable|array|string $handler, array $parameters = []): mixed
  19. {
  20. if (is_array($handler)) {
  21. return $this->callMethod($handler, $parameters);
  22. }
  23. if (is_callable($handler)) {
  24. return $this->callFunction($handler, $parameters);
  25. }
  26. throw new Exception('Invalid handler.');
  27. }
  28. protected function callFunction(callable $handler, array $parameters): mixed
  29. {
  30. $reflection = new ReflectionFunction($handler);
  31. return $reflection->invokeArgs(array_values($parameters));
  32. }
  33. protected function callMethod(array $handler, array $parameters): mixed
  34. {
  35. [$class, $method] = $handler;
  36. if (is_string($class)) {
  37. $class = new $class();
  38. }
  39. $reflection = new ReflectionMethod($class, $method);
  40. return $reflection->invokeArgs($class, array_values($parameters));
  41. }
  42. }

Powered by TurnKey Linux.