Instantiator.php 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. * @throws ExceptionInterface When the instance cannot be created
  53. */
  54. public static function instantiate(string $class, array $properties = [], array $privateProperties = []): object
  55. {
  56. $reflector = Registry::$reflectors[$class] ?? Registry::getClassReflector($class);
  57. if (Registry::$cloneable[$class]) {
  58. $wrappedInstance = [clone Registry::$prototypes[$class]];
  59. } elseif (Registry::$instantiableWithoutConstructor[$class]) {
  60. $wrappedInstance = [$reflector->newInstanceWithoutConstructor()];
  61. } elseif (null === Registry::$prototypes[$class]) {
  62. throw new NotInstantiableTypeException($class);
  63. } elseif ($reflector->implementsInterface('Serializable') && (\PHP_VERSION_ID < 70400 || !method_exists($class, '__unserialize'))) {
  64. $wrappedInstance = [unserialize('C:'.\strlen($class).':"'.$class.'":0:{}')];
  65. } else {
  66. $wrappedInstance = [unserialize('O:'.\strlen($class).':"'.$class.'":0:{}')];
  67. }
  68. if ($properties) {
  69. $privateProperties[$class] = isset($privateProperties[$class]) ? $properties + $privateProperties[$class] : $properties;
  70. }
  71. foreach ($privateProperties as $class => $properties) {
  72. if (!$properties) {
  73. continue;
  74. }
  75. foreach ($properties as $name => $value) {
  76. // because they're also used for "unserialization", hydrators
  77. // deal with array of instances, so we need to wrap values
  78. $properties[$name] = [$value];
  79. }
  80. (Hydrator::$hydrators[$class] ?? Hydrator::getHydrator($class))($properties, $wrappedInstance);
  81. }
  82. return $wrappedInstance[0];
  83. }
  84. }