Container.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\DependencyInjection;
  3. use Illuminate\Contracts\Container\Container as ContainerContract;
  4. use ThurData\Servers\KerioEmail\Core\Helper\WhmcsVersionComparator;
  5. class Container extends \Illuminate\Container\Container
  6. {
  7. protected static $instance = null;
  8. public static function getInstance()
  9. {
  10. if (is_null(self::$instance))
  11. {
  12. self::$instance = new static;
  13. }
  14. return static::$instance;
  15. }
  16. public static function setInstance(ContainerContract $container)
  17. {
  18. self::$instance = $container;
  19. }
  20. /**
  21. * @param $parameters
  22. * @param array $primitives
  23. * @return array
  24. */
  25. protected function getDependencies($parameters, array $primitives = array())
  26. {
  27. $dependencies = array();
  28. foreach ($parameters as $parameter)
  29. {
  30. if($parameter->isOptional())
  31. {
  32. break;
  33. }
  34. $dependency = $parameter->getClass();
  35. // If the class is null, it means the dependency is a string or some other
  36. // primitive type which we can not resolve since it is not a class and
  37. // we will just bomb out with an error since we have no-where to go.
  38. if (array_key_exists($parameter->name, $primitives))
  39. {
  40. $dependencies[] = $primitives[$parameter->name];
  41. }
  42. elseif (is_null($dependency))
  43. {
  44. $dependencies[] = $this->resolveNonClass($parameter);
  45. }
  46. else
  47. {
  48. $dependencies[] = $this->resolveClass($parameter);
  49. }
  50. }
  51. return (array) $dependencies;
  52. }
  53. /**
  54. * Set null value as default parameter when cannot find default value
  55. * @param ReflectionParameter $parameter
  56. * @return null
  57. */
  58. protected function resolveNonClass(\ReflectionParameter $parameter)
  59. {
  60. if ($parameter->isDefaultValueAvailable())
  61. {
  62. return $parameter->getDefaultValue();
  63. }
  64. return null;
  65. }
  66. /* --------------------------------------- WHMCS 8 --------------------------------------- */
  67. /**
  68. * Resolve the given type from the container.
  69. *
  70. * @param string $abstract
  71. * @param array $parameters
  72. * @return mixed
  73. *
  74. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  75. */
  76. public function make($abstract, array $parameters = [])
  77. {
  78. logModuleCall(
  79. 'kerioEmail',
  80. __FUNCTION__,
  81. $abstract,
  82. 'Debug Make',
  83. $parameters
  84. );
  85. /* If $abstract contains namespace without slash at the begging, we need to add it */
  86. $explodedAbstract = explode('\\', $abstract);
  87. if($explodedAbstract[0] == 'ThurData' && count($explodedAbstract) > 1)
  88. {
  89. $abstract = '\\'.$abstract;
  90. }
  91. /* This function executes a different code, depending on the version of the container - WHMCS 8 has a much newer version */
  92. $version8OrHigher = (new WhmcsVersionComparator)->isWVersionHigherOrEqual('8.0.0');
  93. if($version8OrHigher)
  94. {
  95. return $this->resolve($abstract, $parameters);
  96. }
  97. $abstract = $this->getAlias($this->normalize($abstract));
  98. if (isset($this->instances[$abstract])) {
  99. return $this->instances[$abstract];
  100. }
  101. $concrete = $this->getConcrete($abstract);
  102. if ($this->isBuildable($concrete, $abstract)) {
  103. $object = $this->build($concrete, $parameters);
  104. } else {
  105. $object = $this->make($concrete, $parameters);
  106. }
  107. foreach ($this->getExtenders($abstract) as $extender) {
  108. $object = $extender($object, $this);
  109. }
  110. if ($this->isShared($abstract)) {
  111. $this->instances[$abstract] = $object;
  112. }
  113. $this->fireResolvingCallbacks($abstract, $object);
  114. $this->resolved[$abstract] = true;
  115. return $object;
  116. }
  117. /**
  118. * Resolve all of the dependencies from the ReflectionParameters.
  119. *
  120. * @param array $dependencies
  121. * @return array
  122. *
  123. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  124. */
  125. protected function resolveDependencies(array $dependencies)
  126. {
  127. $results = [];
  128. foreach ($dependencies as $dependency) {
  129. if($dependency->isOptional())
  130. {
  131. break;
  132. }
  133. if ($this->hasParameterOverride($dependency)) {
  134. $results[] = $this->getParameterOverride($dependency);
  135. continue;
  136. }
  137. $result = is_null($dependency->getClass())
  138. ? $this->resolvePrimitive($dependency)
  139. : $this->resolveClass($dependency);
  140. if ($dependency->isVariadic()) {
  141. $results = array_merge($results, $result);
  142. } else {
  143. $results[] = $result;
  144. }
  145. }
  146. return $results;
  147. }
  148. /**
  149. * Resolve a non-class hinted primitive dependency.
  150. *
  151. * @param \ReflectionParameter $parameter
  152. * @return mixed
  153. *
  154. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  155. */
  156. protected function resolvePrimitive(\ReflectionParameter $parameter)
  157. {
  158. if (! is_null($concrete = $this->getContextualConcrete('$'.$parameter->name))) {
  159. return $concrete instanceof \Closure ? $concrete($this) : $concrete;
  160. }
  161. if ($parameter->isDefaultValueAvailable()) {
  162. return $parameter->getDefaultValue();
  163. }
  164. if($parameter->hasType())
  165. {
  166. $returnEmptyType = [];
  167. switch(strtolower($parameter->getType()->getName()))
  168. {
  169. case 'string':
  170. $returnEmptyType = '';
  171. break;
  172. case 'array':
  173. $returnEmptyType = [];
  174. break;
  175. default:
  176. return null;
  177. }
  178. return $returnEmptyType;
  179. }
  180. return null;
  181. }
  182. }