Advertisement
Guest User

Untitled

a guest
Jul 14th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. <?php
  2.  
  3. class Reference
  4. {
  5. private $id;
  6.  
  7. public function __construct($id) {
  8. $this->id = strtolower($id);
  9. }
  10.  
  11. public function __toString() {
  12. return (string) $this->id;
  13. }
  14. }
  15.  
  16. class ServiceContainer
  17. {
  18. private $services;
  19. private $lastRegistered;
  20.  
  21. /**
  22. * Register a service with the DI container
  23. *
  24. * @param string $id Name of the service
  25. *
  26. * @param string $class Class to attach to service
  27. *
  28. * @return object $this
  29. */
  30. public function register($id, $class) {
  31. $this->addService($id, $class);
  32. return $this;
  33. }
  34.  
  35. /**
  36. * Add a parameter to a service
  37. *
  38. * @param mixed $parameter Parameter to add
  39. *
  40. * @param string $id Name of the service
  41. *
  42. * @return object $this
  43. */
  44. public function addParameter($parameter, $id = null) {
  45. $id = isset($id) ? $id : $this->lastRegistered;
  46. $this->addService($id, null, array($parameter));
  47. return $this;
  48. }
  49.  
  50. /**
  51. * Check if a service is registered
  52. *
  53. * @param string $id Name of the service
  54. *
  55. * @return bool
  56. */
  57. public function isRegistered($id) {
  58. $id = strtolower($id);
  59. return array_key_exists($id, $this->services);
  60. }
  61.  
  62. /**
  63. * Add a service to the DI container
  64. *
  65. * @param string $id Name of the service
  66. *
  67. * @param string $class Class to attach to service
  68. *
  69. * @param array $parameters Array of parameters
  70. */
  71. public function addService($id, $class, array $parameters = array()) {
  72. $id = strtolower($id);
  73.  
  74. // Initialize the parameter array
  75. if (! isset($this->services[$id]['parameters'])) $this->services[$id]['parameters'] = array();
  76.  
  77. foreach ($parameters as $parameter) {
  78. $this->services[$id]['parameters'][] = $parameter;
  79. }
  80.  
  81. if (isset($class)) $this->services[$id]['class'] = $class;
  82.  
  83. $this->lastRegistered = $id;
  84. }
  85.  
  86. /**
  87. * Get a service
  88. *
  89. * @param string $id Name of the service
  90. *
  91. * @return object Return the service
  92. */
  93. public function getService($id) {
  94. $id = strtolower($id);
  95.  
  96. if ($this->isRegistered($id)) {
  97. // Build only when needed
  98. return (is_object($this->services[$id])) ? $this->services[$id] : $this->buildService($id);
  99. } else {
  100. throw new InvalidArgumentException(sprintf('Service "%s" does not exist.', $id));
  101. }
  102. }
  103.  
  104. /**
  105. * Build the service
  106. *
  107. * This a low level function with all the DI container magic
  108. *
  109. * @param string $id Name of the service
  110. *
  111. * @return object Return the service
  112. */
  113. private function buildService($id) {
  114. if (! $this->isRegistered($id)) {
  115. throw new InvalidArgumentException(sprintf('Service "%s" does not exist.', $id));
  116. }
  117.  
  118. if (! isset($this->services[$id]['class'])) {
  119. throw new RuntimeException(sprintf('Service "%s" has no class to construct.', $id));
  120. }
  121.  
  122. $reflection = new ReflectionClass($this->services[$id]['class']);
  123.  
  124. // If the paramters is a reference replace the parameter with the referenced service object
  125. foreach ($this->services[$id]['parameters'] as &$parameter) {
  126. if ($parameter instanceof Reference) {
  127. $parameter = (string) $parameter;
  128. $parameter = $this->buildService($parameter);
  129. }
  130. }
  131.  
  132. // Replace the whole array with the service object, this is destructive for the parameters
  133. // but they are no longer needed
  134. // If the constructor is null pass no arguments to the class
  135. $this->services[$id] = (null === $reflection->getConstructor()) ? $reflection->newInstance() : $reflection->newInstanceArgs($this->services[$id]['parameters']);
  136.  
  137. return $this->services[$id];
  138. }
  139. }
  140.  
  141. $svc->register('foo', array(5, 'bar'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement