annotate core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 4c8ae668cc8c
children c2387f117808
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\DependencyInjection;
Chris@0 4
Chris@0 5 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Provides dependency injection friendly methods for serialization.
Chris@0 9 */
Chris@0 10 trait DependencySerializationTrait {
Chris@0 11
Chris@0 12 /**
Chris@0 13 * An array of service IDs keyed by property name used for serialization.
Chris@0 14 *
Chris@0 15 * @var array
Chris@0 16 */
Chris@0 17 protected $_serviceIds = [];
Chris@0 18
Chris@0 19 /**
Chris@0 20 * {@inheritdoc}
Chris@0 21 */
Chris@0 22 public function __sleep() {
Chris@0 23 $this->_serviceIds = [];
Chris@0 24 $vars = get_object_vars($this);
Chris@0 25 foreach ($vars as $key => $value) {
Chris@0 26 if (is_object($value) && isset($value->_serviceId)) {
Chris@0 27 // If a class member was instantiated by the dependency injection
Chris@0 28 // container, only store its ID so it can be used to get a fresh object
Chris@0 29 // on unserialization.
Chris@0 30 $this->_serviceIds[$key] = $value->_serviceId;
Chris@0 31 unset($vars[$key]);
Chris@0 32 }
Chris@0 33 // Special case the container, which might not have a service ID.
Chris@0 34 elseif ($value instanceof ContainerInterface) {
Chris@0 35 $this->_serviceIds[$key] = 'service_container';
Chris@0 36 unset($vars[$key]);
Chris@0 37 }
Chris@0 38 }
Chris@0 39
Chris@0 40 return array_keys($vars);
Chris@0 41 }
Chris@0 42
Chris@0 43 /**
Chris@0 44 * {@inheritdoc}
Chris@0 45 */
Chris@0 46 public function __wakeup() {
Chris@0 47 // Tests in isolation potentially unserialize in the parent process.
Chris@0 48 $phpunit_bootstrap = isset($GLOBALS['__PHPUNIT_BOOTSTRAP']);
Chris@0 49 if ($phpunit_bootstrap && !\Drupal::hasContainer()) {
Chris@0 50 return;
Chris@0 51 }
Chris@0 52 $container = \Drupal::getContainer();
Chris@0 53 foreach ($this->_serviceIds as $key => $service_id) {
Chris@0 54 // In rare cases, when test data is serialized in the parent process,
Chris@0 55 // there is a service container but it doesn't contain all expected
Chris@0 56 // services. To avoid fatal errors during the wrap-up of failing tests, we
Chris@0 57 // check for this case, too.
Chris@0 58 if ($phpunit_bootstrap && !$container->has($service_id)) {
Chris@0 59 continue;
Chris@0 60 }
Chris@0 61 $this->$key = $container->get($service_id);
Chris@0 62 }
Chris@0 63 $this->_serviceIds = [];
Chris@0 64 }
Chris@0 65
Chris@0 66 }