Instantiator.php 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\VarExporter;
  11. use Symfony\Component\VarExporter\Exception\ExceptionInterface;
  12. use Symfony\Component\VarExporter\Exception\NotInstantiableTypeException;
  13. use Symfony\Component\VarExporter\Internal\Hydrator;
  14. use Symfony\Component\VarExporter\Internal\Registry;
  15. /**
  16. * A utility class to create objects without calling their constructor.
  17. *
  18. * @author Nicolas Grekas <p@tchwork.com>
  19. */
  20. final class Instantiator
  21. {
  22. /**
  23. * Creates an object and sets its properties without calling its constructor nor any other methods.
  24. *
  25. * For example:
  26. *
  27. * // creates an empty instance of Foo
  28. * Instantiator::instantiate(Foo::class);
  29. *
  30. * // creates a Foo instance and sets one of its properties
  31. * Instantiator::instantiate(Foo::class, ['propertyName' => $propertyValue]);
  32. *
  33. * // creates a Foo instance and sets a private property defined on its parent Bar class
  34. * Instantiator::instantiate(Foo::class, [], [
  35. * Bar::class => ['privateBarProperty' => $propertyValue],
  36. * ]);
  37. *
  38. * Instances of ArrayObject, ArrayIterator and SplObjectHash can be created
  39. * by using the special "\0" property name to define their internal value:
  40. *
  41. * // creates an SplObjectHash where $info1 is attached to $obj1, etc.
  42. * Instantiator::instantiate(SplObjectStorage::class, ["\0" => [$obj1, $info1, $obj2, $info2...]]);
  43. *
  44. * // creates an ArrayObject populated with $inputArray
  45. * Instantiator::instantiate(ArrayObject::class, ["\0" => [$inputArray]]);
  46. *
  47. * @param string $class The class of the instance to create
  48. * @param array $properties The properties to set on the instance
  49. * @param array $privateProperties The private properties to set on the instance,
  50. * keyed by their declaring class
  51. *
  52. * @return object The created instance
  53. *
  54. * @throws ExceptionInterface When the instance cannot be created
  55. */
  56. public static function instantiate(string $class, array $properties = [], array $privateProperties = []): object
  57. {
  58. $reflector = Registry::$reflectors[$class] ?? Registry::getClassReflector($class);
  59. if (Registry::$cloneable[$class]) {
  60. $wrappedInstance = [clone Registry::$prototypes[$class]];
  61. } elseif (Registry::$instantiableWithoutConstructor[$class]) {
  62. $wrappedInstance = [$reflector->newInstanceWithoutConstructor()];
  63. } elseif (null === Registry::$prototypes[$class]) {
  64. throw new NotInstantiableTypeException($class);
  65. } elseif ($reflector->implementsInterface('Serializable') && (\PHP_VERSION_ID < 70400 || !method_exists($class, '__unserialize'))) {
  66. $wrappedInstance = [unserialize('C:'.\strlen($class).':"'.$class.'":0:{}')];
  67. } else {
  68. $wrappedInstance = [unserialize('O:'.\strlen($class).':"'.$class.'":0:{}')];
  69. }
  70. if ($properties) {
  71. $privateProperties[$class] = isset($privateProperties[$class]) ? $properties + $privateProperties[$class] : $properties;
  72. }
  73. foreach ($privateProperties as $class => $properties) {
  74. if (!$properties) {
  75. continue;
  76. }
  77. foreach ($properties as $name => $value) {
  78. // because they're also used for "unserialization", hydrators
  79. // deal with array of instances, so we need to wrap values
  80. $properties[$name] = [$value];
  81. }
  82. (Hydrator::$hydrators[$class] ?? Hydrator::getHydrator($class))($properties, $wrappedInstance);
  83. }
  84. return $wrappedInstance[0];
  85. }
  86. }