Builder.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\DependencyInjection;
  3. use \ThurData\Servers\KerioEmail\Core\SL\Data\DataSL;
  4. class Builder
  5. {
  6. /**
  7. * @var DataSL
  8. */
  9. protected $data = null;
  10. /**
  11. * @var array
  12. */
  13. protected $registers = [];
  14. public function __construct()
  15. {
  16. $this->init();
  17. $this->loadAliases();
  18. $this->loadRewrites();
  19. $this->loadInstances();
  20. }
  21. protected function init()
  22. {
  23. Container::setInstance(new Container());
  24. $this->data = new DataSL();
  25. $this->registers = $this->data->getRegisters();
  26. }
  27. protected function loadRewrites()
  28. {
  29. foreach($this->data->getRewrites() as $alias => $className)
  30. {
  31. Container::getInstance()->alias($className, $alias);
  32. }
  33. }
  34. protected function loadAliases()
  35. {
  36. foreach($this->data->getAllAlias() as $className => $alias)
  37. {
  38. Container::getInstance()->alias($className, $alias);
  39. }
  40. }
  41. protected function loadInstances()
  42. {
  43. foreach($this->data->getConfigurations() as $config)
  44. {
  45. $className = $config['name'];
  46. $method = $config['method'];
  47. $arguments = $config['args'];
  48. if(!$method)
  49. {
  50. $obj = Container::getInstance()->make($className);
  51. }
  52. else
  53. {
  54. $obj = call_user_func_array("$className::$method", $arguments);
  55. }
  56. if(array_key_exists($className, $this->registers) && $this->registers[$className]['singleton'])
  57. {
  58. Container::getInstance()->instance($className, $obj);
  59. }
  60. }
  61. }
  62. }