vendor/jackalope/jackalope/src/Jackalope/Factory.php line 62

Open in your IDE?
  1. <?php
  2. namespace Jackalope;
  3. use InvalidArgumentException;
  4. use ReflectionClass;
  5. /**
  6. * Jackalope implementation factory.
  7. *
  8. * @license http://www.apache.org/licenses Apache License Version 2.0, January 2004
  9. * @license http://opensource.org/licenses/MIT MIT License
  10. */
  11. class Factory implements FactoryInterface
  12. {
  13. /**
  14. * @var array
  15. */
  16. protected $classCache = [];
  17. /**
  18. * @var array
  19. */
  20. protected $reflectionCache = [];
  21. /**
  22. * {@inheritDoc}
  23. *
  24. * @throws InvalidArgumentException
  25. */
  26. public function get($name, array $params = [])
  27. {
  28. if (isset($this->classCache[$name])) {
  29. $name = $this->classCache[$name];
  30. } else {
  31. $originalName = $name;
  32. if (class_exists('Jackalope\\' . $name)) {
  33. $name = 'Jackalope\\' . $name;
  34. } elseif (! class_exists($name)) {
  35. throw new InvalidArgumentException("Neither class Jackalope\\$name nor class $name found. Please check your autoloader and the spelling of $name");
  36. }
  37. $this->classCache[$originalName] = $name;
  38. }
  39. if (0 === strpos($name, 'Jackalope\\')) {
  40. array_unshift($params, $this);
  41. }
  42. if (! count($params)) {
  43. return new $name;
  44. }
  45. if (isset($this->reflectionCache[$name])) {
  46. $class = $this->reflectionCache[$name];
  47. } else {
  48. $class = new ReflectionClass($name);
  49. $this->reflectionCache[$name] = $class;
  50. }
  51. return $class->newInstanceArgs($params);
  52. }
  53. }