Container.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\Core\DependencyInjection;
  3. use Illuminate\Contracts\Container\Container as ContainerContract;
  4. use ModulesGarden\Servers\ProxmoxCloudVps\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=null)
  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] == 'ModulesGarden' && 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. return $this->resolve($abstract, $parameters);
  89. }
  90. $abstract = $this->getAlias($this->normalize($abstract));
  91. if (isset($this->instances[$abstract])) {
  92. return $this->instances[$abstract];
  93. }
  94. $concrete = $this->getConcrete($abstract);
  95. if ($this->isBuildable($concrete, $abstract)) {
  96. $object = $this->build($concrete, $parameters);
  97. } else {
  98. $object = $this->make($concrete, $parameters);
  99. }
  100. foreach ($this->getExtenders($abstract) as $extender) {
  101. $object = $extender($object, $this);
  102. }
  103. if ($this->isShared($abstract)) {
  104. $this->instances[$abstract] = $object;
  105. }
  106. $this->fireResolvingCallbacks($abstract, $object);
  107. $this->resolved[$abstract] = true;
  108. return $object;
  109. }
  110. /**
  111. * Resolve all of the dependencies from the ReflectionParameters.
  112. *
  113. * @param array $dependencies
  114. * @return array
  115. *
  116. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  117. */
  118. protected function resolveDependencies(array $dependencies)
  119. {
  120. $results = [];
  121. foreach ($dependencies as $dependency) {
  122. if($dependency->isOptional())
  123. {
  124. break;
  125. }
  126. if ($this->hasParameterOverride($dependency)) {
  127. $results[] = $this->getParameterOverride($dependency);
  128. continue;
  129. }
  130. $result = is_null($dependency->getClass())
  131. ? $this->resolvePrimitive($dependency)
  132. : $this->resolveClass($dependency);
  133. if ($dependency->isVariadic()) {
  134. $results = array_merge($results, $result);
  135. } else {
  136. $results[] = $result;
  137. }
  138. }
  139. return $results;
  140. }
  141. /**
  142. * Resolve a non-class hinted primitive dependency.
  143. *
  144. * @param \ReflectionParameter $parameter
  145. * @return mixed
  146. *
  147. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  148. */
  149. protected function resolvePrimitive(\ReflectionParameter $parameter)
  150. {
  151. if (! is_null($concrete = $this->getContextualConcrete('$'.$parameter->name))) {
  152. return $concrete instanceof Closure ? $concrete($this) : $concrete;
  153. }
  154. if ($parameter->isDefaultValueAvailable()) {
  155. return $parameter->getDefaultValue();
  156. }
  157. if($parameter->hasType())
  158. {
  159. $returnEmptyType = [];
  160. switch(strtolower($parameter->getType()->getName()))
  161. {
  162. case 'string':
  163. $returnEmptyType = '';
  164. break;
  165. case 'array':
  166. $returnEmptyType = [];
  167. break;
  168. default:
  169. return null;
  170. }
  171. return $returnEmptyType;
  172. }
  173. return null;
  174. }
  175. }