Manager.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * This file is part of the ForkBB <https://github.com/forkbb>.
  4. *
  5. * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
  6. * @license The MIT License (MIT)
  7. */
  8. declare(strict_types=1);
  9. namespace ForkBB\Models;
  10. use ForkBB\Core\Container;
  11. class Manager
  12. {
  13. /**
  14. * Контейнер
  15. * @var Container
  16. */
  17. protected $c;
  18. /**
  19. * Ключ модели для контейнера
  20. * @var string
  21. */
  22. protected $cKey = 'unknown';
  23. /**
  24. * @var array
  25. */
  26. protected $repository = [];
  27. public function __construct(Container $container)
  28. {
  29. $this->c = $container;
  30. }
  31. public function get($key)
  32. {
  33. return $this->repository[$key] ?? null;
  34. }
  35. public function set($key, /* mixed */ $value): self
  36. {
  37. $this->repository[$key] = $value;
  38. return $this;
  39. }
  40. public function isset($key): bool
  41. {
  42. return \array_key_exists($key, $this->repository);
  43. }
  44. /**
  45. * Возвращает action по его имени
  46. */
  47. public function __get(string $name) /* : mixed */
  48. {
  49. $x = \ord($name);
  50. if ($x > 90 || $x < 65) {
  51. return null;
  52. } else {
  53. $key = $this->cKey . '/' . \lcfirst($name);
  54. return $this->c->$key->setManager($this);
  55. }
  56. }
  57. /**
  58. * Выполняет подгружаемый метод при его наличии
  59. */
  60. public function __call(string $name, array $args) /* : mixed */
  61. {
  62. $key = $this->cKey . '/' . $name;
  63. return $this->c->$key->setManager($this)->$name(...$args);
  64. }
  65. }