Chris@0: _serviceIds = []; Chris@0: $vars = get_object_vars($this); Chris@0: foreach ($vars as $key => $value) { Chris@0: if (is_object($value) && isset($value->_serviceId)) { Chris@0: // If a class member was instantiated by the dependency injection Chris@0: // container, only store its ID so it can be used to get a fresh object Chris@0: // on unserialization. Chris@0: $this->_serviceIds[$key] = $value->_serviceId; Chris@0: unset($vars[$key]); Chris@0: } Chris@0: // Special case the container, which might not have a service ID. Chris@0: elseif ($value instanceof ContainerInterface) { Chris@0: $this->_serviceIds[$key] = 'service_container'; Chris@0: unset($vars[$key]); Chris@0: } Chris@0: } Chris@0: Chris@0: return array_keys($vars); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function __wakeup() { Chris@0: // Tests in isolation potentially unserialize in the parent process. Chris@0: $phpunit_bootstrap = isset($GLOBALS['__PHPUNIT_BOOTSTRAP']); Chris@0: if ($phpunit_bootstrap && !\Drupal::hasContainer()) { Chris@0: return; Chris@0: } Chris@0: $container = \Drupal::getContainer(); Chris@0: foreach ($this->_serviceIds as $key => $service_id) { Chris@0: // In rare cases, when test data is serialized in the parent process, Chris@0: // there is a service container but it doesn't contain all expected Chris@0: // services. To avoid fatal errors during the wrap-up of failing tests, we Chris@0: // check for this case, too. Chris@0: if ($phpunit_bootstrap && !$container->has($service_id)) { Chris@0: continue; Chris@0: } Chris@0: $this->$key = $container->get($service_id); Chris@0: } Chris@0: $this->_serviceIds = []; Chris@0: } Chris@0: Chris@0: }