Container.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. /* If $abstract contains namespace without slash at the begging, we need to add it */
  79. $explodedAbstract = explode('\\', $abstract);
  80. if($explodedAbstract[0] == 'ThurData' && count($explodedAbstract) > 1)
  81. {
  82. $abstract = '\\'.$abstract;
  83. }
  84. /* This function executes a different code, depending on the version of the container - WHMCS 8 has a much newer version */
  85. $version8OrHigher = (new WhmcsVersionComparator)->isWVersionHigherOrEqual('8.0.0');
  86. if($version8OrHigher)
  87. {
  88. $result = $this->resolve($abstract, $parameters);
  89. if ($abstract === 'whmcsParams') {
  90. logModuleCall(
  91. 'kerioEmail',
  92. __FUNCTION__,
  93. $this,
  94. 'Debug resolve',
  95. $result
  96. );
  97. }
  98. return $result;
  99. // return $this->resolve($abstract, $parameters);
  100. }
  101. $abstract = $this->getAlias($this->normalize($abstract));
  102. if (isset($this->instances[$abstract])) {
  103. return $this->instances[$abstract];
  104. }
  105. $concrete = $this->getConcrete($abstract);
  106. if ($this->isBuildable($concrete, $abstract)) {
  107. $object = $this->build($concrete, $parameters);
  108. } else {
  109. $object = $this->make($concrete, $parameters);
  110. }
  111. foreach ($this->getExtenders($abstract) as $extender) {
  112. $object = $extender($object, $this);
  113. }
  114. if ($this->isShared($abstract)) {
  115. $this->instances[$abstract] = $object;
  116. }
  117. $this->fireResolvingCallbacks($abstract, $object);
  118. $this->resolved[$abstract] = true;
  119. return $object;
  120. }
  121. /**
  122. * Resolve all of the dependencies from the ReflectionParameters.
  123. *
  124. * @param array $dependencies
  125. * @return array
  126. *
  127. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  128. */
  129. protected function resolveDependencies(array $dependencies)
  130. {
  131. $results = [];
  132. foreach ($dependencies as $dependency) {
  133. if($dependency->isOptional())
  134. {
  135. break;
  136. }
  137. if ($this->hasParameterOverride($dependency)) {
  138. $results[] = $this->getParameterOverride($dependency);
  139. continue;
  140. }
  141. $result = is_null($dependency->getClass())
  142. ? $this->resolvePrimitive($dependency)
  143. : $this->resolveClass($dependency);
  144. if ($dependency->isVariadic()) {
  145. $results = array_merge($results, $result);
  146. } else {
  147. $results[] = $result;
  148. }
  149. }
  150. return $results;
  151. }
  152. /**
  153. * Resolve a non-class hinted primitive dependency.
  154. *
  155. * @param \ReflectionParameter $parameter
  156. * @return mixed
  157. *
  158. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  159. */
  160. protected function resolvePrimitive(\ReflectionParameter $parameter)
  161. {
  162. if (! is_null($concrete = $this->getContextualConcrete('$'.$parameter->name))) {
  163. return $concrete instanceof \Closure ? $concrete($this) : $concrete;
  164. }
  165. if ($parameter->isDefaultValueAvailable()) {
  166. return $parameter->getDefaultValue();
  167. }
  168. if($parameter->hasType())
  169. {
  170. $returnEmptyType = [];
  171. switch(strtolower($parameter->getType()->getName()))
  172. {
  173. case 'string':
  174. $returnEmptyType = '';
  175. break;
  176. case 'array':
  177. $returnEmptyType = [];
  178. break;
  179. default:
  180. return null;
  181. }
  182. return $returnEmptyType;
  183. }
  184. return null;
  185. }
  186. }